Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'ScrollViewWithContext.js' component to TypeScript #29674

Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import React, {useState, useRef} from 'react';
import {ScrollView} from 'react-native';
import React, {useState, useRef, ForwardedRef} 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<ScrollView>;
};

// eslint-disable-next-line react/forbid-foreign-prop-types
const propTypes = ScrollView.propTypes;
const ScrollContext = React.createContext<ScrollContextValue>({
contentOffsetY: 0,
scrollViewRef: null,
});

type ScrollViewWithContextProps = {
onScroll: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
children?: React.ReactNode;
scrollEventThrottle: number;
} & Partial<ScrollView>;

/*
* <ScrollViewWithContext /> is a wrapper around <ScrollView /> that provides a ref to the <ScrollView />.
Expand All @@ -15,12 +26,12 @@ const propTypes = ScrollView.propTypes;
* Using this wrapper will automatically handle scrolling to the picker's <TextInput />
* when the picker modal is opened
*/
function ScrollViewWithContext({onScroll, scrollEventThrottle, children, innerRef, ...restProps}) {
function ScrollViewWithContext({onScroll, scrollEventThrottle, children, ...restProps}: ScrollViewWithContextProps, ref: ForwardedRef<ScrollView>) {
const [contentOffsetY, setContentOffsetY] = useState(0);
const defaultScrollViewRef = useRef();
const scrollViewRef = innerRef || defaultScrollViewRef;
const defaultScrollViewRef = useRef<ScrollView>(null);
const scrollViewRef = ref ?? defaultScrollViewRef;

const setContextScrollPosition = (event) => {
const setContextScrollPosition = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
if (onScroll) {
onScroll(event);
}
Expand All @@ -47,14 +58,7 @@ function ScrollViewWithContext({onScroll, scrollEventThrottle, children, innerRe
);
}

ScrollViewWithContext.propTypes = propTypes;
ScrollViewWithContext.displayName = 'ScrollViewWithContext';

export default React.forwardRef((props, ref) => (
<ScrollViewWithContext
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
));
export default React.forwardRef(ScrollViewWithContext);
export {ScrollContext};