Transfer ListPro
FormKit Pro Quick Installation Guide 🚀
Introduction
The transfer list input is ideal for situations where the end-user needs to select and sort multiple values from a list of options. In this example, we are allowing the end-user to select from a group of guests and move them to a VIP list:
<FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="getGuests" :option-loader="loadGuest"> <template #targetOption="{ option }"> <div class="flex"> <span class="avatar"> <span class="initials">{{ initials(option.label) }}</span> </span> <div class="info"> <div class="item"> {{ option.label }} </div> <div class="item-small"> {{ option.email }} </div> <div class="item-small"> {{ option.phone }} </div> </div> </div> </template></FormKit>Getting started
In this section, we will be covering the basics of how to replicate the 'Guests vs VIPs' example from above.
Base input
Below is an example of the transfer list input with the minimum required props. As you can see, there are two list boxes: the source list box and the target list box. The source list box will contain the list of options, and the target list box will contain the selected options:
<template> <FormKit type="transferlist" /></template>Labels
Let's add some label props to make clear to the end-user how to use the transfer list input. We'll add a label prop to explain the directive to the user, and source-label and target-label props to indicate which list box is the source and which is the target:
<template> <FormKit type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" /></template>Source and target empty messages
In this state, with no options passed and no values selected, we can display a custom message to the user by setting the source-empty-message and target-empty-message props:
<template> <FormKit type="transferlist" name="vips" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" /></template>Defining options
The options prop can accept three different formats of values:
- An array of objects with
valueandlabelkeys - An array of strings
'A', 'B', 'C' - An object literal with key-value pairs
{ a: 'A', b: 'B', c: 'C' } - A function that returns any of the above
Let's go ahead and populate the transfer list's options with a list of guest names:
<script setup>import { guests } from './guests.js'</script><template> <FormKit type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="guests" /></template>Values
The value of the transfer list input is an array. Selected option values from the source list will be appended to the array. To show the value changing in the example below, let's wrap the transfer list input in a FormKit form, set the name of the transfer list input to vips, and show the value of the form itself in a <pre> tag (if you are unfamiliar with FormKit forms, you can read more here):
<script setup>import { guests } from './guests.js'</script><template> <FormKit type="form" #default="{ value }" :actions="false"> <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="guests" /> <pre wrap>{{ value }}</pre> </FormKit></template>Initial values
The transfer list input can be pre-populated with values by setting the value prop on the transferlist itself or a wrapping form or group. Remember that the values you pass to the value prop need to match the keys of the values in your option list:
<script setup>import { guests } from './guests.js'</script><template> <FormKit type="form" #default="{ value }" :value="{ vips: ['Monica Baker', 'Marion Blanchard'], }" :actions="false" > <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="guests" /> <pre wrap>{{ value }}</pre> </FormKit></template>Searchable
The transfer list input can be made searchable by setting the searchable prop. In this example we'll set the searchable prop and also set a placeholder prop for the search input:
<script setup>import { guests } from './guests.js'</script><template> <FormKit type="form" #default="{ value }" :value="{ vips: ['Monica Baker', 'Marion Blanchard'], }" :actions="false" > <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="guests" searchable placeholder="Search guests" /> <pre wrap>{{ value }}</pre> </FormKit></template>The search input only searches through the options in the source options list. It does not return options that have already been transferred to the target list.
Filtering
The transfer list input will filter options with its own internal search function. You can replace this search function by providing the filter prop a function of your own. Your function will receive two arguments, the option being iterated over and the current search value:
<script setup>import { guests } from './guests.js'const customFilter = (option, search) => option.label.toLowerCase().startsWith(search.toLowerCase())</script><template> <FormKit type="form" #default="{ value }" :value="{ vips: ['Monica Baker', 'Marion Blanchard'], }" :actions="false" > <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="guests" searchable placeholder="Search guests" :filter="customFilter" /> <pre wrap>{{ value }}</pre> </FormKit></template>Clear on select
By default, the transfer list input will clear the search input on select. You can change this behavior by setting the clear-on-select prop to false:
Max
The transfer list input can be limited to a maximum number of selected values by setting the max prop. For just this example, let's set the max prop to 2 to limit the number of VIPs that can be selected:
<script setup>import { guests } from './guests.js'const customFilter = (option, search) => option.label.toLowerCase().startsWith(search.toLowerCase())</script><template> <FormKit type="form" #default="{ value }" :value="{ vips: ['Monica Baker', 'Marion Blanchard'], }" :actions="false" > <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="guests" searchable placeholder="Search guests" :filter="customFilter" :max="2" /> <pre wrap>{{ value }}</pre> </FormKit></template>Transfer on select
By default, the transfer list input will add or remove options on click. You can change this behavior by setting the transfer-on-select prop to false. Now, the transfer list will behave more like a traditional transfer list:
<script setup>import { guests } from './guests.js'const customFilter = (option, search) => option.label.toLowerCase().startsWith(search.toLowerCase())</script><template> <FormKit type="form" #default="{ value }" :value="{ vips: ['Monica Baker', 'Marion Blanchard'], }" :actions="false" > <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="guests" searchable placeholder="Search guests" :filter="customFilter" :transfer-on-select="false" /> <pre wrap>{{ value }}</pre> </FormKit></template>Asynchrony
Asynchronous options
Here we have a transfer list input that loads its options from an asynchronous function. The function is called when the component is mounted and the options are subsequently loaded into the source list box:
<script setup>import { getGuests } from './api.js'</script><template> <FormKit type="form" #default="{ value }" :actions="false"> <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="getGuests" /> <pre wrap>{{ value }}</pre> </FormKit></template>Pagination
Now let's say that our API request does not fetch all the options we need, but instead returns a paginated response. The transfer list input can handle pagination with minor configuration to the asynchronous function.
When assigning the options prop to an asynchronous function, the function will be called with the FormKit context object as its first argument. This context object contains a page property (the current page we are attempting to load) that is tracked by the transfer list input, and hasNextPage, which is a callback function we can use to tell the transfer list that there are more options to load:
<script setup>import { paginateGuests } from './api.js'</script><template> <FormKit type="form" #default="{ value }" :actions="false"> <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="paginateGuests" /> <pre wrap>{{ value }}</pre> </FormKit></template>Search
The transfer list input can also load options asynchronously when the user searches. In this example, we'll add back the searchable prop, and change getGuests() to searchGuests(). When the user searches, searchGuests() will now be called with the same context object as before, but this time, we will destructure just the search property. Additionally, we'll modify getGuests() to only return guests when a search value is provided:
<script setup>import { searchGuests } from './api.js'</script><template> <FormKit type="form" #default="{ value }" :actions="false"> <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="searchGuests" searchable placeholder="Search guests" /> <pre wrap>{{ value }}</pre> </FormKit></template>Option loader
Rehydrating values
The transfer list input also provides an optionLoader prop that allows you to rehydrate values that are not in the options list. In this example, we'll provide the transfer list an initial value (a guest ID), and assign the optionLoader to a function that will make a request to the API to fetch the individual guest data:
<script setup>import { searchGuests, getGuest } from './api.js'</script><template> <FormKit type="form" #default="{ value }" :value="{ vips: [22, 10], }" :actions="false" > <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="searchGuests" searchable placeholder="Search guests" :option-loader="getGuest" /> <pre wrap>{{ value }}</pre> </FormKit></template>Notice in the example above that the optionLoader function getGuest is passed two arguments: the value of the selected option (in this case, the movie ID) and the cachedOption. The cachedOption is used for preventing unnecessary lookups; if the cachedOption is not null it means the selected option has already been loaded, and you can return the cachedOption directly.
Fetching additional data
You can also use use the optionLoader to fetch additional data on selected values that is not already in the options list. In this example, after selecting an option, we are going to perform a look-up to load the selected guest's age:
<script setup>import { getGuests, loadGuest } from './api.js'</script><template> <FormKit type="form" #default="{ value }" :actions="false"> <FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="getGuests" :option-loader="loadGuest" /> <pre wrap>{{ value }}</pre> </FormKit></template>Slots
Just like any other FormKit input, the transfer list input allows you to utilize slots to customize its markup.
Source and target options
Now that we are loading additional data on selected values (the age and email address of the selected guest), let's customize the look of the selected values by using the target-option slots:
<FormKit name="vips" type="transferlist" label="Choose VIPs for the party" source-label="Guests" target-label="VIPs" source-empty-message="No guests found" target-empty-message="No VIPs selected" :options="getGuests" :option-loader="loadGuest"> <template #targetOption="{ option }"> <div class="flex"> <span class="avatar"> <span class="initials">{{ initials(option.label) }}</span> </span> <div class="info"> <div class="item"> {{ option.label }} </div> <div class="item-small"> {{ option.email }} </div> <div class="item-small"> {{ option.phone }} </div> </div> </div> </template></FormKit>Examples
Ranked order
The transfer list input can be used to create a ranked list, let's do that with the greatest NBA players:
<FormKit type="transferlist" label="Rank the top 10 NBA players" help="Drag and drop to rank your top 10 NBA players." :options="nbaTopPlayers" searchable placeholder="Search" target-label="Your top 10" source-label="All-time NBA players" validation="michaelJordan" :validation-rules="{ michaelJordan }" validation-visibility="dirty" :validation-messages="{ michaelJordan: 'Michael Jordan is the undisputed GOAT.', }"> <template #sourceOption="{ option }"> <div class="flex"> <span class="avatar"> <img :src="option.img_url" alt="" /> </span> <span class="name"> {{ option.label }} </span> </div> </template> <template #targetOption="{ option, index }"> <div class="flex"> <span class="rank"> {{ index + 1 }} </span> <span class="avatar"> <img :src="option.img_url" alt="" /> </span> <span class="name"> {{ option.label }} </span> </div> </template></FormKit>Props & Attributes
| Prop | Type | Default | Description |
|---|---|---|---|
debounce | number | 200 | Number of milliseconds to debounce calls to an options function. |
options | any | [] | The list of options the user can select from. |
filter | function | null | Used to apply your own custom filter function for static options. |
option-loader | function | null | Used for hydrating initial value, or performing an additional request to load more information of a selected option. |
source-empty-message | string | undefined | Renders a message when there are no options to display. |
target-empty-message | string | undefined | Renders a message when there are no values to display |
max | number | undefined | Limits the number of options that can be selected. |
clear-on-select | boolean | true | Clears the search input after selecting an option (only for options that are not loaded via function). |
searchable | boolean | false | Enables the search input. |
source-label | string | undefined | Renders a label for the source list. |
target-label | string | undefined | Renders a label for the target list. |
transfer-on-select | boolean | true | Automatically transfers selected options to the target list. If set to false, will render transfer forward and transfer backward buttons. |
| 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 & slot data
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.
Wrapper structure
The outer container structure showing the source list, transfer controls, and target list.
Click on a section to lock focus to the chosen section. Click locked section again to unlock.
Source list
The source list structure containing search controls and available options.
Click on a section to lock focus to the chosen section. Click locked section again to unlock.
Transfer controls
The transfer control buttons for moving items between source and target lists.
Click on a section to lock focus to the chosen section. Click locked section again to unlock.
Target list
The target list structure showing selected items with empty message and load more.
Click on a section to lock focus to the chosen section. Click locked section again to unlock.
| Section-key | Description |
|---|---|
fieldset | A fieldset element that acts as the root element for the transfer list input. |
legend | A legend element that renders the label. |
source | A div element that contains the sourceHeader, sourceControls, and sourceListItems sections. |
sourceHeader | A div element that contains the sourceHeaderLabel and sourceHeaderItemCount sections |
sourceHeaderLabel | A label element that renders the sourceLabel prop. |
sourceHeaderItemCount | A span element that renders the number of items and number of items selected in the source list. |
sourceControls | A div element that contains the sourceSearchINput and sourceSearchClear sections |
sourceSearch | A div element that contains the sourceSearchInput and sourceSearchClear sections |
sourceSearchInput | A text input element used for searching. |
sourceSearchClear | A button element that clears the search input. |
closeIcon | The span used containing the icon for the clear search input. |
sourceListItems | A ul element that contains the sourceListItems. |
sourceEmptyMessage | A li element that contains the emptyMessageInner section. |
emptyMessageInner | A span element that renders the provided empty message text. |
sourceListItem | A li element for the sourceListItems section that contains the sourceOption section. |
selectIcon | A span elemenet that renders the selected icon when the sourceListItem is set to selected. |
sourceOption | A div element that renders the option label. |
sourceLoadMore | A li element that contains the loadMoreInner section. |
loadMoreInner | A span element that renders the loading icon. |
loaderIcon | A span element that outputs an icon when loading is occurring. |
transferControls | A div element that contains the transferButtonForwardAll, transferButtonForward, transferButtonBackward, and transferButtonBackwardAll sections. |
transferButtonForwardAll | A button element that transfers all options to the target list. |
transferButtonForward | A button element that transfers selected options to the target list. |
transferButtonBackward | A button element that transfers selected options to the source list. |
transferButtonBackwardAll | A button element that transfers all options to the source list. |
controlLabel | A span element that renders the control label. |
fastForwardIcon | A span element that renders the fast forward icon. |
moveRightIcon | A span element that renders the move right icon. |
moveLeftIcon | A span element that renders the move left icon. |
rewindIcon | A span element that renders the rewind icon. |
target | A div element that contains the targetHeader, targetControls, and targetListItems sections. |
targetHeader | A div element that contains the targetHeaderLabel and targetHeaderItemCount sections |
targetHeaderLabel | A label element that renders the targetLabel prop. |
targetHeaderItemCount | A span element that renders the number of items and number of items selected in the target list. |
targetListItems | A ul element that contains the targetListItems. |
targetEmptyMessage | A li element that contains the emptyMessageInner section. |
targetListItem | A li element for the targetListItems section that contains the targetOption section. |
targetLoadMore | A li element that contains the loadMoreInner section. |
| 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. |
Accessibility
All FormKit inputs are designed with the following accessibility considerations in mind. Help us continually improve accessibility for all by filing accessibility issues here:
- Semantic markup
- ARIA attributes
- Keyboard accessibility
- Focus indicators
- Color contrast with the provided theme
- Accessible labels, help text, and errors
Accessibility attributes
| Section Key | Attribute | Value | Description |
|---|---|---|---|
fieldset | role | presentation | Indicates to assistive technologies that this element functions as presentation. |
aria-describedby | Associates an element with a description, aiding screen readers. | ||
sourceHeader | role | presentation | Indicates to assistive technologies that this element functions as presentation. |
sourceHeaderLabel | for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | |
sourceHeaderItemCount | role | presentation | Indicates to assistive technologies that this element functions as presentation. |
aria-label | Provides an accessible name. | ||
sourceSearchInput | role | searchbox | Indicates to assistive technologies that this element functions as a searchbox. |
aria-label | Provides an accessible name. | ||
sourceSearchClear | aria-label | Provides an accessible name. | |
sourceListItems | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when searchable or there are no source options and 0 when otherwise. |
aria-activedescendant | Manage focus to the current active descendent element. | ||
role | listbox | Indicates to assistive technologies that this element functions as a listbox. | |
aria-multiselectable | true | Indicate it allows multiple items to be selected simultaneously. | |
aria-roledescription | Provides a description that this element role. | ||
sourceListItem | aria-selected | Indicate this element is currently selected. | |
role | option | Indicates to assistive technologies that this element functions as a option. | |
sourceLoadMore | tabindex | -1 | Prioritizes keyboard focus order by setting it to -1. |
aria-selected | false | Indicate this element is never selected. | |
sourceEmptyMessage | role | presentation | Indicates to assistive technologies that this element functions as presentation. |
targetHeader | role | presentation | Indicates to assistive technologies that this element functions as presentation. |
targetHeaderLabel | for | Associates the label to an input element. Users can click on the label to focus the input or to toggle between states. | |
targetHeaderItemCount | role | presentation | Indicates to assistive technologies that this element functions as presentation. |
aria-label | Provides an accessible name. | ||
targetListItems | tabindex | -1 or 0 | Prioritizes keyboard focus order by setting it to -1 when searchable or there are no source options and 0 when otherwise. |
aria-activedescendant | Manage focus to the current active descendent element. | ||
role | listbox | Indicates to assistive technologies that this element functions as a listbox. | |
aria-multiselectable | true | Indicate it allows multiple items to be selected simultaneously. | |
aria-roledescription | Provides a description that this element role. | ||
targetListItem | aria-selected | Indicate this element is currently selected. | |
role | option | Indicates to assistive technologies that this element functions as a option. | |
targetLoadMore | tabindex | -1 | Prioritizes keyboard focus order by setting it to -1. |
aria-selected | false | Indicate this element is never selected. | |
targetEmptyMessage | role | presentation | Indicates to assistive technologies that this element functions as presentation. |
transferButtonForward | aria-label | Provides an accessible name. | |
transferButtonForwardAll | aria-label | Provides an accessible name. | |
transferButtonBackward | aria-label | Provides an accessible name. | |
transferButtonBackwardAll | aria-label | Provides an accessible name. | |
| Universal Accessibility Attributes | |||
label | label | for | Associates a label to an input element. Users can click on the label to focus the input or to toggle between states. |
input | input | disabled | Disables an HTML element, preventing user interaction and signaling a non-interactive state. |
aria-describedby | Associates an element with a description, aiding screen readers. | ||
aria-required | Added when input validation is set to required. | ||
icon | icon | for | Links icon to input element when icon in rendered as a label. |
Keyboard Interactions
| Keyboard Event | Description |
|---|---|
| Space | Invokes the currently selected UI button. |
| Enter | Moves the currently selected list item between lists |
| ↑+↓ | Moves focus between list items. |
| Universal Keyboard Events | |
| Tab | Moves the focus to the next focusable element on the page. |
| Shift+Tab | Moves the focus to the previous focusable element on the page. |