Skip to main content

Autocomplete

Makes use of Reka UI Dialog

Basic example

This example asks you to select your favorite fruit and provides suggestions as you type. You can enter any fruit, but the suggestions give you optional assistance for faster text entry.

Note that the suggested completion text (e.g. "anana" in "banana" if you type "b") is bolded in the list of suggestions, making it easy to scan the list for the completion you want.

To avoid overwhelming you with options, the autocomplete displays a maximum of five suggestions at a time. If you type "p", you won't initially see "pomegranate" listed, but if you type "po", you will.

value: [empty]

Custom rendering

It's often important to be able to display more than just text for each suggestion. In this case, since the suggestions are names of icons, it's useful to display the icon next to the name.

To give you this control but still make the completion text bold for easy scanning, we provide a component that handles the bolding for you. It's recommended to use this component whenever displaying suggestion text.

value: [empty]

Requiring a selection from the list

When you need one of the suggestions to be chosen (e.g. to guarantee only a validated address gets submitted), it's recommended to mark the autocomplete as required, enable auto-select, and enable selection on blur. You can also take full control of validation to show an error exactly when needed.

With auto-select, the top suggestion becomes active and auto-fills into the input as you type.

With selection on blur, if you leave the input, the active suggestion is automatically accepted and becomes the value of the input field. In most cases, especially for screen readers, this can facilitate easier address selection. In the rare case where the wrong suggestion is chosen based on the text you've entered, you have a chance to review and correct the selection before submitting the form.

value: [empty]

Search and navigation

This example allows you to search for the name of a component in this design system and navigate to its documentation page. With a site search, it is recommended to use auto-select and show an overlay on focus. It's often useful to hide the label, as shown here.

With auto-select, the top suggestion becomes active and auto-fills into the input as you type.

Showing an overlay on focus dims the rest of the page when the autocomplete is open. This is most appropriate when the autocomplete is prominently placed in a sticky bar at the top of the page, and serves to minimize distractions so the user can focus on selecting the right option.

Limited width

On mobile touch devices, interacting with the autocomplete opens a fullscreen takeover to ensure the full list of suggestions can be seen and interacted with. On desktop, the autocomplete may appear with its width limited by the page layout. In this case, the suggestions pane will expand in width as necessary to show the full list of suggestions. Try searching for "solar" or "heat pump".

value: [empty]

Error state

Please enter a search term.
Please enter something other than banana.

Message

We will send you one of these every month.

value: [empty]

Disabled state

EsAutocomplete props

Required

Name
label
Type
String
Default
n/a
Description
Required. Label text for the input. Also used as the accessible title of the mobile takeover.
Name
suggestions
Type
Array
Default
n/a
Description
Required. Array of suggestion objects to display, each with id and text keys and an optional value key.

Optional

Name
auto-select
Type
Boolean
Default
false
Description
Makes the top suggestion active and auto-fills it into the input as the user types. The active suggestion is accepted as the value when they leave the field.
Name
autocomplete
Type
String
Default
off
Description
Passed directly to the autocomplete attribute on the HTML input element. The default, 'off', keeps the browser's own saved-value dropdown from competing with the suggestion list, which is appropriate in most cases, but this prop allows you to override that behavior.
Name
clear-text
Type
String
Default
Clear
Description
Accessible label for the X button that clears the input. The button appears whenever the input has text.
Name
close-text
Type
String
Default
Close
Description
Text for the button that closes the full-screen takeover on mobile, keeping whatever is in the input.
Name
delay
Type
Number
Default
300
Description
Milliseconds to debounce typing before the 'complete' event is emitted.
Name
disabled
Type
Boolean
Default
false
Description
When disabled, the input has a gray background and cannot be interacted with.
Name
help-text
Type
String
Default
Type your search and select from dropdown suggestions.
Description
Hint read out by screen readers when the input takes focus.
Name
label-sr-only
Type
Boolean
Default
false
Description
Visually hides the label so the autocomplete can stand on its own, described only by its placeholder. The label is still announced to screen readers.
Name
min-chars
Type
Number
Default
1
Description
Minimum number of characters (after trimming) before the 'complete' event is emitted and suggestions are shown.
Name
no-results-text
Type
String
Default
No results found
Description
Message shown inside the suggestions panel once a search has come back with no suggestions. Never shown while a search is still in flight (prompt-text shows instead).
Name
placeholder
Type
String
Default
n/a
Description
Text to display inside the input when it is empty.
Name
prompt-text
Type
String
Default
Type for suggestions
Description
Message shown inside the suggestions panel when there is nothing else to show: before typing begins, below min-chars, or while the first search is in flight.
Name
required
Type
Boolean
Default
false
Description
When true, a red asterisk is displayed next to the label and a default error message is available.
Name
select-on-blur
Type
Boolean
Default
false
Description
Takes the active suggestion as the value when the user leaves the field. Only applies with auto-select enabled. Suited to a field that holds a value, such as an address; leave it off where choosing a suggestion does something instead, since a search that navigates would then navigate on being left.
Name
show-overlay-on-focus
Type
Boolean
Default
false
Description
On desktop, dims the rest of the page with an overlay while the input has focus. Suits a standalone primary search (e.g. site search in a sticky bar). Leave this as false for an autocomplete within a larger form, where the overlay would obscure sibling fields.
Name
state
Type
Boolean | null
Default
null
Description
Specifies the validity of the input. Can be true (success), false (error), or null (default).
Name
suggestion-count-text
Type
Function
Default
(count) => '{count} suggestions available'
Description
Builds the screen-reader announcement made when suggestions arrive, given the number of suggestions displayed (after the display cap and the row trim). An empty result announces no-results-text instead.
Name
trigger-help-text
Type
String
Default
Opens a search with suggestions as you type.
Description
Hint read out by screen readers for the trigger field on mobile (which opens the full screen takeover on tap rather than taking text directly).

EsAutocomplete events

Name
blur
Payload
—
Description
Emitted when the user leaves the field (e.g. focus moving to another control, a click outside) so form validation can occur.
Name
complete
Payload
query: string
Description
Emitted (debounced) when the user has typed at least min-chars characters. Fetch or filter your suggestions in response and update the 'suggestions' prop. If fetching, ignore a response that arrives after a newer query has been emitted: each update is taken as the answer to the latest query, and with auto-select its top suggestion becomes what Enter or leaving the field chooses.
Name
select
Payload
suggestion
Description
Emitted when a suggestion is chosen, by click/tap or by pressing Enter on a highlighted suggestion. The full suggestion object is passed, including its 'value' payload if provided.

EsAutocomplete slots

Name
item
Slot props
suggestion, query
Description
Custom renderer for each suggestion. When not provided, the suggestion text is rendered with the predictive portion bolded. Use the EsAutocompleteSuggestionText component to apply the same predictive bolding to your own text (see the custom item rendering example).
Name
errorMessage
Slot props
n/a
Description
Error message shown below the input when 'state' is false.
Name
message
Slot props
n/a
Description
Muted helper message shown below the input when there is no error.