-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathBidirectionalFlatList.tsx
253 lines (226 loc) · 8.15 KB
/
BidirectionalFlatList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import React, { MutableRefObject, useRef, useState } from 'react';
import {
ActivityIndicator,
FlatList as FlatListType,
FlatListProps,
ScrollViewProps,
StyleSheet,
View,
} from 'react-native';
import { FlatList } from '@stream-io/flat-list-mvcp';
const styles = StyleSheet.create({
indicatorContainer: {
paddingVertical: 5,
width: '100%',
},
});
export type Props<T> = Omit<
FlatListProps<T>,
'maintainVisibleContentPosition'
> & {
/**
* Called once when the scroll position gets close to end of list. This must return a promise.
* You can `onEndReachedThreshold` as distance from end of list, when this function should be called.
*/
onEndReached: () => Promise<void>;
/**
* Called once when the scroll position gets close to begining of list. This must return a promise.
* You can `onStartReachedThreshold` as distance from beginning of list, when this function should be called.
*/
onStartReached: () => Promise<void>;
/** Color for inline loading indicator */
activityIndicatorColor?: string;
/**
* Enable autoScrollToTop.
* In chat type applications, you want to auto scroll to bottom, when new message comes it.
*/
enableAutoscrollToTop?: boolean;
/**
* If `enableAutoscrollToTop` is true, the scroll threshold below which auto scrolling should occur.
*/
autoscrollToTopThreshold?: number;
/** Scroll distance from beginning of list, when onStartReached should be called. */
onStartReachedThreshold?: number;
/**
* Scroll distance from end of list, when onStartReached should be called.
* Please note that this is different from onEndReachedThreshold of FlatList from react-native.
*/
onEndReachedThreshold?: number;
/** If true, inline loading indicators will be shown. Default - true */
showDefaultLoadingIndicators?: boolean;
/** Custom UI component for header inline loading indicator */
HeaderLoadingIndicator?: React.ComponentType;
/** Custom UI component for footer inline loading indicator */
FooterLoadingIndicator?: React.ComponentType;
/** Custom UI component for header indicator of FlatList. Only used when `showDefaultLoadingIndicators` is false */
ListHeaderComponent?: React.ComponentType;
/** Custom UI component for footer indicator of FlatList. Only used when `showDefaultLoadingIndicators` is false */
ListFooterComponent?: React.ComponentType;
};
/**
* Note:
* - `onEndReached` and `onStartReached` must return a promise.
* - `onEndReached` and `onStartReached` only get called once, per content length.
* - maintainVisibleContentPosition is fixed, and can't be modified through props.
* - doesn't accept `ListFooterComponent` via prop, since it is occupied by `FooterLoadingIndicator`.
* Set `showDefaultLoadingIndicators` to use `ListFooterComponent`.
* - doesn't accept `ListHeaderComponent` via prop, since it is occupied by `HeaderLoadingIndicator`
* Set `showDefaultLoadingIndicators` to use `ListHeaderComponent`.
*/
export const BidirectionalFlatList = (React.forwardRef(
<T extends any>(
props: Props<T>,
ref:
| ((instance: FlatListType<T> | null) => void)
| MutableRefObject<FlatListType<T> | null>
| null
) => {
const {
activityIndicatorColor = 'black',
autoscrollToTopThreshold = 100,
data,
enableAutoscrollToTop,
FooterLoadingIndicator,
HeaderLoadingIndicator,
ListHeaderComponent,
ListFooterComponent,
onEndReached = () => Promise.resolve(),
onEndReachedThreshold = 10,
onScroll,
onStartReached = () => Promise.resolve(),
onStartReachedThreshold = 10,
showDefaultLoadingIndicators = true,
} = props;
const [onStartReachedInProgress, setOnStartReachedInProgress] = useState(
false
);
const [onEndReachedInProgress, setOnEndReachedInProgress] = useState(false);
const onStartReachedTracker = useRef<Record<number, boolean>>({});
const onEndReachedTracker = useRef<Record<number, boolean>>({});
const onStartReachedInPromise = useRef<Promise<void> | null>(null);
const onEndReachedInPromise = useRef<Promise<void> | null>(null);
const maybeCallOnStartReached = () => {
// If onStartReached has already been called for given data length, then ignore.
if (data?.length && onStartReachedTracker.current[data.length]) {
return;
}
if (data?.length) {
onStartReachedTracker.current[data.length] = true;
}
setOnStartReachedInProgress(true);
const p = () => {
return new Promise<void>((resolve) => {
onStartReachedInPromise.current = null;
setOnStartReachedInProgress(false);
resolve();
});
};
if (onEndReachedInPromise.current) {
onEndReachedInPromise.current.finally(() => {
onStartReachedInPromise.current = onStartReached().then(p);
});
} else {
onStartReachedInPromise.current = onStartReached().then(p);
}
};
const maybeCallOnEndReached = () => {
// If onEndReached has already been called for given data length, then ignore.
if (data?.length && onEndReachedTracker.current[data.length]) {
return;
}
if (data?.length) {
onEndReachedTracker.current[data.length] = true;
}
setOnEndReachedInProgress(true);
const p = () => {
return new Promise<void>((resolve) => {
onStartReachedInPromise.current = null;
setOnEndReachedInProgress(false);
resolve();
});
};
if (onStartReachedInPromise.current) {
onStartReachedInPromise.current.finally(() => {
onEndReachedInPromise.current = onEndReached().then(p);
});
} else {
onEndReachedInPromise.current = onEndReached().then(p);
}
};
const handleScroll: ScrollViewProps['onScroll'] = (event) => {
// Call the parent onScroll handler, if provided.
onScroll?.(event);
const offset = event.nativeEvent.contentOffset.y;
const visibleLength = event.nativeEvent.layoutMeasurement.height;
const contentLength = event.nativeEvent.contentSize.height;
// Check if scroll has reached either start of end of list.
const isScrollAtStart = offset < onStartReachedThreshold;
const isScrollAtEnd =
contentLength - visibleLength - offset < onEndReachedThreshold;
if (isScrollAtStart) {
maybeCallOnStartReached();
}
if (isScrollAtEnd) {
maybeCallOnEndReached();
}
};
const renderHeaderLoadingIndicator = () => {
if (!showDefaultLoadingIndicators) {
if (ListHeaderComponent) {
return <ListHeaderComponent />;
} else {
return null;
}
}
if (!onStartReachedInProgress) return null;
if (HeaderLoadingIndicator) {
return <HeaderLoadingIndicator />;
}
return (
<View style={styles.indicatorContainer}>
<ActivityIndicator size={'small'} color={activityIndicatorColor} />
</View>
);
};
const renderFooterLoadingIndicator = () => {
if (!showDefaultLoadingIndicators) {
if (ListFooterComponent) {
return <ListFooterComponent />;
} else {
return null;
}
}
if (!onEndReachedInProgress) return null;
if (FooterLoadingIndicator) {
return <FooterLoadingIndicator />;
}
return (
<View style={styles.indicatorContainer}>
<ActivityIndicator size={'small'} color={activityIndicatorColor} />
</View>
);
};
return (
<>
<FlatList<T>
{...props}
ref={ref}
progressViewOffset={50}
ListHeaderComponent={renderHeaderLoadingIndicator}
ListFooterComponent={renderFooterLoadingIndicator}
onEndReached={null}
onScroll={handleScroll}
maintainVisibleContentPosition={{
autoscrollToTopThreshold: enableAutoscrollToTop
? autoscrollToTopThreshold
: undefined,
minIndexForVisible: 1,
}}
/>
</>
);
}
) as unknown) as BidirectionalFlatListType;
type BidirectionalFlatListType = <T extends any>(
props: Props<T>
) => React.ReactElement;