For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /dolan/select.md.

Select

A field that opens a bottom sheet of options and shows the one the user picks. Long lists get a search field.

Installation

npx @blotch/dolan add select
import { UiSelect, UiSelectItem } from '../components/ui/select';
Tip

This also adds Bottom Sheet, which holds the options, and Search Field, for a searchable select.

Usage

src/app/app.ts
import { Component, signal } from '@angular/core';
import { UiSelect, UiSelectItem } from '../components/ui/select';

@Component({
  selector: 'app-root',
  imports: [UiSelect, UiSelectItem],
  template: `
    <ui-select placeholder="Pick a country" [(value)]="country">
      <ui-select-item value="us" label="United States" />
      <ui-select-item value="uk" label="United Kingdom" />
      <ui-select-item value="jp" label="Japan" />
    </ui-select>
  `,
})
export class App {
  protected readonly country = signal('');
}
  • Tapping the field opens the sheet. Tapping an item selects it and closes the sheet.
  • Tapping the dimmed backdrop, or dragging the sheet's handle down, closes it without a change.
  • The field shows the selected item's label, or placeholder when nothing is selected. The selected item has a check mark.
  • The field is the same shape as an Input: 48px tall, on the same fill, so the two line up in a form. It dims while a finger is on it.
  • The list scrolls once it's taller than 300 pixels. A searchable select's list fills the sheet instead.

With a label

<ui-select
  label="Country"
  placeholder="Choose your country"
  [(value)]="country"
>
  …
</ui-select>

label is shown above the field and as the sheet's title, and names the field for screen readers. A label of your own beside the field, such as a Label, isn't tied to it, so give the select an accessibility-label with the same text:

<view class="flex flex-col gap-2">
  <ui-label>Country</ui-label>
  <ui-select accessibility-label="Country" [(value)]="country">…</ui-select>
</view>

Add searchable for a long list, such as time zones or countries:

<ui-select label="Time zone" searchable [(value)]="timeZone">
  @for (zone of timeZones; track zone) {
    <ui-select-item [value]="zone" [label]="zone" />
  }
</ui-select>
  • The sheet opens at full height, with a Search Field at the top. The keyboard rises below it.
  • Typing hides every item whose label doesn't contain the text. Case doesn't matter.
  • emptyText shows when nothing matches. It defaults to No results.
  • The text clears when the sheet closes, so it opens on the full list next time.
  • Bind [(query)] to read or set the text.

Results of your own

Set [filter]="false" to show your own results for the text, such as suggestions from a server:

<ui-select
  label="Address"
  searchable
  [filter]="false"
  [emptyText]="query() ? 'No addresses found' : 'Start typing an address'"
  [(query)]="query"
  [(value)]="address"
>
  @for (result of results(); track result.id) {
    <ui-select-item [value]="result.id" [label]="result.text" />
  }
</ui-select>
protected readonly query = signal('');
protected readonly address = signal('');
// Fetches suggestions whenever the query changes.
protected readonly suggestions = httpResource<Suggestion[]>(() => {
  const query = this.query().trim();
  return query
    ? `https://api.example.com/addresses?q=${encodeURIComponent(query)}`
    : undefined;
});
protected readonly results = computed(() => this.suggestions.value() ?? []);

See HTTP requests for httpResource on Lynx.

The field shows the chosen item's label. Once the item is gone from the list, it shows the value itself. Keep the chosen result among the items to keep its label in the field.

Icons

Put content in an item, such as an Icon, and it goes before the label. Mark content leading on the select itself to show it in the field, before the text:

<ui-select label="Weather" [(value)]="weather">
  <ui-icon leading [name]="weather()" size="xs" />
  <ui-select-item value="sun" label="Sunny">
    <ui-icon name="sun" size="sm" />
  </ui-select-item>
  <ui-select-item value="cloud" label="Cloudy">
    <ui-icon name="cloud" size="sm" />
  </ui-select-item>
</ui-select>

Accessibility

  • The field is one button to VoiceOver and TalkBack, named by label (or accessibility-label), with the chosen item's label, or the placeholder, as its value: VoiceOver says "Country, Germany, button". Without either, the field's name is the text it shows.
  • A disabled field is dimmed to VoiceOver as well.
  • Each item is a button named by its label, and the chosen one is selected, as its check mark shows.
  • Content in the field and the items, such as icons, isn't read. The label says what they mean.

So a spec finds them by role:

fireEvent.tap(screen.getByRole('button', { name: 'Country' }));
fireEvent.tap(await screen.findByRole('button', { name: 'Germany' }));
expect(
  screen.getByRole('button', { name: 'Germany', selected: true }),
).toBeTruthy();

Disabled

<ui-select value="admin" disabled>
  <ui-select-item value="admin" label="Administrator" />
  <ui-select-item value="viewer" label="Viewer" />
</ui-select>

A disabled select dims and doesn't open.

Open from code

UiSelect has public open(), close() and toggle() methods. Get it with viewChild(UiSelect) and call them from a handler. open() and toggle() do nothing while disabled.

Forms

value is a model(), so it works with every Angular forms API:

<ui-select [formField]="profile.field" />
<ui-select [formControl]="control" />
<ui-select [(ngModel)]="value" />

The sheet closing marks the field touched, whether an item was chosen or not. UiSelect implements Signal Forms' FormValueControl. See Forms.

API

UiSelect

InputTypeDefaultDescription
valuestring''The selected item's value. Supports [(value)].
labelstring''Shown above the field and as the sheet's title, and read by screen readers as the field's name.
placeholderstring'Select...'Shown in the field while nothing is selected.
accessibility-labelstring''The field's name for screen readers when it has no label.
disabledbooleanfalseDims the field and stops it from opening.
classstring''Extra Tailwind classes for the column that holds the label and the field.
searchablebooleanfalseShows a search field in the sheet, which opens at full height.
querystring''The search text. Supports [(query)]. Cleared when the sheet closes.
filterbooleantrueHides the items whose label doesn't contain query, ignoring case.
searchPlaceholderstring'Search'Shown in the search field while it's empty.
emptyTextstring'No results'Shown in a searchable sheet while no item is.
OutputTypeDescription
changedstringEmits the value of the item the user tapped.
valueChangestringEmits the new value, for [(value)].
queryChangestringEmits the search text, for [(query)].
touchvoidEmits when the sheet closes, for forms: the field counts as touched.
ContentDescription
[leading]Shown in the field, before the text.
Anything elseThe items, <ui-select-item>, shown in the sheet.

UiSelectItem

InputTypeDefaultDescription
valuestringRequiredThe value this item selects.
labelstringRequiredThe text shown in the list and in the field.
classstring''Extra Tailwind classes for the item's row.

No outputs. Content inside the item, such as an icon, goes before its label.