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.
createItemgives the new row andrenderItempaints its fields, withupdateto change it.itemLabelnames it: "Phone 2" sounds better than "Item 2".minprotects the rows that must remain andmaxsets the limit, and says so.reorderableadds buttons to move up and down.- In a form from
@amset/ui/form,FormRepeatableFieldconnects it toreact-hook-form.
When not to
- A list of loose texts:
TagInputorOptionList. - Many columns edited like a sheet:
Tablewith 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
<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.
| Prop | Type | What it does |
|---|---|---|
createItemrequired | () => Item | The new row added on pressing. |
labelrequired | string | What is repeated: "Phones". Required: a group without a name makes no sense with a screen reader. |
renderItemrequired | (item: Item, state: RepeatableItemState<Item>) => ReactNode | The fields of a row. |
addLabel | string | The text of the add button. By default, "Add". |
className | string | Merged with the library classes; it does not replace them. |
defaultValue | readonly Item[] | — |
disabled | boolean | — |
emptyText | string | What is said when there are no rows. |
error | string | — |
hint | string | — |
insertable | boolean | Between 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) => string | What a row is called, for the buttons and notices: "Phone 2". By default, "Item 2". |
max | number | Rows at most: on reaching it, the add button is disabled and says so. |
min | number | Rows that cannot be removed. By default, 0. |
onChange | (items: readonly Item[]) => void | — |
reorderable | boolean | Adds buttons to move each row up and down. |
value | readonly 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.
| Variable | Default |
|---|---|
--amset-repeat-gap | var(--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:
