From bff4cfc0ea234179cacddc2d971f0000fef7d550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=AF=97=E7=84=95?= Date: Tue, 1 Mar 2022 11:45:58 +0800 Subject: [PATCH] fix: make sure that the onChange event is triggered after onKeyDown --- src/InputNumber.tsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/InputNumber.tsx b/src/InputNumber.tsx index 541be08f..4c298ce3 100644 --- a/src/InputNumber.tsx +++ b/src/InputNumber.tsx @@ -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 @@ -401,6 +403,13 @@ const InputNumber = React.forwardRef( // >>> Input const onInternalInput: React.ChangeEventHandler = (e) => { + if (!keydownRef.current) { + const v = e.target.value; + onChangeRef.current = () => { + collectInputValue(v); + }; + return; + } collectInputValue(e.target.value); }; @@ -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; } @@ -482,6 +497,7 @@ const InputNumber = React.forwardRef( const onKeyUp = () => { userTypingRef.current = false; + keydownRef.current = false; }; // >>> Focus & Blur