List
The list input does not render any output to the DOM. It is used exclusively for structuring data in your form. It is equivalent to an array in JavaScript.
The list input allows you to structure data from child inputs as an array. The list itself outputs no markup (by default) and can be used in conjunction with any other type of input — including nested groups and lists.
The value of a list input is an array where each item is the value of the input at that index. Sub-inputs do not need to be of the same type. In addition to structuring data, lists can determine the validation state, provide initial values, and supply plugins and configuration to all of its children.
Basic example
<script setup>import { ref } from 'vue'const list = ref([ '[email protected]', '[email protected]', '[email protected]'])</script><template> <FormKit v-model="list" type="list" > <p class="text-base mb-4">Please provide a list of emails.</p> <FormKit label="Email address" validation="required|email" /> <FormKit label="Email address" validation="required|email" /> <FormKit label="Email address" validation="required|email" /> </FormKit> <pre wrap>{{ list }}</pre></template>Vue’s handy v-model is fully supported in FormKit with bi-directional data flow even on lists and groups. However, if your form needs extremely high performance consider using the core node to read/write instead of v-model.
List types
There are 2 flavors of lists:
- Static lists (default) have pre-determined children. They should not be used for iterating over values or adding/removing items.
- Dynamic lists allow iteration over the list’s value to create its children. They are useful for creating dynamic-length lists like repeaters.
Static lists
Static lists are the simplest type of list to create. Wrap any inputs in a <FormKit type="list"> and those inputs will automatically be set as values of their array. Immediate children of lists do not have a name (even if specified) instead they are always identified by their index. In a static list, this index is stable and is based on the order of the inputs.
<script setup>import { nbaTopPlayers } from './nba-top-players.js'</script><template> <h2 class="text-2xl font-bold mb-4">NBA All Time Starting Five</h2> <FormKit type="list" #default="{ value }"> <FormKit label="Point Guard" type="autocomplete" placeholder="Select a player" :options="nbaTopPlayers" /> <FormKit label="Shooting Guard" type="autocomplete" placeholder="Select a player" :options="nbaTopPlayers" /> <FormKit label="Small Forward" type="autocomplete" placeholder="Select a player" :options="nbaTopPlayers" /> <FormKit label="Power Forward" type="autocomplete" placeholder="Select a player" :options="nbaTopPlayers" /> <FormKit label="Center" type="autocomplete" placeholder="Select a player" :options="nbaTopPlayers" /> <pre wrap>{{ value }}</pre> </FormKit></template>NBA All Time Starting Five
[]
Static lists should not be used for iterating over their own value. While it is safe to iterate over other arbitrary data iterating over the value of the list itself will cause recursive rendering and infinite loops. Instead use a dynamic list.
Dynamic lists
Dynamic lists allow you to iterate over the values of the list to create and hydrate the inputs inside the list. Dynamic lists are useful for creating dynamic array structures like repeaters.
You can mutate the structure of your dynamic list by adding and removing items from the value array. FormKit will automatically update the underlying form structure to match the new value.
In order for dynamic lists to keep your data in sync, you must use items from the default slot to create your v-for and you must also pass the index of the v-for as the index prop to each item in of the list.
To use a prebuilt repeater, check out the free FormKit Pro repeater input.
Dynamic list example
<script setup></script><template> <h2 class="text-2xl font-bold mb-4">Guest list</h2> <FormKit type="list" :value="['']" dynamic #default="{ items, node, value }"> <FormKit v-for="(item, index) in items" :key="item" :index="index" label="Guest name" placeholder="Guest name" suffix-icon="trash" @suffix-icon-click="() => node.input(value.filter((_, i) => i !== index))" :sections-schema="{ suffixIcon: { // change wrapper to a button for accessibility $el: 'button', }, }" /> <FormKit type="button" @click="() => node.input(value.concat(''))"> + Add another </FormKit> <pre wrap>{{ value }}</pre> </FormKit></template><style scoped>.formkit-suffix-icon { appearance: none; background: none; border: none; font-size: 1em;}</style>Guest list
[ "" ]
Dynamic list with a nested group
<script setup></script><template> <h2>Guest list</h2> <FormKit type="list" :value="[{}]" dynamic #default="{ items, node, value }"> <FormKit type="group" v-for="(item, index) in items" :key="item" :index="index" > <div class="group"> <FormKit type="text" name="name" label="Guest name" placeholder="Guest name" /> <FormKit type="number" name="age" label="Guest age" /> <button type="button" @click="() => node.input(value.filter((_, i) => i !== index))" class="border border-blue-600 text-blue-600 p-3"> - Remove </button> </div> </FormKit> <button type="button" @click="() => node.input(value.concat({}))" class="border border-blue-600 text-blue-600 p-3 mb-4"> + Add another </button> <pre wrap>{{ value }}</pre> </FormKit></template><style>.group { position: relative; padding: 10px; border: 1px solid grey; border-radius: 10px; margin-bottom: 5px;}.group button { position: absolute; top: 10px; right: 10px;}</style>Guest list
[
{}
]Dynamic list using v-model
<script setup>import { ref } from 'vue'import { FormKitIcon } from '@formkit/vue'import { vAutoAnimate } from '@formkit/auto-animate'const todos = ref([ 'Buy groceries', 'Do laundry', 'Clean the bathroom', 'Pay bills', 'Call mom', 'Go for a run',])</script><template> <h1>Todo list</h1> <div v-auto-animate> <FormKit v-model="todos" type="list" dynamic #default="{ items, value }"> <div v-for="(item, index) in items" :key="item" class="todo"> <FormKit type="text" :index="index" /> <ul class="controls"> <li> <button type="button" @click="todos.splice(index - 1, 0, todos.splice(index, 1)[0])" class="button" > <FormKitIcon icon="arrowUp" /> </button> </li> <li> <button type="button" @click="todos.splice(index + 1, 0, todos.splice(index, 1)[0])" class="button" > <FormKitIcon icon="arrowDown" /> </button> </li> <li> <button type="button" @click="todos.splice(index, 1)" class="button close" > <FormKitIcon icon="close" /> </button> </li> </ul> </div> <FormKit type="button" @click="todos.push('')">Add a todo</FormKit> <pre wrap>{{ value }}</pre> </FormKit> </div></template><style scoped>.todo { display: flex; align-items: center; margin-bottom: 0.5rem;}.todo:deep(.formkit-outer) { margin-bottom: 0; flex-grow: 1;}.controls { list-style-type: none; margin: 0; padding: 0; display: flex; align-items: center;}.controls .button { border: none; background: none; padding: 0; margin: 0; cursor: pointer; color: #999; line-height: 1; transition: color 0.3s ease; appearance: none; font-size: 1em; color: var(--fk-color-primary); margin-left: 0.5rem;}.controls:deep(svg) { display: block; width: 1em; max-height: 1.25em; height: auto; fill: currentColor;}.controls .close { color: var(--fk-color-danger);}</style>Todo list
[ "Buy groceries", "Do laundry", "Clean the bathroom", "Pay bills", "Call mom", "Go for a run" ]
Dynamic list in schema
<script setup>import { reactive } from 'vue'const data = reactive({ addItem: (node) => () => node.input(node._value.concat([''])), stringify: JSON.stringify,})const schema = { $formkit: 'form', children: [ { $formkit: 'list', name: 'links', value: [''], // 👈 Starts with an empty item dynamic: true, children: [ { $formkit: 'text', for: ['item', 'index', '$items'], // 👈 $items is in the slot’s scope key: '$item', // 👈 Use $item as the key index: '$index', // 👈 Pass the $index to the FormKit component label: 'Link', validation: 'required|url', validationVisibility: 'submit', }, { $formkit: 'button', onClick: '$addItem($node)', // 👈 Call $addItem from data children: 'Add a link', }, ], }, { $el: 'pre', attrs: { wrap: true }, children: '$stringify($value)', }, ],}</script><template> <FormKitSchema :schema="schema" :data="data" /></template>Validity of children
Lists (and groups) are always aware of the validation state of their children (including nested children). You can access this data in the context object of the input (context.state.valid).
<FormKit name="social" type="list"> <template #default="{ state: { valid } }"> <h2 class="text-2xl font-bold mb-2">Social media handles</h2> <p class="text-base mb-4"> Please provide a your twitter and facebook social media profiles. </p> <div v-if="!valid" class="not-valid"> Your social profile is not complete! </div> <div v-else class="valid">It all looks good 👍</div> <FormKit label="Twitter handle" placeholder="@your_profile" validation="required|starts_with:@" help="Enter your twitter profile handle" /> <FormKit label="Facebook profile" placeholder="https://facebook.com/your-profile" validation="required|starts_with:https://www.facebook.com/" help="Enter the URL to your facebook profile." /> </template></FormKit>Social media handles
Please provide a your twitter and facebook social media profiles.
Showing error & validation messages
Even though a list can have validation rules and input errors, it does not include any functionality to show validation messages and errors by default. The list ships no HTML at all. If you’d like to display those errors — you can add the <FormKitMessages /> as a child of the list.
Further documentation on the FormKitMessages component can be found on the form documentation page.
Props & Attributes
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | Boolean | false | Disables all the inputs in the list. |
dynamic | Boolean | false | Enables dynamic mode for the list and provides items in the default slot creating an iterator (v-for). |
| 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.. |
Sections
The list input renders no output to the DOM so there are no sections to display. The list input is a renderless input that is used exclusively for structuring data in your form. It is equivalent to an array in JavaScript.