Alternatives
- Autocomplete — Presents a filtered list only when the user has provided input. For extremely large datasets, where browsing the complete list would be inappropriate.
- SelectInput — For small lists.
Usage
The Combobox is a controlled component. You "own" the state, which is managed by providing handlers to the component.
NOTE: All handlers must be memoized with
React.useCallback()for performance.
NOTE: Pass the window object to the environment prop in Storybook (or when using inside an iframe) to prevent interaction issues.
Basic
The following props are required:
inputValue— The input valueitems— The array of items for the user to select fromonChange— Handle changes to the selected itemonInputChange— Handle changes to the input valuevalue— The selected item
Clear
Optionally provide an onClear handler, allowing users to "clear" their selection.
NOTE: Like all handlers,
onClearmust be memoized withReact.useCallback()for performance.
Data shape
By default the Combobox assumes that items will take the following shape:
type Item = { label: string; value: string | number; disabled?: boolean;};However, if your items have a different shape, you can provide "transform" functions to let the Combobox know how to handle your data:
itemToDisableditemToLabelitemToValue
NOTE: All "transform" functions must be memoized with
React.useCallback()for performance.
Appearance
For brevity, search and selection are not implemented in the "appearance" examples.
Disabled
When "disabled" the Combobox will not be interactive and take on a dim appearance.
Invalid
When the parent Field has an "invalidMessage" the Combobox controls will take on a "critical" appearance.
Size
The Combobox suppports a size property, which influences the input controls and the default item height. For dense interfaces where screen reale state is limited you may want to use the "small" size.
Placeholder
The text that appears in the form control when it has no value set.
Custom item
To display a custom item you can provide an itemRenderer to the combobox.
NOTE: Should be a pure function (no side-effects), declared outside of the component.
Item height
Because the scroll menu is virtualized we need an itemHeight (in pixels) to safely render the list. You only need to provide an itemHeight when using a custom itemRenderer and the height of your items differs from the default item implementation.
Menu width
By default, the menu dialog will match the width of the input container. For situations where horizontal real estate is limited this might mean that menu items are clipped. Provide a menuWidth prop to resolve the issue, accomodating expected item label lengths.
Async
The logic for filtering items typically lives on the server rather than the client because it’s impractical to send all possible items over the network. However, when prototyping in Playroom or working with smaller datasets, you may want to perform this filtering on the client instead.
The async examples simulate a server request using the simulateFetch utility. You should not use this in your application.
Filtering
When fetching data from the server, update the loadingState to keep users informed. While the request is being processed, the loading indicator will let users know that their input has been recognised and that something is happening.
<Combobox loadingState="loading" />NOTE: When fetching from the server based on user input you should always implement a constraint technique, like debounce or throttle, to limit the amount of requests.
Pagination
Paginated data is handled in the Combobox using "infinite scrolling", a technique for loading more results as the user nears the bottom of a scroll view.
To paginate results you must provide:
mode="paginated"canLoadMore— signal that more content is availableonLoadMore— called when the user nears the bottom of the menu (ifcanLoadMore)loadingState— must be kept in-sync to avoid duplicate calls
NOTE: Like all handlers,
onLoadMoremust be memoized withReact.useCallback()for performance.
Advanced
The Combobox is tailored for the most common usage—where possible, simplicity and reduced API surface-area is preferred. There are however features available should you need them.
Footer action
Actions such as "Create new" can be shown in the footer using the footerItem prop. The footer item is just a normal item rendered differently (for accessibility reasons) so most of the behavior is left to the consumer.
Below is a sample of how to implement a "Create new" item in the footer.
Grouping, dividers and sticky items
We cannot have hierarchical data in a combobox, instead we can have the illusion of grouped items by inserting disabled and unselectable items into the list and rendering them differently.
State changes
The Combobox implements Downshift under-the-hood. Both onChange and onInputChange will provide "next state" as their second argument. This state is a result of the respective events, and can be used to alter the behaviour of your component.
type ComboboxState = Partial<{ highlightedIndex: number; inputValue: string; isOpen: boolean; selectedItem: Item | null;}>;In the example below we're checking whether the input value exactly matches the recently selected item. If this is the case, we reset the list of items so they're visible next time the menu is opened.
State reducer
For granular control of the state changing process, you can provide your own reducer. When stateReducer is called it will receive the previous state and the actionAndChanges object. actionAndChanges contains the change type, which explains why the state is being changed. It also contains the changes proposed that should occur as a consequence of that change type.
NOTE: The
stateReducershould be a pure function (no side-effects), it should be declared it outside of your component.
The TS types you'll need are re-exported from this package as UseCombobox*. While Balance prefers string literals, Downshift implements an Enum for the change "type", which is exposed as stateChangeTypesEnum.
In the example below we'll uppercase the input value, and return the new value along with the rest of the changes. For all other state change types, we return the default changes.
Messages
You can customise the message displayed to users when no items are available by providing a getMessageText callback, which has the signature:
(status: 'error' | 'noResults', inputValue: string) => string;You can define this function outside of your component, or implement React.useCallback() to memoize it. Prefer the former where possible.
NOTE: You must account for each
statusin your function definition.
The environment prop
This prop is only useful if you're rendering the combobox within a different window context from where your JavaScript is running; for example, an iframe or a shadow-root.
Simply pass the window object to the environment.
<Combobox {...otherProps} environment={window} />Recipes
Reference implementations for comboboxes of varying complexity.