-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
91 lines (76 loc) · 1.71 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
'use strict';
import React, {Component} from "react";
import {
AppRegistry,
View,
StyleSheet,
FlatList,
} from 'react-native';
export default class GridView extends Component {
groupItems (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;
}
renderGroup= ({ item })=> {
var that = this;
var items = item.map(function (obj, index) {
return that.props.renderItem(obj, index);
});
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
}
}
render () {
console.log('props is',this.props.items, this.props.itemsPerRow);
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',
alignItems: 'flex-start',
justifyContent: 'center',
overflow: 'hidden'
}
});