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:
In this section, we will be covering the basics of how to replicate the 'Guests vs VIPs' example from above.
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:
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:
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:
The options
prop can accept three different formats of values:
value
and label
keys'A', 'B', 'C'
{ a: 'A', b: 'B', c: 'C' }
Let's go ahead and populate the transfer list's options with a list of guest names:
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):
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:
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:
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:
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
:
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:
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:
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:
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:
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:
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:
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.
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:
Just like any other FormKit input, the transfer list input allows you to utilize slots to customize its markup.
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:
The transfer list input can be used to create a ranked list, let's do that with the greatest NBA players:
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 for the source list. |
target-searchable | boolean | false | Enables the search input for the target list. |
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.. |
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 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. |
All FormKit inputs are designed with the following accessibility considerations in mind. Help us continually improve accessibility for all by filing accessibility issues here:
Section Key | Attribute | Default | 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. | |
Show Universal section key | |||
label | label | for | Associates the 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 | Adds this attribute when validation is required. | ||
icon | icon | for | Whenever icon is defined as a label it links it to an input element. |
Keyboard Event | Description |
---|---|
Tab | Moves the focus to the next focusable input on the page. |
Shift + Tab | Moves the focus to the previous focusable input on the page. |