-
Notifications
You must be signed in to change notification settings - Fork 3k
/
BaseInvertedFlatList.js
160 lines (136 loc) · 5.57 KB
/
BaseInvertedFlatList.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import PropTypes from 'prop-types';
import React, {forwardRef, useCallback, useRef} from 'react';
import {FlatList as NativeFlatlist, View} from 'react-native';
import _ from 'underscore';
import FlatList from '@components/FlatList';
import * as CollectionUtils from '@libs/CollectionUtils';
import variables from '@styles/variables';
const propTypes = {
/** Same as FlatList can be any array of anything */
// eslint-disable-next-line react/forbid-prop-types
data: PropTypes.arrayOf(PropTypes.any),
/** Same as FlatList although we wrap it in a measuring helper before passing to the actual FlatList component */
renderItem: PropTypes.func.isRequired,
/** This must be set to the minimum size of one of the renderItem rows. Web experiences issues when inaccurate. */
initialRowHeight: PropTypes.number.isRequired,
/** Passed via forwardRef so we can access the FlatList ref */
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(NativeFlatlist)})]).isRequired,
/** Should we measure these items and call getItemLayout? */
shouldMeasureItems: PropTypes.bool,
};
const defaultProps = {
data: [],
shouldMeasureItems: false,
};
function BaseInvertedFlatList(props) {
const {initialRowHeight, shouldMeasureItems, innerRef, renderItem} = props;
// Stores each item's computed height after it renders
// once and is then referenced for the life of this component.
// This is essential to getting FlatList inverted to work on web
// and also enables more predictable scrolling on native platforms.
const sizeMap = useRef({});
/**
* Return default or previously cached height for
* a renderItem row
*
* @param {*} data
* @param {Number} index
*
* @return {Object}
*/
const getItemLayout = (data, index) => {
const size = sizeMap.current[index];
if (size) {
return {
length: size.length,
offset: size.offset,
index,
};
}
// If we don't have a size yet means we haven't measured this
// item yet. However, we can still calculate the offset by looking
// at the last size we have recorded (if any)
const lastMeasuredItem = CollectionUtils.lastItem(sizeMap.current);
return {
// We haven't measured this so we must return the minimum row height
length: initialRowHeight,
// Offset will either be based on the lastMeasuredItem or the index +
// initialRowHeight since we can only assume that all previous items
// have not yet been measured
offset: _.isUndefined(lastMeasuredItem) ? initialRowHeight * index : lastMeasuredItem.offset + initialRowHeight,
index,
};
};
/**
* Measure item and cache the returned length (a.k.a. height)
*
* @param {React.NativeSyntheticEvent} nativeEvent
* @param {Number} index
*/
const measureItemLayout = useCallback((nativeEvent, index) => {
const computedHeight = nativeEvent.layout.height;
// We've already measured this item so we don't need to
// measure it again.
if (sizeMap.current[index]) {
return;
}
const previousItem = sizeMap.current[index - 1] || {};
// If there is no previousItem this can mean we haven't yet measured
// the previous item or that we are at index 0 and there is no previousItem
const previousLength = previousItem.length || 0;
const previousOffset = previousItem.offset || 0;
sizeMap.current[index] = {
length: computedHeight,
offset: previousLength + previousOffset,
};
}, []);
/**
* Render item method wraps the prop renderItem to render in a
* View component so we can attach an onLayout handler and
* measure it when it renders.
*
* @param {Object} params
* @param {Object} params.item
* @param {Number} params.index
*
* @return {React.Component}
*/
const renderItemFromProp = useCallback(
({item, index}) => {
if (shouldMeasureItems) {
return <View onLayout={({nativeEvent}) => measureItemLayout(nativeEvent, index)}>{renderItem({item, index})}</View>;
}
return renderItem({item, index});
},
[shouldMeasureItems, measureItemLayout, renderItem],
);
return (
<FlatList
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={innerRef}
renderItem={renderItemFromProp}
// Native platforms do not need to measure items and work fine without this.
// Web requires that items be measured or else crazy things happen when scrolling.
getItemLayout={shouldMeasureItems ? getItemLayout : undefined}
windowSize={15}
maintainVisibleContentPosition={{
minIndexForVisible: 0,
autoscrollToTopThreshold: variables.listItemHeightNormal,
}}
inverted
/>
);
}
BaseInvertedFlatList.propTypes = propTypes;
BaseInvertedFlatList.defaultProps = defaultProps;
BaseInvertedFlatList.displayName = 'BaseInvertedFlatList';
const BaseInvertedFlatListWithRef = forwardRef((props, ref) => (
<BaseInvertedFlatList
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
));
BaseInvertedFlatListWithRef.displayName = 'BaseInvertedFlatListWithRef';
export default BaseInvertedFlatListWithRef;