createTagsPlugin
The Autocomplete Tags plugin lets you manage and display tags in the autocomplete.
You can use tags for a wide variety of use cases: filtering, displaying refinements, representing navigation steps, and more.
Installation
First, you need to install the plugin.
1
2
3
yarn add @algolia/autocomplete-plugin-tags
# or
npm install @algolia/autocomplete-plugin-tags
Then import it in your project:
1
import { createTagsPlugin } from '@algolia/autocomplete-plugin-tags';
The plugin provides a theme compatible with the autocomplete-theme-classic
package.
1
import '@algolia/autocomplete-plugin-tags/dist/theme.min.css';
If you don’t use a package manager, you can use the HTML script
element:
1
2
3
4
5
6
7
8
9
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@algolia/autocomplete-plugin-tags/dist/theme.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/@algolia/autocomplete-plugin-tags"></script>
<script>
const { createTagsPlugin } = window['@algolia/autocomplete-plugin-tags'];
</script>
Examples
1
2
3
4
5
6
7
8
9
10
import { autocomplete } from '@algolia/autocomplete-js';
import { createTagsPlugin } from '@algolia/autocomplete-plugin-tags';
const tagsPlugin = createTagsPlugin();
autocomplete({
// ...
container: '#autocomplete',
plugins: [tagsPlugin],
});
The plugin lets you pass initialTags
to start Autocomplete with initial tags. You can use this when you’re deriving tags from existing state, such as the route, or existing refinements.
For example, if the URL reflects the current refinements, you can initialize the plugin with them when the page loads, before the Autocomplete instance starts.
1
2
3
4
5
6
// Current URL: https://example.org/?category=Phones&brand=Apple
const parameters = Array.from(new URLSearchParams(location.search));
const initialTags = parameters.map(([facet, label]) => ({ label, facet })); // [{ label: 'Phones', facet: 'category' }, { label: 'Apple', facet: 'brand' }]
const tagsPlugin = createTagsPlugin({ initialTags });
By default, the plugin populates a source with the current tags. You can navigate through them like with any other source, and delete them on click or on select. You can customize the source using transformSource
.
If you prefer displaying your tags by hand, for example in the render
function of Autocomplete or in the search box, near the query, you can prevent the plugin from returning a source and display tags yourself. The plugin exposes an API on the Context to let you access tags.
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
// ...
const tagsPlugin = createTagsPlugin({
// ...
transformSource({ source }) {
return undefined;
},
});
autocomplete({
// ...
render({ sections, html, render }, root) {
render(
html`<div class="aa-PanelLayout aa-Panel--scrollable">
<ul>
${state.context.tagsPlugin.tags.map(
(tag) =>
html`<li key="${tag.label}" onClick="${() => tag.remove()}">
${tag.label}
</li>`
)}
</ul>
<div>${sections}</div>
</div>`,
root
);
},
});
Returned tags expose a remove
function to conveniently remove individual tags.
The plugin also exposes an addTags
and a setTags
function on the Context to update the list of tags.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// ...
autocomplete({
// ...
render({ sections, state, html, render }, root) {
render(
html`<div class="aa-PanelLayout aa-Panel--scrollable">
<button onClick="${() => state.context.tagsPlugin.setTags([])}">
Clear all tags
</button>
/* ... */
</div>`,
root
);
},
});
You can access the same API to retrieve and modify tags directly on the plugin. This is helpful to interact with tags outside of the Autocomplete instance.
1
2
3
document.getElementById('#clear-speakers').addEventListener('click', () => {
tagsPlugin.data.setTags([]);
});
Parameters
initialTags
|
type: BaseTag<TTag>[]
A set of initial tags to pass to the plugin. You can use this to pass initial refinements (for example, from local state) without triggering the Autocomplete lifecycle. |
||||
Copy
|
|||||
getTagsSubscribers
|
type: (): Array<{ sourceId: string, getTag(params: { item: TItem }): BaseTag<TTag> }>
A function to specify what sources the plugin should subscribe to. The plugin adds a tag when selecting an item from these sources.
|
||||
Copy
|
|||||
transformSource
|
type: (params: { source: AutocompleteSource<Tag<TTag>>, state: AutocompleteState<Tag<TTag>> }): AutocompleteSource<Tag<TTag>> | undefined
A function to transform the returned tags source. |
||||
Copy
To avoid rendering tags as a source, you can return
Copy
|
|||||
onChange
|
type: (params: OnChangeParams<TTag>): void
The function called when the list of tags changes. This is useful to customize the behavior of Autocomplete when such an event occurs, or integrate with third-party code. |
||||
Copy
|
Returns
data
tags
|
type: Tag<TTag>[]
Returns the current list of tags. If you’re not using the default tags source returned by the plugin, you can use this to display tags anywhere in your autocomplete. |
||||
Copy
|
|||||
addTags
|
type: (tags: BaseTag<TTag>[]): void
Adds tags to the list. The only required property is a |
||||
Copy
|
|||||
setTags
|
type: (tags: BaseTag<TTag>[]): void
Sets the list of tags. This is helpful to replace the current list. |
||||
Copy
To remove some of the tags, you can retrieve the current tags list using
Copy
|