Skip to content

Commit c7e5e34

Browse files
authored
Merge pull request #413 from CraveFood/initial-amountselector
AmountSelectors should render its initial value
2 parents e5df258 + 7ef110e commit c7e5e34

File tree

3 files changed

+416
-8
lines changed

3 files changed

+416
-8
lines changed

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ const selectorSizeToFontSize = {
1919
};
2020

2121
class AmountSelectors extends React.Component {
22-
state = {
23-
value: 0,
24-
disableBoth: false,
25-
tooltipText: "",
26-
displayValue: ""
27-
};
22+
constructor(props) {
23+
super(props);
2824

29-
componentDidMount() {
30-
this.updateStateWithNewValue(this.props.value);
25+
const { value } = this.props;
26+
const parsedValue = parseFloat(value) || 0;
27+
const displayValue = parsedValue.toFixed(2);
28+
29+
this.state = {
30+
value: Number(displayValue),
31+
disableBoth: false,
32+
tooltipText: "",
33+
displayValue
34+
};
3135
}
3236

3337
componentDidUpdate(prevProps, prevState) {

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

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ storiesOf("Amount selectors", module)
4040
"With a max value of 3",
4141
withInfo()(() => <AmountSelectors max={3} value={2} step={0.5} />)
4242
)
43+
.add(
44+
"With a max value of 3 and initial value = 5",
45+
withInfo()(() => <AmountSelectors max={3} value={5} />)
46+
)
4347
.add(
4448
"With onChange function",
4549
withInfo()(() => <AmountSelectors onChange={action("amount changed")} />)

0 commit comments

Comments
 (0)