-
Notifications
You must be signed in to change notification settings - Fork 153
/
utils.js
183 lines (161 loc) · 5.24 KB
/
utils.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { StyleSheet } from 'react-native';
function chunkArray(array, size) {
if (!array || array.length === 0) return [];
return array.reduce((acc, val) => {
if (acc.length === 0) acc.push([]);
const last = acc[acc.length - 1];
const rowHadFullWidth = last[0] && last[0]._fullWidth;
const currentIsFullWidth = !!val._fullWidth;
if (last.length < size && !rowHadFullWidth && !currentIsFullWidth) {
last.push(val);
} else {
acc.push([val]);
}
return acc;
}, []);
}
function calculateDimensions({
itemDimension,
staticDimension,
totalDimension,
fixed,
spacing,
maxItemsPerRow,
}) {
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.min(Math.floor(availableDimension / itemTotalDimension), maxItemsPerRow || Infinity);
const containerDimension = availableDimension / itemsPerRow;
let fixedSpacing;
if (fixed) {
fixedSpacing = (totalDimension - (itemDimension * itemsPerRow)) / (itemsPerRow + 1);
}
return {
itemTotalDimension,
availableDimension,
itemsPerRow,
containerDimension,
fixedSpacing,
};
}
function getStyleDimensions(
style,
horizontal = false,
) {
let space1 = 0;
let space2 = 0;
let maxStyleDimension;
if (style) {
const flatStyle = Array.isArray(style) ? StyleSheet.flatten(style) : style;
let sMaxDimensionXY = 'maxWidth';
let sPaddingXY = 'paddingHorizontal';
let sPadding1 = 'paddingLeft';
let sPadding2 = 'paddingRight';
if (horizontal) {
sMaxDimensionXY = 'maxHeight';
sPaddingXY = 'paddingVertical';
sPadding1 = 'paddingTop';
sPadding2 = 'paddingBottom';
}
if (flatStyle[sMaxDimensionXY] && typeof flatStyle[sMaxDimensionXY] === 'number') {
maxStyleDimension = flatStyle[sMaxDimensionXY];
}
const padding = flatStyle[sPaddingXY] || flatStyle.padding;
const padding1 = flatStyle[sPadding1] || padding || 0;
const padding2 = flatStyle[sPadding2] || padding || 0;
space1 = (typeof padding1 === 'number' ? padding1 : 0);
space2 = (typeof padding2 === 'number' ? padding2 : 0);
}
return { space1, space2, maxStyleDimension };
}
function getAdjustedTotalDimensions({
totalDimension,
maxDimension,
contentContainerStyle,
style,
horizontal = false,
adjustGridToStyles = false,
}) {
let adjustedTotalDimension = totalDimension;
let actualMaxDimension = totalDimension; // keep track of smallest max dimension
// adjust for maxDimension prop
if (maxDimension && totalDimension > maxDimension) {
actualMaxDimension = maxDimension;
adjustedTotalDimension = maxDimension;
}
if (adjustGridToStyles) {
if (contentContainerStyle) {
const { space1, space2, maxStyleDimension } = getStyleDimensions(contentContainerStyle, horizontal);
// adjust for maxWidth or maxHeight in contentContainerStyle
if (maxStyleDimension && adjustedTotalDimension > maxStyleDimension) {
actualMaxDimension = maxStyleDimension;
adjustedTotalDimension = maxStyleDimension;
}
// subtract horizontal or vertical padding from adjustedTotalDimension
if (space1 || space2) {
adjustedTotalDimension = adjustedTotalDimension - space1 - space2;
}
}
if (style) {
const edgeSpaceDiff = (totalDimension - actualMaxDimension) / 2; // if content is floating in middle of screen get margin on either side
const { space1, space2 } = getStyleDimensions(style, horizontal);
// only subtract if space is greater than the margin on either side
if (space1 > edgeSpaceDiff) {
adjustedTotalDimension -= (space1 - edgeSpaceDiff); // subtract the padding minus any remaining margin
}
if (space2 > edgeSpaceDiff) {
adjustedTotalDimension -= (space2 - edgeSpaceDiff); // subtract the padding minus any remaining margin
}
}
}
return adjustedTotalDimension;
}
function generateStyles({
itemDimension,
containerDimension,
spacing,
fixed,
horizontal,
fixedSpacing,
itemsPerRow,
}) {
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,
};
const containerFullWidthStyle = {
flexDirection: 'column',
justifyContent: 'center',
width: containerDimension * itemsPerRow - spacing,
marginBottom: 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 {
containerFullWidthStyle,
containerStyle,
rowStyle,
};
}
export {
chunkArray, calculateDimensions, generateStyles, getAdjustedTotalDimensions,
};