<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5HGSQD2L" height="0" width="0" style="display:none;visibility:hidden" title="GTM"></iframe>

Listbox

Input field that displays a list of items when selected.
styleKeys: listboxstyleKeys: popover

Use Listbox...

  • To display complex information like titles, sections, thumbnail images or callouts.

Don't use Listbox...

  • To display a list with plain text. Use Dropdown instead.
  • To display a list with links. Use Menu instead.
  • To leverage the native browser select functionality.

Basic Listbox

A BasicListbox is a simple-to-use convenience component that handles most typical use cases of a Listbox.

It takes a set of ListboxOption components as children:

  • Each ListboxOption must have a key prop holding that option's value.
  • Optionally, use a label prop for each ListboxOption. This feature allows you to display specific content or have callouts in the Listbox's button element when an option is selected.

Inside each ListboxOption you can put any HTML content, including bold text, colored text, Callouts, etc. For more information, see With HTML or JSX content.

BasicListbox also needs an accessible label: either an aria-labelledby prop whose value is the id of the element on the page that labels the Listbox, or an aria-label prop whose value is localized text for the label.

The preview has been updated.

Full Listbox

Use a full Listbox for more advanced features, like for instance, an inner or floating label.

The preview has been updated.

Size

Use size="mini" to reduce the height of a Listbox. Use also the "mini" size for components that should play a supporting role in the layout, ensuring a clear visual hierarchy. This size works well in situations where there are limitations on vertical space.

The preview has been updated.

Full width

Use fullWidth to grow a Listbox to fill it's container.

The preview has been updated.

Label prefix

Use labelPrefix to display a "prefix" on the left and the selected value on the right.

The preview has been updated.

Button placeholder

Use the placeholder property if you want to show a text when no options are selected.

The preview has been updated.

With floating label

An alternate way to display the listbox with the label positioned inside it, above the selection. To achieve that, use the component InputWithFloatingLabel.

The preview has been updated.

With thumbnail container

Use ListboxOptionThumbnailContainer, which contains a square FluidImage, to display square thumbnail images in selections.

The preview has been updated.

With sections

Use ListboxSection to display a section title to wrap a group of selections. Each ListboxSection requires a title and a key prop.

The preview has been updated.

With HTML or JSX content

ListboxOption can accept inner HTML content, such as a Callout. When using HTML content, include a textValue prop whose value is an accessible label for the option.

Additionally, the label prop can be used to dictate how the selected option is rendered inside the ListboxButton.

The preview has been updated.

Options

Default option

By default, the Listbox displays the first option in the button, but doesn't select it until the user clicks an option: this is to mirror the behavior of an HTML select tag. To preselect an option in the Listbox, use the value prop.

The preview has been updated.

Custom Listbox options

You can use any core prop from SWAN or enclose any SWAN component to create your custom Listbox Option. A very peculiar use case of this across Vista is showing Quantity and Pricing in conjunction, which can be achieved using the FlexBox component as below.

The preview has been updated.

Use href on ListboxOption to make it a link.

The preview has been updated.

The onSelectionChange callback is incompatible with links because they are not treated as selectable items, but rather as navigation items. Instead, use onClick on ListboxList to perform any side effects upon click of an option (e.g. firing a tracking event).

The preview has been updated.

If the side effect is asynchronous, use preventDefault to prevent the redirect while you wait for your logic to resolve, and then manually navigate.

The preview has been updated.

Multi select

You can make a Listbox a multi select by setting the selectionMode="multiple" prop. This will allow the user to select multiple items from the Listbox.

The preview has been updated.

Disabled

Use disabled on Listbox to prevent it from being opened.

Use disabled or disabledKeys on ListboxOption to indicate that individual options are not relevant or available within the user's current context.

The preview has been updated.

Managing state

