forked from saleel/react-native-super-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
85 lines (75 loc) · 2 KB
/
utils.js
File metadata and controls
85 lines (75 loc) · 2 KB
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
function chunkArray(array = [], size) {
if (array === []) return [];
return array.reduce((acc, val) => {
if (acc.length === 0) acc.push([]);
const last = acc[acc.length - 1];
if (last.length < size) {
last.push(val);
} else {
acc.push([val]);
}
return acc;
}, []);
}
function calculateDimensions({
itemDimension,
staticDimension,
totalDimension,
fixed,
spacing,
}) {
const usableTotalDimension = staticDimension || totalDimension;
const availableDimension = usableTotalDimension - spacing; // One spacing extra
const itemTotalDimension = Math.min(itemDimension + spacing, availableDimension); // itemTotalDimension should not exceed availableDimension
const itemsPerRow = Math.floor(availableDimension / itemTotalDimension);
const containerDimension = availableDimension / itemsPerRow;
let fixedSpacing;
if (fixed) {
fixedSpacing = (totalDimension - (itemDimension * itemsPerRow)) / (itemsPerRow + 1);
}
return {
itemTotalDimension,
availableDimension,
itemsPerRow,
containerDimension,
fixedSpacing,
};
}
function generateStyles({
itemDimension,
containerDimension,
spacing,
fixed,
horizontal,
fixedSpacing,
}) {
let rowStyle = {
flexDirection: 'row',
paddingLeft: fixed ? fixedSpacing : spacing,
paddingBottom: spacing,
};
let containerStyle = {
flexDirection: 'column',
justifyContent: 'center',
width: fixed ? itemDimension : (containerDimension - spacing),
marginRight: fixed ? fixedSpacing : spacing,
};
if (horizontal) {
rowStyle = {
flexDirection: 'column',
paddingTop: fixed ? fixedSpacing : spacing,
paddingRight: spacing,
};
containerStyle = {
flexDirection: 'row',
justifyContent: 'center',
height: fixed ? itemDimension : (containerDimension - spacing),
marginBottom: fixed ? fixedSpacing : spacing,
};
}
return {
containerStyle,
rowStyle,
};
}
export { chunkArray, calculateDimensions, generateStyles };