InstantSearch / InstantSearch.js / V3 / API reference

Autocomplete | InstantSearch.js V3 (Deprecated)

This version of InstantSearch.js has been deprecated in favor of the latest version of InstantSearch.js.

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.addWidgets([
  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 highlight function, directly from the connector’s render function. Each index is provided with:

  • index: string: the name of the index.
  • label: string: the label of the index (for display purpose).
  • hits: object[]: the resolved hits from the index matching the query.
  • results: object: the full results object from the Algolia API.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const renderIndexListItem = ({ label, hits }) => `
  <li>
    Index: ${label}
    <ol>
      ${hits
        .map(
          hit =>
            `<li>${instantsearch.highlight({ attribute: 'name', hit })}</li>`
        )
        .join('')}
    </ol>
  </li>
`;

const renderAutocomplete = (renderOptions, isFirstRender) => {
  const { indices } = renderOptions;

  document.querySelector('#autocomplete').innerHTML = `
    <ul>
      ${indices.map(renderIndexListItem).join('')}
    </ul>
  `;
};
currentRefinement
type: string

The current value of the query.

1
2
3
4
5
6
7
const renderAutocomplete = (renderOptions, isFirstRender) => {
  const { currentRefinement } = renderOptions;

  document.querySelector('#autocomplete').innerHTML = `
    <input type="search" value="${currentRefinement}" />
  `;
};
refine
type: function

Searches into the indices with the provided query.

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
const renderIndexListItem = ({ label, hits }) => `
  <li>
    Index: ${label}
    <ol>
      ${hits
        .map(
          hit =>
            `<li>${instantsearch.highlight({ attribute: 'name', hit })}</li>`
        )
        .join('')}
    </ol>
  </li>
`;

const renderAutocomplete = (renderOptions, isFirstRender) => {
  const { indices, currentRefinement, refine } = renderOptions;
  const container = document.querySelector('#autocomplete');

  if (isFirstRender) {
    const input = document.createElement('input');
    const ul = document.createElement('ul');

    input.addEventListener('input', event => {
      refine(event.currentTarget.value);
    });

    container.appendChild(input);
    container.appendChild(ul);
  }

  container.querySelector('input').value = currentRefinement;
  container.querySelector('ul').innerHTML = indices
    .map(renderIndexListItem)
    .join('');
};
widgetParams
type: object

All original widget options forwarded to the render function.

1
2
3
4
5
6
7
8
9
10
11
12
13
const renderAutocomplete = (renderOptions, isFirstRender) => {
  const { widgetParams } = renderOptions;

  widgetParams.container.innerHTML = '...';
};

// ...

search.addWidgets([
  customAutocomplete({
    container: document.querySelector('#autocomplete'),
  })
]);

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.addWidgets([
  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:

  • label: string: an identifier for the index.
  • value: string: the name of the index to search into.
1
2
3
4
5
6
customAutocomplete({
  indices: [
    { label: 'Price (asc)', value: 'instant_search_price_asc' },
    { label: 'Price (desc)', value: 'instant_search_price_desc' },
  ],
});
escapeHTML
type: boolean
default: true
Optional

Escapes HTML entities from hits string values.

1
2
3
customAutocomplete({
  escapeHTML: false,
});

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.addWidgets([
  customAutocomplete({
    container: document.querySelector('#autocomplete'),
    indices: [
      { label: 'Price (asc)', value: 'instant_search_price_asc' },
      { label: 'Price (desc)', value: 'instant_search_price_desc' },
    ],
  })
]);
Did you find this page helpful?