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

[core] fix(NumericInput): correct rounding of default value #3894

Merged
merged 1 commit into from Jan 6, 2020
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
6 changes: 3 additions & 3 deletions packages/core/src/components/forms/numericInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ export class NumericInput extends AbstractPureComponent2<HTMLInputProps & INumer
const didValuePropChange = props.value !== state.prevValueProp;
const value = getValueOrEmptyValue(didValuePropChange ? props.value : state.value);

const stepMaxPrecision = NumericInput.getStepMaxPrecision(props);

const sanitizedValue =
value !== NumericInput.VALUE_EMPTY
? NumericInput.getSanitizedValue(value, /* delta */ 0, props.min, props.max)
? NumericInput.getSanitizedValue(value, stepMaxPrecision, props.min, props.max)
: NumericInput.VALUE_EMPTY;

const stepMaxPrecision = NumericInput.getStepMaxPrecision(props);

// if a new min and max were provided that cause the existing value to fall
// outside of the new bounds, then clamp the value to the new valid range.
if (didBoundsChange && sanitizedValue !== state.value) {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/controls/numericInputTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,16 @@ describe("<NumericInput>", () => {
expect(component.find(InputGroup).find(Button)).to.exist;
});

it("passed decimal value should be rounded by stepSize", () => {
const component = mount(<NumericInput value={9.001} min={0} />);
expect(component.find("input").prop("value")).to.equal("9");
});

it("passed decimal value should be rounded by minorStepSize", () => {
const component = mount(<NumericInput value={"9.01"} min={0} minorStepSize={0.01} />);
expect(component.find("input").prop("value")).to.equal("9.01");
});

it("changes max precision of displayed value to that of the smallest step size defined", () => {
const component = mount(<NumericInput majorStepSize={1} stepSize={0.1} minorStepSize={0.001} />);
const incrementButton = component.find(Button).first();
Expand Down