Skip to main content

Combobox

    The Combobox component allows users to choose one option from a collapsed dropdown, filter options by typing, or create a new one.

    Import​

    import { Combobox } from '@dynatrace/strato-components/forms';

    Demo​

    Options​

    Group options together​

    Use Combobox.Group to organize options into named groups. Add a Combobox.GroupLabel inside each group to show its title. Group labels aren't selectable.

    Add icons​

    Add an icon or other content in front of the option label with Combobox.Prefix, or after it with Combobox.Suffix. If any option has a prefix, all options reserve space for it so the labels stay aligned. Suffixes only take up space in the options that define them.

    If the option content isn't a plain string, add a textValue prop with the text representation of the option. The Combobox uses it for filtering and for the text it shows after selection.

    Disable options​

    Disable individual options by adding the disabled prop to Combobox.Option. Disabled options stay visible, but users can't select them. To disable the whole Combobox, add the disabled prop to it or to the surrounding FormField.

    Pass large amounts of data​

    The Combobox can handle many options. The options are virtualized if the content is too large to fit. The virtualization only affects the first-level children, whether they're grouped or ungrouped. For a good user experience, organize options into logical groups with a reasonable number of options. For example, not more than 10 options per group.

    Control state​

    Control the selected value of the Combobox with the value and onChange props. The onChange handler receives the value of the selected option, or null when the selection is cleared. To let the Combobox manage its own state, use the defaultValue prop instead.

    Create custom values​

    When users enter text that doesn't match an existing option, the Combobox shows an option to create a new value. Use the onCreateOption prop to react to the new value. The handler receives the trimmed text that the user entered.

    In a controlled Combobox, onChange isn't called for the created value. Add the new option to your list and set value in onCreateOption to apply it.

    Change create label​

    Use the createLabel prop to change the action word in the create option. For example, createLabel="Use" shows "Use xyz" instead of "Create xyz".

    States​

    The Combobox supports an initial value, a disabled state, and a read-only state. In the read-only state, users can see the value but can't change it or open the dropdown.

    Show loading state​

    You can show a loading state by adding the loading prop to Combobox.Content. While loading is true, a loading indicator replaces the options, and the option to create a new value is hidden. Screen readers announce the loading state while the dropdown is open. When loading is false again, the options reappear.

    Use in a form field​

    Wrap the Combobox in a FormField to add a label, hint, required indicator, and validation messages. Add the name prop to include the value in native form submissions. Add the required prop to the Combobox. The Label in the FormField then shows the required indicator. The Combobox validates its value when the dropdown closes and when focus leaves the field. If required is set and the field has no value, it reports a validation error through onValidityChange.

    For more validation patterns, see show custom hints and errors in the FormField documentation.

    React to open state changes​

    Use the onOpenChange prop to react when the dropdown opens or closes. The callback receives the new open state as a single boolean argument.

    Still have questions?
    Find answers in the Dynatrace Community