Skip to content

Commit

Permalink
Merge pull request #31697 from fvlvte/25003-migrate-flatlist
Browse files Browse the repository at this point in the history
[TS migration] Migrate 'FlatList' component to TypeScript
  • Loading branch information
srikarparsi authored Dec 11, 2023
2 parents c1a3411 + 50c02eb commit 320dc96
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 79 deletions.
79 changes: 0 additions & 79 deletions src/components/FlatList/index.android.js

This file was deleted.

43 changes: 43 additions & 0 deletions src/components/FlatList/index.android.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {useFocusEffect} from '@react-navigation/native';
import React, {ForwardedRef, forwardRef, useCallback, useContext} from 'react';
import {FlatList, FlatListProps} from 'react-native';
import {ActionListContext} from '@pages/home/ReportScreenContext';

// FlatList wrapped with the freeze component will lose its scroll state when frozen (only for Android).
// CustomFlatList saves the offset and use it for scrollToOffset() when unfrozen.
function CustomFlatList<T>(props: FlatListProps<T>, ref: ForwardedRef<FlatList>) {
const {scrollPosition, setScrollPosition} = useContext(ActionListContext);

const onScreenFocus = useCallback(() => {
if (typeof ref === 'function') {
return;
}
if (!ref?.current || !scrollPosition?.offset) {
return;
}
if (ref.current && scrollPosition.offset) {
ref.current.scrollToOffset({offset: scrollPosition.offset, animated: false});
}
}, [scrollPosition?.offset, ref]);

useFocusEffect(
useCallback(() => {
onScreenFocus();
}, [onScreenFocus]),
);

return (
<FlatList<T>
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
onScroll={(event) => props.onScroll?.(event)}
onMomentumScrollEnd={(event) => {
setScrollPosition({offset: event.nativeEvent.contentOffset.y});
}}
ref={ref}
/>
);
}

CustomFlatList.displayName = 'CustomFlatListWithRef';
export default forwardRef(CustomFlatList);
File renamed without changes.

0 comments on commit 320dc96

Please sign in to comment.