forked from saleel/react-native-super-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlatGrid.js
218 lines (193 loc) · 5.62 KB
/
FlatGrid.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
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
import React, {
forwardRef, memo, useState, useCallback, useMemo,
} from 'react';
import {
View, Dimensions, FlatList,
} from 'react-native';
import PropTypes from 'prop-types';
import { chunkArray, calculateDimensions, generateStyles } from './utils';
const FlatGrid = memo(
forwardRef((props, ref) => {
const {
style,
spacing,
fixed,
data,
itemDimension,
renderItem,
horizontal,
onLayout,
staticDimension,
maxDimension,
additionalRowStyle: externalRowStyle,
itemContainerStyle,
keyExtractor,
...restProps
} = props;
if (props.items && !props.data) {
// eslint-disable-next-line no-console
throw new Error('React Native Super Grid - Prop "items" has been renamed to "data" in version 4');
}
const [totalDimension, setTotalDimension] = useState(() => {
let defaultTotalDimension = staticDimension;
if (!staticDimension) {
const dimension = horizontal ? 'height' : 'width';
defaultTotalDimension = maxDimension || Dimensions.get('window')[dimension];
}
return defaultTotalDimension;
});
const onLayoutLocal = useCallback(
(e) => {
if (!staticDimension) {
const { width, height } = e.nativeEvent.layout || {};
let newTotalDimension = horizontal ? height : width;
if (maxDimension && newTotalDimension > maxDimension) {
newTotalDimension = maxDimension;
}
if (totalDimension !== newTotalDimension) {
setTotalDimension(newTotalDimension);
}
}
// call onLayout prop if passed
if (onLayout) {
onLayout(e);
}
},
[staticDimension, maxDimension, totalDimension, horizontal, onLayout],
);
const renderRow = useCallback(
({
rowItems,
rowIndex,
separators,
isLastRow,
itemsPerRow,
rowStyle,
containerStyle,
}) => {
// To make up for the top padding
let additionalRowStyle = {};
if (isLastRow) {
additionalRowStyle = {
...(!horizontal ? { marginBottom: spacing } : {}),
...(horizontal ? { marginRight: spacing } : {}),
};
}
return (
<View style={[rowStyle, additionalRowStyle, externalRowStyle]}>
{rowItems.map((item, i) => (
<View
key={
keyExtractor
? keyExtractor(item, i)
: `item_${rowIndex * itemsPerRow + i}`
}
style={[containerStyle, itemContainerStyle]}
>
{renderItem({
item,
index: rowIndex * itemsPerRow + i,
separators,
rowIndex,
itemsPerRow,
})}
</View>
))}
</View>
);
},
[renderItem, spacing, keyExtractor, externalRowStyle, itemContainerStyle, horizontal],
);
const { containerDimension, itemsPerRow, fixedSpacing } = useMemo(
() => calculateDimensions({
itemDimension,
staticDimension,
totalDimension,
spacing,
fixed,
}),
[itemDimension, staticDimension, totalDimension, spacing, fixed],
);
const { containerStyle, rowStyle } = useMemo(
() => generateStyles({
horizontal,
itemDimension,
containerDimension,
spacing,
fixedSpacing,
fixed,
}),
[horizontal, itemDimension, containerDimension, spacing, fixedSpacing, fixed],
);
const rows = chunkArray(data, itemsPerRow); // Splitting the data into rows
const localKeyExtractor = useCallback(
(rowItems, index) => {
if (keyExtractor) {
return rowItems
.map((rowItem, rowItemIndex) => keyExtractor(rowItem, rowItemIndex))
.join('_');
}
return `row_${index}`;
},
[keyExtractor],
);
return (
<FlatList
data={rows}
ref={ref}
extraData={totalDimension}
renderItem={({ item, index }) => renderRow({
rowItems: item,
rowIndex: index,
isLastRow: index === rows.length - 1,
itemsPerRow,
rowStyle,
containerStyle,
})
}
style={[
{
...(horizontal
? { paddingLeft: spacing }
: { paddingTop: spacing }),
},
style,
]}
onLayout={onLayoutLocal}
keyExtractor={localKeyExtractor}
{...restProps}
horizontal={horizontal}
/>
);
}),
);
FlatGrid.displayName = 'FlatGrid';
FlatGrid.propTypes = {
renderItem: PropTypes.func.isRequired,
data: PropTypes.arrayOf(PropTypes.any).isRequired,
itemDimension: PropTypes.number,
fixed: PropTypes.bool,
spacing: PropTypes.number,
style: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
additionalRowStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
itemContainerStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
staticDimension: PropTypes.number,
horizontal: PropTypes.bool,
onLayout: PropTypes.func,
keyExtractor: PropTypes.func,
listKey: PropTypes.string,
};
FlatGrid.defaultProps = {
fixed: false,
itemDimension: 120,
spacing: 10,
style: {},
additionalRowStyle: undefined,
itemContainerStyle: undefined,
staticDimension: undefined,
horizontal: false,
onLayout: null,
keyExtractor: null,
listKey: undefined,
};
export default FlatGrid;