Static sources
The most straightforward way to provide items is to return static sources. Each source returns a collection of items.JavaScript
Whatever the autocomplete state, it always returns these two items.
Search in static sources
Access the autocomplete state in your sources to search within static sources and update them as users type.JavaScript
getSources function returns an array of sources.
Each source implements a getItems function to return the items to display.
These items can be a simple static array, but you can also use a function to refine items based on the query.
The getItems function is called whenever the input changes.
By default, autocomplete items are meant to be hyperlinks.
To decide which page to visit, create a getItemURL function.
It enables the keyboard accessibility feature,
allowing users to open items in the current tab, a new tab, or a new window from their keyboard.
Customize items with templates
In addition to defining data sources for items, a source lets you customize how to display items withtemplates.
JavaScript
JavaScript
Dynamic sources
Static sources can be useful, especially when users haven’t typed anything. However, you might want more robust search capabilities beyond exact matches in strings. In this case, you could search into one or more Algolia indices using the built-ingetAlgoliaResults function.
JavaScript
Asynchronous sources
ThegetSources function supports promises to fetch sources from any asynchronous API, such as Algolia or any third-party API you can query with an HTTP request.
For example, you could use Google Places Query Autocomplete to search for locations and retrieve popular queries that match points of interest.
JavaScript
getItemInputValue function returns the value of the item.
It lets you fill the search box with a new value whenever users select an item, allowing them to refine their query and retrieve more relevant results.
Multiple sources
You don’t have to return just one set of results. Autocomplete lets you fetch data from several sources and show different kinds of results that are used for different things. For example, you may want to display Algolia search results and Query Suggestions.JavaScript
Use the
autocomplete-plugin-query-suggestions plugin to retrieve Query Suggestions.Reference
function
Type definition
AutocompleteSource
string
Unique identifier for the source.
It’s used as value for the
data-autocomplete-source-id attribute of the source section container.function
required
Type definition
Example
function
default: ({ state }) => state.query
Type definition
Example
function
Type definition
Example
function
default: ({ setIsOpen }) => setIsOpen(false)
Type definition
Example
function
Type definition
event parameter.AutocompleteTemplates
A set of templates to customize how sections and their items are displayed.For more information, see Displaying items with Templates.