import type { PropsWithChildren } from "react"; import React, { Children } from "react"; import { View } from "react-native"; import chunk from "lodash/chunk"; import { columnStyles, gapStyles, styles } from "./Flex.styles"; import type { FlexProps } from "./types"; import { Content } from "../Content"; export function Flex({ template = [], align = "center", gap = "base", children, }: PropsWithChildren) { if (template.length === 1) { console.warn("Please use component for a stacked layout"); } const childrenArray = Children.toArray(children); const chunkedChildren = chunk( childrenArray, template.length || childrenArray.length, ); return ( {chunkedChildren.map((childArray, rowIndex) => ( {injectChild(childArray)} ))} ); function injectChild(value: ReturnType) { const hasMoreRows = chunkedChildren.length > 1; const childrenCount = value.length; const templateCount = template.length; if (hasMoreRows && childrenCount < templateCount) { const missingChildCount = templateCount - childrenCount; for (let index = 0; index < missingChildCount; index++) { value.push(); } } return value; } } function Row({ template = [], align = "center", gap = "base", children, }: PropsWithChildren) { return ( {Children.map(children, (child, index) => ( 0 && gap && gapStyles[gap], ]} > {child} ))} ); }