Skip to content

Commit fe6d19c

Browse files
author
Roger Roelofs
committed
feat(clearAll): Add optional excludeAttributes to list protected filters
Implemented by getting refinements in the widget and removing the protected ones before passing the attribute list to clearRefinementsAndSearch
1 parent 3db8358 commit fe6d19c

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/widgets/clear-all/clear-all.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ clearAll({
4141
[ cssClasses.{root,header,body,footer,link}={} ],
4242
[ templates.{header,link,footer}={link: 'Clear all'} ],
4343
[ autoHideContainer=true ],
44-
[ collapsible=false ]
44+
[ collapsible=false ],
45+
[ excludeAttributes=[] ]
4546
})`;
4647
function clearAll({
4748
container,
4849
templates = defaultTemplates,
4950
cssClasses: userCssClasses = {},
5051
collapsible = false,
51-
autoHideContainer = true
52+
autoHideContainer = true,
53+
excludeAttributes = []
5254
} = {}) {
5355
if (!container) {
5456
throw new Error(usage);
@@ -70,17 +72,20 @@ function clearAll({
7072

7173
return {
7274
init({helper, templatesConfig}) {
73-
this._clearRefinementsAndSearch = clearRefinementsAndSearch.bind(null, helper);
75+
this.clearAll = this.clearAll.bind(this, helper);
7476
this._templateProps = prepareTemplateProps({defaultTemplates, templatesConfig, templates});
7577
},
7678

7779
render({results, state, createURL}) {
78-
let hasRefinements = getRefinements(results, state).length !== 0;
80+
this.clearAttributes = getRefinements(results, state)
81+
.map(one => one.attributeName)
82+
.filter(one => excludeAttributes.indexOf(one) === -1);
83+
let hasRefinements = this.clearAttributes.length !== 0;
7984
let url = createURL(clearRefinementsFromState(state));
8085

8186
ReactDOM.render(
8287
<ClearAll
83-
clearAll={this._clearRefinementsAndSearch}
88+
clearAll={this.clearAll}
8489
collapsible={collapsible}
8590
cssClasses={cssClasses}
8691
hasRefinements={hasRefinements}
@@ -90,6 +95,12 @@ function clearAll({
9095
/>,
9196
containerNode
9297
);
98+
},
99+
100+
clearAll(helper) {
101+
if (this.clearAttributes.length > 0) {
102+
clearRefinementsAndSearch(helper, this.clearAttributes);
103+
}
93104
}
94105
};
95106
}

0 commit comments

Comments
 (0)