The repeater
input is for repeating groups of inputs. You can use any number of FormKit inputs within a repeater, and repeaters themselves can be infinitely nested.
By default, the repeater input has the ability to shift, add, and remove items:
Unlike most other FormKit input types, the repeater input is of type list. You can see above that the users
property (our repeater's value) is an array.
Like all other FormKit inputs, the Repeater is able to be used in a form written in FormKit schema.
In the previous example, the 'Add Users' button is provided by default, and the button label is derived from the repeater's label
. We're able to customize the label by using the add-button
prop and setting it to a string
, or remove it completely by setting it to false
. Alternatively, similar to the submit-attrs
prop on the FormKit's forms, we can provide an object of attributes to be applied to the button with add-attrs
. In this example, we change the label of the repeater from Add Users
to My custom label
using the add-label
prop:
Like many other FormKit inputs, the repeater comes with a min
and max
prop. You can use these props to limit the number of items to be added or removed.
When a repeater is allowed to be empty by having its min
prop set to 0
and having no values, you can control the content rendered in the empty state using the empty
slot.
The repeater input by default comes with a set of controls that allow you to shift, add, and remove items. You can control the visibility of these controls by setting the upControl
, downControl
, insertControl
, and removeControl
props to true
or false
.
With FormKit's repeater, you are not restricted to our default controls. You can manipulate the repeater in anyway you want:
You can set errors on a repeater or any repeatable item using dot notation. Here we are using the
setErrors
helper, but there are other methods as well:
// the 2nd argument of setErrors is input-level errors
formNode.setErrors( null, // no form errors,
{
// error on the repeater field:
'teamMembers': ['There was a problem with 1 of your team members.']
// error on a specific repeater item:
'teamMembers.1.email': ['emily@formkit.com is already on a team.'],
}
)
Remember, your submit handler is passed the form's core node
and can be used
to conveniently set errors at depth. Read more about error handling
here. Here's an example of a fake backend
returning errors for both the repeater and one child:
Prop | Type | Default | Description |
---|---|---|---|
add-label | string | null | Used to change the label of the add button. |
add-attrs | object | [object Object] | Used to apply attributes to the add button element. |
add-button | boolean | true | Conditional for whether to show the add button. |
up-control | boolean | true | Conditional for whether to show the up control. |
down-control | boolean | true | Conditional for whether to show the down control. |
insert-control | boolean | false | Conditional for whether to show the insert control. |
remove-control | boolean | true | Conditional for whether to show the remove control. |
min | Number | 1 | The minimum number of children. |
max | Number | null | The maximum number of children. |
Show Universal props | |||
config | Object | {} | Configuration options to provide to the input’s node and any descendent node of this input. |
delay | Number | 20 | Number of milliseconds to debounce an input’s value before the commit hook is dispatched. |
dirtyBehavior | string | touched | Determines how the "dirty" flag of this input is set. Can be set to touched or compare — touched (the default) is more performant, but will not detect when the form is once again matching its initial state. |
errors | Array | [] | Array of strings to show as error messages on this field. |
help | String | '' | Text for help text associated with the input. |
id | String | input_{n} | The unique id of the input. Providing an id also allows the input’s node to be globally accessed. |
ignore | Boolean | false | Prevents an input from being included in any parent (group, list, form etc). Useful when using inputs for UI instead of actual values. |
index | Number | undefined | Allows an input to be inserted at the given index if the parent is a list. If the input’s value is undefined, it inherits the value from that index position. If it has a value it inserts it into the lists’s values at the given index. |
label | String | '' | Text for the label element associated with the input. |
name | String | input_{n} | The name of the input as identified in the data object. This should be unique within a group of fields. |
parent | FormKitNode | contextual | By default the parent is a wrapping group, list or form — but this props allows explicit assignment of the parent node. |
prefix-icon | String | '' | Specifies an icon to put in the prefixIcon section. |
preserve | boolean | false | Preserves the value of the input on a parent group, list, or form when the input unmounts. |
preserve-errors | boolean | false | By default errors set on inputs using setErrors are automatically cleared on input, setting this prop to true maintains the error until it is explicitly cleared. |
sections-schema | Object | {} | An object of section keys and schema partial values, where each schema partial is applied to the respective section. |
suffix-icon | String | '' | Specifies an icon to put in the suffixIcon section. |
type | String | text | The type of input to render from the library. |
validation | String, Array | [] | The validation rules to be applied to the input. |
validation-visibility | String | blur | Determines when to show an input's failing validation rules. Valid values are blur , dirty , and live . |
validation-label | String | {label prop} | Determines what label to use in validation error messages, by default it uses the label prop if available, otherwise it uses the name prop. |
validation-rules | Object | {} | Additional custom validation rules to make available to the validation prop. |
value | Any | undefined | Seeds the initial value of an input and/or its children. Not reactive. Can seed entire groups (forms) and lists.. |
You can target a specific section of an input using that section's "key", allowing you to modify that section's classes, HTML (via :sections-schema
, or content (via slots)). Read more about sections here.
Section-key | Description |
---|---|
fieldset | A fieldset element that acts as the repeater’s wrapper. |
legend | A legend element that renders the label. |
items | A ul element that wraps the repeater’s items. |
item | A li element that is rendered for each repeater item. |
content | A container for the group section. |
group | Does not render an element. It structures the default slot into an object data structure. |
controls | A ul element that wraps the repeater’s controls. |
controlLabel | A span element that renders the label of the given control. |
up | A li element that renders the up control. |
upControl | A button element that renders the up control. |
moveUpIcon | A span element that renders the upControl’s icon. |
down | A li element that renders the down control. |
downControl | A button element that renders the down control. |
moveDownIcon | A span element that renders the downControl’s icon. |
insert | A li element that renders the insert control. |
insertControl | A button element that renders the insert control. |
insertIcon | A span element that renders the insertControl’s icon. |
remove | A li element that renders the remove control. |
removeControl | A button element that renders the remove control. |
removeIcon | A span element that renders the removeControl’s icon. |
Show Universal section keys | |
outer | The outermost wrapping element. |
wrapper | A wrapper around the label and input. |
label | The label of the input. |
prefix | Has no output by default, but allows content directly before an input element. |
prefixIcon | An element for outputting an icon before the prefix section. |
inner | A wrapper around the actual input element. |
suffix | Has no output by default, but allows content directly after an input element. |
suffixIcon | An element for outputting an icon after the suffix section. |
input | The input element itself. |
help | The element containing help text. |
messages | A wrapper around all the messages. |
message | The element (or many elements) containing a message — most often validation and error messages. |