Array Field
InputMITDynamic array form field for repeating row inputs with add / remove controls.
bun add @otfdashkit/ui
Preview
Array FieldInput
Loading Storybook preview…
Code
@otfdashkit/uiimport 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.
| Prop | Type | Default | Description |
|---|---|---|---|
| defaultItemrequired | T | ||
| onChangerequired | (v: T[]) => void | ||
| renderItemrequired | (item: T, index: number, remove: () => void) => ReactNode | ||
| valuerequired | T[] | ||
| addLabel | string | 'Add item' | |
| className | string | ||
| maxItems | number |