forked from lucholaf/react-native-grid-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (59 loc) · 1.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
'use strict';
var React = require('react-native');
var {
AppRegistry,
View,
StyleSheet,
ListView,
} = React;
var CollectionView = React.createClass({
groupItems: function(items, itemsPerRow) {
var itemsGroups = [];
var group = [];
items.forEach(function(item) {
if (group.length === itemsPerRow) {
itemsGroups.push(group);
group = [item];
} else {
group.push(item);
}
});
if (group.length > 0) {
itemsGroups.push(group);
}
return itemsGroups;
},
getInitialState: function() {
return {items: [], renderItem: null, style: undefined, itemsPerRow: 1, onEndReached: undefined};
},
renderGroup: function(group) {
var that = this;
var items = group.map(function(item) {
return that.props.renderItem(item);
});
return (
<View style={styles.group}>
{items}
</View>
);
},
render: function() {
var groups = this.groupItems(this.props.items, this.props.itemsPerRow);
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
return (<ListView
dataSource={ds.cloneWithRows(groups)}
renderRow={this.renderGroup}
style={this.props.style}
onEndReached={this.props.onEndReached}
scrollEnabled={this.props.scrollEnabled}
/>);
},
});
var styles = StyleSheet.create({
group: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
}
});
module.exports = CollectionView;