autocomplete
About this widget
The connectAutoComplete
connector provides the logic to create a connected component that renders results from Algolia.
To configure the number of hits to show, use the hitsPerPage
widget or the configure
widget.
Alternatively, we provide a library called autocomplete.js
that gives you an out-of-the-box autocomplete component ready to use.
Customize the UI - connectAutocomplete
If you want to create your own UI of the autocomplete
widget, you can use connectors.
It’s a 3-step process:
// 1. Create a render function
const renderAutocomplete = (renderOptions, isFirstRender) => {
// Rendering logic
};
// 2. Create the custom widget
const customAutocomplete = instantsearch.connectors.connectAutocomplete(
renderAutocomplete
);
// 3. Instantiate
search.addWidget(
customAutocomplete({
// instance params
});
);
Create a render function
This rendering function is called before the first search (init
lifecycle step)
and each time results come back from Algolia (render
lifecycle step).
const renderAutocomplete = (renderOptions, isFirstRender) => {
const {
object[] indices,
string currentRefinement,
function refine,
object widgetParams,
} = renderOptions;
if (isFirstRender) {
// Do some initial rendering and bind events
}
// Render the widget
}
Rendering options
indices
|
type: object[]
The indices you provided with their hits and results, and the main index in first position. You can leverage the highlighting feature of Algolia through the
|
||
Copy
|
|||
currentRefinement
|
type: string
The current value of the query. |
||
Copy
|
|||
refine
|
type: function
Searches into the indices with the provided query. |
||
Copy
|
|||
widgetParams
|
type: object
All original widget options forwarded to the render function. |
||
Copy
|
Create and instantiate the custom widget
We first create custom widgets from our rendering function, then we instantiate them. When doing that, there are two types of parameters you can give:
- Instance parameters: they are predefined parameters that you can use to configure the behavior of Algolia.
- Your own parameters: to make the custom widget generic.
Both instance and custom parameters are available in connector.widgetParams
, inside the renderFunction
.
const customAutocomplete = instantsearch.connectors.connectAutocomplete(
renderAutocomplete
);
search.addWidget(
customAutocomplete({
// Optional parameters
indices: object[],
escapeHTML: boolean,
})
);
Instance options
indices
|
type: object[]
default: []
Optional
A list of objects that describe other indices to search into. With each index:
|
||
Copy
|
|||
escapeHTML
|
type: boolean
default: true
Optional
Escapes HTML entities from hits string values. |
||
Copy
|
Full example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Helper for the render function
const renderIndexListItem = ({ label, hits }) => `
<li>
Index: ${label}
<ol>
${hits
.map(
hit =>
`<li>${instantsearch.highlight({ attribute: 'name', hit })}</li>`
)
.join('')}
</ol>
</li>
`;
// Create the render function
const renderAutocomplete = (renderOptions, isFirstRender) => {
const { indices, currentRefinement, refine, widgetParams } = renderOptions;
if (isFirstRender) {
const input = document.createElement('input');
const ul = document.createElement('ul');
input.addEventListener('input', event => {
refine(event.currentTarget.value);
});
widgetParams.container.appendChild(input);
widgetParams.container.appendChild(ul);
}
widgetParams.container.querySelector('input').value = currentRefinement;
widgetParams.container.querySelector('ul').innerHTML = indices
.map(renderIndexListItem)
.join('');
};
// Create the custom widget
const customAutocomplete = instantsearch.connectors.connectAutocomplete(
renderAutocomplete
);
// Instantiate the custom widget
search.addWidget(
customAutocomplete({
container: document.querySelector('#autocomplete'),
indices: [
{ label: 'Price (asc)', value: 'instant_search_price_asc' },
{ label: 'Price (desc)', value: 'instant_search_price_desc' },
],
})
);