Skip to Content

React FieldArray and RepeaterField

Build accessible repeatable and nested React form fields with caller-controlled values, add/remove constraints, ordering, and typed validation.

Use FieldArray to render a controlled list of repeated form fields. It supplies semantic groups and add, remove, and optional reorder actions; the caller owns each item’s data, nested fields, validation rules, and submission. RepeaterField is an alias for the same component and props.

Beta API maturity: Beta. The API is available for evaluation and may change before it is considered stable.

Import

import { FieldArray, RepeaterField } from "@puzzle-ui/ui";

Repeatable fields

Provide items, onChange, a createItem factory, and a renderItem function. The rendering function receives the current item, index, and total count so you can compose any UI controls, including another FieldArray for nested values.

390 × 844

The RepeaterField name is an equivalent alias for teams that prefer that terminology:

import { useState } from "react";
import { FormField, RepeaterField, TextInput } from "@puzzle-ui/ui";

const Tags = () => {
  const [tags, setTags] = useState(["news"]);

  return (
    <RepeaterField
      label="Tags"
      items={tags}
      onChange={setTags}
      createItem={() => ""}
      renderItem={({ item, index }) => (
        <FormField label={`Tag ${index + 1}`}>
          <TextInput
            value={item}
            onChange={(event) =>
              setTags((current) =>
                current.map((tag, tagIndex) =>
                  tagIndex === index ? event.currentTarget.value : tag,
                ),
              )
            }
          />
        </FormField>
      )}
    />
  );
};

Use getItemKey when entries have a stable identifier, especially when reordering. By default, the array index is used as the React key.

Validation and bounds

Use minItems and maxItems to disable removal or addition at the configured limits. The optional synchronous validate callback returns an error string or null; error can carry a caller-owned server or form error. Error state is associated with the fieldset through aria-describedby and aria-invalid. Your application remains the source of truth for validation and submission.

API

PropTypeDescription
itemsreadonly T[]Current controlled array value.
onChange(items: T[]) => voidReceives the next array after an action.
createItem() => TCreates the value appended by the add action.
renderItem(context: FieldArrayItemContext<T>) => React.ReactNodeRenders one item’s fields. The context contains item, index, and count.
labelReactNodeVisible group label, rendered as the fieldset legend.
descriptionReactNodeSupporting text associated with the array.
itemLabel(item: T, index: number) => ReactNodeItem legend; defaults to Item {index + 1}.
getItemKey(item: T, index: number) => React.KeyStable React key for each item.
minItems, maxItemsnumberBounds that disable remove or add actions.
reorderablebooleanShows keyboard-operable move-up and move-down buttons.
validate(items: readonly T[]) => string | nullOptional synchronous caller-provided validation.
errorReactNode | booleanExternal validation message; true uses a default message.
invalidbooleanExplicit invalid state.
addLabel, removeLabel, moveUpLabel, moveDownLabelstring or callbackAccessible action labels.
emptyMessageReactNodeMessage displayed when there are no items.
disabledbooleanDisables the fieldset and its controls.
className and native fieldset attributes-Applied to the outer fieldset.

FieldArrayItemContext<T> and FieldArrayProps<T> are exported from @puzzle-ui/ui; T remains the caller's item type.

Accessibility and compatibility

The outer and item groups use native fieldset and legend elements. Actions are native buttons and remain keyboard operable; their labels identify the affected item. Descriptions and errors are programmatically associated with the outer group. Use labels for each nested input, and supply stable item keys when ordering values.

The component renders without browser APIs and uses Puzzle theme tokens. UI targets React 19; verify framework-specific SSR and hydration behavior in your application.