Chapter 05 Choices
Select
One choice from a list. Arrow keys, Home and End, and typing the first letters all move through the options; Escape and Tab close the list and return focus.
import { SelectComponent, type SelectOption } from '@okaapi-ui/angular';
// Works with formControlName, [formControl] and ngModel.Works with
Basic
cities: SelectOption<string>[] = [
{ value: 'cai', label: 'Cairo' },
{ value: 'alx', label: 'Alexandria' },
];
<ui-select label="City" placeholder="Choose a city" [options]="cities" formControlName="city" />With an error
Choose a size
sizes: SelectOption<string>[] = [
{ value: 's', label: 'Small' },
{ value: 'm', label: 'Medium' },
{ value: 'l', label: 'Large', disabled: true },
];
<ui-select label="Size" [options]="sizes" formControlName="size" error="Choose a size" />In Arabic
<!-- dir="rtl" on the page or any ancestor -->
<ui-select label="المدينة" placeholder="اختار المدينة" [options]="cities" formControlName="city" />When to use something else
| If you need | Use |
|---|---|
| A long list the reader searches | Combobox |
| Two to five options that fit on screen | Radio group |
| Actions, not values | Menu |
Inputs
| Name | Type | Default | What it does |
|---|---|---|---|
label | string | — | Required. The visible label; it names the button and the list. |
options | SelectOption<T>[] | — | Required. { value, label, disabled? } for each option. |
placeholder | string | '' | Shown while nothing is chosen. |
error | string | — | The translated error. Marks the select invalid and announces the message. |
compareWith | (a: T, b: T) => boolean | === | How to match the form value to an option, for object values. |
testId | string | — | data-testid on the button; options get -option-<index>. |
Keyboard and screen readers
| Keys | What they do |
|---|---|
| Enter or Space or Up or Down | Open the list. |
| Up or Down or Home or End | Move through the options. |
| Letters | Jump to the option that starts with them. |
| Enter or Space | Pick the option. |
| Escape or Tab | Close the list, and put focus back on the button. |
- Read as a list box, with the chosen option and the field’s label.