-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31697 from fvlvte/25003-migrate-flatlist
[TS migration] Migrate 'FlatList' component to TypeScript
- Loading branch information
Showing
3 changed files
with
43 additions
and
79 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.