Form Repeaters
Form repeaters, also known as field arrays, are a way to create a list of fields that may repeat multiple times in a form. This is useful for creating forms that collect a dynamic number of items, like a list of emails or addresses. It can also be used for a group of fields that fill an object’s information.
Features
- Adding, removing, and swapping items.
- Auto-name prefixing for nested fields.
- Min and Max item count enforcement.
- Accessibility for add/remove and move buttons is automatically managed.
Anatomy
Creating a Form Repeater
To create a form repeater, you can use the useFormRepeater
composable.
Typically, you need to create a FormRepeater
component that you can use to structure your form fields. There is no specific element that you need to use at this time, as this is considered a utility container.
At its simplest, the useFormRepeater
composable returns the items you need to render the repeater.
Note that the Iteration
component used here is a component that is returned by the useFormRepeater
composable. This component is a wrapper that is used to render each item in the repeater in a managed way.
Under the hood, the Iteration
component prefixes the nested fields with the repeater’s name and the index of the item.
The Iteration
default slot props contain the following properties:
removeButtonProps
: The props for the remove button.moveUpButtonProps
: The props for the move up button.moveDownButtonProps
: The props for the move down button.
The items
array is a readonly array of strings that represent the keys of the items in the repeater. These are unique identifiers for each item in the repeater, and you should not attempt to set them manually.
Putting it all together, you get a simple example like this:
Example
Here is a fully styled example that makes use of some of the fields we created in the field guides. We also integrate a TransitionGroup
from Vue to animate the entering and leaving of the repeater items. This is why we are not abstracting the looping aspect of the repeater, as it gives you the flexibility to use any animation library/tool you like.
Usage
Min and Max Items
You can enforce a minimum and maximum number of items in a repeater by passing the min
and max
props. The remove and add buttons will be disabled when the minimum or maximum number of items is reached.
The repeater will also automatically add items to match the minimum number of items when the form is first rendered.
Field Path Prefixing
Repeaters require a name
prop to know which part of the form they are going to repeat. Any fields nested within a repeater will have a name that is prefixed with the repeater’s name and the index of the item. This is done automatically for you.
You can observe this by submitting the previous examples.
Unnamed Fields
As you’ve learned previously in the form guide, not having a name prop on the field will make it an uncontrolled field. This is also true for repeaters; however, if you pass an empty string as the field name, the field would still be controlled and will set its value to the iteration path directly.
Try adding new items to the following example and fill out the email fields. Notice how the values are set on the array indices directly.
If you need a field to be uncontrolled, avoid passing a name prop to the field.
API
Props
These are the properties that can be passed to the useFormRepeater
composable.
Name | Type | Description |
---|---|---|
addButtonLabel | The label for the add button. | |
max | The maximum number of iterations allowed. | |
min | The minimum number of iterations allowed. | |
moveDownButtonLabel | The label for the move down button. | |
moveUpButtonLabel | The label for the move up button. | |
name | The name/path of the repeater field. | |
removeButtonLabel | The label for the remove button. |
Returns
These are the properties in the object returned by the useFormRepeater
composable.
Name | Type | Description |
---|---|---|
add | Adds a number of items to the repeater, defaulting to 1. Cannot exceed the max. | |
addButtonProps | Props for the add item button. | |
insert | Inserts an item into the repeater at a given index. | |
items | The items in the repeater. | |
Iteration | The iteration component. You should use this component to wrap each repeated item. | |
move | Moves an item in the repeater from one index to another. | |
remove | Removes an item from the repeater. Cannot go below the min. | |
swap | (indexA: number, indexB: number) => void | Swaps two items in the repeater. |
<Iteration />
Props
Name | Type | Description |
---|---|---|
as | The tag name to render the iteration as. | |
index | The index of the current repeated item. This is required. |
Slot Props
These are the properties passed to the default slot of the Iteration
component.
Name | Type | Description |
---|---|---|
moveDownButtonProps | Props for the move item down button. | |
moveUpButtonProps | Props for the move item up button. | |
removeButtonProps | Props for the remove item button. |