diff --git a/src/components/ScrollViewWithContext.js b/src/components/ScrollViewWithContext.tsx similarity index 57% rename from src/components/ScrollViewWithContext.js rename to src/components/ScrollViewWithContext.tsx index 0773f7496d12..6122d5508a38 100644 --- a/src/components/ScrollViewWithContext.js +++ b/src/components/ScrollViewWithContext.tsx @@ -1,12 +1,23 @@ -import React, {useMemo, useRef, useState} from 'react'; -import {ScrollView} from 'react-native'; +import React, {ForwardedRef, useMemo, useRef, useState} from 'react'; +import {NativeScrollEvent, NativeSyntheticEvent, ScrollView} from 'react-native'; const MIN_SMOOTH_SCROLL_EVENT_THROTTLE = 16; -const ScrollContext = React.createContext(); +type ScrollContextValue = { + contentOffsetY: number; + scrollViewRef: ForwardedRef; +}; -// eslint-disable-next-line react/forbid-foreign-prop-types -const propTypes = ScrollView.propTypes; +const ScrollContext = React.createContext({ + contentOffsetY: 0, + scrollViewRef: null, +}); + +type ScrollViewWithContextProps = { + onScroll: (event: NativeSyntheticEvent) => void; + children?: React.ReactNode; + scrollEventThrottle: number; +} & Partial; /* * is a wrapper around that provides a ref to the . @@ -15,12 +26,12 @@ const propTypes = ScrollView.propTypes; * Using this wrapper will automatically handle scrolling to the picker's * when the picker modal is opened */ -function ScrollViewWithContext({onScroll, scrollEventThrottle, children, innerRef, ...restProps}) { +function ScrollViewWithContextWithRef({onScroll, scrollEventThrottle, children, ...restProps}: ScrollViewWithContextProps, ref: ForwardedRef) { const [contentOffsetY, setContentOffsetY] = useState(0); - const defaultScrollViewRef = useRef(); - const scrollViewRef = innerRef || defaultScrollViewRef; + const defaultScrollViewRef = useRef(null); + const scrollViewRef = ref ?? defaultScrollViewRef; - const setContextScrollPosition = (event) => { + const setContextScrollPosition = (event: NativeSyntheticEvent) => { if (onScroll) { onScroll(event); } @@ -48,18 +59,7 @@ function ScrollViewWithContext({onScroll, scrollEventThrottle, children, innerRe ); } -ScrollViewWithContext.propTypes = propTypes; -ScrollViewWithContext.displayName = 'ScrollViewWithContext'; - -const ScrollViewWithContextWithRef = React.forwardRef((props, ref) => ( - -)); - ScrollViewWithContextWithRef.displayName = 'ScrollViewWithContextWithRef'; -export default ScrollViewWithContextWithRef; +export default React.forwardRef(ScrollViewWithContextWithRef); export {ScrollContext};