1+ var find = require ( 'lodash/collection/find' ) ;
12var React = require ( 'react' ) ;
23
34var 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+
70105module . exports = toggle ;
0 commit comments