As is the case with most form elements, folks often want to "control" this component. Controlling a component means that you take ownership over its state rather than letting the browser manage the state for you. When you control a component, you own the source of truth for the UI. This means that you don't need to query the DOM in order to determine the current value of the element. So, when it comes time to use the value of the element somewhere else (e.g. submitting the selection to a back-end service) you can trust your stored value.

The preview has been updated.

Dynamic Collections

Use the Dynamics Collections API with a render function as the child of ListboxList for better performance with large or dynamic data.

The preview has been updated.

Accessibility

BasicListbox requires an aria-labelledby attribute (preferred) or aria-label attribute.


ListboxList requires an aria-label attribute (do not use aria-labelledby , it is not supported).


ListboxButton accepts an aria-labelledby attribute or aria-label attribute, but neither are required because an accessible name will be assigned by default.

The preview has been updated.

Number of listed items

A Listbox containing numerous options or only a few can be overwhelming, difficult to navigate, and may introduce unnecessary challenges by concealing information that could be easily visible.

Do


Use a Listbox when listing 5 or more items.

Dont


Use a Listbox when listing fewer than 4 items. Use a Selection Set of Radio Buttons instead, if space permits

Dont


Use a Listbox when listing more than 15 items. This can create a lack of overview and potential scrolling issues. Use a Combobox instead.

Placement

The Listbox prioritizes usability through generous padding and a large font, resulting in a minimum width determined by the widest option within the Listbox.

  • Ensure the Listbox has sufficient room for rendering on the page.
  • Account for the possibility of translated text being longer than English text to prevent display issues.

Props

