import React from 'react'; import { View, StyleSheet } from 'react-native'; import { getRandomColor } from '../../utils'; import type { TilesProps } from './Tiles.types'; export const Tiles: React.FC = ({ space, columns, empty, fill, justifyContent, alignItems, children, centered, devMode, style, }) => { const numberOfRows = Math.ceil(React.Children.count(children) / columns); const lastRowItemCount = React.Children.count(children) % columns; const shouldFillLastRow = empty && lastRowItemCount !== 0; const dev = devMode && { backgroundColor: getRandomColor(), borderColor: getRandomColor(), borderWidth: 1, }; const position = centered ? ({ justifyContent: 'center', alignItems: 'center', } as const) : { justifyContent, alignItems, }; const filled = fill && ({ height: `${100 / numberOfRows}%` } as const); const renderChildren = () => { const elements = React.Children.toArray(children); if (shouldFillLastRow) { for (let i = 0; i < columns - lastRowItemCount; i++) { elements.push( ); } } return elements.map((child, index) => ( {child} )); }; return ( {new Array(numberOfRows).fill(null).map((_, rowIndex) => ( {renderChildren().slice(rowIndex * columns, (rowIndex + 1) * columns)} ))} ); }; Tiles.displayName = 'Tiles'; const styles = StyleSheet.create({ container: { flexDirection: 'row', flexWrap: 'wrap', }, row: { flexDirection: 'row', width: '100%', }, });