You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Everything you need to create fantastic autocomplete experiences.
7
+
8
+
We've built Autocomplete to give you unlimited flexibility while freeing you from having to think about things like keyboard navigation, accessibility, or UI state. **The library offers a wide variety of APIs to let you fully customize the behavior and rendering of your autocomplete.**
9
+
10
+
Yet, only two parameters are required to create an autocomplete:
11
+
- The **container** you want your autocomplete to go in.
12
+
- The **sources** from which to get the items to display (see more in [**Sources**](sources)).
The `container` options refers to where to inject the autocomplete in your HTML. It can be a [CSS selector](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) or an [Element](https://developer.mozilla.org/docs/Web/API/HTMLElement). Make sure to provide a container (e.g., a `div`), not an `input`. Autocomplete generates a fully accessible search box for you.
45
+
46
+
```html title="HTML"
47
+
<divid="autocomplete"></div>
48
+
```
49
+
50
+
This is all you need to build a [fully functional, accessible, keyboard-navigable autocomplete](https://codesandbox.io/s/vigilant-dew-g2ezl).
51
+
52
+
Now, this is a great start, but **you can go much further**. Here are some of the options you'll probably want to use next:
53
+
- Use [`placeholder`](autocomplete-js#placeholder) to define the text that appears in the input before the user types anything.
54
+
- Use [`autoFocus`](autocomplete-js#autofocus) to focus on the search box on page load, and [`openOnFocus`](autocomplete-js#openonfocus) to display items as soon as a user selects the autocomplete, even without typing.
55
+
- Use the [`onStateChange`](autocomplete-js#onstatechange) lifecycle hook to execute code whenever the state changes.
56
+
- Use [`debug: true`](autocomplete-js#debug) to keep the autocomplete panel open even when the blur event occurs (see [**Debugging**](debugging)).
57
+
58
+
For a full list of all available parameters, check out the [API reference](autocomplete-js).
The autocomplete context allows to store data in the state to use in different lifecycle hooks.
6
+
The Context lets you store data and access it in different lifecycle hooks.
7
7
8
-
You can use this API to access data in the templates that you would otherwise have access only in `getSources` for instance. The `setContext` setters expects an object that will be merged with the previous context. You can then read the context in `state.context`.
8
+
Sometimes you need to store arbitrary data so you can access it later in your autocomplete. For example, when retrieving hits from Algolia, you may want to reuse the total number of retrieved hits in a template.
9
9
10
-
The following example stores the number of hits from an Algolia response to display it in the templates.
10
+
Autocomplete lets you store data using its Context API and access it anywhere from the [state](/docs/state).
11
+
12
+
## Usage
13
+
14
+
Context exposes a `setContext` function, which takes an object and merges it with the existing context. You can then access the context in `state.context`.
15
+
16
+
The following example stores the number of hits from an Algolia response, making it accessible everywhere in your autocomplete.
Context can be handy when developing [Autocomplete plugins](/docs/plugins). It avoids polluting the global namespace while still being able to pass data around across different lifecycle hooks.
47
+
48
+
```js
49
+
functioncreateAutocompletePlugin() {
50
+
return {
51
+
// ...
52
+
subscribe({ setContext }) {
53
+
setContext({
54
+
autocompletePlugin: {
55
+
// ...
56
+
},
57
+
});
58
+
},
59
+
};
60
+
}
61
+
```
62
+
63
+
## Reference
64
+
65
+
The `setContext` function is accessible on your `autocomplete` instance.
66
+
67
+
It's also provided in:
68
+
-[`getSources`](createAutocomplete#getsources)
69
+
-[`onInput`](createAutocomplete#oninput)
70
+
-[`onSubmit`](createAutocomplete#onsubmit)
71
+
-[`onReset`](createAutocomplete#onreset)
72
+
-[`source.onActive`](sources#onactive)
73
+
-[`source.onSelect`](sources#onselect)
74
+
-[`source.getItems`](sources#getitems)
75
+
-`plugin.subscribe`
76
+
77
+
The `context` object is available on the [`state`](/docs/state) object.
78
+
79
+
### `setContext`
80
+
81
+
> `(value: Record<string, unknown>) => void`
82
+
83
+
The function to pass data to to store it in the context.
0 commit comments