Listbox is only available in the React API for SWAN.
Listbox
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.
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
ListboxOptionmust have akeyprop holding that option's value. - Optionally, use a
labelprop for eachListboxOption. 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.
You will need to include some sub-components that BasicListbox takes care of for you.
The preview has been updated.
Size
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.
Label prefix
Use labelPrefix to display a "prefix" on the left and the selected value on the right.
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
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.
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.
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.
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.
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.
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
<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.| prop | type | required | description | default |
|---|---|---|---|---|
| size | "standard" | "mini" | false | ⚠️ Deprecated - Sizing is now handled automatically by standardMode/compactMode
The size (height) of the Listbox.
One of: "standard", "mini". | standard |
| skin | "standard" | "error" | false | The visual style of the Listbox.
One of: "standard", "error". | standard |
| fullWidth | boolean | false | Whether or not the Listbox should expand to fill the entire width of its container. | false |
| disabled | boolean | false | Whether or not the Listbox is disabled. | false |
| 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 |
| disabledKeys | Set<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 |
| value | string | null | false | 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 |
| items | Iterable<any> | false | Optional collection of dynamic options. Use with a render function as a child of ListboxList/ComboboxList | null |
| maxHeight | number | false | Overwrites the max-height of the ListboxPopover to a custom value (px) | null |
| inModal | boolean | null | false | Set to "true" if the Basic Listbox is rendering inside a Modal. | false |
<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.| prop | type | required | description | default |
|---|---|---|---|---|
| size | "standard" | "mini" | false | ⚠️ Deprecated - Sizing is now handled automatically by standardMode/compactMode
The size (height) of the Listbox.
One of: "standard", "mini". | standard |
| skin | "standard" | "error" | false | The visual style of the Listbox.
One of: "standard", "error". | standard |
| fullWidth | boolean | false | Whether or not the Listbox should expand to fill the entire width of its container. | false |
| disabled | boolean | false | Whether or not the Listbox is disabled. | false |
| 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 |
| disabledKeys | Set<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 |
| value | string | null | false | 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 |
| items | Iterable<any> | false | Optional collection of dynamic options. Use with a render function as a child of ListboxList/ComboboxList | null |
| maxHeight | number | false | Overwrites the max-height of the ListboxPopover to a custom value (px) | null |
<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.| prop | type | required | description | default |
|---|---|---|---|---|
| labelPrefix | ReactNode | false | Content to be displayed before the selected items. | null |
| placeholder | ReactNode | false | Placeholder Text to display when no item is selected, instead of the first item. | null |
<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.<li /> as the root element. You can utilize the native attributes supported by <li /> tag.| prop | type | required | description | default |
|---|---|---|---|---|
| label | ReactNode | false | The contents to render inside the ListboxButton. Can include JSX | null |
| disabled | boolean | null | false | Whether or not the option is disabled. | null |
| textValue | string | null | false | The option's human-readable string. Required if using JSX inside of an option | null |
| withThumbnail | boolean | null | false | Whether or not the option has a thumbnail. | null |
<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.<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.| prop | type | required | description | default |
|---|---|---|---|---|
| fullWidth | boolean | null | false | Whether or not the Listbox should expand to fill the entire width of its container. | false |
<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.| prop | type | required | description | default |
|---|---|---|---|---|
| offset | number | false | The offset distance from the trigger element. | null |
| children | ReactNode | false | The content to be displayed within the popover. | null |
| fullBleed | boolean | false | Specifies whether the popover should take up the full available width. | null |
| placement | Placement | false | Specifies where the popover should be placed in relation to the trigger element
The placement of the popover. eg: "bottom start", "top end", "top", "bottom" | top bottom start |
| isNonModal | boolean | false | Specifies whether the popover is used in a non-modal context. | false |
| hideArrow | boolean | false | Whether to hide the popover arrow
Specifies whether to hide the arrow of the popover. | false true |
| labelText | string | false | ⚠️ Deprecated - This is deprecated without a replacement. | null |
| portalContainer | Element | false | The container for the portal where the popover is rendered. | null |
| inModal | boolean | null | false | Set to "true" if the Listbox Popover is rendering inside a Modal. | null |
<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.<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.<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.How does Listbox meet your needs?