- Implementation
- Combobox and Combobox.Select Props
- Combobox.SearchField and Combobox.SelectTrigger Props
- Combobox.Content Props
- Combobox.List Props
- Combobox.Item Props
- Combobox.ItemAddNew Props
- Combobox.Empty Props
Common Examples
Combobox components
The Anvil2 combobox is made up of aCombobox component and a few sub-components that can be used together to build simple or complex select drop-downs.Combobox: the parent component to configure comboboxes.Combobox.SearchField: used to create a searchable select input to use with a combobox list in a drop-down.Combobox.Content: popover that passes anitemsarray to itschildrento construct combobox items.Combobox.List: organizes combobox items in the drop-down list.Combobox.Item: used to construct the items within the combobox drop-down.Combobox.Empty: optionally used to customize empty results message.
Multi-selects
To allow the user to select multiple items, set themultiple prop of the Combobox to true. In addition, props such as selectedItem will be replaced with selectedItems (note the “s” at the end) and will expect an array.Select all for multi-selects
To add a “Select All” option at the top of a multi-select combobox dropdown, use theselectAll prop. This prop accepts an object with label, onSelection, and isChecked properties. The onSelection callback is called when the user clicks the Select All option, and isChecked controls whether the checkbox appears checked.Note: The “Select All” option does not automatically select all items when the combobox is opened. It is up to the developer to handle the selection of all items when the “Select All” option is clicked. This means it is incompatible with using Combobox in an uncontrolled way.Building selects
The Anvil2Combobox component is versatile due to its flexibility and robust set of sub-components. It can be used in place of the Select component from the original Anvil library to create a searchable select component.Notes for building selects
- The
itemToStringprop passes anitemobject and expects astringreturn type. This is used to format the text of each combobox item within the search field input (after selecting an item) based on the provideditemobject type. - The
itemToKeyprop passes anitemobject and expects something that can be used to identify the item, such as anid. This is used for identity comparisons, so if this prop is omitted, theitems will be compared by reference equality instead. - The
filterOptionsprop is used to filter the items using the match-sorter library. See their docs for more info on the options that can be provided. - The
selectedItem/selectedItemsprops allow theComboboxto be used as a controlled component. - For easily managing the selected item state, the
onChangeprop accepts asetStatefunction, which is the second item in the array that auseStatefunction returns:
Compare using itemToKey or reference equality
The itemToKey prop is used to compare items using a unique identifier. By default, the Combobox component does searches and comparisons using reference equality. That means that the Combobox may not behave how you expect if your code is generating new references for your items on every render:React.useMemo to memoize computations on items and only update the references when something has meaningfully changed.However, in cases like this, it’s probably more likely you’d prefer to have your items compared by a specific key in your object rather than by reference equality. And so you can do that by passing in an itemToKey function that takes an item and returns a unique key:Passing filtered items through the combobox components
Theitems array passed to the Combobox is automatically filtered based on the user typing in the search input and the keys in the filterOption prop. After filtering, it is passed through the Combobox.Content as a parameter used to generate the children.Adding a new item when there is no exact match
To allow the user to add a new item when their search returns no exact matches, use theCombobox.ItemAddNew component. This component provides an onSelection callback, and that callback will need to be used to update both your list of items and your selected item.Your onSelection callback will need to both update the list of items and update the selectedItem/selectedItems, so we recommend that you use a controlled Combobox when you need the ability to add a new item:Select-only comboboxes
By default, comboboxes include a text input for searching and filtering items. To create a select-only combobox that acts similarly to an HTML select, use theCombobox.Select and Combobox.SelectTrigger instead of Combobox and Combobox.SearchField.Disable clearing selection on select-only combobox
To remove the “x” button that clears a selection in a select-only combobox, use thedisableClearSelection prop.Note: this does not work when multiple is true, due to the nature of clearing options in multi-select comboboxes.Sorting combobox options
By default, Comboboxes sort options based on the order that they are provided via theitems prop.Controlled combobox
Comboboxes can be used uncontrolled by default, or can be used in a fully-controlled way by setting theselectedItem prop (or by using the selectedItems prop when used with multiple select):Default combobox selection
When using a Combobox in an uncontrolled way, Comboboxes can set an item as a default by using thedefaultSelectedItem prop (or by using the defaultSelectedItems prop when used with multiple select):Selected item customization
Comboboxes with multiple select can customize the selected state of an item using the selectedItemProps prop on the Combobox.SearchField. This prop accepts a function that will pass the selected item as an parameter to allow for things like customizing the display color:Disabling combobox popovers
Comboboxes can be used to create searchable select lists without a drop-down. IfdisablePopover is set to true on the Combobox.Content, the list will always render. This is especially useful if the combobox exists within a custom popover.Custom combobox empty state
By default, if no items exist in the filtered combobox results, a simple “No results found” message is displayed. To customize this, use theCombobox.Empty component.Combobox virtualization example
Combobox.Content renders a function that passes down the list of filtered items as children, allowing the Combobox.List component to be used with virtualization libraries, which often require an additional wrapper component around the items being virtualized. The following example uses react-window’s FixedSizeList component for virtualizing the list of items:












