Pro installation quickstart 🚀

    Basic example

    The dropdown input allows users to select a value from a customizable list of options:

    Load Live Example

    Defining options

    The options prop can accept three different formats of values:

    • An array of objects with value and label keys (see example above)
    • 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

    Slots

    Unlike native select elements, the dropdown input allows you to customize the options list with markup.

    Option slot

    The dropdown input allows you to customize the look and feel of each option by using the option slot. In this example, we are using the option slot to display each option's asset; logo and name:

    Load Live Example

    Selection slot

    If you only want to customize the display of the selected option, use the selection slot:

    Load Live Example

    Loading options

    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.

    Single request

    Let's say we had an API endpoint that returned all the options we needed for a given dropdown input. Here is an example of how we could write the dropdown input to load options from a single request:

    Load Live Example

    In the example above, we are assigning the options prop to the loadHorrorMovies function. After the request, we're iterating over the results to ensure that we return an array of objects with explicit value and label properties.

    Multiple pages

    What about loading options from an API where you need to be able to make multiple requests to perform pagination? When a function is set to the options prop it is passed FormKit node's context object as an argument. Within this context object are 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:

    Load Live Example

    In the above example, we are calling hasNextPage when we determine there are more pages to load. When this is done, FormKit appends a Load more option at the end of the rendered options list and automatically increments its page property. When the user selects the Load more option, the function assigned to the options prop is called again, and the process repeats.

    Option loader

    FormKit's dropdown 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:

    Load Live Example

    You can also 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:

    Load Live Example

    Load on scroll

    If you would rather allow the user to load more options without having to click the Load more option at the bottom of the options list, you can set the load-on-scroll prop to true, and our function, loadCurrentlyPopularMovies will be called again:

    Load Live Example

    Full example

    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:

    Load Live Example

    Overscroll

    When using the dropdown with static options, FormKit's dropdown also comes with a unique feature called overscroll. In this example, we'll see what the behavior is when setting overscroll to true:

    Load Live Example

    Props & Attributes

    Prop Type Default Description
    optionsany[]The list of options the user can select from.
    load-on-scrollbooleanfalseWhen set to `true`, the dropdown will try loading more options based on the end-user`s scroll position
    option-loaderfunctionnullUsed for hydrating initial value, or performing an additional request to load more information of a selected option.
    Show universal props
    configObject{}Configuration options to provide to the input’s node and any descendent node of this input.
    delayNumber20Number of milliseconds to debounce an input’s value before the commit hook is dispatched.
    errorsArray[]Array of strings to show as error messages on this field.
    helpString''Text for help text associated with the input.
    idStringinput_{n}The unique id of the input. Providing an id also allows the input’s node to be globally accessed.
    ignoreBooleanfalsePrevents an input from being included in any parent (group, list, form etc). Useful when using inputs for UI instead of actual values.
    indexNumberundefinedAllows 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.
    labelString''Text for the label element associated with the input.
    nameStringinput_{n}The name of the input as identified in the data object. This should be unique within a group of fields.
    parentFormKitNodecontextualBy default the parent is a wrapping group, list or form — but this props allows explicit assignment of the parent node.
    prefix-iconString''Specifies an icon to put in the prefixIcon section.
    preservebooleanfalsePreserves the value of the input on a parent group, list, or form when the input unmounts.
    preserve-errorsbooleanfalseBy 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-schemaObject{}An object of section keys and schema partial values, where each schema partial is applied to the respective section.
    suffix-iconString''Specifies an icon to put in the suffixIcon section.
    typeStringtextThe type of input to render from the library.
    validationString, Array[]The validation rules to be applied to the input.
    validation-visibilityStringblurDetermines when to show an input's failing validation rules. Valid values are blur, dirty, and live.
    validation-labelString{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-rulesObject{}Additional custom validation rules to make available to the validation prop.
    valueAnyundefinedSeeds the initial value of an input and/or its children. Not reactive. Can seed entire groups (forms) and lists..

    Sections

    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.

    Outer structure

      Select t-shirt color
      Gray
      Blue Gray Tan
      Turn sound effects on and off.
      Something wrong happened.

        Inner listbox structure

        Below is the inner options list (listbox) structure from the diagram above:

            Section-key Description
            selectorThe selector section is a button element that opens the dropdown options list.
            selectionContains the selected option.
            listitemA list item element that contains the option section.
            optionA div that contains the option content.
            listboxThe listbox section is a ul element that contains the options list.
            dropdownWrapperWraps the listbox section. A div that handles scrolling the listbox.
            optionLoadingA span element that is conditionally rendered within the selected option when loading is occurring.
            loaderIconAn element for outputting an icon in the selector element when loading is occurring.
            selectIconAn element for outputting an icon in the selector element when the dropdown is closed.
            selectedIconAn element for outputting an icon next to the selected option when inside the listbox.
            loadMoreA list item element that is conditionally rendered at the bottom of the options list when there are more pages to load.
            loadMoreInnerA span element that acts as a wrapper for the loaderIcon within the loadMore section.
            Show universal section keys
            outerThe outermost wrapping element.
            wrapperA wrapper around the label and input.
            labelThe label of the input.
            prefixHas no output by default, but allows content directly before an input element.
            prefixIconAn element for outputting an icon before the prefix section.
            innerA wrapper around the actual input element.
            suffixHas no output by default, but allows content directly after an input element.
            suffixIconAn element for outputting an icon after the suffix section.
            inputThe input element itself.
            helpThe element containing help text.
            messagesA wrapper around all the messages.
            messageThe element (or many elements) containing a message — most often validation and error messages.