The autocomplete input allows you to search through a list of options. In this first example, let's provide the options
prop a list of countries:
The options
prop can accept three different formats of values:
value
and label
keys (see example above)'A', 'B', 'C'
{ a: 'A', b: 'B', c: 'C' }
If you assign options as an empty array, the input will be rendered in a disabled state.
The autocomplete 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:
The autocomplete input, by default, will close the listbox when no search results are found while filtering. You can change this behavior by assigning the empty-message
prop a message to display when no results are found:
The autocomplete input has a selectionAppearance
prop that determines the way a selected option is displayed.
The autocomplete input will default to the text-input
selection appearance. When an option is selected, the text input will display the selected option's label:
Alternatively, you can set the selection appearance of the autocomplete to option
. In this mode, when a selection is made the text input is removed and will be replaced with a div containing the selected option's label. Additionally, the option
selection appearance will render a removeSelection
button that will clear the selection when clicked:
Just like the Dropdown input, the autocomplete input allows you to utilize slots to customize the look and feel of the options list and the selected option by leveraging the renderless component pattern.
In this example, we'll use the options slot to display car brands and their logos:
If you only want to customize the display of the selected option, use the selection
slot:
The autocomplete supports multi-select and is available for either the text-input
or option
selection appearance. To enable multi-select, simply set the multiple
prop.
When setting the multiple
prop, the autocomplete's value must always be an array.
For the text-input
selection appearance, the selected options display as a comma-separated list:
For the option
selection appearance, the selected options display as a list of divs
which render below the text input:
The max
prop allows you to limit the number of options that can be selected. When the max
limit is reached, the autocomplete input will disable the listbox:
If you would like the autocomplete's listbox to remain open in between selections, set the close-on-select
prop to false
:
Instead of passing a static list of options to the options
prop, you can assign it to a function. Doing so is useful when you need to load options from an API or another source.
In this example, we'll assign the options
prop the searchMovies
function. By doing so, searchMovies
will receive the context
object as an argument. Within this context
object is the search
property, which is the current search value. To perform our search, we'll use the search
value as the query parameter for our API request:
A likely scenario you'll encounter is needing to search through a paginated API. This can be done by referencing the same context
object as before. Within this object, we can utilize the page
and hasNextPage
properties. The page
property is the current page number, and the hasNextPage
property is a function to be called when there are more pages to load:
Instead of requiring your users to click the Load more button to load additional options, you can set the loadOnScroll
prop to true, which will paginate options as you scroll to the bottom of the options list.
FormKit's autocomplete 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 autocomplete an initial value (a movie ID), and assign the optionLoader to a function that will make a request to the API to get the movie:
Notice in the example above that the optionLoader function is passed two arguments: the value
of the selected option (in this case, the movie ID) and the cachedOption
. The cachedOption prop is used for preventing unnecessary lookups. If the cachedOption is not null
it means that the selected option has already been loaded, and you can return the cachedOption directly.
Instead of using the optionLoader
prop to rehydrate values that are not in the options list, you can use the optionLoader to perform a look-up to fetch additional data. In this example, after selecting an option, we are going to perform a look-up to load the selected option's movie review:
Now let's combine what we've learned so far by leveraging the option
slot for custom markup, and setting the options
prop to a function that will return pages of movies from an API:
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. |
load-on-scroll | boolean | false | When set to `true`, the dropdown will try loading more options based on the end-user`s scroll position |
selection-appearance | string | text-input | Changes the way the option label is display. |
multiple | boolean | false | Allows for multiple selections. |
open-on-click | boolean | false | The autocomplete is expanded upon focus of the input, as opposed to waiting to expand until a search value is entered. |
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. |
empty-message | string | undefined | Renders a message when there are no options to display. |
max | number | undefined | Limits the number of options that can be selected. |
close-on-select | boolean | true | Closes the listbox when an option is selected. |
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.
The autocomplete's structure changes depending on a few different scenarios:
selection-appearance
has been set to text-input
(the default) or option
.multiple
attribute.text-input
option
, single selectionoption
, multiple selectionBelow is the inner options list (listbox
) structure from the diagrams above:
Section-key | Description |
---|---|
selector | The selector section is a button element that opens the dropdown options list. |
selections | Contains individual selection sections. |
selection | Contains the selected option. |
listitem | A list item element that contains the option section. |
option | A div that contains the option content. |
listbox | The listbox section is a ul element that contains the options list. |
dropdownWrapper | Wraps the listbox section. A div that handles scrolling the listbox. |
optionLoading | A span element that is conditionally rendered within the selected option when loading is occurring. |
loaderIcon | An element for outputting an icon in the selector element when loading is occurring. |
selectIcon | An element for outputting an icon in the selector element when the dropdown is closed. |
loadMore | A list item element that is conditionally rendered at the bottom of the options list when there are more pages to load. |
loadMoreInner | A span element that acts as a wrapper for the loaderIcon within the loadMore section. |
removeSelection | A button element used for removing a specific selection. |
closeIcon | An element for outputting an icon within the removeSelection button. |
listboxButton | A button element that is used to open the dropdown. |
emptyMessage | A list item element that is conditionally rendered when there are no options to display. |
emptyMessageInner | A span element that acts as a wrapper for the emptyMessage 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. |