Skip to content

Commit

Permalink
[@mantine/core] NumberInput: Fix value reset to zero when leading non…
Browse files Browse the repository at this point in the history
…-zero number is deleted (#4916)

* [@mantine/core] NumberInput value type fix

* [@mantine/core] NumberInput Test Fix
  • Loading branch information
ShaifArfan authored Oct 1, 2023
1 parent 7dc4a72 commit 2c2e703
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('@mantine/core/NumberInput', () => {
const spy = jest.fn();
render(<NumberInput max={10} min={0} step={6} onChange={spy} />);
await enterText('5');
expect(spy).toHaveBeenLastCalledWith(5);
expect(spy).toHaveBeenLastCalledWith('5');
await enterText('{backspace}');
expect(spy).toHaveBeenLastCalledWith('');
expectValue('');
Expand All @@ -85,15 +85,15 @@ describe('@mantine/core/NumberInput', () => {

focusInput();
await enterText('3');
expect(spy).toHaveBeenLastCalledWith(3);
expect(spy).toHaveBeenLastCalledWith('3');
expect(spy).toHaveBeenCalledTimes(1);

await enterText('2');
expect(spy).toHaveBeenLastCalledWith(32);
expect(spy).toHaveBeenLastCalledWith('32');
expect(spy).toHaveBeenCalledTimes(2);

await enterText('a');
expect(spy).toHaveBeenLastCalledWith(32);
expect(spy).toHaveBeenLastCalledWith('32');
expect(spy).toHaveBeenCalledTimes(2);
});

Expand Down
6 changes: 5 additions & 1 deletion src/mantine-core/src/components/NumberInput/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,11 @@ export const NumberInput = factory<NumberInputFactory>((_props, ref) => {
});

const handleValueChange: OnValueChange = (payload, event) => {
setValue(isValidNumber(payload.floatValue) ? payload.floatValue : payload.value);
setValue(
typeof _value === 'number' && isValidNumber(payload.floatValue)

This comment has been minimized.

Copy link
@mmahalwy

mmahalwy Oct 19, 2023

Contributor

@ShaifArfan @rtivital this is causing errors where if the component is first mounted with undefined or null, it will always return a string. Will never switch over to a float.

? payload.floatValue
: payload.value
);
onValueChange?.(payload, event);
};

Expand Down

0 comments on commit 2c2e703

Please sign in to comment.