Form
Native Select
Usage#
import { WdsNativeSelectComponent } from '@wds/angular/native-select';Examples#
Default#
<option> elements inside. An option with an empty value works as a prompt; disabled options cannot be picked.
import { Component } from '@angular/core';
import { WdsNativeSelectComponent } from '@wds/angular/native-select';
@Component({
selector: 'app-native-select-default-demo',
imports: [WdsNativeSelectComponent],
template: `
<wds-native-select aria-label="Station type">
<option value="">Select a station type</option>
<option value="main">Main station</option>
<option value="junction">Junction</option>
<option value="halt">Halt</option>
<option value="freight">Freight yard</option>
<option value="depot" disabled>Depot</option>
</wds-native-select>
`,
})
export class NativeSelectDefaultDemo {}Groups#
<optgroup> puts options under a heading.
import { Component } from '@angular/core';
import { WdsNativeSelectComponent } from '@wds/angular/native-select';
@Component({
selector: 'app-native-select-groups-demo',
imports: [WdsNativeSelectComponent],
template: `
<wds-native-select aria-label="Train">
<option value="">Select a train</option>
<optgroup label="Passenger">
<option value="regional">Regional</option>
<option value="intercity">Intercity</option>
<option value="night">Night train</option>
</optgroup>
<optgroup label="Freight">
<option value="container">Container</option>
<option value="tank">Tank wagons</option>
<option value="bulk">Bulk cargo</option>
</optgroup>
</wds-native-select>
`,
})
export class NativeSelectGroupsDemo {}Size#
size="sm" makes the field 28px high instead of 32px.
import { Component } from '@angular/core';
import { WdsNativeSelectComponent } from '@wds/angular/native-select';
@Component({
selector: 'app-native-select-size-demo',
imports: [WdsNativeSelectComponent],
template: `
<wds-native-select size="sm" aria-label="Priority">
<option value="normal">Normal</option>
<option value="high">High</option>
</wds-native-select>
`,
})
export class NativeSelectSizeDemo {}Field#
wds-field the label and description name and describe the select. Without a matching value the first option is selected.
import { Component } from '@angular/core';
import { WdsFieldComponent, WdsFieldDescriptionComponent, WdsFieldLabelComponent } from '@wds/angular/field';
import { WdsNativeSelectComponent } from '@wds/angular/native-select';
@Component({
selector: 'app-native-select-field-demo',
imports: [WdsFieldComponent, WdsFieldLabelComponent, WdsFieldDescriptionComponent, WdsNativeSelectComponent],
template: `
<wds-field class="w-full max-w-xs">
<wds-field-label>Region</wds-field-label>
<wds-native-select class="w-full" name="region" autocomplete="address-level1">
<option value="north">North</option>
<option value="central">Central</option>
<option value="south">South</option>
</wds-native-select>
<wds-field-description>Trains in this region appear first.</wds-field-description>
</wds-field>
`,
})
export class NativeSelectFieldDemo {}Disabled#
import { Component } from '@angular/core';
import { WdsNativeSelectComponent } from '@wds/angular/native-select';
@Component({
selector: 'app-native-select-disabled-demo',
imports: [WdsNativeSelectComponent],
template: `
<wds-native-select disabled aria-label="Track gauge">
<option value="standard">Standard gauge</option>
<option value="broad">Broad gauge</option>
</wds-native-select>
`,
})
export class NativeSelectDisabledDemo {}Invalid#
invalid draws a red border and sets aria-invalid; in wds-field set invalid on the field.
import { Component } from '@angular/core';
import { WdsNativeSelectComponent } from '@wds/angular/native-select';
@Component({
selector: 'app-native-select-invalid-demo',
imports: [WdsNativeSelectComponent],
template: `
<wds-native-select invalid aria-label="Operator">
<option value="">Select an operator</option>
<option value="north-rail">North Rail</option>
<option value="metro-lines">Metro Lines</option>
</wds-native-select>
`,
})
export class NativeSelectInvalidDemo {}Forms#
Integration with Angular forms. Pick the API your app uses.
Signal forms#
WdsSelectField connects the select to [formField] (a string).
import { Component, computed, signal } from '@angular/core';
import { FormField, FormRoot, form, required } from '@angular/forms/signals';
import { WdsButtonComponent } from '@wds/angular/button';
import { WDS_FIELD } from '@wds/angular/field';
import { WdsSelectField } from '@wds/angular/forms';
import { WdsNativeSelectComponent } from '@wds/angular/native-select';
@Component({
selector: 'app-native-select-signal-forms-demo',
imports: [...WDS_FIELD, FormField, FormRoot, WdsButtonComponent, WdsNativeSelectComponent, WdsSelectField],
template: `
<form class="flex w-sm max-w-full flex-col gap-4" [formRoot]="tripForm">
<wds-field [invalid]="showClassError()">
<wds-field-label>Class</wds-field-label>
<wds-native-select class="w-full" [formField]="tripForm.travelClass">
<option value="">Select a class</option>
<option value="first">First class</option>
<option value="second">Second class</option>
</wds-native-select>
<wds-field-error [errors]="showClassError() ? tripForm.travelClass().errors() : []" />
</wds-field>
<wds-button type="submit" class="self-start">Submit</wds-button>
<p class="m-0 font-mono text-sm text-muted-foreground">Submitted: {{ submitted() }}</p>
</form>
`,
})
export class NativeSelectSignalFormsDemo {
protected readonly trip = signal({ travelClass: '' });
protected readonly submitted = signal('—');
protected readonly tripForm = form(
this.trip,
(path) => {
required(path.travelClass, { message: 'Select a class.' });
},
{
submission: {
action: async (form) => {
this.submitted.set(JSON.stringify(form().value()));
},
},
}
);
/** The error appears after the user leaves the field or submits the form. */
protected readonly showClassError = computed(() => this.tripForm.travelClass().touched() && this.tripForm.travelClass().invalid());
}Template-driven#
WdsSelectValueAccessor makes ngModel work on wds-native-select.
import { Component, signal } from '@angular/core';
import { FormsModule, type NgForm } from '@angular/forms';
import { WdsButtonComponent } from '@wds/angular/button';
import { WDS_FIELD } from '@wds/angular/field';
import { WdsSelectValueAccessor } from '@wds/angular/forms';
import { WdsNativeSelectComponent } from '@wds/angular/native-select';
@Component({
selector: 'app-native-select-template-driven-demo',
imports: [...WDS_FIELD, FormsModule, WdsButtonComponent, WdsNativeSelectComponent, WdsSelectValueAccessor],
template: `
<form #tripForm="ngForm" class="flex w-sm max-w-full flex-col gap-4" (ngSubmit)="save(tripForm)">
<wds-field [invalid]="!!travelClass.invalid && !!travelClass.touched">
<wds-field-label>Class</wds-field-label>
<wds-native-select #travelClass="ngModel" class="w-full" name="travelClass" [(ngModel)]="trip.travelClass" required>
<option value="">Select a class</option>
<option value="first">First class</option>
<option value="second">Second class</option>
</wds-native-select>
@if (travelClass.invalid && travelClass.touched) {
<wds-field-error>Select a class.</wds-field-error>
}
</wds-field>
<wds-button type="submit" class="self-start">Submit</wds-button>
<p class="m-0 font-mono text-sm text-muted-foreground">Submitted: {{ submitted() }}</p>
</form>
`,
})
export class NativeSelectTemplateDrivenDemo {
protected readonly trip = { travelClass: '' };
protected readonly submitted = signal('—');
protected save(form: NgForm): void {
if (form.invalid) {
form.form.markAllAsTouched();
return;
}
this.submitted.set(JSON.stringify(form.value));
}
}Reactive forms#
WdsSelectValueAccessor works with formControl and formControlName.
import { Component, signal } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { WdsButtonComponent } from '@wds/angular/button';
import { WDS_FIELD } from '@wds/angular/field';
import { WdsSelectValueAccessor } from '@wds/angular/forms';
import { WdsNativeSelectComponent } from '@wds/angular/native-select';
@Component({
selector: 'app-native-select-reactive-forms-demo',
imports: [...WDS_FIELD, ReactiveFormsModule, WdsButtonComponent, WdsNativeSelectComponent, WdsSelectValueAccessor],
template: `
<form class="flex w-sm max-w-full flex-col gap-4" [formGroup]="tripForm" (ngSubmit)="save()">
<wds-field [invalid]="travelClass.invalid && travelClass.touched">
<wds-field-label>Class</wds-field-label>
<wds-native-select class="w-full" formControlName="travelClass">
<option value="">Select a class</option>
<option value="first">First class</option>
<option value="second">Second class</option>
</wds-native-select>
@if (travelClass.invalid && travelClass.touched) {
<wds-field-error>Select a class.</wds-field-error>
}
</wds-field>
<wds-button type="submit" class="self-start">Submit</wds-button>
<p class="m-0 font-mono text-sm text-muted-foreground">Submitted: {{ submitted() }}</p>
</form>
`,
})
export class NativeSelectReactiveFormsDemo {
protected readonly tripForm = new FormGroup({
travelClass: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
});
protected readonly travelClass = this.tripForm.controls.travelClass;
protected readonly submitted = signal('—');
protected save(): void {
if (this.tripForm.invalid) {
this.tripForm.markAllAsTouched();
return;
}
this.submitted.set(JSON.stringify(this.tripForm.getRawValue()));
}
}API References#
wds-native-select#
<select>: the browser's own list opens, which suits mobile devices and long, simple lists.
Write <option> and <optgroup> elements inside; the element copies them into its <select> and follows
their changes. It works in a <form> and with wds-field.
Without a matching value the first option is selected (or the one with selected), as in a native <select>.
For a list with search, icons or several values use wds-select or wds-combobox.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
| value | string | '' | |
| name | string | '' | |
| size | WdsNativeSelectSize | 'default' | |
| disabled | boolean | false | |
| required | boolean | false | |
| invalid | boolean | false | aria-invalid. |
| autocomplete | string | '' | autocomplete hint, e.g. country. |
Events
| Event | Description |
|---|---|
| wds-changeCustomEvent<WdsNativeSelectChangeDetail> |
Methods
| Method | Description |
|---|---|
| formResetCallback() | |
| formDisabledCallback(disabled: boolean) | |
| formStateRestoreCallback(state: string | File | FormData | null) | |
| checkValidity() | true when the value is valid; otherwise fires invalid on the element. |
| reportValidity() | checkValidity(), and shows the browser validation bubble when the value is invalid. |
| setCustomValidity(message: string) | input.setCustomValidity(). |
Slots
| Slot | Description |
|---|---|
| (default) | <option> and <optgroup> elements. They are not shown themselves: the native <select> shows their copies. |
CSS parts (::part)
| Part | Description |
|---|---|
| control | <select>. |
| icon |
Accessibility#
Screen reader#
<select>: screen readers announce it as a combo box or pop-up button with the selected option, and the browser draws the list.
wds-field-label in wds-field, or aria-label on the element. A prompt option with an empty value is not a name.
<optgroup> labels are read with their options. invalid, required and disabled reach the native <select>; in a form the value is submitted under name, and a reset restores the option with selected.
Keyboard support#
| Key | Function |
|---|---|