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 diff --git a/tests/github.test.tsx b/tests/github.test.tsx index e616c0dc..68acba71 100644 --- a/tests/github.test.tsx +++ b/tests/github.test.tsx @@ -458,10 +458,12 @@ describe('InputNumber.Github', () => { wrapper .find('input') .last() + .simulate('keyDown') .simulate('change', { target: { value: '1.23' } }); wrapper .find('input') .first() + .simulate('keyDown') .simulate('change', { target: { value: '0' } }); expect(wrapper.find('input').last().props().value).toEqual('1'); diff --git a/tests/keyboard.test.tsx b/tests/keyboard.test.tsx index 12ccefb9..b1f8e050 100644 --- a/tests/keyboard.test.tsx +++ b/tests/keyboard.test.tsx @@ -35,7 +35,10 @@ describe('InputNumber.Keyboard', () => { const onChange = jest.fn(); const wrapper = mount(); - wrapper.find('input').simulate('change', { target: { value: '2.3333' } }); + wrapper + .find('input') + .simulate('keyDown') + .simulate('change', { target: { value: '2.3333' } }); expect(onChange).toHaveBeenCalledWith(2.3333); onChange.mockReset();