-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
178 lines (163 loc) · 4.16 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
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
/**
* React Native Grid Component
* https://github.com/phil-r/react-native-grid-component
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
View,
Dimensions,
FlatList,
SectionList
} from 'react-native';
const { width } = Dimensions.get('window');
// http://stackoverflow.com/questions/8495687/split-array-into-chunks
// I don't see the reason to take lodash.chunk for this
const chunk = (arr, n) =>
Array.from(Array(Math.ceil(arr.length / n)), (_, i) =>
arr.slice(i * n, i * n + n)
);
const prepareData = ({ data, numColumns, itemsPerRow }) => {
const col = numColumns || itemsPerRow;
const missing = (col - (data.length % col)) % col;
return [
...data,
...Array.from(Array(missing)).map(() => ({ __empty: true }))
];
};
const prepareSectionedData = ({ data, numColumns, itemsPerRow }) => {
const col = numColumns || itemsPerRow;
if (Array.isArray(data)) {
return data.map(row => ({
...row,
data: chunk(prepareData({ data: row.data, numColumns: col }), col)
}));
} else if (typeof data === 'object') {
// support of v1
return Object.keys(data).map(title => ({
title,
data: chunk(prepareData({ data: data[title], numColumns: col }), col)
}));
}
return [];
};
export default class Grid extends Component {
static propTypes = {
itemsPerRow: PropTypes.number,
numColumns: PropTypes.number,
renderItem: PropTypes.func.isRequired,
renderPlaceholder: PropTypes.func,
data: PropTypes.arrayOf(PropTypes.any).isRequired,
sections: PropTypes.bool,
style: PropTypes.object
};
static defaultProps = {
itemsPerRow: 3,
renderPlaceholder: null,
sections: false
};
constructor(props) {
super(props);
if (props.sections === true) {
this.state = {
data: prepareSectionedData(this.props)
};
} else {
this.state = {
data: prepareData(this.props)
};
}
}
static getDerivedStateFromProps(nextProps) {
if (nextProps.sections === true) {
return {
data: prepareSectionedData(nextProps)
};
} else {
return {
data: prepareData(nextProps)
};
}
}
_renderPlaceholder = () => (
<View style={{ width: width / this.props.itemsPerRow }} />
);
_renderItem = ({ item, index }) => {
if (item) {
if (typeof item === 'object' && item.__empty) {
// render a placeholder
if (this.props.renderPlaceholder) {
return this.props.renderPlaceholder(index);
}
return this._renderPlaceholder(index);
}
return this.props.renderItem(item, index);
}
};
_renderRow = ({ item }) => (
<View style={styles.row}>
{item.map((item, i) => {
if (item) {
return this.props.renderItem(item, i);
}
// render a placeholder
if (this.props.renderPlaceholder) {
return this.props.renderPlaceholder(i);
}
return this._renderPlaceholder(i);
})}
</View>
);
render() {
// TODO: find a better way to filter props that we pass to ListView
/* eslint-disable no-unused-vars */
const {
renderPlaceholder,
renderItem,
itemsPerRow,
sections,
style,
data,
numColumns, // To depricate itemsPerRow
...props
} = this.props;
/* eslint-enable no-unused-vars */
return (
<View style={styles.container}>
{sections ? (
<SectionList
{...props}
sections={this.state.data}
numColumns={numColumns || itemsPerRow}
style={[styles.list, style]}
renderItem={this._renderRow}
/>
) : (
<FlatList
{...props}
data={this.state.data}
numColumns={numColumns || itemsPerRow}
style={[styles.list, style]}
renderItem={this._renderItem}
/>
)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
list: {
flex: 1
},
row: {
justifyContent: 'space-around',
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'flex-start',
flex: 1
}
});