Skip to content

Commit

Permalink
[core] fix(NumericInput): correct rounding of default value (#3894)
Browse files Browse the repository at this point in the history
Fixes #3889
  • Loading branch information
TheRealSpaceShip authored and adidahiya committed Jan 6, 2020
1 parent 918fc23 commit 1127ac5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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

1 comment on commit 1127ac5

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Previews: documentation | landing | table

Please sign in to comment.