File size: 671 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type { Dict } from "../../utils"

export interface ForProps<T> {
  /**
   * The array to iterate over
   */
  each: T[] | readonly T[] | undefined
  /**
   * The fallback content to render when the array is empty
   */
  fallback?: React.ReactNode | undefined
  /**
   * The render function to render each item in the array
   */
  children: (item: Exclude<T, undefined>, index: number) => React.ReactNode
}

export function For<T extends string | number | Dict | undefined>(
  props: ForProps<T>,
): React.ReactNode {
  const { each, fallback, children } = props

  if (each?.length === 0) {
    return fallback || null
  }

  return each?.map(children as any)
}