1
- import React , { useMemo , useRef , useState } from 'react' ;
2
- import { ScrollView } from 'react-native' ;
1
+ import React , { ForwardedRef , useMemo , useRef , useState } from 'react' ;
2
+ import { NativeScrollEvent , NativeSyntheticEvent , ScrollView } from 'react-native' ;
3
3
4
4
const MIN_SMOOTH_SCROLL_EVENT_THROTTLE = 16 ;
5
5
6
- const ScrollContext = React . createContext ( ) ;
6
+ type ScrollContextValue = {
7
+ contentOffsetY : number ;
8
+ scrollViewRef : ForwardedRef < ScrollView > ;
9
+ } ;
7
10
8
- // eslint-disable-next-line react/forbid-foreign-prop-types
9
- const propTypes = ScrollView . propTypes ;
11
+ const ScrollContext = React . createContext < ScrollContextValue > ( {
12
+ contentOffsetY : 0 ,
13
+ scrollViewRef : null ,
14
+ } ) ;
15
+
16
+ type ScrollViewWithContextProps = {
17
+ onScroll : ( event : NativeSyntheticEvent < NativeScrollEvent > ) => void ;
18
+ children ?: React . ReactNode ;
19
+ scrollEventThrottle : number ;
20
+ } & Partial < ScrollView > ;
10
21
11
22
/*
12
23
* <ScrollViewWithContext /> is a wrapper around <ScrollView /> that provides a ref to the <ScrollView />.
@@ -15,12 +26,12 @@ const propTypes = ScrollView.propTypes;
15
26
* Using this wrapper will automatically handle scrolling to the picker's <TextInput />
16
27
* when the picker modal is opened
17
28
*/
18
- function ScrollViewWithContext ( { onScroll, scrollEventThrottle, children, innerRef , ...restProps } ) {
29
+ function ScrollViewWithContextWithRef ( { onScroll, scrollEventThrottle, children, ...restProps } : ScrollViewWithContextProps , ref : ForwardedRef < ScrollView > ) {
19
30
const [ contentOffsetY , setContentOffsetY ] = useState ( 0 ) ;
20
- const defaultScrollViewRef = useRef ( ) ;
21
- const scrollViewRef = innerRef || defaultScrollViewRef ;
31
+ const defaultScrollViewRef = useRef < ScrollView > ( null ) ;
32
+ const scrollViewRef = ref ?? defaultScrollViewRef ;
22
33
23
- const setContextScrollPosition = ( event ) => {
34
+ const setContextScrollPosition = ( event : NativeSyntheticEvent < NativeScrollEvent > ) => {
24
35
if ( onScroll ) {
25
36
onScroll ( event ) ;
26
37
}
@@ -48,18 +59,7 @@ function ScrollViewWithContext({onScroll, scrollEventThrottle, children, innerRe
48
59
) ;
49
60
}
50
61
51
- ScrollViewWithContext . propTypes = propTypes ;
52
- ScrollViewWithContext . displayName = 'ScrollViewWithContext' ;
53
-
54
- const ScrollViewWithContextWithRef = React . forwardRef ( ( props , ref ) => (
55
- < ScrollViewWithContext
56
- // eslint-disable-next-line react/jsx-props-no-spreading
57
- { ...props }
58
- innerRef = { ref }
59
- />
60
- ) ) ;
61
-
62
62
ScrollViewWithContextWithRef . displayName = 'ScrollViewWithContextWithRef' ;
63
63
64
- export default ScrollViewWithContextWithRef ;
64
+ export default React . forwardRef ( ScrollViewWithContextWithRef ) ;
65
65
export { ScrollContext } ;
0 commit comments