import { useMemo, type ReactNode } from 'react' export interface ForProps { each: readonly T[] fallback?: NonNullable | null children: (item: T, index: number) => U } /** * Creates ann array of elements from a list * * @param props.each The list to iterate over * @param props.fallback An optional fallback to render if the list is empty * * @returns a map callback as its child; if the list is empty, the optional fallback is returned. * * @example * No items}> * {(item, index) =>
{item}
} *
* * @see https://design.pluralsight.com/docs/reference/components/for */ export function For(props: ForProps) { const { each, fallback, children } = props return useMemo(() => { if (each.length) { return each.map(children) } return fallback ?? null }, [each, fallback, children]) as unknown as U }