Skip to content

Commit 12ebde7

Browse files
authored
fix(pricesRange): fill the form according to the current refinement (#1126)
fixes #1009
1 parent 70a190c commit 12ebde7

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

src/components/PriceRanges/PriceRanges.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import isEqual from 'lodash/lang/isEqual';
88
class PriceRanges extends React.Component {
99
componentWillMount() {
1010
this.refine = this.refine.bind(this);
11-
this.form = this.getForm();
1211
}
1312

1413
shouldComponentUpdate(nextProps) {
@@ -21,9 +20,20 @@ class PriceRanges extends React.Component {
2120
...this.props.labels
2221
};
2322

23+
let currentRefinement;
24+
if (this.props.facetValues.length === 1) {
25+
currentRefinement = {
26+
from: this.props.facetValues[0].from !== undefined ? this.props.facetValues[0].from : '',
27+
to: this.props.facetValues[0].to !== undefined ? this.props.facetValues[0].to : ''
28+
};
29+
} else {
30+
currentRefinement = {from: '', to: ''};
31+
}
32+
2433
return (
2534
<PriceRangesForm
2635
cssClasses={this.props.cssClasses}
36+
currentRefinement={currentRefinement}
2737
labels={labels}
2838
refine={this.refine}
2939
/>
@@ -67,7 +77,7 @@ class PriceRanges extends React.Component {
6777
return this.getItemFromFacetValue(facetValue);
6878
})}
6979
</div>
70-
{this.form}
80+
{this.getForm()}
7181
</div>
7282
);
7383
}

src/components/PriceRanges/PriceRangesForm.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
11
import React from 'react';
22

33
class PriceRangesForm extends React.Component {
4+
constructor(props) {
5+
super(props);
6+
this.state = {
7+
from: props.currentRefinement.from,
8+
to: props.currentRefinement.to
9+
};
10+
}
11+
412
componentWillMount() {
513
this.handleSubmit = this.handleSubmit.bind(this);
614
}
715

8-
shouldComponentUpdate() {
9-
return false;
16+
componentWillReceiveProps(props) {
17+
this.setState({
18+
from: props.currentRefinement.from,
19+
to: props.currentRefinement.to
20+
});
1021
}
1122

1223
getInput(type) {
1324
return (
1425
<label className={this.props.cssClasses.label}>
1526
<span className={this.props.cssClasses.currency}>{this.props.labels.currency} </span>
16-
<input className={this.props.cssClasses.input} ref={type} type="number" />
27+
<input
28+
className={this.props.cssClasses.input}
29+
onChange={e => this.setState({[type]: e.target.value})}
30+
ref={type}
31+
type="number"
32+
value={this.state[type]}
33+
/>
1734
</label>
1835
);
1936
}
2037

2138
handleSubmit(event) {
22-
let from = +this.refs.from.value || undefined;
23-
let to = +this.refs.to.value || undefined;
39+
let from = this.refs.from.value !== '' ?
40+
parseInt(this.refs.from.value, 10) : undefined;
41+
let to = this.refs.to.value !== '' ?
42+
parseInt(this.refs.to.value, 10) : undefined;
2443
this.props.refine(from, to, event);
2544
}
2645

@@ -48,6 +67,10 @@ PriceRangesForm.propTypes = {
4867
label: React.PropTypes.string,
4968
separator: React.PropTypes.string
5069
}),
70+
currentRefinement: React.PropTypes.shape({
71+
from: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.number]),
72+
to: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.number])
73+
}),
5174
labels: React.PropTypes.shape({
5275
button: React.PropTypes.string,
5376
currency: React.PropTypes.string,
@@ -64,4 +87,3 @@ PriceRangesForm.defaultProps = {
6487

6588

6689
export default PriceRangesForm;
67-

src/components/PriceRanges/__tests__/PriceRanges-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ describe('PriceRanges', () => {
139139
cssClasses: 'cssClasses',
140140
labels: {button: 'hello'},
141141
currency: '$',
142-
refine: 'refine'
142+
refine: 'refine',
143+
facetValues: [{from: 0, to: 10}, {from: 10, to: 20}]
143144
};
144145
let component = getComponentWithMockRendering(props);
145146

@@ -150,6 +151,7 @@ describe('PriceRanges', () => {
150151
expect(form).toEqualJSX(
151152
<PriceRangesForm
152153
cssClasses={props.cssClasses}
154+
currentRefinement={{from: '', to: ''}}
153155
labels={{button: 'hello', currency: '$'}}
154156
refine={() => {}}
155157
/>

src/components/PriceRanges/__tests__/PriceRangesForm-test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ describe('PriceRangesForm', () => {
4545
currency: 'currency',
4646
separator: 'separator',
4747
button: 'button'
48+
},
49+
currentRefinement: {
50+
from: 10,
51+
to: 20
4852
}
4953
});
5054
expect(out).toEqualJSX(
5155
<form className="form" onSubmit={() => {}} ref="form">
5256
<label className="label">
5357
<span className="currency">$ </span>
54-
<input className="input" ref="from" type="number" />
58+
<input className="input" onChange={() => {}} ref="from" type="number" value={10} />
5559
</label>
5660
<span className="separator"> to </span>
5761
<label className="label">
5862
<span className="currency">$ </span>
59-
<input className="input" ref="to" type="number" />
63+
<input className="input" onChange={() => {}} ref="to" type="number" value={20} />
6064
</label>
6165
<button className="button" type="submit">Go</button>
6266
</form>
@@ -69,7 +73,15 @@ describe('PriceRangesForm', () => {
6973
// Given
7074
let refine = sinon.spy();
7175
let handleSubmitMock = sinon.spy(PriceRangesForm.prototype, 'handleSubmit');
72-
let component = TestUtils.renderIntoDocument(<PriceRangesForm refine={refine} />);
76+
let component = TestUtils.renderIntoDocument(
77+
<PriceRangesForm
78+
currentRefinement={{
79+
from: 10,
80+
to: 20
81+
}}
82+
refine={refine}
83+
/>
84+
);
7385

7486
// When
7587
component.refs.from.value = 10;

0 commit comments

Comments
 (0)