Sources define where to retrieve items from and their behavior.
Autocomplete is also available as an experimental widget in InstantSearch,
making it easier to integrate into your search experience.
For more information,
see the API reference for InstantSearch.js or
React InstantSearch.
The most important aspect of an autocomplete experience is the items you display.
They’re usually search results for a but there are other ways to use them.
Before moving on to more complex sources, here’s a closer look at the code.The 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.
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-in getAlgoliaResults function.
The getSources 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.
The 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.
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.
The function called to get the URL of the item.
The value is used to add keyboard accessibility
features to let users open items in the current tab, a new tab, or a new window.
The function called whenever an item is active.You can trigger different behaviors if the item is active depending on the triggering event using the event parameter.