Listbox
Listboxes are menus that display options, allowing the user to select one. Listboxes are displayed when clicking on a <rapid-select></rapid-select>
.
If you simply want to use a form item that allows the user to select one value, use a <rapid-radio-group></rapid-radio-group>
or <rapid-segmented-control></rapid-segmented-control>
instead. These components are the easiest way of meeting more than 90 percent of your requirements.
Only use the Listbox component when you have a custom requirement.
While any DOM content is permissible as a child of the listbox, only <rapid-option>
elements, option elements, and slotted items with the role="option"
attribute will be treated as options and receive keyboard support.
Use cases:
- constructing menus as part of form items
Example
Selected:
- Genesis
- React
- Angular
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;
}
}
Unlike most other Genesis components, you cannot use the sync
directive here. This is because the listbox doesn't fire events. This must be controlled by the parent form element.
Declaration
<rapid-listbox>
<rapid-option></rapid-option>
<rapid-option></rapid-option>
<rapid-option></rapid-option>
</rapid-listbox>
Usage
export function MyComponent() {
const [selected, setSelected] = useState('');
return (
<div>
<rapid-listbox onClick={(e) => setSelected(e.target.value)}>
<rapid-option value="apple">Apple</rapid-option>
<rapid-option value="banana">Banana</rapid-option>
<rapid-option value="strawberry">Strawberry</rapid-option>
</rapid-listbox>
<p>Selected: {selected}</p>
</div>)
}
Declaration
<rapid-listbox>
<rapid-option></rapid-option>
<rapid-option></rapid-option>
<rapid-option></rapid-option>
</rapid-listbox>
Usage
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'my-component',
template: `
<rapid-listbox (click)="setSelected($event)">
<rapid-option value="apple">Apple</rapid-option>
<rapid-option value="banana">Banana</rapid-option>
<rapid-option value="strawberry">Strawberry</rapid-option>
</rapid-listbox>
<p>Selected: {{ selected }}</p>
`,
standalone: true,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [FormsModule],
})
export class MyComponent {
selected: string = '';
setSelected(event: any) {
this.selected = event.target.value;
}
}
API
Attributes
Attribute | Type | Description | Example |
---|---|---|---|
disabled | boolean | When disabled, the user is not able to interact with the component. |
|
Properties
These properties are intended to be selected in the TypeScript code with a reference to the component, not declaratively via the html.
Property | Type | Description |
---|---|---|
selectedIndex | number | The index of the selected option. Starts at -1 indicating no item selected. |
selectedOptions | ListboxOption[] | A collection of the selected options. |
Slots
Slot | Description |
---|---|
Default | The 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>
:
Name | Type | Description |
---|---|---|
disabled | boolean | Disables an option so that the user cannot interact with it, but the option is still visible |
selected | boolean | Selects the option, so it turns to the selected mode |
value | string | The 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
Method | Modifier | Type | Description |
---|---|---|---|
selectFirstOption | public | () => void | Moves focus to the first selectable option. |
setSelectedOption | protected | () => void | Sets 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, fire your change events from that component when the user interacts with the underlying listbox.
Events listened to
This component doesn't listen to any events.