Skip to main content

useCombobox

useCombobox lets people type to filter a list and choose a suggestion with the keyboard or pointer.

Quickstart​

import React from 'react'
import {useCombobox} from 'downshift'

const fruits = ['Apple', 'Apricot', 'Banana']

export default function FruitCombobox() {
const [items, setItems] = React.useState(fruits)
const combobox = useCombobox({
items,
onInputValueChange({inputValue}) {
const query = inputValue.toLowerCase()
setItems(fruits.filter(fruit => fruit.toLowerCase().startsWith(query)))
},
})

return (
<div>
<label {...combobox.getLabelProps()}>Search fruit</label>
<input {...combobox.getInputProps()} />
<ul {...combobox.getMenuProps()} hidden={!combobox.isOpen}>
{items.map((item, index) => (
<li
key={item}
{...combobox.getItemProps({item, index})}
style={{fontWeight: combobox.highlightedIndex === index ? 'bold' : 'normal'}}
>
{item}
</li>
))}
</ul>
</div>
)
}

Type ap to see Apple and Apricot. Press Arrow Down to highlight a fruit and Enter to put it in the input.

Using object items​

When each item is a book, use itemToString to display its title in the input. The example below searches both the title and the author.

Open the book example in CodeSandbox.

Live Editor
Result
Loading...

Props used in examples​

In the combobox pattern, the input keeps focus while the highlight moves through the options. getInputProps() sets role="combobox" and connects the input to the listbox from getMenuProps() through aria-controls.

Returned propElementPurpose
getLabelProps<label>Labels the input.
getInputProps<input>Handles typing and keyboard navigation.
getToggleButtonProps<button>Opens or closes the list from an optional button.
getMenuProps<ul>Identifies the listbox.
getItemProps<li>Wires each option to its item and index.
isOpenWhether the list is open.
highlightedIndexIndex of the highlighted option, or -1 when none is highlighted.
selectedItemThe selected item.
inputValueThe text in the input.

See the useCombobox API reference for all props and return values.

Type p, then quickly type r. The simulated search for pr finishes first and shows Pride and Prejudice. When the slower search for p finishes, its results go into a Map under p. Press Backspace to see both Piranesi and Pride and Prejudice immediately. Try xyz to see the empty state.

The menu reads only the entry for the current query. Trimming and lowercasing the input lets P and p reuse the same search, even while it is in flight.

Each search returns fresh book objects. itemToKey gives Downshift a stable ID for comparing books across responses, and itemToString supplies the title to display after selection.

Open the async search example in CodeSandbox.

Live Editor
Result
Loading...

React Native​

The hook can be used with React Native as well. The HTML elements and styles are replaced with React Native equivalents, but the useCombobox usage is exactly the same as on React web.

MaterialUI​

A custom combobox/autocomplete element can be created using UI Library components as well. Many libraries will provide basic elements such as buttons, texts/labels, inputs and lists, which can be styled according to each library guidelines. useCombobox is providing the additional stateful logic that will transform this selection of basic components into a fully working dropdown component.

Material UI's Input renders a wrapper around the native input. Pass getInputProps() to its inputProps prop so the native input receives the combobox role, ARIA attributes, event handlers, and ref.

Selecting a book displays its title in the input because itemToString returns the title for a book and an empty string for null. In v9, custom live region messages are opt-in. To announce a custom selection message, pass getA11yStatusMessage to useCombobox.

CodeSandbox for MaterialUI usage example.

Live Editor
Result
Loading...

Controlling state​

Controlling state is possible by receiving the state changes done by Downshift via onChange props (onHighlightedIndexChange, onSelectedItemChange, onStateChange etc.). You can then change them based on your requirements and pass them back to useCombobox as props, such as for instance highlightedIndex or selectedItem.

Both comboboxes share the same selectedItem. Choose a book in either one to see its title in both inputs. Open the other picker to choose another book, or type to filter its options.

CodeSandbox for controlling state example.

Live Editor
Result
Loading...

State Reducer​

Cancel an edit with Escape​

Type war, then press Arrow Down and Enter to select War and Peace. Replace the input text with idi and press Escape. The menu closes and the input shows War and Peace again.

On Escape, Downshift already closes the open menu and clears the highlight. The stateReducer keeps those changes and restores inputValue from state.selectedItem. If you haven't selected a book yet, it clears the input.

Open the state reducer example in CodeSandbox.

Live Editor
Result
Loading...

Outside dismissal​

If a parent popover closes and unmounts the combobox on an outside click or tap, it can handle that dismissal itself. Since v9.5, Downshift marks that InputBlur action with source === 'outside'. Return the current state for that action and pass through the usual changes when the user tabs away.

useCombobox({
items,
stateReducer(state, {type, source, changes}) {
if (
type === useCombobox.stateChangeTypes.InputBlur &&
source === 'outside'
) {
return state
}
return changes
},
})

Try the select inside a popover to compare an outside click with Tab.

Custom window​

When using useCombobox in an iframe or in any other scenario that uses a window object different than the default browser window, it is required to provide that window object to the hook as well. Internally, we rely on the window for DOM related logic and working with the wrong object will make the hook behave unexpectedly. For example, when using react-frame-component to produce an iframe container, we should pass its window object to the hook as shown below. The playground’s FrameStyles component loads the docs stylesheet into the iframe and applies the current color theme.

CodeSandbox for custom window example.

Live Editor
Result
Loading...

Basic Multiple Selection​

Select any number of books from the list. To display selected items as removable tags, see the useTagGroup example.

Set selectedItem: null and toggle membership in selectedItems from onSelectedItemChange. The reducer keeps the menu open and the chosen book highlighted after selection. Moving focus away closes the menu and preserves the selection.

The listbox has aria-multiselectable="true", and each option exposes its selection state through aria-selected. A decorative checkmark shows which books are selected.

CodeSandbox for basic multiple selection example.

Live Editor
Result
Loading...

Multiple selection with Tag Group​

If your multiple selection scenario uses a tag group to display selected items, you can use the hook in combination with useTagGroup.

In the example below, we use:

  • null to control selectedItem as the hook should not care about selection.
  • onSelectedItemChange to be able to call addItem from useTagGroup.
  • control inputValue to be able to filter combobox items by query and selected items.
  • stateReducer to keep the next available book highlighted after selection. Moving focus to a tag closes the menu and preserves the selection.

CodeSandbox for combobox with tag group example.

Live Editor
Result
Loading...

Using action props​

Choose a book, then use the clear button. Its click handler calls selectItem(null) to clear both the selection and the text in the input.

CodeSandbox for action props example.

Live Editor
Result
Loading...

Virtualizing items with react-virtual​

This example uses react-virtual to render a window of a 1,000-book list. Import useVirtual and defaultRangeExtractor from react-virtual. The example adds the highlighted option to the rendered range so aria-activedescendant still identifies it when you scroll past it. Downshift handles keyboard scrolling. Each option exposes its position and the size of the list to assistive technology.

CodeSandbox for virtualized list example.

Live Editor
Result
Loading...

Breaking Changes in v8 and v9​

useCombobox has been affected by breaking changes in v8 and v9, so check out the migration pages for v8 and v9.

Breaking Changes in v7​

Since version 7, useCombobox supports the ARIA 1.2 pattern for the combobox, which contains some changes from the ARIA 1.1 pattern. This brings changes in the API and the behaviour of useCombobox, detailed in the migration page.

Other usage examples​

For search backed by a service, see the Algolia autocomplete and Apollo character search. Browse the examples catalog for more recipes.