createAutocomplete
function returns methods to help you create an autocomplete experience from scratch.
This is fully headless: you’re in charge of creating your own autocomplete renderer.
Building a custom renderer is an advanced pattern.
You don’t need it unless you’ve reached limitations with
autocomplete-js
and its templating capabilities.Installation
First, you need to install the package.JavaScript
script
element:
HTML
Example
This example uses the package along with thealgoliasearch
API client and getAlgoliaResults
function from the Autocomplete Algolia preset.
It returns a set of functions to build an autocomplete experience.
JavaScript
Parameters
The function called to reshape the sources after they’re resolved.This is useful to transform sources before rendering them.
You can group sources by attribute, remove duplicates, create shared limits between sources, etc.See Reshaping sources for more information.
TypeScript
Whether to enable the Algolia Insights plugin.This option accepts an object to configure the plugin.
You can see the available options in the plugin’s documentation.If you don’t pass an
insightsClient
,
it will be automatically detected from the window
object,
or downloaded from the jsDelivr CDN.If you manually enable the Insights plugin, this option won’t have any effect.An ID for the autocomplete to create accessible attributes.
The function called when the internal state changes.
The action label or icon to present for the enter key on virtual keyboards.
Whether to update the search input value in the middle of a composition session.
This is useful when users need to search using non-latin characters.
The placeholder text to show in the search input when there’s no query.
Whether to focus the search input or not when the page is loaded.
The default item index to pre-select.You should use
0
when the autocomplete is used to open links,
instead of triggering a search in an application.Whether to open the panel on focus when there’s no query.
How many milliseconds must elapse before considering the autocomplete experience stalled.
The initial state to apply when autocomplete is created.
The environment in which your application is running.This is useful if you’re using autocomplete in a different context than
window
.An implementation of Autocomplete’s Navigator API to redirect users when opening a link.Learn more on the Navigator API documentation.
The function called to determine whether the panel should open or not.By default, the panel opens when there are items in the state.
The function called when submitting the Autocomplete form.
The function called when resetting the Autocomplete form.
A flag to activate the debug mode.This is useful while developing because it keeps the panel open even when the blur event occurs.
Make sure to turn it off in production.See Debugging for more information.
plugins
The plugins that encapsulate and distribute custom Autocomplete behaviors.See Plugins for more information.
Returns
ThecreateAutocomplete
function returns prop getters,
state setters,
and a refresh
method that updates the UI state with fresh sources.
JavaScript
createAutocomplete
function returns state setters and additional helpers:
Updates the UI state with fresh sources.
You must call this function whenever you mutate the state with setters and want to reflect the changes in the UI.
Updates the Autocomplete instance with new options.
Destroys the Autocomplete instance and removes it from the DOM.