Skip to content

Commit 8a70c7d

Browse files
author
vvo
committed
feat(toggle): add headerFooter decorator
Added to toggle: - templates.header - templates.body - templates.footer - cssClasses.root BREAKING CHANGES: - toggle: removed template
1 parent 5974a88 commit 8a70c7d

File tree

3 files changed

+51
-60
lines changed

3 files changed

+51
-60
lines changed

components/Toggle.js

-45
This file was deleted.

example/templates/free-shipping.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div class="checkbox panel-body">
22
<label>
33
<input type="checkbox" {{#isRefined}}checked{{/isRefined}} />
4-
<span class="badge">{{label}}</span>
4+
{{label}}
55
</label>
6+
<span class="badge pull-right">{{count}}</span>
67
</div>

widgets/toggle.js

+49-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
var find = require('lodash/collection/find');
12
var React = require('react');
23

34
var utils = require('../lib/utils.js');
4-
var defaultTemplate = '<label>{{label}}<input type="checkbox" {{#isRefined}}checked{{/isRefined}} /></label>';
5+
6+
var defaultTemplates = {
7+
header: '',
8+
body: `<label>
9+
<input type="checkbox" {{#isRefined}}checked{{/isRefined}} />{{label}} <span>{{count}}</span>
10+
</label>`,
11+
footer: ''
12+
};
513

614
/**
715
* Instantiate the toggling of a boolean facet filter on and off.
@@ -10,7 +18,12 @@ var defaultTemplate = '<label>{{label}}<input type="checkbox" {{#isRefined}}chec
1018
* @param {String|DOMElement} options.container CSS Selector or DOMElement to insert the widget
1119
* @param {String} options.facetName Name of the attribute for faceting (eg. "free_shipping")
1220
* @param {String} options.label Human-readable name of the filter (eg. "Free Shipping")
13-
* @param {String|Function} [options.template] Item template, provided with `label` and `isRefined`
21+
* @param {Object} [options.cssClasses] CSS classes to add to the wrapping elements: root
22+
* @param {String|String[]} [options.cssClasses.root=null]
23+
* @param {Object} [options.templates] Templates to use for the widget
24+
* @param {String|Function} [options.templates.header=''] Header template
25+
* @param {String|Function} [options.templates.body='<label>{{label}}<input type="checkbox" {{#isRefined}}checked{{/isRefined}} /></label>'] Body template
26+
* @param {String|Function} [options.templates.footer=''] Footer template
1427
* @param {Function} [options.transformData] Function to change the object passed to the item template
1528
* @param {boolean} [hideWhenNoResults=true] Hide the container when no results match
1629
* @return {Object}
@@ -19,11 +32,14 @@ function toggle({
1932
container = null,
2033
facetName = null,
2134
label = null,
22-
template = defaultTemplate,
35+
templates = defaultTemplates,
36+
cssClasses = {
37+
root: null
38+
},
2339
transformData = null,
2440
hideWhenNoResults = true
2541
}) {
26-
var Toggle = require('../components/Toggle');
42+
var RefinementList = require('../components/RefinementList');
2743

2844
var containerNode = utils.getContainerNode(container);
2945
var usage = 'Usage: toggle({container, facetName, label[, template, transformData]})';
@@ -46,25 +62,44 @@ function toggle({
4662
count: values && values.count || null
4763
};
4864

49-
function toggleFilter() {
50-
var methodToCall = isRefined ? 'removeFacetRefinement' : 'addFacetRefinement';
51-
helper[methodToCall](facetName, true).search();
52-
}
65+
var _templates = {
66+
header: templates.header,
67+
item: templates.body,
68+
footer: templates.footer
69+
};
5370

5471
React.render(
55-
<Toggle
56-
isRefined={isRefined}
57-
label={label}
58-
template={template}
59-
transformData={transformData}
72+
<RefinementList
73+
facetValues={[facetValue]}
74+
templates={_templates}
75+
cssClasses={cssClasses}
76+
transformData={prepareData(transformData)}
6077
hideWhenNoResults={hideWhenNoResults}
6178
hasResults={results.hits.length > 0}
62-
toggleFilter={toggleFilter}
79+
toggleRefinement={toggleRefinement.bind(null, helper, facetName, facetValue.isRefined)}
6380
/>,
6481
containerNode
6582
);
6683
}
6784
};
6885
}
6986

87+
function prepareData(transformData) {
88+
return function(data) {
89+
var newData = {
90+
label: data.name, // Toggle API exposes `label`
91+
isRefined: data.isRefined,
92+
count: data.count
93+
};
94+
95+
return transformData && transformData(newData) || newData;
96+
};
97+
}
98+
99+
function toggleRefinement(helper, facetName, isRefined) {
100+
var action = isRefined ? 'remove' : 'add';
101+
102+
helper[action + 'FacetRefinement'](facetName, true).search();
103+
}
104+
70105
module.exports = toggle;

0 commit comments

Comments
 (0)