Skip to content

Commit 592564a

Browse files
committed
Support onEdge and onNearEdge callbacks for inifinitelist
1 parent 2b5eb1e commit 592564a

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/infinite-list/index.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// TODO: Make this a common component for all horizontal lists in this lib
22
import React, {forwardRef, useCallback, useMemo, useRef} from 'react';
33
import {DataProvider, LayoutProvider, RecyclerListView, RecyclerListViewProps} from 'recyclerlistview';
4+
import inRange from 'lodash/inRange';
5+
46
import constants from '../commons/constants';
57

68
const 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,

src/timeline-list/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Context from '../expandableCalendar/Context';
77
import {UpdateSources} from '../expandableCalendar/commons';
88
import Timeline, {TimelineProps} from '../timeline/Timeline';
99
import InfiniteList from '../infinite-list';
10-
import useTimelinePages, {INITIAL_PAGE} from './useTimelinePages';
10+
import useTimelinePages, {INITIAL_PAGE, NEAR_EDGE_THRESHOLD} from './useTimelinePages';
1111

1212
export interface TimelineListProps {
1313
events: {[date: string]: TimelineProps['events']};
@@ -65,15 +65,15 @@ const TimelineList = (props: TimelineListProps) => {
6565
const newDate = pagesRef.current[pageIndex];
6666
if (newDate !== prevDate.current) {
6767
setDate(newDate, UpdateSources.LIST_DRAG);
68-
69-
if (isNearEdges(pageIndex)) {
70-
shouldResetPages.current = isNearEdges(pageIndex);
71-
}
7268
}
7369
}, 0),
7470
[]
7571
);
7672

73+
const onReachNearEdge = useCallback(() => {
74+
shouldResetPages.current = true;
75+
}, []);
76+
7777
const onTimelineOffsetChange = useCallback(offset => {
7878
setTimelineOffset(offset);
7979
}, []);
@@ -109,6 +109,8 @@ const TimelineList = (props: TimelineListProps) => {
109109
data={pages}
110110
renderItem={renderPage}
111111
onPageChange={onPageChange}
112+
onReachNearEdge={onReachNearEdge}
113+
nearEdgeThreshold={NEAR_EDGE_THRESHOLD}
112114
onScroll={onScroll}
113115
extendedState={{todayEvents: events[date], pages}}
114116
initialPageIndex={INITIAL_PAGE}

src/timeline-list/useTimelinePages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import debounce from 'lodash/debounce';
77
import constants from '../commons/constants';
88
import {generateDay} from '../dateutils';
99

10-
const PAGES_COUNT = 100;
11-
const NEAR_EDGE_THRESHOLD = 10;
10+
const PAGES_COUNT = 30;
11+
export const NEAR_EDGE_THRESHOLD = 10;
1212
export const INITIAL_PAGE = Math.floor(PAGES_COUNT / 2);
1313

1414

0 commit comments

Comments
 (0)