Web · Components
FieldArray
Repeating form rows with a ready-made look: title, rows, remove / duplicate buttons, drag to reorder (sortable), add button, empty state and the array's error.
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
Single-value rows (phones), plain appearance
Nested arrays: sections and their items
Import
Repeating form rows with a ready-made look: title, rows, remove / duplicate buttons, drag to reorder (sortable), add button, empty state and the array's error. The logic is the same as useFieldArray.
- When a row is added its first field is focused; when removed, focus moves to the next row (or the add button if none). Both are announced to screen readers.
min/maxlock the buttons automatically; when full, the add button's tooltip says why.- With
sortablerows are dragged by the handle; on the keyboard focus the handle and use Space + arrow keys or Alt + arrow.
Examples
useFieldArray
functionRepeating rows inside a Form (invoice items, phone numbers, education history…): adds, removes, moves and duplicates rows.
- Fields are uncontrolled as usual; when row order changes, the typed values move with the row and the form does not re-render.
- Row field names become
row.name("name")→items[0].name; Form turns them into an array, and Zod schema errors (items[1].quantity) appear under the matching field. - Initial rows come from Form
defaultValues(and the draft); the fields of an added row are filled with the given value. - Even with no rows left the value is sent as
[], and a row with all fields left empty as{}(indexes don't shift). - Rows return to their initial state when the form is reset.
For a ready-made look (title, add/remove buttons, drag to reorder, error, empty state) use FieldArray; this hook is for your own layout.
Examples
UseFieldArrayOptions
interfaceuseFieldArray options.
| Name | Type | Default | Description |
|---|---|---|---|
name | string | Field name of the array ("items"). Names of the fields in rows are generated with row.name("name") as items[0].name; Form turns them into an array on submit, and schema errors come back under the same name. For nested arrays use the parent row's name: row.name("features"). | |
defaultValue? | readonly T[] | Initial rows. If omitted, the array in the nearest Form's defaultValues (or in a restored draft) is used. | |
emptyItem? | T | (() => T) | {} | Initial value of a new row (e.g. { quantity: 1 }); a function is called on each add. If rows are single values (tags[0]), give a string: "". |
min? | number | 0 | Minimum number of rows. If there are fewer initially, empty rows are added; at this count rows cannot be removed. To show the message of a rule like "at least one item", also give min in the schema. |
max? | number | Maximum number of rows; nothing can be added when full. | |
focusOnAdd? | boolean | true | Focuses the first field inside a row when it is added. |
FieldArrayRow
interfaceAn item of useFieldArray fields: one row.
| Name | Type | Description |
|---|---|---|
key | string | Stable key of the row; pass it as key (not the index). |
index | number | Current index of the row (from 0). |
name | (path?: string) => string | Name of a field in the row: row.name("name") → "items[0].name", nested row.name("address.city") → "items[0].address.city". Without an argument, the row's own name ("items[0]"; for single-value rows). |
defaultValue | T | undefined | Initial value of the row (given on add or from defaultValues). Fields already read it themselves; this is for your own controlled component. |
error | string | undefined | Error of the row itself (e.g. the refine of the row object in the schema: "End must be after start"). Field errors appear under the fields. |
FieldArrayAddOptions
interfaceuseFieldArray add options.
| Name | Type | Default | Description |
|---|---|---|---|
focus? | boolean | focusOnAdd | Focus the first field of the new row. |
FieldArrayApi
interfaceResult of useFieldArray.
| Name | Type | Description |
|---|---|---|
name | string | Name of the array. |
fields | FieldArrayRow<T>[] | Rows, in order. See FieldArrayRow. |
length | number | Number of rows. |
append | (value?: T, options?: FieldArrayAddOptions) => void | Appends a row at the end. If no value is given, emptyItem. |
prepend | (value?: T, options?: FieldArrayAddOptions) => void | Prepends a row at the start. |
insert | (index: number, value?: T, options?: FieldArrayAddOptions) => void | Inserts a row at position index. |
remove | (index: number | readonly number[]) => void | Removes the row (or rows); does not go below min. |
move | (from: number, to: number) => void | Moves a row from from to to; typed values move with the row. |
swap | (a: number, b: number) => void | Swaps two rows. |
duplicate | (index: number, options?: FieldArrayAddOptions) => void | Copies the row, with its current typed values, right below it. |
replace | (values: readonly T[]) => void | Replaces all rows with the given ones (fields are rebuilt with these values). |
getValues | () => T[] | Current values of the rows in the form (converted to their types if there is a schema). Outside a form, the rows' initial values are returned. |
canAdd | boolean | max has not been reached. |
canRemove | boolean | Above min. |
error | string | undefined | Error of the array itself (e.g. z.array(...).min(1, "Add at least one item") in the schema) or a server error. A schema error appears once the user adds/removes rows or tries to submit. |
FieldArrayProps
interfaceProps of the FieldArray component.
Extends: Omit<ComponentProps<"fieldset">, "children" | "name" | "defaultValue">
| Name | Type | Default | Description |
|---|---|---|---|
label? | ReactNode | Title of the group (<legend>). | |
description? | ReactNode | Description under the title. | |
children | (row: FieldArrayRow<T>, array: FieldArrayApi<T>) => ReactNode | Fields of a row. Give field names with row.name("name"). The second argument is the whole array (e.g. for a "Move up" button in the row). The row is a container query (@container) box: size columns by the row's width, not the screen (grid @md:grid-cols-[1fr_8rem]); it doesn't overflow in a narrow panel either. | |
sortable? | boolean | Rows are reordered by dragging the handle (mouse, touch, keyboard). See SortableList. | |
removable? | boolean | true | Row remove button. |
duplicable? | boolean | Button that copies the row with its typed values. | |
addLabel? | ReactNode | "Ekle" | Text of the add button. |
footer? | false | ((array: FieldArrayApi<T>) => ReactNode) | Your own footer instead of the add button (e.g. buttons adding a few preset rows). With false the add button is hidden. | |
empty? | ReactNode | "Henüz satır yok" | Content shown when there are no rows. |
rowLabel? | (row: FieldArrayRow<T>) => string | "{sıra}. satır" | Readable name of the row: used in the remove button's name and screen reader announcements (e.g. (row) => \Item ${row.index + 1}\``). |
rowVariant? | "card" | "plain" | "card" | Look of the rows: card is a bordered box, plain just the rows. |
rowClassName? | string | Class of the row box. | |
name | string | Field name of the array ("items"). Names of the fields in rows are generated with row.name("name") as items[0].name; Form turns them into an array on submit, and schema errors come back under the same name. For nested arrays use the parent row's name: row.name("features"). | |
defaultValue? | readonly T[] | Initial rows. If omitted, the array in the nearest Form's defaultValues (or in a restored draft) is used. | |
emptyItem? | T | (() => T) | {} | Initial value of a new row (e.g. { quantity: 1 }); a function is called on each add. If rows are single values (tags[0]), give a string: "". |
min? | number | 0 | Minimum number of rows. If there are fewer initially, empty rows are added; at this count rows cannot be removed. To show the message of a rule like "at least one item", also give min in the schema. |
max? | number | Maximum number of rows; nothing can be added when full. | |
focusOnAdd? | boolean | true | Focuses the first field inside a row when it is added. |