-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (50 loc) · 1.41 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
import React, { Component } from "react";
import { StyleSheet, View, ListView, Dimensions } from "react-native";
import PropTypes from "prop-types";
const { width } = Dimensions.get("window");
export default (Tiles = (
{ style = {}, tilesPerRow = 3, dataSource, renderItem }
) => {
const tileDimensions = calcTileDimensions(width, tilesPerRow);
return (
<View style={{ flex: 1 }}>
<ListView
contentContainerStyle={[style || {}, styles.list]}
dataSource={dataSource}
renderRow={i => {
const Hoc = MakeHOCItem({
...tileDimensions
})(renderItem);
return <Hoc itemData={i} />;
}}
/>
</View>
);
});
Tiles.propTypes = {
style: PropTypes.object,
tilesPerRow: PropTypes.number,
renderItem: PropTypes.func.isRequired,
dataSource: PropTypes.instanceOf(ListView.DataSource).isRequired
};
const MakeHOCItem = ({ size, margin }) =>
Comp =>
({ itemData }) => {
return (
<View style={{ width: size, height: size, marginHorizontal: margin }}>
{Comp(itemData, size)}
</View>
);
};
const calcTileDimensions = (deviceWidth, tpr) => {
const margin = deviceWidth / (tpr * 10);
const size = (deviceWidth - margin * (tpr * 2)) / tpr;
return { size, margin };
};
const styles = StyleSheet.create({
list: {
justifyContent: "flex-start",
flexDirection: "row",
flexWrap: "wrap"
}
});