Skip to content

Commit

Permalink
[@mantine/core] NumberInput: Fix incorrect onChange value with high p…
Browse files Browse the repository at this point in the history
…recision (#518)

* Fix for #516

* Fix for #519

* Add use-os story

* Formatting error corrected

* Added a check for the precision on blur
  • Loading branch information
jerebtw authored Dec 16, 2021
1 parent e226a08 commit 8827dda
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/mantine-core/src/components/NumberInput/NumberInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ describe('@mantine/core/NumberInput', () => {
expect(input.getDOMNode().getAttribute('value')).toBe('');
});

it('precision on blur', () => {
const spy = jest.fn();
const element = mount(
<NumberInput value={undefined} max={10} min={0} step={6} precision={2} onChange={spy} />
);

const input = element.find('input').at(0);
//change value to 6.123, blur and that should be result in a rounded result of 6.12
input.simulate('change', { target: { value: '6.123' } });
expect(spy).toHaveBeenLastCalledWith(6.123);
input.simulate('blur', { target: { value: '6.123' } });
expect(input.getDOMNode().getAttribute('value')).toBe('6.12');
expect(spy).toHaveBeenLastCalledWith(6.12);
});

it('sets state to min or 0 if input is empty and is incremented/decremented', () => {
const spy = jest.fn();
const element = mount(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>(
if (!Number.isNaN(val)) {
if (!noClampOnBlur) {
setTempValue(val.toFixed(precision));
handleValueChange(val);
handleValueChange(parseFloat(val.toFixed(precision)));
}
} else {
setTempValue(finalValue?.toFixed(precision) ?? '');
Expand Down
19 changes: 19 additions & 0 deletions src/mantine-hooks/src/use-os/use-os.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import { useOs } from './use-os';

const Demo = () => {
const os = useOs();

return (
<>
Your os is <b>{os}</b>
</>
);
};

storiesOf('@mantine/hooks/use-os', module).add('Usage', () => (
<div style={{ padding: 40 }}>
<Demo />
</div>
));
2 changes: 1 addition & 1 deletion src/mantine-hooks/src/use-os/use-os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getOS() {
os = 'windows';
} else if (/Android/.test(userAgent)) {
os = 'android';
} else if (!os && /Linux/.test(platform)) {
} else if (/Linux/.test(platform)) {
os = 'linux';
}

Expand Down

0 comments on commit 8827dda

Please sign in to comment.