BasicListbox
This component is implemented using the <div /> as the root element. You can utilize the native attributes supported by <div /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.
proptyperequireddefault
aria-label
Defines a string value that labels the current element. Provides a localized accessible label for the listbox. e.g. "Select your preferred payment method" @see aria-labelledby.
stringfalse
null
aria-labelledby
Identifies the element (or elements) that labels the current element. ID of an element whose text content labels the listbox. The referenced element's text should be localized. e.g. id of element with text "Payment method options" @see aria-describedby.
stringfalse
null
size
The size (height) of the Listbox.
⚠️ Deprecated - Sizing is now handled automatically by standardMode/compactMode
"standard" | "mini"false
standard
skin
The visual style of the Listbox.
"standard" | "error"false
standard
fullWidth
Whether or not the Listbox should expand to fill the entire width of its container.
booleanfalse
false
disabled
Whether or not the Listbox is disabled.
booleanfalse
false
selectionMode
The selection mode of the Listbox.
"multiple" | "single"false
single
selectedKeys
The keys of the selected items in the Listbox.
"all" | Set<string | number>false
null
disabledKeys
The keys of the disabled items in the Listbox.
Set<string | number>false
null
onSelectionChange
Callback function that gets triggered when the selection changes.
((selectedKeys: ListboxSelectedValues) => void)false
null
value
The value of the selected item in single selection mode (for backward compatibility).
string | nullfalse
null
onChange
Callback function that gets triggered when the value changes (for backward compatibility).
((value: string) => void)false
null
items
Optional collection of dynamic options. Use with a render function as a child of ListboxList/ComboboxList
Iterable<any>false
null
maxHeight
Overwrites the max-height of the ListboxPopover to a custom value (px)
numberfalse
null
inModal
Set to "true" if the Basic Listbox is rendering inside a Modal.
boolean | nullfalse
false
Listbox
This component is implemented using the <div /> as the root element. You can utilize the native attributes supported by <div /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.
proptyperequireddefault
aria-label
Defines a string value that labels the current element. Provides a localized accessible label for the listbox. e.g. "Select your preferred payment method" @see aria-labelledby.
stringfalse
null
aria-labelledby
Identifies the element (or elements) that labels the current element. ID of an element whose text content labels the listbox. The referenced element's text should be localized. e.g. id of element with text "Payment method options" @see aria-describedby.
stringfalse
null
size
The size (height) of the Listbox.
⚠️ Deprecated - Sizing is now handled automatically by standardMode/compactMode
"standard" | "mini"false
standard
skin
The visual style of the Listbox.
"standard" | "error"false
standard
fullWidth
Whether or not the Listbox should expand to fill the entire width of its container.
booleanfalse
false
disabled
Whether or not the Listbox is disabled.
booleanfalse
false
selectionMode
The selection mode of the Listbox.
"multiple" | "single"false
single
selectedKeys
The keys of the selected items in the Listbox.
"all" | Set<string | number>false
null
disabledKeys
The keys of the disabled items in the Listbox.
Set<string | number>false
null
onSelectionChange
Callback function that gets triggered when the selection changes.
((selectedKeys: ListboxSelectedValues) => void)false
null
value
The value of the selected item in single selection mode (for backward compatibility).
string | nullfalse
null
onChange
Callback function that gets triggered when the value changes (for backward compatibility).
((value: string) => void)false
null
items
Optional collection of dynamic options. Use with a render function as a child of ListboxList/ComboboxList
Iterable<any>false
null
maxHeight
Overwrites the max-height of the ListboxPopover to a custom value (px)
numberfalse
null
ListboxButton
This component is implemented using the <button /> as the root element. You can utilize the native attributes supported by <button /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.
proptyperequireddefault
labelPrefix
Content to be displayed before the selected items.
ReactNodefalse
null
placeholder
Placeholder Text to display when no item is selected, instead of the first item.
ReactNodefalse
null
ListboxList
This component is implemented using the <ul /> as the root element. You can utilize the native attributes supported by <ul /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.
ListboxOption
This component is implemented using the <li /> as the root element. You can utilize the native attributes supported by <li /> tag.
See core props for additional props that can be applied to this component.
proptyperequireddefault
label
The contents to render inside the ListboxButton. Can include JSX
ReactNodefalse
null
disabled
Whether or not the option is disabled.
boolean | nullfalse
null
textValue
The option's human-readable string. Required if using JSX inside of an option
string | nullfalse
null
withThumbnail
Whether or not the option has a thumbnail.
boolean | nullfalse
null
ListboxFloatingLabel
This component is implemented using the <label /> as the root element. You can utilize the native attributes supported by <label /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.
ListboxWithFloatingLabel
This component is implemented using the <div /> as the root element. You can utilize the native attributes supported by <div /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.
proptyperequireddefault
fullWidth
Whether or not the Listbox should expand to fill the entire width of its container.
boolean | nullfalse
false
ListboxPopover
This component is implemented using the <div /> as the root element. You can utilize the native attributes supported by <div /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.
proptyperequireddefault
offset
The offset distance from the trigger element.
numberfalse
null
children
The content to be displayed within the popover.
ReactNodefalse
null
fullBleed
Specifies whether the popover should take up the full available width.
booleanfalse
null
placement
Specifies where the popover should be placed in relation to the trigger element The placement of the popover.
Placementfalse
top
bottom start
isNonModal
Specifies whether the popover is used in a non-modal context.
booleanfalse
false
hideArrow
Whether to hide the popover arrow Specifies whether to hide the arrow of the popover.
booleanfalse
false
true
labelText
⚠️ Deprecated - This is deprecated without a replacement.
stringfalse
null
portalContainer
The container for the portal where the popover is rendered.
Elementfalse
null
inModal
Set to "true" if the Listbox Popover is rendering inside a Modal.
boolean | nullfalse
null
ListboxOptionTextContainer
This component is implemented using the <div /> as the root element. You can utilize the native attributes supported by <div /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.
ListboxOptionThumbnailContainer
This component is implemented using the <div /> as the root element. You can utilize the native attributes supported by <div /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.
ListboxSection
This component is implemented using the <section /> as the root element. You can utilize the native attributes supported by <section /> tag. The ref is directly assigned to the root element, allowing you to access and manipulate it as needed.
See core props for additional props that can be applied to this component.

Related