Command Palette

Search for a command to run...

Components · Complex component

ComboBox

ComboBoxes combine a text entry with a picker menu, allowing users to filter longer lists to only the selections matching a query. ComboBox also supports an optional hint element, used for description and error messages, which can be used to provide more context about the field.

Features

ComboBox is automatically connected to handle accessibility features.

  • Adheres to the WCAG ComboBox Pattern
  • Visual and ARIA labeling support
  • Required and invalid states exposed to assistive technology via ARIA
  • Support for description and error message help text linked to the input via ARIA
  • Support for mouse, touch, and keyboard input events
  • Ability to open the combo box list via arrow keys on the keyboard
  • Ability to filter options through text input
  • Ability to choose a singular option
  • Support for disabled options
  • Supports grouping of items into sections
  • Adjusts positioning of popover to fit in viewport

Usage

TSX
import { ComboBox } from '@console/nimbus-ui/core';

Examples

Full Width

The fullWidth prop can be used to make the text input full width.

Invalid

The invalid prop can be used to render an error state.

Disabled

The disabled prop can be used to disable the input.

Read Only

Whether the input can be selected but not changed by the user.

Allows Custom Value

Whether the ComboBox allows a non-item matching input value to be set.

Disabled Options

Options can be individually disabled by using the disabledKeys prop.

Custom Empty State

A custom element can be displayed when no results are found. Try searching searching for a country that is not in the options:

Controlled

Most (but not all) aspects of the ComboBox can be controlled. This example shows controlling the selected item with the selectedKey and onSelectionChange props.

Async Loading

This example uses the [useAsyncList](https://react-spectrum.adobe.com/react-stately/useAsyncList.html) hook to handle infinite loading and filtering of data from a server. useAsyncList is exported by Nimbus UI for ease of use with async ComboBoxes. The state can also be handled manually if required. Use loadingState="loading" or loadingState="filtering" to display a loading indicator inside the input field, and loadingState="loadingMore" to display it inside the dropdown listbox. When paired with useAsyncList, the spinner automatically appears on the input during the initial load and switches to the listbox when the user scrolls down to load more items.

Dynamic Collections

The ComboBox accepts both static and dynamic collections. This is useful when options come from dynamic data sources, such as from an API call. The ComboBox receives its options through the defaultItems prop. Every item can take a key prop which is relayed to the onSelectionChange handler to determine the chosen item. However, if the item objects have an id property, as demonstrated in the example below, it will be used by default, eliminating the need for a key prop.

Sections

Items can be grouped in Sections. Each Section takes a title and a key prop.

Complex Items

Items can contain additional content, such as icons and descriptions to better communicate options.

API

ComboBox

PropTypeDefaultDescription
allowsEmptyCollectionbooleantrueWhether the combo box allows the menu to be open when the collection is empty.
classesComboBoxClasses—Custom class names to add to underlying DOM elements for styling.
data-testidstring—ID data attribute for use in tests.
disabledboolean—Whether the input is disabled
emptyStateNodeReactNode—Optional element to display when there are no options to display.
fullWidthboolean—Whether the input is full width
hintReactNode—A description for the field. Provides a hint such as specific requirements for what to choose.
invalidboolean—Whether the input is invalid
loadingboolean—Whether the input is loading @deprecated Use `loadingState` instead
loadingStateenum—The loading state of the ComboBox
onLoadMore() => void—Handler that is called when more items should be loaded
readonlyboolean—Whether the input can be selected but not changed by the user.
requiredboolean—Whether the input is requied

Group

PropTypeDefaultDescription
classesComboBoxClasses—
section*Node<T>—
state*ComboBoxState<T, "single">—

ListBox

PropTypeDefaultDescription
classesComboBoxClasses—
emptyStateNodeReactNode—
isLoadingboolean—
onLoadMore() => void—
state*ComboBoxState<T, "single">—

Option

PropTypeDefaultDescription
classesComboBoxClasses—
item*Node<T>—
state*ComboBoxState<T, "single">—

Popover

PropTypeDefaultDescription
classesComboBoxClasses—
popoverRef*RefObject<HTMLDivElement>—The ref for the popover element.
portalContainerElement—
state*OverlayTriggerState—