Skip to main content

Listbox

Listboxes are menus that display options and allow the user to select one. Listboxes are the menu which displays when clicking a <rapid-select></rapid-select>.

Use cases:

  • Constructing menus as part of form items

While any DOM content is permissible as a child of the listbox, only <rapid-option> elements, option elements, and slotted items with role="option" attribute will be treated as options and receive keyboard support.

tip

The primary use of this component is to be used as part of other custom form components you wish to construct. If you simply want to use a form item which allows the user to select one value we recommend you use a <rapid-radio-group></rapid-radio-group> or <rapid-segmented-control></rapid-segmented-control> instead.

Example

AppleBananaStrawberry

Selected:

Declaration

<rapid-listbox>
<rapid-option></rapid-option>
<rapid-option></rapid-option>
<rapid-option></rapid-option>
</rapid-listbox>

Usage

@customElement({
name: 'my-element',
template: html`
<rapid-listbox @click=${(x, ctx) => x.setSelected(ctx.event)}>
<rapid-option value="apple">Apple</rapid-option>
<rapid-option value="banana">Banana</rapid-option>
<rapid-option value="strawberry">Strawberry</rapid-option>
</rapid-listbox>
Listbox value: ${x => x.listboxValue}
`,
})
export class MyElement extends GenesisElement {
@observable listboxValue: string;
setSelected(event: Event & { target: { value: string } }) {
this.listboxValue = event.target.value;
}
}
warning

Unlike most other Genesis components you cannot use the sync directive here. This is because the listbox doesn't fire events, that is intended to be controlled by the parent form element.

API

Attributes

AttributeTypeDescriptionExample
disabledbooleanWhen disabled the user won't be able to interact with the component.
<rapid-listbox disabled>

Properties

These properties are intended to be selected in the TypeScript code with a reference to the component, not declaratively via the html.

PropertyTypeDescription
selectedIndexnumberThe index of the selected option. Starts at -1 indicating no item selected.
selectedOptionsListboxOption[]A collection of the selected options.

Slots

SlotDescription
DefaultThe default slot which should be filled with <rapid-option>.

Option attributes

In order to use the component, you need to create a list of options for the user to select from. You create this list using <rapid-option>. You can define the following attributes for an <rapid-option>:

NameTypeDescription
disabledbooleanDisables an option so that the user cannot interact with it, but the option is still visible
selectedbooleanSelects the option, so it turns to the selected mode
valuestringThe value of the attribute

You can also use the repeat directive to create options dynamically in the code:

import { html, repeat } from '@genesislcap/web-core';
const sampleSelectOptions = [
{ label: 'Large', value: 'l' },
{ label: 'Small', value: 's' },
];
html`
<rapid-listbox>
${repeat(
(x) => x.sampleSelectOptions,
html`
<rapid-option value="${(x) => x.value}">${(x) => x.label}</rapid-option>
`
)}
</rapid-listbox>
`;

Methods

MethodModifierTypeDescription
selectFirstOptionpublic() => voidMoves focus to the first selectable option.
setSelectedOptionprotected() => voidSets an option as selected (according to the select status of the child option components) and gives it focus.

Parts

This component doesn't have any parts.

Events fired

This component doesn't fire any events. When used as part of a larger form component it's recommended you'd fire change events from that component when the user interacts with the underlying listbox.

Events listened to

This component doesn't listen to any events.