1
+ var find = require ( 'lodash/collection/find' ) ;
1
2
var React = require ( 'react' ) ;
2
3
3
4
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
+ } ;
5
13
6
14
/**
7
15
* Instantiate the toggling of a boolean facet filter on and off.
@@ -10,7 +18,12 @@ var defaultTemplate = '<label>{{label}}<input type="checkbox" {{#isRefined}}chec
10
18
* @param {String|DOMElement } options.container CSS Selector or DOMElement to insert the widget
11
19
* @param {String } options.facetName Name of the attribute for faceting (eg. "free_shipping")
12
20
* @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
14
27
* @param {Function } [options.transformData] Function to change the object passed to the item template
15
28
* @param {boolean } [hideWhenNoResults=true] Hide the container when no results match
16
29
* @return {Object }
@@ -19,11 +32,14 @@ function toggle({
19
32
container = null ,
20
33
facetName = null ,
21
34
label = null ,
22
- template = defaultTemplate ,
35
+ templates = defaultTemplates ,
36
+ cssClasses = {
37
+ root : null
38
+ } ,
23
39
transformData = null ,
24
40
hideWhenNoResults = true
25
41
} ) {
26
- var Toggle = require ( '../components/Toggle ' ) ;
42
+ var RefinementList = require ( '../components/RefinementList ' ) ;
27
43
28
44
var containerNode = utils . getContainerNode ( container ) ;
29
45
var usage = 'Usage: toggle({container, facetName, label[, template, transformData]})' ;
@@ -46,25 +62,44 @@ function toggle({
46
62
count : values && values . count || null
47
63
} ;
48
64
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
+ } ;
53
70
54
71
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 ) }
60
77
hideWhenNoResults = { hideWhenNoResults }
61
78
hasResults = { results . hits . length > 0 }
62
- toggleFilter = { toggleFilter }
79
+ toggleRefinement = { toggleRefinement . bind ( null , helper , facetName , facetValue . isRefined ) }
63
80
/> ,
64
81
containerNode
65
82
) ;
66
83
}
67
84
} ;
68
85
}
69
86
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
+
70
105
module . exports = toggle ;
0 commit comments