Skip to content

Commit

Permalink
chore: fix prevValue to be empty string if not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
bombillazo committed Sep 29, 2024
1 parent 296327d commit 81e3375
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const InternalInputNumber = React.forwardRef(
const numStr = String(num);

if (parser) {
return parser(numStr, { prevValue: String(prevValueRef.current) });
return parser(numStr, { prevValue: String(prevValueRef.current ?? '') });
}

let parsedStr = numStr;
Expand All @@ -227,7 +227,7 @@ const InternalInputNumber = React.forwardRef(
return formatter(number, {
userTyping,
input: String(inputValueRef.current),
prevValue: String(prevValueRef.current),
prevValue: String(prevValueRef.current ?? ''),
});
}

Expand Down Expand Up @@ -389,10 +389,9 @@ const InternalInputNumber = React.forwardRef(

// >>> Collect input value
const collectInputValue = (inputStr: string) => {

// validate string
if(validator){
if(!validator(inputStr)) return;
if (validator) {
if (!validator(inputStr)) return;
}

recordCursor();
Expand Down
2 changes: 1 addition & 1 deletion tests/formatter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('InputNumber.Formatter', () => {

fireEvent.change(container.querySelector('input'), { target: { value: '1' } });
expect(formatter).toHaveBeenCalledTimes(1);
expect(formatter).toHaveBeenCalledWith('1', { userTyping: true, input: '1' });
expect(formatter).toHaveBeenCalledWith('1', { userTyping: true, input: '1', prevValue: '' });
});

describe('dynamic formatter', () => {
Expand Down

0 comments on commit 81e3375

Please sign in to comment.