Repeater

Slant / Forms

Rows of fields you add, remove and reorder, between a minimum and a maximum.

Demo

Entry editor

Usage

@tuttn/slant
import { Field, Input, Repeater, Row } from '@tuttn/slant'

// Uncontrolled, in a Field: the label names the list. You render each row's
// fields from its item, and row.update writes them back.
<Field>
  <Field.Label>Links</Field.Label>
  <Repeater defaultValue={[{ label: 'Site', url: 'https://tuttn.io' }]} newItem={() => ({ label: '', url: '' })}
    itemLabel={(link, index) => link.label || `Link ${index + 1}`} max={5}>
    {(link, row) => <Row gap={8}>
      <Input aria-label="Label" value={link.label} onChange={e => row.update({ ...link, label: e.target.value })} />
      <Input id={`${row.id}-url`} aria-label="URL" value={link.url} onChange={e => row.update({ ...link, url: e.target.value })} />
    </Row>}
  </Repeater>
</Field>

// Controlled, with a stable key from your data, and at least one step.
const [steps, setSteps] = useState<Step[]>(recipe.steps)
<Repeater value={steps} onValueChange={setSteps} getKey={step => step.id} min={1}
  newItem={() => ({ id: crypto.randomUUID(), text: '' })} label="Steps" addLabel="Add step">
  {(step, row) => <Textarea aria-label={`Step ${row.index + 1}`} value={step.text} autosize
    onChange={e => row.update(current => ({ ...current, text: e.target.value }))} />}
</Repeater>

// Disabled or read-only: the list drops or disables its own buttons; pass
// the same state to your fields, which the list can't reach.
<Repeater value={links} newItem={blankLink} readOnly label="Links">
  {(link, row) => <Input readOnly aria-label={`URL ${row.index + 1}`} value={link.url} />}
</Repeater>

Props

PropTypeDefaultDescription
valueT[]—The items, one row each. Pass it with onValueChange to control the list; leave it out to let the list keep its own.
defaultValueT[][]The items an uncontrolled list starts with.
onValueChange(value: T[]) => void—Called with the new list after every add, remove, move and row update.
newItem() => T—Makes the item a press on Add appends.
children(item: T, row: RepeaterRow<T>) => ReactNode—Renders a row's fields from its item. `row` carries its index, a stable `id`, its `label`, and `update`, `remove` and `move`. Pass `disabled` and `readOnly` to the fields yourself: the list can't reach them.
getKey(item: T, index: number) => string | number—A stable key for each item, such as its database id. Without one, the list keeps its own ids beside the items, moved with each move and dropped with each remove; a controlled value changed from outside keeps them by position.
itemLabel(item: T, index: number) => stringmessages.itemName(index + 1), 'Item 2'Names each row: its group's accessible name, and the name in its buttons and announcements ("Remove Site").
minnumber0The fewest rows: at this count Remove is unavailable, and says why.
maxnumber—The most rows: at this count Add is unavailable, and says why.
addLabelReactNode'Add'The Add button's text. From the root's `messages.addItem`.
reorderablebooleantrueShows the drag grip and the Move up and Move down buttons, which also answer Alt+Arrow keys.
emptyReactNode'Nothing here yet.'What shows while there are no rows. From the root's `messages.noItems`.
labelstring—The group's accessible name, unless a Field's label or `aria-labelledby` names it.
disabledboolean—Disables the list's own buttons and grip, and marks the group (`aria-disabled`). Inside a Field, the field's `disabled` does this.
readOnlyboolean—Leaves out Add, the move and remove buttons and the grip. Inside a Field, the field's `readOnly` does this.
invalidboolean—Marks the group invalid (`data-invalid`, a danger border round the rows). Inside a Field, the field's `invalid` does this.
size'sm' | 'md''md'The size of the row buttons and Add: 28 or 34px at regular density.
RepeaterRow{ index; count; id; label; update(next | item => next); remove(); move(to) }—The second argument of children. `id` stays with the row while it moves: build the ids of the fields inside it from it. `label` is the row's name from itemLabel. `update` writes the row's item; `remove` and `move` do what the row's buttons do, limits, announcements and focus included.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref reach the root, the role="group" element around the rows and Add.

Keyboard

Tab / Shift+Tabmoves through each row's fields, then its Move up, Move down and Remove buttons, row by row, then Add
Enter / Space on Move up or Move downmoves the row one place; focus stays on the button, now in the moved row
Alt+ArrowUp / Alt+ArrowDown on a move buttonmoves the row one place up or down
Alt+Home / Alt+End on a move buttonmoves the row to the top or the bottom
Enter / Space on Removeremoves the row; focus moves into the next row, or the previous one when it was the last, or to Add when none are left
Enter / Space on Addappends a row from newItem and focuses its first field
Escape during a dragputs the dragged row back where the drag began

Accessibility

  • The root is a role="group". Inside a Field, the field's label names it through aria-labelledby and its description and error describe it; outside one, label names it. The rows are an ordered list, so a screen reader reads each row's place ("2 of 3").
  • Each row is a role="group" named by itemLabel ("Item 2" by default), and its buttons say whose they are: "Move Site up", "Move Site down", "Remove Site".
  • Move up on the first row, Move down on the last and Remove at min are aria-disabled, not disabled, so focus stays on a button whose row has just moved to an end. Remove at min is described by messages.minItems ("At least 1"), and Add at max by messages.maxItems ("At most 5").
  • Every change is announced politely: "Site added", "Site removed", "Site, moved to 1 of 3", and after Escape in a drag, "Site is back where it started".
  • The grip is hidden from assistive technology: dragging is for pointers, and the move buttons are the keyboard's way to do the same.
  • The Alt+Arrow keys belong to the move buttons, not the whole row, so a Select, Combobox or DatePicker in a row still opens on Alt+ArrowDown.

Rows and keys

The value is your array, and each row renders its item through children. Without getKey, Repeater keeps its own id beside each item, moved with each move and dropped with each remove, so a row keeps its fields' state and focus as it moves; when a controlled value changes from outside, the ids are kept by position. With getKey, your key is React's key, and it must not change while the row lives. row.id is stable either way: use it as the prefix of ids inside the row, so an error summary can link to a field in it.

Limits

At max rows Add is unavailable and at min Remove is, both saying why in their description; row.remove() keeps to min too. A controlled value outside the limits is shown as it is.

Disabled and read-only

disabled disables the list's own buttons and grip and marks the group aria-disabled; readOnly leaves out Add, the move and remove buttons and the grip. Either comes from a Field too. Repeater can't reach the fields you render, so pass the same state to them.

Dragging

A drag from the grip moves the row live, each time the pointer passes the middle of its neighbour, and the new place is announced when it ends. Escape during the drag puts the row back.

Full source