import * as React from 'react'; import { SpaceContext } from './context'; export interface ItemProps { className: string; children: React.ReactNode; index: number; direction?: 'horizontal' | 'vertical'; marginDirection: 'marginLeft' | 'marginRight'; split?: React.ReactNode; wrap?: boolean; style?: React.CSSProperties; } export default function Item({ className, direction, index, marginDirection, children, split, wrap, style: customStyle, }: ItemProps) { const { horizontalSize, verticalSize, latestIndex, supportFlexGap } = React.useContext(SpaceContext); let style: React.CSSProperties = {}; if (!supportFlexGap) { if (direction === 'vertical') { if (index < latestIndex) { style = { marginBottom: horizontalSize / (split ? 2 : 1) }; } } else { style = { ...(index < latestIndex && { [marginDirection]: horizontalSize / (split ? 2 : 1) }), ...(wrap && { paddingBottom: verticalSize }), }; } } if (children === null || children === undefined) { return null; } return ( <>
{children}
{index < latestIndex && split && ( {split} )} ); }