Skip to content
EspañolEnglish
All components (283)

Repeatable list

Fields · RepeatableField · .amset-repeat

What it is and when to use it

A list of rows of fields that are added and removed: phones, shipping addresses, order lines. It does not impose what goes in each row: renderItem paints that.

What it brings is what gets forgotten when writing it by hand: focus does not fall to body when removing a row, a new row receives focus in its first field and each change is said aloud.

When to use it

  • Any data repeated a variable number of times.
  • createItem gives the new row and renderItem paints its fields, with update to change it. itemLabel names it: "Phone 2" sounds better than "Item 2".
  • min protects the rows that must remain and max sets the limit, and says so. reorderable adds buttons to move up and down.
  • In a form from @amset/ui/form, FormRepeatableField connects it to react-hook-form.

When not to

  • A list of loose texts: TagInput or OptionList.
  • Many columns edited like a sheet: Table with editing.
  • Invoice lines, with their sums: InvoiceLinesEditor.

Keyboard: it is all Tab and Enter or Space. On adding, focus goes to the first field of the new row; on removing, to the remove button of the next one (or the previous one, or "Add" if none remain); on moving, it stays on the same button, and if the row reaches the end, on the other one.

Accessibility: it is a fieldset with its legend, and each button says which row it touches ("Remove Phone 2"). A spoken notice says what happened. error is announced as an alert and stays linked to the group.

Fine-tuning: --amset-repeat-gap is the gap between rows. The buttons are IconButton and Button.

What it looks like

Phones

Add all you use

<RepeatableField
  label="Phones"
  hint="Add all you use"
  defaultValue={["600 123 456"]}
  createItem={() => ""}
  itemLabel={(_telefono, indice) => `Teléfono ${indice + 1}`}
  addLabel="Add phone"
  renderItem={(telefono, { label, update }) => (
    <TextField label={label} value={telefono} onChange={update} />
  )}
/>

Props

Import it with import { RepeatableField } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
createItemrequired() => ItemThe new row added on pressing.
labelrequiredstringWhat is repeated: "Phones". Required: a group without a name makes no sense with a screen reader.
renderItemrequired(item: Item, state: RepeatableItemState<Item>) => ReactNodeThe fields of a row.
addLabelstringThe text of the add button. By default, "Add".
classNamestringMerged with the library classes; it does not replace them.
defaultValuereadonly Item[]—
disabledboolean—
emptyTextstringWhat is said when there are no rows.
errorstring—
hintstring—
insertablebooleanBetween one row and the next, a "+" that inserts another right there, with the line where it will land when pointed at. It appears when passing over the row or reaching it with the keyboard, and without a mouse it is always visible. There is none under the last one: that is "Add".
itemLabel(item: Item, index: number) => stringWhat a row is called, for the buttons and notices: "Phone 2". By default, "Item 2".
maxnumberRows at most: on reaching it, the add button is disabled and says so.
minnumberRows that cannot be removed. By default, 0.
onChange(items: readonly Item[]) => void—
reorderablebooleanAdds buttons to move each row up and down.
valuereadonly Item[]Controlled.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

CSS variables

These CSS variables are how you adapt it. Declare them on the component's selector, not on :root: the stylesheet redeclares them in the rule itself, and a declaration on the same element always wins.

VariableDefault
--amset-repeat-gapvar(--amset-space-3)

How to change it

.amset-repeat {
  --amset-repeat-gap: ...;
}

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: