forked from ThienMD/react-native-gridview-flatlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (106 loc) · 2.43 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
'use strict';
import React from 'react';
import {
AppRegistry,
View,
StyleSheet,
FlatList,
} from 'react-native';
var CollectionView = React.createClass({
groupItems: function (items, itemsPerRow) {
var itemsGroups = [];
var group = [];
var flag=false;
items.forEach(function(item) {
if(flag)
{
if (group.length === itemsPerRow) {
itemsGroups.push(group);
group = [item];
} else {
group.push(item);
}
}
else {
flag=true;
}
});
if (group.length > 0) {
itemsGroups.push(group);
}
itemsGroups.unshift([items[0]]);
return itemsGroups;
},
renderGroup: function ({ item, index }) {
var that = this;
if(index==0)
{
var items = item.map(function (obj) {
return that.props.renderHeader(obj, that.props.openModal, that.props.mCon);
});
return (
<View>
{
items
}
{this.getBlankCell(item.length, this.props.itemsPerRow)
}
</View>
);
}
else {
var items = item.map(function (obj, a) {
return that.props.renderItem(obj, that.props.openModal, a, that.props.mCon);
});
items = items.map((obj, i) => {
return (
<View style={{ flex: 1 }}>
{obj}
</View>)
})
return (
<View style={styles.group}>
{
items
}
{this.getBlankCell(item.length, this.props.itemsPerRow)
}
</View>
);
}
},
getBlankCell(currentItems, itemsPerRow) {
if (currentItems < itemsPerRow) {
var arr = []
for (var i = 0; i < itemsPerRow - currentItems; i++) {
arr.push(
<View style={{ flex: 1 }}>
</View>
)
}
return arr
}
},
shouldComponentUpdate: function (nextProps, nextState) {
return JSON.stringify(nextProps.items) !== JSON.stringify(this.props.items);
},
render: function () {
var groups = this.groupItems(this.props.items, this.props.itemsPerRow);
return (<FlatList
data={groups}
{...this.props}
renderItem={this.renderGroup}
/>);
},
});
var styles = StyleSheet.create({
group: {
flexDirection: 'row-reverse',
alignItems: 'center',
justifyContent: 'center',
overflow: 'hidden',
paddingHorizontal: 25,
paddingBottom: 8
}
});
module.exports = CollectionView;