-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.js
145 lines (125 loc) · 4.59 KB
/
index.js
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
import PropTypes from 'prop-types';
import React, {forwardRef, useEffect, useRef} from 'react';
import {DeviceEventEmitter, FlatList, StyleSheet} from 'react-native';
import _ from 'underscore';
import styles from '@styles/styles';
import CONST from '@src/CONST';
import BaseInvertedFlatList from './BaseInvertedFlatList';
const propTypes = {
/** Passed via forwardRef so we can access the FlatList ref */
innerRef: PropTypes.shape({
current: PropTypes.instanceOf(FlatList),
}).isRequired,
/** Any additional styles to apply */
// eslint-disable-next-line react/forbid-prop-types
contentContainerStyle: PropTypes.any,
/** Same as for FlatList */
onScroll: PropTypes.func,
};
// This is adapted from https://codesandbox.io/s/react-native-dsyse
// It's a HACK alert since FlatList has inverted scrolling on web
function InvertedFlatList(props) {
const {innerRef, contentContainerStyle} = props;
const listRef = React.createRef();
const lastScrollEvent = useRef(null);
const scrollEndTimeout = useRef(null);
const updateInProgress = useRef(false);
const eventHandler = useRef(null);
useEffect(() => {
if (!_.isFunction(innerRef)) {
// eslint-disable-next-line no-param-reassign
innerRef.current = listRef.current;
} else {
innerRef(listRef);
}
return () => {
if (scrollEndTimeout.current) {
clearTimeout(scrollEndTimeout.current);
}
if (eventHandler.current) {
eventHandler.current.remove();
}
};
}, [innerRef, listRef]);
/**
* Emits when the scrolling is in progress. Also,
* invokes the onScroll callback function from props.
*
* @param {Event} event - The onScroll event from the FlatList
*/
const onScroll = (event) => {
props.onScroll(event);
if (!updateInProgress.current) {
updateInProgress.current = true;
eventHandler.current = DeviceEventEmitter.emit(CONST.EVENTS.SCROLLING, true);
}
};
/**
* Emits when the scrolling has ended.
*/
const onScrollEnd = () => {
eventHandler.current = DeviceEventEmitter.emit(CONST.EVENTS.SCROLLING, false);
updateInProgress.current = false;
};
/**
* Decides whether the scrolling has ended or not. If it has ended,
* then it calls the onScrollEnd function. Otherwise, it calls the
* onScroll function and pass the event to it.
*
* This is a temporary work around, since react-native-web doesn't
* support onScrollBeginDrag and onScrollEndDrag props for FlatList.
* More info:
* https://github.com/necolas/react-native-web/pull/1305
*
* This workaround is taken from below and refactored to fit our needs:
* https://github.com/necolas/react-native-web/issues/1021#issuecomment-984151185
*
* @param {Event} event - The onScroll event from the FlatList
*/
const handleScroll = (event) => {
onScroll(event);
const timestamp = Date.now();
if (scrollEndTimeout.current) {
clearTimeout(scrollEndTimeout.current);
}
if (lastScrollEvent.current) {
scrollEndTimeout.current = setTimeout(() => {
if (lastScrollEvent.current !== timestamp) {
return;
}
// Scroll has ended
lastScrollEvent.current = null;
onScrollEnd();
}, 250);
}
lastScrollEvent.current = timestamp;
};
return (
<BaseInvertedFlatList
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={listRef}
shouldMeasureItems
contentContainerStyle={StyleSheet.compose(contentContainerStyle, styles.justifyContentEnd)}
onScroll={handleScroll}
// We need to keep batch size to one to workaround a bug in react-native-web.
// This can be removed once https://github.com/Expensify/App/pull/24482 is merged.
maxToRenderPerBatch={1}
/>
);
}
InvertedFlatList.propTypes = propTypes;
InvertedFlatList.defaultProps = {
contentContainerStyle: {},
onScroll: () => {},
};
InvertedFlatList.displayName = 'InvertedFlatList';
const InvertedFlatListWithRef = forwardRef((props, ref) => (
<InvertedFlatList
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
));
InvertedFlatListWithRef.displayName = 'InvertedFlatListWithRef';
export default InvertedFlatListWithRef;