11import React from 'react' ;
2- import { View , FlatList , FlatListProps , StyleSheet } from 'react-native' ;
2+ import { View , FlatListProps , StyleSheet } from 'react-native' ;
33import { useSafeAreaInsets } from 'react-native-safe-area-context' ;
4- import Animated , {
5- useAnimatedRef ,
6- useScrollViewOffset ,
7- AnimateProps ,
8- } from 'react-native-reanimated' ;
4+ import Animated , { useAnimatedRef , AnimateProps } from 'react-native-reanimated' ;
95
106import FadingView from '../containers/FadingView' ;
117import { useScrollContainerLogic } from './useScrollContainerLogic' ;
@@ -16,40 +12,45 @@ type AnimatedFlatListType<ItemT = any> = React.ComponentClass<
1612 ItemT
1713> ;
1814type AnimatedFlatListBaseProps < ItemT > = React . ComponentProps < AnimatedFlatListType < ItemT > > ;
19- type AnimatedFlatListProps < ItemT > = AnimatedFlatListBaseProps < ItemT > & {
20- children ?: React . ReactNode ;
21- } ;
22- const AnimatedFlatList : AnimatedFlatListType = Animated . createAnimatedComponent ( FlatList ) ;
15+ type AnimatedFlatListProps < ItemT > = AnimatedFlatListBaseProps < ItemT > ;
2316
24- const AnimatedFlatListWithHeaders = < ItemT extends unknown > ( {
25- largeHeaderShown,
26- containerStyle,
27- LargeHeaderComponent,
28- largeHeaderContainerStyle,
29- HeaderComponent,
30- onLargeHeaderLayout,
31- onScrollBeginDrag,
32- onScrollEndDrag,
33- onMomentumScrollBegin,
34- onMomentumScrollEnd,
35- ignoreLeftSafeArea,
36- ignoreRightSafeArea,
37- disableAutoFixScroll,
38- ...rest
39- } : AnimatedFlatListProps < ItemT > & SharedScrollContainerProps ) => {
17+ const FlatListWithHeadersInputComp = < ItemT extends unknown > (
18+ {
19+ largeHeaderShown,
20+ containerStyle,
21+ LargeHeaderComponent,
22+ largeHeaderContainerStyle,
23+ HeaderComponent,
24+ onLargeHeaderLayout,
25+ onScrollBeginDrag,
26+ onScrollEndDrag,
27+ onMomentumScrollBegin,
28+ onMomentumScrollEnd,
29+ ignoreLeftSafeArea,
30+ ignoreRightSafeArea,
31+ disableAutoFixScroll,
32+ /** At the moment, we will not allow overriding of this since the scrollHandler needs it. */
33+ onScroll : _unusedOnScroll ,
34+ ...rest
35+ } : AnimatedFlatListProps < ItemT > & SharedScrollContainerProps ,
36+ ref : React . Ref < AnimatedFlatListType < ItemT > >
37+ ) => {
4038 const insets = useSafeAreaInsets ( ) ;
4139 const scrollRef = useAnimatedRef < Animated . FlatList < ItemT > > ( ) ;
42- // Need to use `any` here because useScrollViewOffset is not typed for Animated.FlatList
43- const scrollY = useScrollViewOffset ( scrollRef as any ) ;
4440
45- const { showNavBar, largeHeaderHeight, largeHeaderOpacity, debouncedFixScroll } =
46- useScrollContainerLogic ( {
47- scrollRef,
48- scrollY,
49- largeHeaderShown,
50- disableAutoFixScroll,
51- largeHeaderExists : ! ! LargeHeaderComponent ,
52- } ) ;
41+ const {
42+ scrollY,
43+ showNavBar,
44+ largeHeaderHeight,
45+ largeHeaderOpacity,
46+ scrollHandler,
47+ debouncedFixScroll,
48+ } = useScrollContainerLogic ( {
49+ scrollRef,
50+ largeHeaderShown,
51+ disableAutoFixScroll,
52+ largeHeaderExists : ! ! LargeHeaderComponent ,
53+ } ) ;
5354
5455 return (
5556 < View
@@ -61,10 +62,16 @@ const AnimatedFlatListWithHeaders = <ItemT extends unknown>({
6162 ] }
6263 >
6364 { HeaderComponent ( { showNavBar } ) }
64- < AnimatedFlatList
65- ref = { scrollRef }
65+ < Animated . FlatList
66+ ref = { ( _ref ) => {
67+ // @ts -ignore
68+ scrollRef . current = _ref ;
69+ // @ts -ignore
70+ if ( ref ) ref . current = _ref ;
71+ } }
6672 scrollEventThrottle = { 16 }
6773 overScrollMode = "auto"
74+ onScroll = { scrollHandler }
6875 automaticallyAdjustContentInsets = { false }
6976 onScrollBeginDrag = { ( e ) => {
7077 debouncedFixScroll . cancel ( ) ;
@@ -103,6 +110,12 @@ const AnimatedFlatListWithHeaders = <ItemT extends unknown>({
103110 ) ;
104111} ;
105112
106- export default AnimatedFlatListWithHeaders ;
113+ // The typecast is needed to make the component generic.
114+ const FlatListWithHeaders = React . forwardRef ( FlatListWithHeadersInputComp ) as < ItemT = any > (
115+ props : AnimatedFlatListProps < ItemT > &
116+ SharedScrollContainerProps & { ref ?: React . Ref < Animated . FlatList < ItemT > > }
117+ ) => React . ReactElement ;
118+
119+ export default FlatListWithHeaders ;
107120
108121const styles = StyleSheet . create ( { container : { flex : 1 } } ) ;
0 commit comments