mirror of
https://github.com/zhigang1992/react-native-super-grid.git
synced 2026-01-12 22:50:58 +08:00
86 lines
1.9 KiB
JavaScript
86 lines
1.9 KiB
JavaScript
|
|
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 itemTotalDimension = itemDimension + spacing;
|
|
const availableDimension = usableTotalDimension - spacing; // One spacing extra
|
|
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 };
|