Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions tests/github.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
5 changes: 4 additions & 1 deletion tests/keyboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ describe('InputNumber.Keyboard', () => {
const onChange = jest.fn();
const wrapper = mount(<InputNumber precision={0} onChange={onChange} />);

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();

Expand Down