Skip to content

Commit

Permalink
fix: make sure that the onChange event is triggered after onKeyDown
Browse files Browse the repository at this point in the history
  • Loading branch information
刘诗焕 committed Mar 1, 2022
1 parent d3de147 commit bff4cfc
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ const InputNumber = React.forwardRef(

const userTypingRef = React.useRef(false);
const compositionRef = React.useRef(false);
const keydownRef = React.useRef(false);
const onChangeRef = React.useRef<() => void>();

// ============================ Value =============================
// Real value control
Expand Down Expand Up @@ -401,6 +403,13 @@ const InputNumber = React.forwardRef(

// >>> Input
const onInternalInput: React.ChangeEventHandler<HTMLInputElement> = (e) => {
if (!keydownRef.current) {
const v = e.target.value;
onChangeRef.current = () => {
collectInputValue(v);
};
return;
}
collectInputValue(e.target.value);
};

Expand Down Expand Up @@ -469,6 +478,12 @@ const InputNumber = React.forwardRef(
onPressEnter?.(event);
}

keydownRef.current = true;
if (onChangeRef.current) {
onChangeRef.current();
onChangeRef.current = undefined;
}

if (keyboard === false) {
return;
}
Expand All @@ -482,6 +497,7 @@ const InputNumber = React.forwardRef(

const onKeyUp = () => {
userTypingRef.current = false;
keydownRef.current = false;
};

// >>> Focus & Blur
Expand Down

0 comments on commit bff4cfc

Please sign in to comment.