1
1
import _ from 'lodash' ;
2
2
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' ;
4
4
import { Constants } from '.' ;
5
5
6
6
export enum OffsetType {
@@ -55,11 +55,6 @@ export type ResultProps = {
55
55
* This should be called by each ot the items' onLayout
56
56
*/
57
57
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 ;
63
58
/**
64
59
* The items' width
65
60
*/
@@ -81,7 +76,7 @@ const focusItemsHelper = (props: Props): ResultProps => {
81
76
innerSpacing = 0
82
77
} = props ;
83
78
const itemsWidths = useRef < ( number | null ) [ ] > ( _ . times ( itemsCount , ( ) => null ) ) ;
84
- const contentOffset = useRef < number > ( 0 ) ;
79
+ const currentIndex = useRef < number > ( selectedIndex || 0 ) ;
85
80
const [ offsets , setOffsets ] = useState < Offsets > ( { CENTER : [ ] , LEFT : [ ] , RIGHT : [ ] } ) ;
86
81
87
82
// TODO: reset?
@@ -135,10 +130,6 @@ const focusItemsHelper = (props: Props): ResultProps => {
135
130
} ,
136
131
[ setSnapBreakpoints ] ) ;
137
132
138
- const onScroll = useCallback ( ( { nativeEvent} : NativeSyntheticEvent < NativeScrollEvent > ) => {
139
- contentOffset . current = nativeEvent . contentOffset . x ;
140
- } , [ ] ) ;
141
-
142
133
const scroll = ( scrollTo : number , animated : boolean ) => {
143
134
// @ts -ignore
144
135
if ( _ . isFunction ( scrollViewRef . current . scrollToOffset ) ) {
@@ -156,8 +147,9 @@ const focusItemsHelper = (props: Props): ResultProps => {
156
147
if ( offsetType !== OffsetType . DYNAMIC ) {
157
148
scroll ( offsets [ offsetType ] [ index ] , animated ) ;
158
149
} 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 ) ;
161
153
}
162
154
}
163
155
} ,
@@ -171,7 +163,6 @@ const focusItemsHelper = (props: Props): ResultProps => {
171
163
172
164
return {
173
165
onItemLayout,
174
- onScroll,
175
166
itemsWidths : offsets . CENTER . length > 0 ? ( itemsWidths . current as number [ ] ) : [ ] ,
176
167
focusIndex
177
168
} ;
0 commit comments