<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
Warning:

⚠️ Some props/prop values deprecated in SWAN v3.3
Refer to the information on props/prop values provided further down this page. Find more information and migration documentation in the Deprecation roadmap.

Info:

Listbox is only available in the React API for SWAN.

Warning:

For a better mobile experience, use Dropdown when the list contains simple text choices. Use Listbox only if you require images, sections, or other styling inside the choices.

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. When using HTML or JSX inside a ListboxOption, include a textValue prop whose value is an accessible label for the option.

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.

Info:

You will need to include some sub-components that BasicListbox takes care of for you.

The preview has been updated.

Size

Warning:

⚠️ size is deprecated in SWAN v3.3

The component now automatically handles sizing based on the selected mode(standardMode/compactMode). Please update your implementation to rely on the new mode-based sizing.

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.

Inner label

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

Info:

Note that the BasicListbox convenience component cannot be used for this purpose.

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

Use ListboxWithFloatingLabel to move the label inside the input above the selection. This creates visual harmony with other form elements like text inputs that support floating labels. Keep label text short or use the fullWidth prop to maximize width.

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 callout

Use Callout size="mini" when adding a Callout to a ListboxOption. Specify the content of the Callout in the ListboxOption's label field, using brackets to display it next to the selected option, as in 《New!》.

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.

Disabled options

Use disabled or disabledKeys to display a disabled state.

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.

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.

Info:

To control a Listbox, store the selected value (e.g., in React state or a Redux store) and provide a handler to update this value when users interact with the component.

The preview has been updated.

Info:

If you don't need to manage the state of the Listbox, you can omit the selectedKeys and onSelectionChange props, allowing the browser to handle the state. Consider using the value prop to specify the initial selected value.

Dynamic Collections

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

Info:

To use the API, pass an array into the items prop of the Listbox. Be sure to keep the items reference stable, by storing it in state or using useMemo, when the data isn’t changing.

The preview has been updated.

Accessibility

Every Listbox requires an aria-labelledby prop with a value representing the id of the labeling element, even if it has an "inner label." This labeling element can be visually hidden if not intended for display on screens.

Alternatively, you can use the aria-label attribute on ListboxButton in Listbox or directly on BasicListbox in the convenience component.

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.

Don't...

  • Use a Listbox when listing fewer than 4 items. Use a Selection Set of Radio Buttons instead, if space permits.
  • 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.
proptyperequireddescriptiondefault
size"standard" | "mini"false
⚠️ Deprecated - This is deprecated without a replacement.
The size (height) of the Listbox. One of: "standard", "mini".
null
skin"standard" | "error"false
The visual style of the Listbox. One of: "standard", "error". The visual style of the Listbox. One of: "standard", "error".
null
fullWidthbooleanfalse
Whether or not the Listbox should expand to fill the entire width of its container.
null
disabledbooleanfalse
Whether or not the Listbox is disabled.
null
selectionMode"multiple" | "single"false
The selection mode of the Listbox. One of: "single", "multiple".
null
selectedKeys"all" | Set<string | number>false
The keys of the selected items in the Listbox.
null
disabledKeysSet<string | number>false
The keys of the disabled items in the Listbox.
null
onSelectionChange((selectedKeys: ListboxSelectedValues) => void)false
Callback function that gets triggered when the selection changes.
null
valuestring | nullfalse
The value of the selected item in single selection mode (for backward compatibility).
null
onChange((value: string) => void)false
Callback function that gets triggered when the value changes (for backward compatibility).
null
itemsIterable<any>false
Optional collection of dynamic options. Use with a render function as a child of ListboxList/ComboboxList
null
inModalboolean | nullfalse
Set to "true" if the Basic Listbox is rendering inside a Modal.
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.
proptyperequireddescriptiondefault
size"standard" | "mini"false
⚠️ Deprecated - This is deprecated without a replacement.
The size (height) of the Listbox. One of: "standard", "mini".
null
skin"standard" | "error"false
The visual style of the Listbox. One of: "standard", "error". The visual style of the Listbox. One of: "standard", "error".
null
fullWidthbooleanfalse
Whether or not the Listbox should expand to fill the entire width of its container.
null
disabledbooleanfalse
Whether or not the Listbox is disabled.
null
selectionMode"multiple" | "single"false
The selection mode of the Listbox. One of: "single", "multiple".
single
selectedKeys"all" | Set<string | number>false
The keys of the selected items in the Listbox.
null
disabledKeysSet<string | number>false
The keys of the disabled items in the Listbox.
null
onSelectionChange((selectedKeys: ListboxSelectedValues) => void)false
Callback function that gets triggered when the selection changes.
null
valuestring | nullfalse
The value of the selected item in single selection mode (for backward compatibility).
null
onChange((value: string) => void)false
Callback function that gets triggered when the value changes (for backward compatibility).
null
itemsIterable<any>false
Optional collection of dynamic options. Use with a render function as a child of ListboxList/ComboboxList
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.
proptyperequireddescriptiondefault
placeholderReactNodeLikefalse
Placeholder Text to display when no item is selected, instead of the first item.
null
labelPrefixReactNodeLikefalse
Content to be displayed before the selected items.
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. 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.
proptyperequireddescriptiondefault
labelReactNodeLikefalse
The contents to render inside the ListboxButton. Can include JSX
null
disabledboolean | nullfalse
Whether or not the option is disabled.
null
textValuestring | nullfalse
The option's human-readable string. Required if using JSX inside of an option
null
withThumbnailboolean | nullfalse
Whether or not the option has a thumbnail.
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.
proptyperequireddescriptiondefault
fullWidthboolean | nullfalse
-
null
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.
proptyperequireddescriptiondefault
childrenReactNodefalse
The content to be displayed within the popover.
null
placementPlacementfalse
The placement of the popover. eg: "bottom start", "top end", "top", "bottom"
bottom start
offsetnumberfalse
The offset distance from the trigger element.
null
isNonModalbooleanfalse
Specifies whether the popover is used in a non-modal context.
null
hideArrowbooleanfalse
Specifies whether to hide the arrow of the popover.
true
fullBleedbooleanfalse
Specifies whether the popover should take up the full available width.
null
labelTextstringfalse
The label text for accessibility.
null
portalContainerElementfalse
The container for the portal where the popover is rendered.
null
inModalboolean | nullfalse
Set to "true" if the Listbox Popover is rendering inside a Modal.
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

Was this page useful?

Thanks for your feedback!

Feel free to include your name if you wish to have a follow up conversation.


Published: Dec 5, 2023Last updated: Sep 3, 2024