Skip to content

Commit cd65641

Browse files
committed
Change the DYNAMIC logic (did not look good when scrolling pages)
1 parent 3f268b1 commit cd65641

File tree

3 files changed

+7
-22
lines changed

3 files changed

+7
-22
lines changed

generatedTypes/helpers/FocusItemHelper.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RefObject } from 'react';
2-
import { LayoutChangeEvent, ScrollView, FlatList, NativeSyntheticEvent, NativeScrollEvent } from 'react-native';
2+
import { LayoutChangeEvent, ScrollView, FlatList } from 'react-native';
33
export declare enum OffsetType {
44
CENTER = "CENTER",
55
DYNAMIC = "DYNAMIC",
@@ -43,11 +43,6 @@ export declare type ResultProps = {
4343
* This should be called by each ot the items' onLayout
4444
*/
4545
onItemLayout: (event: LayoutChangeEvent, index: number) => void;
46-
/**
47-
* This should be called by the ScrollView (or FlatList)
48-
* If this is not used OffsetType.DYNAMIC will not work properly
49-
*/
50-
onScroll: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
5146
/**
5247
* The items' width
5348
*/

src/components/tabController/TabBar.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ const TabBar = (props: Props) => {
200200

201201
const itemsCount = useRef<number>(items ? _.size(items) : React.Children.count(children.current));
202202

203-
const {onItemLayout, onScroll, itemsWidths, focusIndex} = focusItemsHelper({
203+
const {onItemLayout, itemsWidths, focusIndex} = focusItemsHelper({
204204
scrollViewRef: tabBar,
205205
itemsCount: itemsCount.current,
206206
selectedIndex,
@@ -354,7 +354,6 @@ const TabBar = (props: Props) => {
354354
horizontal
355355
contentContainerStyle={scrollViewContainerStyle}
356356
scrollEnabled // TODO:
357-
onScroll={onScroll}
358357
testID={testID}
359358
>
360359
<View style={indicatorContainerStyle}>{renderTabBarItems}</View>

src/helpers/FocusItemHelper.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash';
22
import {RefObject, useState, useCallback, useEffect, useRef} from 'react';
3-
import {LayoutChangeEvent, ScrollView, FlatList, NativeSyntheticEvent, NativeScrollEvent} from 'react-native';
3+
import {LayoutChangeEvent, ScrollView, FlatList} from 'react-native';
44
import {Constants} from '.';
55

66
export enum OffsetType {
@@ -55,11 +55,6 @@ export type ResultProps = {
5555
* This should be called by each ot the items' onLayout
5656
*/
5757
onItemLayout: (event: LayoutChangeEvent, index: number) => void;
58-
/**
59-
* This should be called by the ScrollView (or FlatList)
60-
* If this is not used OffsetType.DYNAMIC will not work properly
61-
*/
62-
onScroll: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
6358
/**
6459
* The items' width
6560
*/
@@ -81,7 +76,7 @@ const focusItemsHelper = (props: Props): ResultProps => {
8176
innerSpacing = 0
8277
} = props;
8378
const itemsWidths = useRef<(number | null)[]>(_.times(itemsCount, () => null));
84-
const contentOffset = useRef<number>(0);
79+
const currentIndex = useRef<number>(selectedIndex || 0);
8580
const [offsets, setOffsets] = useState<Offsets>({CENTER: [], LEFT: [], RIGHT: []});
8681

8782
// TODO: reset?
@@ -135,10 +130,6 @@ const focusItemsHelper = (props: Props): ResultProps => {
135130
},
136131
[setSnapBreakpoints]);
137132

138-
const onScroll = useCallback(({nativeEvent}: NativeSyntheticEvent<NativeScrollEvent>) => {
139-
contentOffset.current = nativeEvent.contentOffset.x;
140-
}, []);
141-
142133
const scroll = (scrollTo: number, animated: boolean) => {
143134
// @ts-ignore
144135
if (_.isFunction(scrollViewRef.current.scrollToOffset)) {
@@ -156,8 +147,9 @@ const focusItemsHelper = (props: Props): ResultProps => {
156147
if (offsetType !== OffsetType.DYNAMIC) {
157148
scroll(offsets[offsetType][index], animated);
158149
} else {
159-
const indexIsOnLeftOfCenter = contentOffset.current < offsets[OffsetType.CENTER][index];
160-
scroll(indexIsOnLeftOfCenter ? offsets[OffsetType.LEFT][index] : offsets[OffsetType.RIGHT][index], animated);
150+
const movingLeft = index < currentIndex.current;
151+
currentIndex.current = index;
152+
scroll(movingLeft ? offsets[OffsetType.RIGHT][index] : offsets[OffsetType.LEFT][index], animated);
161153
}
162154
}
163155
},
@@ -171,7 +163,6 @@ const focusItemsHelper = (props: Props): ResultProps => {
171163

172164
return {
173165
onItemLayout,
174-
onScroll,
175166
itemsWidths: offsets.CENTER.length > 0 ? (itemsWidths.current as number[]) : [],
176167
focusIndex
177168
};

0 commit comments

Comments
 (0)