Forms

Autocomplete

A text input that suggests values as the user types. The field keeps whatever was typed: a suggestion is only a shortcut to fill it in. Use it when free text is allowed; to make the user pick one of a fixed set of values use tk:combobox or tk:select.

The suggestions usually come from options; tk:listbox.item is only written to give one of them an icon, a badge or other markup.

  • Recife
  • Rio de Janeiro
  • Rio Branco
  • Salvador
Blade

A text input labelled "City". Typing rio opens a list under it with Rio de Janeiro and Rio Branco; choosing one, with the mouse or with the arrow keys and Enter, writes it in the input and closes the list.

Bound to Livewire #

$city receives the text of the input: 'Rio de Janeiro' after choosing that suggestion, or 'Rio das Ostras' if the user typed a city that is not in the list.

  • Recife
  • Rio de Janeiro
  • Rio Branco
  • Salvador
$destination:
Blade

Suggestions filtered on the server #

Each update re-renders the options, and the list on screen follows them without the input losing focus. Use this when the full set is too large to send to the browser.

  • Acme Corporation
  • Globex Inc.
  • Initech
  • Umbrella Ltd.
  • Stark Industries
  • Wayne Enterprises
  • Wonka Industries
  • Cyberdyne Systems
  • Soylent Corp.
  • Hooli
Blade

Custom suggestions #

Items in the slot take the element props. A disabled item shows in the list but is skipped by the arrow keys and cannot be chosen. Slot items come before the ones from options when both are given.

  • Dashboard
  • Invoices 12
  • Archived
Blade

Acting on the choice #

With clear-on-select the input is emptied after a suggestion is chosen, ready for the next one, and the choice is read from the selected event.

  • laravel
  • livewire
  • alpine
  • tailwind
  • php
  • vite
Added: —
Blade

Show all on focus #

By default nothing shows while the input is empty. hide-empty set to false lists every suggestion as soon as the input is focused.

  • Engineering
  • Marketing
  • Sales
  • Support
  • Finance
Blade

Field props and states #

The field props (label, help, icon, size...) work as on tk:input. A disabled or readonly input never opens the list.

Free text is allowed.

  • Brazil
  • Portugal
  • United States
  • Germany
  • Japan
  • Brazil
  • Portugal
  • Brazil
  • Portugal
Blade

Typo tolerance #

The search ignores case and accents but not typos. fuse-options raises the Fuse.js threshold so aple still finds Apple.

  • Apple
  • Apricot
  • Banana
  • Blueberry
  • Strawberry
Blade

Customization #

Classes go to the <input>; prefixed attributes reach the panel (popover:), the list (items:) and each suggestion (items:item:).

  • Paraná
  • Pernambuco
  • Paraíba
  • Piauí
  • Santa Catarina
Blade

Reference

tk:autocomplete
└── tk:listbox.item

tk:autocomplete

Prop Type Default Description
options array | Collection | LazyCollection | enum | query null The suggestions. Only the labels are used: the keys of a keyed array are ignored, and grouped options are flattened into one list. The text is read from name, label, title or text, or from the attribute named by option-label. A keyed array of arrays without option-value, option-label or option-group-children is read as groups, so records indexed by id need option-label.
hideEmpty bool true Shows nothing while the input is empty. false lists every suggestion as soon as the input is focused.
clearOnSelect bool false Empties the input after a suggestion is chosen, instead of filling it.
fuseOptions array null Options for the Fuse.js search, merged over the defaults (threshold 0.1, ignoreLocation and ignoreDiacritics on).

Also accepts the field props (label, help, placeholder...) and the field control props (icon, loading...), and everything else a tk:input takes (name, disabled, readonly, clearable...).

wire:model and x-model bind the text of the input, a string.

Slots

Slot Description
default tk:listbox.item tags, as suggestions.

Events

Event Detail When
selected { index, item, button }; item.title is the text of the suggestion A suggestion was chosen.
searched { query } The text of the input changed, also when a choice filled it.
filtered { list, items, filteredItems } The list was filtered. filteredItems are the matches, in the order of the list.
ready The list finished its first filtering after the component started.
highlighted { index, item } A suggestion became the active one, by arrow key or pointer.
opened The list opened.
closed The list closed.

Prefixes

Prefix Goes to
container: The element wrapping the input and the list.
popover: The panel that floats under the input.
items: The list (role="listbox").
items:item: Each suggestion coming from options.

tk:listbox.item

Takes the element props (label, icon, badge...) and disabled.

Slots

Slot Description
default The item content, in place of label.

Events

Events are dispatched on the input, the element the tag's attributes go to, and do not bubble: listen on the tag itself.

With Alpine:

blade

Calling a Livewire action:

blade

Write x-on:selected, not @selected: Blade takes @selected as its own directive. When selected fires the input already holds the chosen text. The native events of the input (input, change, blur) are listened to on the tag as well.

Customization

Classes and attributes on the tag go to the <input>. To reach another element, prefix the attribute (listed in the Reference above).

The prefixes of tk:input work too (label:, field:, help:, error:...).

blade

Accessibility

Follows the WAI-ARIA combobox pattern with list autocomplete. The input has role="combobox", aria-autocomplete="list", aria-expanded and aria-controls pointing to the list, which is a role="listbox" of role="option" items. Focus never leaves the input: the active suggestion is exposed through aria-activedescendant. After typing, the number of results is announced through a live region ("2 results available", "No records found").

Key Action
Arrow Down / Arrow Up Moves to the next / previous suggestion, wrapping around. With the list closed, opens it.
Enter Chooses the active suggestion. With none active, the key is left alone, so it submits the form.
Escape Closes the list and keeps the text.
Tab Leaves the field without choosing anything.

Behavior notes

  • The search is fuzzy only slightly: it ignores case and accents and matches anywhere in the text (PAULO finds "São Paulo"), but a typo finds nothing (aple does not find "Apple"). Raise threshold in fuseOptions to tolerate typos.
  • When nothing matches, the list stays closed; nothing is shown on the page.
  • The list opens 150 ms after the last keystroke, and has the width of the input.
  • After a suggestion is chosen, or after Escape, the list stays closed until the user types again or presses an arrow key. A Livewire update does not reopen it.
  • Choosing with the mouse keeps the focus in the input.
  • The value sent is the text. With ['sp' => 'São Paulo'] the input holds São Paulo, never sp.
  • A disabled or readonly input never opens the list.
  • Several autocompletes on one page do not share anything: each list has its own ids.