Skip to content

Array Field

InputMIT
View in Storybook

Dynamic array form field for repeating row inputs with add / remove controls.

bun add @otfdashkit/ui

Preview

Array Field
Loading Storybook preview…

Code

import React, { useState } from 'react'
import { Field, ArrayField, Input } from '@otfdashkit/ui'

function ArrayFieldDemo() {
  const [items, setItems] = useState([''])
  return (
    <div className="max-w-sm">
      <Field label="Email addresses">
        <ArrayField
          value={items}
          onChange={setItems}
          defaultItem=""
          addLabel="Add email"
          maxItems={5}
          renderItem={(item, index, remove) => (
            <Input
              value={item}
              placeholder={`Email ${index + 1}`}
              onChange={e => {
                const next = [...items]
                next[index] = e.target.value
                setItems(next)
              }}
              onKeyDown={e => e.key === 'Backspace' && item === '' && remove()}
            />
          )}
        />
      </Field>
    </div>
  )
}

export function Example() {
  return (
    <ArrayFieldDemo />
  )
}

Free with the SDK. This component ships in @otfdashkit/ui under the MIT license.

Props

Props declared in the ArrayField source. Inherited HTML attributes are not listed.

PropTypeDefaultDescription
defaultItemrequiredT
onChangerequired(v: T[]) => void
renderItemrequired(item: T, index: number, remove: () => void) => ReactNode
valuerequiredT[]
addLabelstring'Add item'
classNamestring
maxItemsnumber