Examples
Select builds off of the menu component suite to adapt commonly used properties and functions to create a select menu. See the menu documentation for a full list of properties that may be used to further customize a select menu. View the custom menu examples to see examples of fully functional select menus.
For use cases that do not require a lot of customization, there are various template components available to use in the @react-templates
package. These templates have a streamlined API and logic, making them easier to set up and use, but are limited in scope and flexibility. See the templates page for details.
Single select
To let users select a single item from a list, use a single select menu.
You can add multiple <SelectOption>
components to build out a list of menu items. For each select option, pass a relevant option label to the value
property.
To disable the select menu toggle, use the isDisabled
property. In the following example, select the checkbox to observe this behavior.
Select option variants
The following example showcases different option variants and customizations that are commonly used in a select menu.
To create these variants, you can pass different properties into a <SelectOption>
component.
This example provides examples of:
- An option with a description, which is created by using the
description
property. - An option with a link, which is created by passing a URL into the
to
property. For external links, use theisExternalLink
property so that the option is styled with an outbound link icon. - An option with an icon, which is created by using the
icon
property. - An option that is disabled by using the
isDisabled
property.
With grouped items
To group related select options together, use 1 or more <SelectGroup>
components and title each group using the label
property.
With validation
To create a toggle with a status, pass in status
to the MenuToggle
. The default icon associated with each status may be overridden by using the statusIcon
property.
When the status value is "warning" or "danger", you must include helper text that conveys what is causing the warning/error.
Checkbox select
To let users select multiple list options via checkbox input, use a checkbox select.
To create a checkbox select, pass role="menu"
to the <Select>
, and hasCheckbox
into each <SelectOption>
component. To indicate that an option is selected, use the isSelected
property.
By default, the menu toggle will display a badge to indicate the number of items that a user has selected.
Typeahead
Typeahead is a select variant that replaces the typical button toggle for opening the select menu with a text input and button toggle combo. As a user enters characters into the text input, the menu options will be filtered to match.
Typeahead is a select variant that replaces the typical button toggle for opening the select menu with a text input and button toggle combo. As a user enters characters into the text input, the menu options will be filtered to match.
To make a typeahead, pass variant=typeahead
into the <MenuToggle>
component and link an onClick
function to the <TextInputGroupMain>
component.
Typeahead is a select variant that replaces the typical button toggle for opening the select menu with a text input and button toggle combo. As a user enters characters into the text input, the menu options will be filtered to match.
To make a typeahead, pass variant=typeahead
into the <MenuToggle>
component and link an onClick
function to the <TextInputGroupMain>
component.
Typeahead with create option
If a user enters a value into a typeahead select menu that does not exist, you can allow them to create an option of that value.
If a user enters a value into a typeahead select menu that does not exist, you can allow them to create an option of that value.
To enable the creation ability, pass a predetermined value
into a <SelectOption>
component. You can use the placeholder
property to change the default text shown in the text input.
The following example outlines the code implementation required to create a working typeahead menu that allows for creation.
If a user enters a value into a typeahead select menu that does not exist, you can allow them to create an option of that value.
To enable the creation ability, pass a predetermined value
into a <SelectOption>
component. You can use the placeholder
property to change the default text shown in the text input.
The following example outlines the code implementation required to create a working typeahead menu that allows for creation.
Multiple typeahead with chips
A multiple typeahead can be used to allow users to select multiple options from a list. Additionally, you can render a chip group to be placed in the select toggle.
A multiple typeahead can be used to allow users to select multiple options from a list. Additionally, you can render a chip group to be placed in the select toggle.
When more items than the allowed limit are selected, overflowing items will be hidden under a "more" button. The following example hides items after more than 3 are selected. To show hidden items, select the “more” button. Select "show less" to hide extra items again.
A multiple typeahead can be used to allow users to select multiple options from a list. Additionally, you can render a chip group to be placed in the select toggle.
When more items than the allowed limit are selected, overflowing items will be hidden under a "more" button. The following example hides items after more than 3 are selected. To show hidden items, select the “more” button. Select "show less" to hide extra items again.
Multiple typeahead with create option
If the text that is entered into a typeahead doesn't match a menu item, users can choose to create a new option that matches the text input. You can also combine this create functionality with a chip group to display created items as chips."
If the text that is entered into a typeahead doesn't match a menu item, users can choose to create a new option that matches the text input. You can also combine this create functionality with a chip group to display created items as chips."
If the text that is entered into a typeahead doesn't match a menu item, users can choose to create a new option that matches the text input. You can also combine this create functionality with a chip group to display created items as chips."
Multiple typeahead with checkboxes
By default, a multiple typeahead select allows you to select multiple menu items, placing a checkmark beside selected items. Like basic checkbox select menus, you can add checkboxes to your menu items. This approach may be more accurate and comprehensive for more complex menu scenarios like filtering.
By default, a multiple typeahead select allows you to select multiple menu items, placing a checkmark beside selected items. Like basic checkbox select menus, you can add checkboxes to your menu items. This approach may be more accurate and comprehensive for more complex menu scenarios like filtering.
By default, a multiple typeahead select allows you to select multiple menu items, placing a checkmark beside selected items. Like basic checkbox select menus, you can add checkboxes to your menu items. This approach may be more accurate and comprehensive for more complex menu scenarios like filtering.
With view more
To reduce the processing load for long select lists, you can add a "View more" action to load additional items.
The following example shows 3 items before the "View more" link, but you can adjust the number of visible items for your use case.
With a footer
You can add a <MenuFooter>
component to a select menu to hold additional actions that users can take on menu items, through elements such as link buttons. A footer will be placed beneath a divider at the end of the select menu.
Props
Select
Name | Type | Default | Description |
---|---|---|---|
togglerequired | SelectToggleProps | ((toggleRef: React.RefObject<any>) => React.ReactNode) | Select toggle. The toggle should either be a renderer function which forwards the given toggle ref, or a direct ReactNode that should be passed along with the toggleRef property. | |
children | React.ReactNode | Anything which can be rendered in a select | |
className | string | Classes applied to root element of select | |
isOpen | boolean | Flag to indicate if select is open | |
isPlain | boolean | Indicates if the select should be without the outer box-shadow | |
isScrollable | boolean | Indicates if the select menu should be scrollable | |
maxMenuHeight | string | Maximum height of select menu | |
menuHeight | string | Height of the select menu | |
onOpenChange | (isOpen: boolean) => void | Callback to allow the select component to change the open state of the menu. Triggered by clicking outside of the menu, or by pressing any keys specificed in onOpenChangeKeys. | |
onOpenChangeKeys | string[] | Keys that trigger onOpenChange, defaults to tab and escape. It is highly recommended to include Escape in the array, while Tab may be omitted if the menu contains non-menu items that are focusable. | |
onSelect | (event?: React.MouseEvent<Element, MouseEvent>, value?: string | number) => void | Function callback when user selects an option. | |
popperProps | SelectPopperProps | Additional properties to pass to the popper | |
role | string | Determines the accessible role of the select. For a checkbox select pass in "menu". | |
selected | any | any[] | Single select option value for single select menus, or array of select option values for multi select. You can also specify isSelected on the SelectOption. | |
shouldFocusFirstItemOnOpen Beta | boolean | Flag indicating the first menu item should be focused after opening the menu. | |
shouldFocusToggleOnSelect | boolean | Flag indicating the toggle should be focused after a selection. If this use case is too restrictive, the optional toggleRef property with a node toggle may be used to control focus. | |
zIndex | number | z-index of the select menu |
SelectOption
Name | Type | Default | Description |
---|---|---|---|
children | React.ReactNode | Anything which can be rendered in a select option | |
className | string | Classes applied to root element of select option | |
description | React.ReactNode | Description of the option | |
hasCheckbox | boolean | Indicates the option has a checkbox | |
icon | React.ReactNode | Render option with icon | |
isDisabled | boolean | Indicates the option is disabled | |
isExternalLink | boolean | Render an external link icon on focus or hover, and set the link's "target" attribute to a value of "_blank". | |
isFocused | boolean | Indicates the option is focused | |
isSelected | boolean | Indicates the option is selected | |
value | any | Identifies the component in the Select onSelect callback |
SelectGroup
Name | Type | Default | Description |
---|---|---|---|
childrenrequired | React.ReactNode | Anything which can be rendered in a select group | |
className | string | Classes applied to root element of select group | |
label | string | Label of the select group |
SelectList
Name | Type | Default | Description |
---|---|---|---|
childrenrequired | React.ReactNode | Anything which can be rendered in a select list | |
className | string | Classes applied to root element of select list | |
isAriaMultiselectable | boolean | false | Indicates to assistive technologies whether more than one item can be selected for a non-checkbox select |
MenuToggle
Name | Type | Default | Description |
---|---|---|---|
badge | BadgeProps | React.ReactNode | Optional badge rendered inside the toggle, after the children content | |
children | React.ReactNode | Content rendered inside the toggle | |
className | string | Additional classes added to the toggle | |
icon | React.ReactNode | Optional icon or image rendered inside the toggle, before the children content. It is recommended to wrap most basic icons in our icon component. | |
isDisabled | boolean | Flag indicating the toggle is disabled | |
isExpanded | boolean | Flag indicating the toggle has expanded styling | |
isFullHeight | boolean | Flag indicating the toggle is full height | |
isFullWidth | boolean | Flag indicating the toggle takes up the full width of its parent | |
ouiaId | number | string | Value to overwrite the randomly generated data-ouia-component-id. It will always target the toggle button. | |
ouiaSafe | boolean | Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. | |
size | 'default' | 'sm' | Adds styling which affects the size of the menu toggle | |
splitButtonOptions | SplitButtonOptions | Object used to configure a split button menu toggle | |
status | 'success' | 'warning' | 'danger' | Status styles of the menu toggle | |
statusIcon | React.ReactNode | Overrides the status icon | |
variant | 'default' | 'plain' | 'primary' | 'plainText' | 'secondary' | 'typeahead' | Variant styles of the menu toggle |
SelectToggleProps
Name | Type | Default | Description |
---|---|---|---|
toggleNoderequired | React.ReactNode | Select toggle node. | |
toggleRef | React.RefObject<HTMLButtonElement> | Reference to the toggle. |
SelectPopperProps
Name | Type | Default | Description |
---|---|---|---|
appendTo | HTMLElement | (() => HTMLElement) | 'inline' | The container to append the select to. Defaults to document.body. If your select is being cut off you can append it to an element higher up the DOM tree. Some examples: appendTo="inline" appendTo={() => document.body} appendTo={document.getElementById('target')} | |
direction | 'up' | 'down' | Vertical direction of the popper. If enableFlip is set to true, this will set the initial direction before the popper flips. | |
enableFlip | boolean | Enable to flip the popper when it reaches the boundary | |
maxWidth | string | 'trigger' | Maximum width of the popper. If the value is "trigger", it will set the max width to the select toggle's width | |
minWidth | string | 'trigger' | Minimum width of the popper. If the value is "trigger", it will set the min width to the select toggle's width | |
position | 'right' | 'left' | 'center' | 'start' | 'end' | Horizontal position of the popper | |
preventOverflow | boolean | Flag to prevent the popper from overflowing its container and becoming partially obscured. | |
width | string | 'trigger' | Custom width of the popper. If the value is "trigger", it will set the width to the select toggle's width |
CSS variables
Expand or collapse column | Selector | Variable | Value |
---|