Skip to content

Commit 1953a3f

Browse files
authored
Merge pull request #382 from CraveFood/fix-amount-prop-update
Fix amount prop update
2 parents f216b82 + 29be50c commit 1953a3f

File tree

4 files changed

+458
-6
lines changed

4 files changed

+458
-6
lines changed

packages/amount-selectors/src/components/AmountSelectors.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class AmountSelectors extends React.Component {
4949
return this.props.onChange(validValue);
5050
};
5151

52-
updateDisplayValue = () => {
53-
const parsedValue = parseFloat(this.state.value) || 0;
54-
const validValue = Math.min(
55-
this.props.max,
56-
Math.max(this.props.min, parsedValue)
57-
);
52+
validValue = value => {
53+
const parsedValue = parseFloat(value) || 0;
54+
return Math.min(this.props.max, Math.max(this.props.min, parsedValue));
55+
};
5856

57+
updateDisplayValue = () => {
58+
const validValue = this.validValue(this.state.value);
5959
this.setState({
6060
value: validValue,
6161
displayValue: validValue.toFixed(2)
@@ -76,6 +76,16 @@ class AmountSelectors extends React.Component {
7676
this.onChange(Math.min(value, this.props.max), this.updateDisplayValue);
7777
};
7878

79+
componentDidUpdate(prevProps, prevState) {
80+
if (prevProps.value !== this.props.value) {
81+
const validValue = this.validValue(this.props.value);
82+
this.setState({
83+
value: validValue,
84+
displayValue: validValue.toFixed(2)
85+
});
86+
}
87+
}
88+
7989
render() {
8090
return (
8191
<Wrapper size={this.props.size}>

packages/amount-selectors/src/components/AmountSelectors.story.js

+19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ import AmountSelectors from "./AmountSelectors";
99
storiesOf("Amount selectors", module)
1010
.add("Default", withInfo()(() => <AmountSelectors />))
1111
.add("With value set", withInfo()(() => <AmountSelectors value={2} />))
12+
.add(
13+
"With value set after mount",
14+
withInfo()(() => {
15+
class MyStory extends React.Component {
16+
state = { value: 10 };
17+
render() {
18+
return (
19+
<div>
20+
<AmountSelectors value={this.state.value} />
21+
<button onClick={() => this.setState({ value: Math.random() })}>
22+
Set Value
23+
</button>
24+
</div>
25+
);
26+
}
27+
}
28+
return <MyStory />;
29+
})
30+
)
1231
.add(
1332
"With 0.5 steps",
1433
withInfo()(() => <AmountSelectors value={2} step={0.5} />)

packages/amount-selectors/src/components/AmountSelectors.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe("Amount selectors", () => {
8181

8282
expect(component.state("value")).toBe(onChangeValue);
8383
});
84+
8485
test("should handle an event fired by input", () => {
8586
const initialValue = 0;
8687
const event = {
@@ -126,4 +127,24 @@ describe("Amount selectors", () => {
126127
expect(component.state().disableBoth).toBe(true);
127128
});
128129
});
130+
131+
describe("component update", () => {
132+
test("should update value when prop value is set after mount", () => {
133+
const onChange = jest.fn();
134+
const component = mount(
135+
<AmountSelectors value={1} onChange={onChange} />
136+
);
137+
const newValue = 2;
138+
component.setProps({ value: newValue });
139+
expect(component.state("value")).toBe(newValue);
140+
});
141+
142+
test("should update displayed value when prop value changes", () => {
143+
const nextValue = 10;
144+
const expectedDisplayValue = "10.00";
145+
const component = mount(<AmountSelectors value={0} />);
146+
component.setProps({ value: nextValue });
147+
expect(component.state().displayValue).toBe(expectedDisplayValue);
148+
});
149+
});
129150
});

0 commit comments

Comments
 (0)