ais-current-refinements
<ais-current-refinements // Optional parameters [includedAttributes]="string[]" [excludedAttributes]="string[]" [transformItems]="function" ></ais-current-refinements>
About this widget
The ais-current-refinements
widget displays a list of refinementss applied to the search.
Examples
1
<ais-current-refinements></ais-current-refinements>
Props
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
|
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-itemt">
<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 ais-current-refinements
widget, you can combine the connectCurrentRefinements
connector with the TypedBaseWidget
class.
1. Extend the TypedBaseWidget
class
First of all, you will need to write some boilerplate code to initialize correctly the TypedBaseWidget
class. This happens in the constructor()
of your class extending the TypedBaseWidget
class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { Component, Inject, forwardRef, Optional } from '@angular/core';
import { TypedBaseWidget, NgAisInstantSearch, NgAisIndex } from 'angular-instantsearch';
@Component({
selector: 'app-current-refinements',
template: '<p>It works!</p>'
})
export class CurrentRefinements extends TypedBaseWidget {
constructor(
@Inject(forwardRef(() => NgAisIndex))
@Optional()
public parentIndex: NgAisIndex,
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchInstance: NgAisInstantSearch
) {
super('CurrentRefinements');
}
}
There are a couple of things happening in this boilerplate:
- create a
CurrentRefinements
class extendingTypedBaseWidget
- reference the
<ais-instantsearch>
parent component instance on theCurrentRefinements
widget class - set
app-current-refinements
as a selector, so we can use our component as<app-current-refinements></app-current-refinements>
2. Connect your custom widget
The TypedBaseWidget
class has a method called createWidget()
which takes two arguments: the connector to use and an object of options
(instance options)
for this connector. We call this method at ngOnInit
. This component now implements OnInit
.
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
import { Component, Inject, forwardRef, Optional } from '@angular/core';
import { TypedBaseWidget, NgAisInstantSearch, NgAisIndex } from 'angular-instantsearch';
import connectCurrentRefinements, {
CurrentRefinementsWidgetDescription,
CurrentRefinementsConnectorParams
} from 'instantsearch.js/es/connectors/current-refinements/connectCurrentRefinements';
@Component({
selector: 'app-current-refinements',
template: '<p>It works!</p>'
})
export class CurrentRefinements extends TypedBaseWidget<CurrentRefinementsWidgetDescription, CurrentRefinementsConnectorParams> {
public state: CurrentRefinementsWidgetDescription['renderState']; // Rendering options
constructor(
@Inject(forwardRef(() => NgAisIndex))
@Optional()
public parentIndex: NgAisIndex,
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchInstance: NgAisInstantSearch
) {
super('CurrentRefinements');
}
ngOnInit() {
this.createWidget(connectCurrentRefinements, {
// instance options
});
super.ngOnInit();
}
}
3. Render from the state
Your component instance has access to a this.state
property which holds the rendering options of the widget.
public state: CurrentRefinementsWidgetDescription['renderState'];
// {
// items: object[];
// refine: Function;
// createURL: Function;
// widgetParams: object;
// }
1
2
3
4
5
6
7
8
<ul>
<li *ngFor="let item of state.items">
{{ item.attribute }}:
<span *ngFor="let refinement of item.refinements">
{{ refinement.label }}
</span>
</li>
</ul>
Rendering options
items
|
type: object[]
All the currently refined items, grouped by attribute. With each item:
With each refinement:
|
refine
|
type: function
Clears a single refinement and triggers a new search. |
createURL
|
type: function
Generates a URL for the next state. |
widgetParams
|
type: object
All original widget options forwarded to the render function. |
Instance options
includedAttributes
|
type: string[]
default: []
Optional
The attributes to include in the widget (all by default). Cannot be used with |
excludedAttributes
|
type: string[]
default: ["query"]
Optional
The attributes to exclude from the widget. Cannot be used with |
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 |
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
import { Component, Inject, forwardRef, Optional } from '@angular/core';
import { TypedBaseWidget, NgAisInstantSearch, NgAisIndex } from 'angular-instantsearch';
import connectCurrentRefinements, {
CurrentRefinementsWidgetDescription,
CurrentRefinementsConnectorParams
} from 'instantsearch.js/es/connectors/current-refinements/connectCurrentRefinements';
@Component({
selector: 'app-current-refinements',
template: `
<ul>
<li *ngFor="let item of state.items">
{{ item.attribute }}:
<span *ngFor="let refinement of item.refinements">
{{ refinement.label }}
</span>
</li>
</ul>
`
})
export class CurrentRefinements extends TypedBaseWidget<CurrentRefinementsWidgetDescription, CurrentRefinementsConnectorParams> {
public state: CurrentRefinementsWidgetDescription['renderState']; // Rendering options
constructor(
@Inject(forwardRef(() => NgAisIndex))
@Optional()
public parentIndex: NgAisIndex,
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchInstance: NgAisInstantSearch
) {
super('CurrentRefinements');
}
ngOnInit() {
this.createWidget(connectCurrentRefinements, {
// instance options
});
super.ngOnInit();
}
}