This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Autocomplete

An input that suggests options as you type.

Examples#

Load items asynchronously while typing and render custom status content. The onChange handler of ComboboxInput calls a repository service, and the state keeps the results. ComboboxStatus shows the texts of the states of the search: pending with a spinner while it runs, error when the handler throws, empty when nothing matches, and results when items show. The label of an item is the text that a choice writes in the input. Type "will_error" to see a failure.

This example sends events to the server: PHP runs in your browser.

Inline autocomplete#

Autofill the input with the highlighted item while navigating with arrow keys using the mode prop. Accepts aria-autocomplete values list, both, inline, or none.

Grouped#

Organize related options with ComboboxGroup and ComboboxGroupLabel to add section headings inside the popup.

Render one group for each section of your data, with the heading text in its label and its items in it. A group with no matching item is hidden.

Fuzzy matching#

Use fuzzy matching to find relevant results even when the query doesn’t exactly match the item text. Here filter="fuzzy" matches the characters of the query in order, in the label and in each of the keywords of an item, and ComboboxHighlight marks the text that has the query.

Limit results#

Limit the number of visible items using the limit prop and guide users to refine their query using ComboboxStatus.

Auto highlight#

The first matching item can be automatically highlighted as the user types by specifying the autoHighlight prop on Autocomplete. Set the prop’s value to "always" if the highlight should always be present, such as when the list is rendered inline within a dialog.

The prop can be combined with the keepHighlight and highlightItemOnHover props to configure how the highlight behaves during mouse interactions.

Command palette#

Use the autocomplete input to filter a list of command items that perform an action when clicked. With no ComboboxPopup, the list is inline and always open. Give the items data-phem-dialog-target="close" to close the dialog on a choice. Each open starts with an empty search.

Grid layout#

Display items in a grid layout, wrapping each row in ComboboxRow components, and add the grid prop to Autocomplete. The arrow keys move in the rows and in the columns.

The browser filters the emojis, and the matches of each category fill its rows again. A choice inserts the emoji at the caret of the message (insertInto), and the focus goes back to the message.

Virtualized#

Efficiently handle large datasets by rendering only the first matches. The onChange handler of ComboboxInput searches the dataset on the server, and the state keeps the first 50 matches. ComboboxStatus shows the number of matches, and a choice writes the label of the item in the input.

This example sends events to the server: PHP runs in your browser.

API reference#

Import the components of the family:

use Phem\Ui\Autocomplete;

Autocomplete#

A text field that suggests values while the user types: Base UI Autocomplete.Root, on the phem-combobox controller. The input keeps the free text that the user types: an item fills the input, and does not become a chosen value.

Use the parts of Combobox in it: ComboboxLabel, ComboboxInput, ComboboxPopup, ComboboxList, ComboboxItem, ComboboxHighlight, ComboboxEmpty, ComboboxStatus, ComboboxGroup…

PropTypeDefault
id (required)string—
multipleboolfalse
filter?stringnull
mode?stringnull
limit?intnull
locale?stringnull
autoHighlightbool|stringfalse
keepHighlightboolfalse
highlightItemOnHoverbooltrue
gridboolfalse
insertInto?stringnull
loopFocusbooltrue
side'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end'Side::Bottom
align'start' | 'center' | 'end'Align::Start
sideOffsetint4
alignOffsetint0
collisionPaddingint5
name?stringnull
value?Boundnull
defaultValuestring|array|nullnull
defaultInputValue?stringnull
selectionMode?stringnull
openOnInputClick?boolnull
children?Htmlnull
...attrsarray[]

The source of the examples: examples/ui/Autocomplete.phem.

Phem
Esc
↑↓ to navigate↵ to openEsc to close