Skip to content

Commit f41484a

Browse files
author
vvo
committed
feat(priceRanges): add currency option
This is a feature and also a fix. Previously, we had labels.currency but it was not setting the currency in the item template. So you had to provide your own template item, and even doing so could not access any `currency` template data. No we only provide opts.currency (no more labels.currency, but we are backward compatible). And `currency` is a template data.
1 parent 1baa426 commit f41484a

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

Diff for: src/components/PriceRanges/PriceRanges.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import cx from 'classnames';
66

77
class PriceRanges extends React.Component {
88
getForm() {
9+
let labels = {
10+
currency: this.props.currency,
11+
...this.props.labels
12+
};
13+
914
return (
1015
<PriceRangesForm
1116
cssClasses={this.props.cssClasses}
12-
labels={this.props.labels}
17+
labels={labels}
1318
refine={this.refine.bind(this)}
1419
/>
1520
);
@@ -30,14 +35,18 @@ class PriceRanges extends React.Component {
3035
let url = this.getURLFromFacetValue(facetValue);
3136
let key = facetValue.from + '_' + facetValue.to;
3237
let handleClick = this.refine.bind(this, facetValue.from, facetValue.to);
38+
let data = {
39+
currency: this.props.currency,
40+
...facetValue
41+
};
3342
return (
3443
<div className={cssClassItem} key={key}>
3544
<a
3645
className={this.props.cssClasses.link}
3746
href={url}
3847
onClick={handleClick}
3948
>
40-
<Template data={facetValue} templateKey="item" {...this.props.templateProps} />
49+
<Template data={data} templateKey="item" {...this.props.templateProps} />
4150
</a>
4251
</div>
4352
);
@@ -80,10 +89,10 @@ PriceRanges.propTypes = {
8089
list: React.PropTypes.string,
8190
separator: React.PropTypes.string
8291
}),
92+
currency: React.PropTypes.string,
8393
facetValues: React.PropTypes.array,
8494
labels: React.PropTypes.shape({
8595
button: React.PropTypes.string,
86-
currency: React.PropTypes.string,
8796
to: React.PropTypes.string
8897
}),
8998
refine: React.PropTypes.func.isRequired,

Diff for: src/components/PriceRanges/__tests__/PriceRanges-test.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ describe('PriceRanges', () => {
103103
item: 'item',
104104
link: 'link',
105105
active: 'active'
106-
}
106+
},
107+
currency: '$'
107108
};
108109
facetValue = {
109110
from: 1,
@@ -123,7 +124,7 @@ describe('PriceRanges', () => {
123124
expect(item).toEqualJSX(
124125
<div className="item" key="1_10">
125126
<a className="link" href="url" onClick={() => {}}>
126-
<Template data={facetValue} templateKey="item"/>
127+
<Template data={{currency: '$', ...facetValue}} templateKey="item"/>
127128
</a>
128129
</div>
129130
);
@@ -140,7 +141,7 @@ describe('PriceRanges', () => {
140141
expect(item).toEqualJSX(
141142
<div className="item active" key="1_10">
142143
<a className="link" href="url" onClick={() => {}}>
143-
<Template data={facetValue} templateKey="item"/>
144+
<Template data={{currency: '$', ...facetValue}} templateKey="item"/>
144145
</a>
145146
</div>
146147
);
@@ -172,7 +173,8 @@ describe('PriceRanges', () => {
172173
// Given
173174
let props = {
174175
cssClasses: 'cssClasses',
175-
labels: 'labels',
176+
labels: {button: 'hello'},
177+
currency: '$',
176178
refine: 'refine'
177179
};
178180
let component = getComponentWithMockRendering(props);
@@ -184,7 +186,7 @@ describe('PriceRanges', () => {
184186
expect(form).toEqualJSX(
185187
<PriceRangesForm
186188
cssClasses={props.cssClasses}
187-
labels={props.labels}
189+
labels={{button: 'hello', currency: '$'}}
188190
refine={() => {}}
189191
/>
190192
);

Diff for: src/widgets/price-ranges/__tests__/price-ranges-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ describe('priceRanges()', () => {
9797
},
9898
shouldAutoHideContainer: false,
9999
facetValues: generateRanges(results.getFacetStats()),
100+
currency: '$',
100101
labels: {
101-
currency: '$',
102102
separator: 'to',
103103
button: 'Go'
104104
},

Diff for: src/widgets/price-ranges/defaultTemplates.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
{{^to}}
66
&ge;
77
{{/to}}
8-
\${{from}}
8+
{{currency}}{{from}}
99
{{/from}}
1010
{{#to}}
1111
{{#from}}
@@ -14,7 +14,7 @@ export default {
1414
{{^from}}
1515
&le;
1616
{{/from}}
17-
\${{to}}
17+
{{to}}
1818
{{/to}}
1919
`,
2020
footer: ``

Diff for: src/widgets/price-ranges/price-ranges.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import cx from 'classnames';
1818
* @param {string|DOMElement} options.container Valid CSS Selector as a string or DOMElement
1919
* @param {string} options.attributeName Name of the attribute for faceting
2020
* @param {Object} [options.templates] Templates to use for the widget
21-
* @param {string|Function} [options.templates.item] Item template
21+
* @param {string|Function} [options.templates.item] Item template. Template data: `from`, `to` and `currency`
22+
* @param {string} [options.currency='$'] The currency to display
2223
* @param {Object} [options.labels] Labels to use for the widget
23-
* @param {string|Function} [options.labels.currency] Currency label
2424
* @param {string|Function} [options.labels.separator] Separator label, between min and max
2525
* @param {string|Function} [options.labels.button] Button label
2626
* @param {boolean} [options.autoHideContainer=true] Hide the container when no refinements available
@@ -45,6 +45,7 @@ const usage = `Usage:
4545
priceRanges({
4646
container,
4747
attributeName,
48+
[ currency=$ ],
4849
[ cssClasses.{root,header,body,list,item,active,link,form,label,input,currency,separator,button,footer} ],
4950
[ templates.{header,item,footer} ],
5051
[ labels.{currency,separator,button} ],
@@ -55,11 +56,8 @@ function priceRanges({
5556
attributeName,
5657
cssClasses: userCssClasses = {},
5758
templates = defaultTemplates,
58-
labels = {
59-
currency: '$',
60-
button: 'Go',
61-
separator: 'to'
62-
},
59+
labels: userLabels = {},
60+
currency = '$',
6361
autoHideContainer = true
6462
} = {}) {
6563
if (!container || !attributeName) {
@@ -119,6 +117,14 @@ function priceRanges({
119117

120118
render: function({results, helper, templatesConfig, state, createURL}) {
121119
let facetValues;
120+
let labels = {
121+
button: 'Go',
122+
separator: 'to',
123+
...userLabels
124+
};
125+
126+
// before we had opts.currency, you had to pass labels.currency
127+
if (userLabels.currency !== undefined && userLabels.currency !== currency) currency = userLabels.currency;
122128

123129
if (results.hits.length > 0) {
124130
facetValues = this._extractRefinedRange(helper);
@@ -170,6 +176,7 @@ function priceRanges({
170176
return createURL(newState);
171177
}}
172178
cssClasses={cssClasses}
179+
currency={currency}
173180
facetValues={facetValues}
174181
labels={labels}
175182
refine={this._refine.bind(this, helper)}

0 commit comments

Comments
 (0)