Add onScrolledDownChange

This commit is contained in:
Dan Abramov
2023-12-13 04:11:34 +00:00
parent c0eaaf8360
commit b4de3dda7d
6 changed files with 40 additions and 21 deletions
+25 -4
View File
@@ -1,13 +1,34 @@
import React from 'react'
import React, {useState} from 'react'
import {FlatListProps} from 'react-native'
import {FlatList_INTERNAL} from './Views'
import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
export type ListMethods = FlatList_INTERNAL
export type ListProps<ItemT> = FlatListProps<ItemT>
export type ListProps<ItemT> = FlatListProps<ItemT> & {
onScrolledDownChange?: (isScrolledDown: boolean) => void
}
export type ListRef = React.MutableRefObject<FlatList_INTERNAL | null>
function ListImpl<ItemT>(props: ListProps<ItemT>, ref: React.Ref<ListMethods>) {
return <FlatList_INTERNAL {...props} ref={ref} />
const SCROLLED_DOWN_LIMIT = 200
function ListImpl<ItemT>(
{onScrolledDownChange, ...props}: ListProps<ItemT>,
ref: React.Ref<ListMethods>,
) {
const [isScrolledDown, setIsScrolledDown] = useState(false)
// TODO: This ignores the passed-in onScroll completely.
const scrollHandler = useAnimatedScrollHandler({
onScroll(e) {
const didScrollDown = e.contentOffset.y > SCROLLED_DOWN_LIMIT
if (isScrolledDown !== didScrollDown) {
setIsScrolledDown(didScrollDown)
onScrolledDownChange?.(didScrollDown)
}
},
})
return <FlatList_INTERNAL {...props} onScroll={scrollHandler} ref={ref} />
}
export const List = React.forwardRef(ListImpl) as <ItemT>(