Select

A styled single-choice field. A listbox, not a <select>.

Select

Needs Alpine. See getting started.

Which select

There are two, and the native one is often right:

x-ds::selectx-ds::input as="select"
Rendersa listbox you can stylethe OS control
Needs JSyesno
On a phonea popup listthe native wheel or sheet
Typeaheadnoyes, free

Prefer the native one on anything phone-first, and for long or unfamiliar lists. This exists because the popup of a native <select> cannot be styled at all.

Use it

<x-ds::select
    name="plan"
    label="Plan"
    :options="['free' => 'Free', 'pro' => 'Pro', 'team' => 'Team']"
    :value="old('plan', $user->plan)"
/>

Props

PropTypeDefaultWhat it does
namestringrequiredThe submitted field name.
optionsarray[]value => label.
valuestring—The selected value.
labelstring—Field label.
placeholderstringSelect…Shown when nothing is selected.
helpstring—Guidance under the field.
errorstring—Validation message.
disabledboolfalse
hideLabelboolfalseKeep label as the accessible name but do not show it, for a select its row already names (a filter bar, a per-page control). Or pass aria-label and no label.

Where your attributes go

The select is three elements, so an attribute you pass goes to the one it means something on:

AttributeGoes onWhy
classthe rootLayout: width, margin, grid placement.
wire:model*, x-model*the root, beside x-modelableBinds the value both ways; see below.
aria-label, aria-describedby (added to the select's own), any other aria-*, autofocus, titlethe triggerIt is the control a reader meets. aria-label names the list too.
formthe input that postsSo a select outside its form element still posts with it.
requiredan unnamed text field laid under the trigger, and aria-required on the triggerA hidden input is never validated. The browser's own message points at the field and focus goes on to the trigger. Without the script there is nothing to choose with, so validate on the server too.
anything else (data-*, wire:key, x-on:…)the root

Until 2.0 only class reached anything, and everything else was dropped without a word.

options is an array, not slot children, because the trigger has to render the selected option's label — the component must be able to look a value up.

It posts like a normal field

The value rides in a hidden input, so this submits in a plain form exactly like a <select>:

<form method="POST" action="/settings">
    @csrf
    <x-ds::select name="plan" label="Plan" :options="$plans" :value="old('plan', $user->plan)" />
    <x-ds::button type="submit">Save</x-ds::button>
</form>

With Livewire, bind the same way you would any field. Pass value as well, so the page is right before the script runs:

<x-ds::select name="plan" label="Plan" wire:model.live="plan" :options="$plans" :value="$plan" />

The binding sits on the select's root with x-modelable, so a pick reaches $wire.plan (and, with .live, the server), and a value the component sets on $plan shows on the trigger. x-model works the same way with Alpine state. A bound select keeps its own state under ds-prefixed names, so your property can be called value, label or open without shadowing it. Before 2.0 this example did nothing: the binding was dropped.

Listening for a pick

Choosing a different option fires change from the hidden input, bubbling, as a native select does, once the new value is in the input. Its detail is the value, so x-on:change="filter($event.detail)" works on the select or on any element around it, and a form listening for change (a save bar, an auto-submitting filter row) sees the pick. Choosing the option already chosen fires nothing.

<form x-data x-on:change="$el.requestSubmit()">
    <x-ds::select name="status" label="Status" :options="$statuses" :value="request('status')" />
</form>

Where the list opens

Below the field, at its width. With the package's script on the page, it flips above the field when there is no room below, and it is placed with position: fixed, so a card, a table or a modal body with overflow set cannot clip it. With room it opens exactly where it always did.

Without the script it still works, below the field as before 2.0, and a clipping ancestor cuts it off.

Keyboard

Everything a select should do: ArrowDown opens it, arrows move, Home and End jump, Enter and Space choose, Escape closes, Tab closes and moves on.

Focus enters the selected option, not the first — opening a list of forty countries at the top when "Zambia" is chosen loses the reader's place.

Accessibility

  • The trigger is a combobox with a live aria-expanded; the list is a listbox and the options are options with aria-selected.
  • error sets aria-invalid and wires aria-describedby.
  • The selected option shows a tick as well as a weight change — weight alone is not reliable when comparing two rows.
  • It renders its selected state from PHP, so there is no flash of the wrong option before Alpine boots.

Don't

  • Don't use it for actions. A menu of verbs is a dropdown.
  • Don't put more than about fifteen options in it. There is no typeahead and no search.
  • Don't omit name. The hidden input is the only reason this submits.
  • Don't reach for it on a phone-first screen. See the top of this page.

More in specs/select.md.


From the package repo: edit this page on GitHub.

Was this page helpful?