Skip to content

Commit f0fb7a8

Browse files
authored
Merge pull request #29674 from software-mansion-labs/ts-migration/scroll-view-with-context
[TS migration] Migrate 'ScrollViewWithContext.js' component to TypeScript
2 parents f285c9c + 6733533 commit f0fb7a8

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
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';
33

44
const MIN_SMOOTH_SCROLL_EVENT_THROTTLE = 16;
55

6-
const ScrollContext = React.createContext();
6+
type ScrollContextValue = {
7+
contentOffsetY: number;
8+
scrollViewRef: ForwardedRef<ScrollView>;
9+
};
710

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>;
1021

1122
/*
1223
* <ScrollViewWithContext /> is a wrapper around <ScrollView /> that provides a ref to the <ScrollView />.
@@ -15,12 +26,12 @@ const propTypes = ScrollView.propTypes;
1526
* Using this wrapper will automatically handle scrolling to the picker's <TextInput />
1627
* when the picker modal is opened
1728
*/
18-
function ScrollViewWithContext({onScroll, scrollEventThrottle, children, innerRef, ...restProps}) {
29+
function ScrollViewWithContextWithRef({onScroll, scrollEventThrottle, children, ...restProps}: ScrollViewWithContextProps, ref: ForwardedRef<ScrollView>) {
1930
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;
2233

23-
const setContextScrollPosition = (event) => {
34+
const setContextScrollPosition = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
2435
if (onScroll) {
2536
onScroll(event);
2637
}
@@ -48,18 +59,7 @@ function ScrollViewWithContext({onScroll, scrollEventThrottle, children, innerRe
4859
);
4960
}
5061

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-
6262
ScrollViewWithContextWithRef.displayName = 'ScrollViewWithContextWithRef';
6363

64-
export default ScrollViewWithContextWithRef;
64+
export default React.forwardRef(ScrollViewWithContextWithRef);
6565
export {ScrollContext};

0 commit comments

Comments
 (0)