autocomplete-plugin-tags
plugin to let you manage tags in your autocomplete.
While there are many uses cases for tags, they’re particularly convenient to represent and apply refinements.
Get started
First, begin with some starter code. Create a file calledindex.js
in your src
directory, and add the code below:
JavaScript
autocomplete
as an id
. You should change the container
to match your markup.
Setting openOnFocus
to true
ensures that the drop-down menu appears as soon as a user focuses the input.
For now, plugins
is an empty array, but you’ll learn how to add the tags plugin next.
Set up filters sources
Theautocomplete-plugin-tags
package provides the createTagsPlugin
function to create a tags plugin out-of-the-box.
You can first create a source in your autocomplete to display available filters.
To do so, use Algolia’s faceting feature and use the getAlgoliaFacets
function to retrieve available facet values for a given attribute.
brand
attribute.
By using getAlgoliaFacets
,
you’re also letting users search within these facet values to find meaningful filters.
For now, nothing happens when selecting an item for this source.
In the next step, you’ll bind it to the tags plugin to apply tags on select.
Add tags
When selecting a filter from a source, you want to display them so that users knows what’s affecting their search. The tags plugin renders applied tags as a source, letting users navigate through them with the keyboard and remove them on select. To automatically add a tag when selecting an item from your filters source, you can bind it to the plugin bysourceId
.
JavaScript
sourceId
,
the plugin adds it as a tag and displays it as a source.
You can customize the default rendering for the tags source, or not use a source and render tags where and how you want.
For more information, see the API reference.
Apply filters from tags
Once you’ve set tags in your autocomplete, you can use them to filter results. For example, imagine you’ve set the following tags.JavaScript
getAlgoliaResults
.
JavaScript
AND
) of disjunctions (OR
),
and passed on to the filters
search parameter.
The tags would result in the following filter:
Exclude already applied tags from sources
If you’re displaying your filters as Autocomplete sources usinggetAlgoliaFacets
, you might want to exclude already applied tags from the list. This way, users can discover more possible filters.
To do so, you can derive negative filters from your tags.
JavaScript
The
filters
search parameter works for most cases,
but you can adjust the logic to generate facetFilters
,
numericFilters
,
tagFilters
,
or optionalFilters
.Remove applied tag from the query
UsinggetAlgoliaFacets
to populate your filters list lets you search within filters as well as products.
For example, if you’re looking for a specific brand,
you can start typing it out,
then select it from the refined filters.
When you apply a tag you’ve found after typing,
you might want to delete it from the query.
For example, if a user types “app” then selects “Apple”,
you likely want to remove “app” from the query.
For users, it feels like the autocomplete understands their intent,
and it lets them type the rest of the query without having to clear it first.
To do so, you can use onSelect
to clear the query after applying a tag.
JavaScript
Filter from external refinements
A great way to help users make more meaningful searches is to contextualize the autocomplete behavior based on intent.- In an ecommerce site, a user searching from the “Video games” section might expect different results than if they were on the home page.
- In a dashboard app, you might want to display different results on an empty query for users belonging to the “Billing” group than those in the “Technical” one.
Pass initial tags
When starting your Autocomplete instance with the tags plugin, you can pass initial filters based on external state. For example, you could parse the current URL to retrieve the active category and turn it into a tag.JavaScript
Update tags manually
If you’re using a single-page app with client-side routing, or you’re deriving tags from a local, dynamic state, you might need to update tags manually after the instance has started. A typical use case is when using Autocomplete along with InstantSearch. When applying new refinements with arefinementList
,
you might want to update the list of tags in your autocomplete to reflect the current refinements.
You can imperatively update the list of tags outside the Autocomplete instance using the exposed API on the plugin.
JavaScript
Display tags in the search box
By default, the tags plugin displays tags as a source. This makes it straightforward to navigate through applied filters with the keyboard, as you would with any source. Yet, a popular pattern is to display tags in the search box, near the input. It feels natural to users, especially when used in conjunction to a filters source usinggetAlgoliaFacets
.
As they type, the filters source suggests facets that can apply without leaving the keyboard.
Applied tags display near the search input,
making them more convenient to remove using the backspace key.
This creates a more seamless experience where tags feel like they’re part of the query.
First, you need to keep the plugin from rendering tags as a source.
You can do so using the transformSource
option.
JavaScript
autocomplete-js
,
the search box exposes a .aa-InputWrapperPrefix
element before the search input where you can inject tags.
The plugin lets you perform custom logic with the onChange
option,
which you can use to update the rendered tags.
This example uses Preact’s
render
function with JSX to simplify injecting HTML and attaching event listeners. You can use HTML template strings along with Element.innerHTML
if you don’t use JSX.JavaScript
This solution doesn’t work in Detached mode. You can turn it off manually.