Skip to content

Commit bff4cfc

Browse files
author
刘诗焕
committed
fix: make sure that the onChange event is triggered after onKeyDown
1 parent d3de147 commit bff4cfc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Diff for: src/InputNumber.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ const InputNumber = React.forwardRef(
141141

142142
const userTypingRef = React.useRef(false);
143143
const compositionRef = React.useRef(false);
144+
const keydownRef = React.useRef(false);
145+
const onChangeRef = React.useRef<() => void>();
144146

145147
// ============================ Value =============================
146148
// Real value control
@@ -401,6 +403,13 @@ const InputNumber = React.forwardRef(
401403

402404
// >>> Input
403405
const onInternalInput: React.ChangeEventHandler<HTMLInputElement> = (e) => {
406+
if (!keydownRef.current) {
407+
const v = e.target.value;
408+
onChangeRef.current = () => {
409+
collectInputValue(v);
410+
};
411+
return;
412+
}
404413
collectInputValue(e.target.value);
405414
};
406415

@@ -469,6 +478,12 @@ const InputNumber = React.forwardRef(
469478
onPressEnter?.(event);
470479
}
471480

481+
keydownRef.current = true;
482+
if (onChangeRef.current) {
483+
onChangeRef.current();
484+
onChangeRef.current = undefined;
485+
}
486+
472487
if (keyboard === false) {
473488
return;
474489
}
@@ -482,6 +497,7 @@ const InputNumber = React.forwardRef(
482497

483498
const onKeyUp = () => {
484499
userTypingRef.current = false;
500+
keydownRef.current = false;
485501
};
486502

487503
// >>> Focus & Blur

0 commit comments

Comments
 (0)