Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amount selector should truncate values on blur #410

Merged
merged 4 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 37 additions & 26 deletions packages/amount-selectors/src/components/AmountSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ const selectorSizeToFontSize = {

class AmountSelectors extends React.Component {
state = {
value: this.props.value,
value: 0,
disableBoth: false,
tooltipText: "",
displayValue: this.props.value
displayValue: ""
};

componentDidMount() {
this.updateStateWithNewValue(this.props.value);
}

componentDidUpdate(prevProps, prevState) {
if (prevProps.value !== this.props.value) {
this.updateStateWithNewValue(this.props.value);
}
}

// Conditions to disable both buttons,
// see https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation
disableBoth = validity =>
Expand All @@ -49,21 +59,6 @@ class AmountSelectors extends React.Component {
return this.props.onChange(validValue);
};

validValue = value => {
const parsedValue = parseFloat(value) || 0;
return Math.min(this.props.max, Math.max(this.props.min, parsedValue));
};

updateDisplayValue = () => {
const validValue = this.validValue(this.state.value);
this.setState({
value: validValue,
displayValue: validValue.toFixed(2)
});

return this.props.onChange(validValue);
};

decrement = () => {
const value = this.state.value - this.props.step;

Expand All @@ -76,15 +71,31 @@ class AmountSelectors extends React.Component {
this.onChange(Math.min(value, this.props.max), this.updateDisplayValue);
};

componentDidUpdate(prevProps, prevState) {
if (prevProps.value !== this.props.value) {
const validValue = this.validValue(this.props.value);
this.setState({
value: validValue,
displayValue: validValue.toFixed(2)
});
}
}
updateDisplayValue = () => {
const validValue = this.updateStateWithNewValue(this.state.value);

return this.props.onChange(validValue);
};

getValidValue = value => {
const parsedValue = parseFloat(value) || 0;
const validValue = Math.min(
this.props.max,
Math.max(this.props.min, parsedValue)
);

return Number(validValue.toFixed(2));
};

updateStateWithNewValue = value => {
const validValue = this.getValidValue(value);
this.setState({
value: validValue,
displayValue: validValue.toFixed(2)
});

return validValue;
};

render() {
return (
Expand Down
12 changes: 12 additions & 0 deletions packages/amount-selectors/src/components/AmountSelectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,16 @@ describe("Amount selectors", () => {
expect(component.state().displayValue).toBe(expectedDisplayValue);
});
});

test("getValidValue should return a valid value with 2 decimal places", () => {
const component = shallow(<AmountSelectors />);
const { getValidValue } = component.instance();

const value = 3.345678;
const validValue = getValidValue(value);

const expectedValue = Number(value.toFixed(2));

expect(validValue).toBe(expectedValue);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ exports[`Storyshots Amount selectors Default 1`] = `
size={4}
step={1}
type="number"
value={0}
value="0.00"
/>
</div>
</div>
Expand Down Expand Up @@ -757,7 +757,7 @@ exports[`Storyshots Amount selectors Disabled typing 1`] = `
size={4}
step={1}
type="number"
value={0}
value="0.00"
/>
</div>
</div>
Expand Down Expand Up @@ -1157,7 +1157,7 @@ exports[`Storyshots Amount selectors Small size 1`] = `
size={4}
step={1}
type="number"
value={0}
value="0.00"
/>
</div>
</div>
Expand Down Expand Up @@ -1552,7 +1552,7 @@ exports[`Storyshots Amount selectors With 0.5 steps 1`] = `
size={4}
step={0.5}
type="number"
value={2}
value="2.00"
/>
</div>
</div>
Expand Down Expand Up @@ -1947,7 +1947,7 @@ exports[`Storyshots Amount selectors With 0.5 steps and step mismatch validation
size={4}
step={0.5}
type="number"
value={2}
value="2.00"
/>
</div>
</div>
Expand Down Expand Up @@ -2342,7 +2342,7 @@ exports[`Storyshots Amount selectors With a max value of 3 1`] = `
size={4}
step={0.5}
type="number"
value={2}
value="2.00"
/>
</div>
</div>
Expand Down Expand Up @@ -2742,7 +2742,7 @@ exports[`Storyshots Amount selectors With onChange function 1`] = `
size={4}
step={1}
type="number"
value={0}
value="0.00"
/>
</div>
</div>
Expand Down Expand Up @@ -3137,7 +3137,7 @@ exports[`Storyshots Amount selectors With value set 1`] = `
size={4}
step={1}
type="number"
value={2}
value="2.00"
/>
</div>
</div>
Expand Down Expand Up @@ -3533,7 +3533,7 @@ exports[`Storyshots Amount selectors With value set after mount 1`] = `
size={4}
step={1}
type="number"
value={10}
value="10.00"
/>
</div>
</div>
Expand Down