11// TODO: Make this a common component for all horizontal lists in this lib
22import React , { forwardRef , useCallback , useMemo , useRef } from 'react' ;
33import { DataProvider , LayoutProvider , RecyclerListView , RecyclerListViewProps } from 'recyclerlistview' ;
4+ import inRange from 'lodash/inRange' ;
5+
46import constants from '../commons/constants' ;
57
68const dataProviderMaker = ( items : string [ ] ) => new DataProvider ( ( item1 , item2 ) => item1 !== item2 ) . cloneWithRows ( items ) ;
@@ -12,6 +14,9 @@ export interface InfiniteListProps
1214 pageWidth ?: number ;
1315 pageHeight ?: number ;
1416 onPageChange ?: ( pageIndex : number , prevPageIndex : number ) => void ;
17+ onReachEdge ?: ( pageIndex : number ) => void ;
18+ onReachNearEdge ?: ( pageIndex : number ) => void ;
19+ nearEdgeThreshold ?: number ;
1520 initialPageIndex ?: number ;
1621}
1722
@@ -20,8 +25,11 @@ const InfiniteList = (props: InfiniteListProps, ref: any) => {
2025 renderItem,
2126 data,
2227 pageWidth = constants . screenWidth ,
23- pageHeight = constants . screenHeight ,
28+ pageHeight = constants . screenHeight ,
2429 onPageChange,
30+ onReachEdge,
31+ onReachNearEdge,
32+ nearEdgeThreshold,
2533 initialPageIndex = 0 ,
2634 extendedState,
2735 scrollViewProps
@@ -49,15 +57,25 @@ const InfiniteList = (props: InfiniteListProps, ref: any) => {
4957 if ( pageIndex . current !== currPageIndex ) {
5058 if ( pageIndex . current !== undefined ) {
5159 onPageChange ?.( currPageIndex , pageIndex . current ) ;
60+
61+ if ( currPageIndex === 0 || currPageIndex === data . length - 1 ) {
62+ onReachEdge ?.( currPageIndex ) ;
63+ } else if ( nearEdgeThreshold && ! inRange ( currPageIndex , nearEdgeThreshold , data . length - nearEdgeThreshold ) ) {
64+ onReachNearEdge ?.( currPageIndex ) ;
65+ }
5266 }
5367 pageIndex . current = currPageIndex ;
5468 }
5569
5670 props . onScroll ?.( event , offsetX , offsetY ) ;
5771 } ,
58- [ props . onScroll ]
72+ [ props . onScroll , data . length ]
5973 ) ;
6074
75+ const style = useMemo ( ( ) => {
76+ return { height : pageHeight } ;
77+ } , [ pageHeight ] ) ;
78+
6179 return (
6280 < RecyclerListView
6381 ref = { ref }
@@ -69,6 +87,7 @@ const InfiniteList = (props: InfiniteListProps, ref: any) => {
6987 initialRenderIndex = { initialPageIndex }
7088 renderAheadOffset = { 5 * pageWidth }
7189 onScroll = { onScroll }
90+ style = { style }
7291 scrollViewProps = { {
7392 pagingEnabled : true ,
7493 bounces : false ,
0 commit comments