currentRefinements
currentRefinements({ container: string|HTMLElement, // Optional parameters includedAttributes: string[], excludedAttributes: string[], cssClasses: object, transformItems: function, });
1
import { currentRefinements } from 'instantsearch.js/es/widgets';
About this widget
The currentRefinements
widget displays a list of refinements applied to the search.
Examples
1
2
3
currentRefinements({
container: '#current-refinements',
});
Options
container
|
type: string|HTMLElement
Required
The CSS Selector or |
||
Copy
|
|||
includedAttributes
|
type: string[]
default: []
Optional
The attributes to include in the widget (all by default). Cannot be used with |
||
Copy
|
|||
excludedAttributes
|
type: string[]
default: ["query"]
Optional
The attributes to exclude from the widget. Cannot be used with |
||
Copy
|
|||
cssClasses
|
type: object
default: {}
Optional
The CSS classes to override.
|
||
Copy
|
|||
transformItems
|
type: function
default: items => items
Optional
Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items. In addition, the full |
||
Copy
|
HTML output
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
<div class="ais-CurrentRefinements">
<ul class="ais-CurrentRefinements-list">
<li class="ais-CurrentRefinements-item">
<span class="ais-CurrentRefinements-label">
Category:
</span>
<span class="ais-CurrentRefinements-category">
<span class="ais-CurrentRefinements-categoryLabel">
Movies & TV Shows
</span>
<button class="ais-CurrentRefinements-delete">✕</button>
</span>
<span class="ais-CurrentRefinements-category">
<span class="ais-CurrentRefinements-categoryLabel">
Others
</span>
<button class="ais-CurrentRefinements-delete">✕</button>
</span>
</li>
<li class="ais-CurrentRefinements-item">
<span class="ais-CurrentRefinements-label">
Brand:
</span>
<span class="ais-CurrentRefinements-category">
<span class="ais-CurrentRefinements-categoryLabel">
Algolia
</span>
<button class="ais-CurrentRefinements-delete">✕</button>
</span>
</li>
</ul>
</div>
Customize the UI with connectCurrentRefinements
If you want to create your own UI of the currentRefinements
widget, you can use connectors.
To use connectCurrentRefinements
, you can import it with the declaration relevant to how you installed InstantSearch.js.
1
import { connectCurrentRefinements } from 'instantsearch.js/es/connectors';
Then it’s a 3-step process:
// 1. Create a render function
const renderCurrentRefinements = (renderOptions, isFirstRender) => {
// Rendering logic
};
// 2. Create the custom widget
const customCurrentRefinements = connectCurrentRefinements(
renderCurrentRefinements
);
// 3. Instantiate
search.addWidgets([
customCurrentRefinements({
// 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 renderCurrentRefinements = (renderOptions, isFirstRender) => {
const {
object[] items,
boolean canRefine,
function refine,
function createURL,
object widgetParams,
} = renderOptions;
if (isFirstRender) {
// Do some initial rendering and bind events
}
// Render the widget
}
Rendering options
items
|
type: object[]
All the currently refined items, grouped by attribute. With each item:
With each refinement:
|
||
Copy
|
|||
canRefine
|
type: boolean
Required
Indicates if search state can be refined. |
||
Copy
|
|||
refine
|
type: function
Clears a single refinement and triggers a new search. |
||
Copy
|
|||
createURL
|
type: function
Generates a URL for the next state. |
||
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 customCurrentRefinements = connectCurrentRefinements(
renderCurrentRefinements
);
search.addWidgets([
customCurrentRefinements({
// Optional parameters
includedAttributes: string[],
excludedAttributes: string[],
transformItems: function,
})
]);
Instance options
includedAttributes
|
type: string[]
default: []
Optional
The attributes to include in the widget (all by default). Cannot be used with |
||
Copy
|
|||
excludedAttributes
|
type: string[]
default: ["query"]
Optional
The attributes to exclude from the widget. Cannot be used with |
||
Copy
|
|||
transformItems
|
type: function
default: items => items
Optional
Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items. In addition, the full |
||
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
53
54
55
56
57
58
// Create the render function
const createDataAttribtues = refinement =>
Object.keys(refinement)
.map(key => `data-${key}="${refinement[key]}"`)
.join(' ');
const renderListItem = item => `
<li>
${item.label}
<ul>
${item.refinements
.map(
refinement =>
`<li>
${refinement.label} (${refinement.count})
<button ${createDataAttribtues(refinement)}>X</button>
</li>`
)
.join('')}
</ul>
</li>
`;
const renderCurrentRefinements = (renderOptions, isFirstRender) => {
const { items, refine, widgetParams } = renderOptions;
widgetParams.container.innerHTML = `
<ul>
${items.map(renderListItem).join('')}
</ul>
`;
[...widgetParams.container.querySelectorAll('button')].forEach(element => {
element.addEventListener('click', event => {
const item = Object.keys(event.currentTarget.dataset).reduce(
(acc, key) => ({
...acc,
[key]: event.currentTarget.dataset[key],
}),
{}
);
refine(item);
});
});
};
// Create the custom widget
const customCurrentRefinements = connectCurrentRefinements(
renderCurrentRefinements
);
// Instantiate the custom widget
search.addWidgets([
customCurrentRefinements({
container: document.querySelector('#current-refinements'),
})
]);