Examples
Examples
Basic Usage
A simple, yet accessible Select is provided with a descriptive label and a collection of options.
Preselect option
Options can be pre-selected by either setting the value of the Select or the selected attribute for the selected Option.
Grouped Options
The Select does support grouping options to improve the structure of a larger set of choices for users.
Prefixed Options
Options accept prefixed content which can take any form, from other Web Components to any other visual elements that help users to distinguish a group of options more clearly.
☝️ Watch out
The prefix is a decorative element only and should not contain any meaningful information necessary to understand or distinguish an option. Hence the content of a prefix isn't accessible to assistive technologies.
Meta Information
Each option in the select can provide additional information as meta information to support users with their choices.
Help Text & Placeholder
The help text and placeholder serve the purpose of guiding a user's input by providing additional information.
A help text provides more context as to why and how the data that is requested is relevant. It can either be passed as an attribute for simple string-based content or via a slot for rich formatted content.
A placeholder allows a Select to be initialized without an option being preselected while it automatically selects the first option provided when not specified otherwise.
Required and Optional
Adding a required or optional label helps users to be guided while filling out a form. Usually, only Selects that are an exception to the majority of other form fields in a form are marked as required or optional. For example, Selects are marked as "Required" when the majority of the fields in the same form are optional and vice versa.
States
The Select and its Options do support a wide range of different states.
Clearable
Marking a Select as clearable allows users to empty its value with a single click.
Multiple
The multiple attribute enables the selection of multiple options. Options can be preselected by assigning an array of selected values to the Select's value property or by marking the selected Options with the selected attribute.
Searchable
The searchable attribute enables a search functionality, enabling users to quickly filter down the set of options.
Validation
A Select's validity can either be determined automatically when used inside a form or can be controlled from outside based on custom rules and application business logic.
Inside a form
Selects seamlessly integrate into a form's validation workflows and validate when a form is submitted. When no custom validation message is provided, the Select falls back to the browser's native validation message.
Inside an application
Selects can be individually flagged as invalid based on custom rules or other business logic of an application. When flagged as invalid, a Select expects a descriptive validation message to be set alongside.
Listening to Events
The Select supports the most commonly used events.
💡 Did you know?
Some native events like change are blocked by the Shadow DOM boundary of the web component standard. Hence some of them are proxied through custom events.
Find out more: Form Input Events & API