Skip to content

Commit 2148cc5

Browse files
taoxhsmiletaoxinhua
andauthored
fix: keydown when tab,inputnumber not trigger onKeyUp (#318)
* fix:keydown when tab,inputnumber not trigger onKeyUp * fix:keydown when tab,inputnumber not trigger onKeyUp add test Co-authored-by: taoxinhua <taoxinhua@xzintl.com>
1 parent 852ba70 commit 2148cc5

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/InputNumber.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ const InputNumber = React.forwardRef(
336336
};
337337

338338
// >>> Input
339-
const onInternalInput: React.ChangeEventHandler<HTMLInputElement> = (e) => {
339+
const onInternalInput: React.ChangeEventHandler<HTMLInputElement> = e => {
340340
let inputStr = e.target.value;
341341

342342
// optimize for chinese input experience
@@ -404,12 +404,12 @@ const InputNumber = React.forwardRef(
404404
}
405405
};
406406

407-
const onKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (event) => {
407+
const onKeyDown: React.KeyboardEventHandler<HTMLInputElement> = event => {
408408
const { which } = event;
409409
userTypingRef.current = true;
410410

411411
if (which === KeyCode.ENTER) {
412-
if(!compositionRef.current) {
412+
if (!compositionRef.current) {
413413
userTypingRef.current = false;
414414
}
415415
flushInputValue();
@@ -436,6 +436,8 @@ const InputNumber = React.forwardRef(
436436
flushInputValue();
437437

438438
setFocus(false);
439+
440+
userTypingRef.current = false;
439441
};
440442

441443
// ========================== Controlled ==========================

tests/input.test.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('InputNumber.Input', () => {
9797
it('pressEnter value should be ok', () => {
9898
const Demo = () => {
9999
const [value, setValue] = React.useState(1);
100-
const inputRef = React.useRef<HTMLInputElement>(null)
100+
const inputRef = React.useRef<HTMLInputElement>(null);
101101
return (
102102
<InputNumber
103103
ref={inputRef}
@@ -119,6 +119,24 @@ describe('InputNumber.Input', () => {
119119
expect(wrapper.getInputValue()).toEqual('5');
120120
});
121121

122+
it('keydown Tab, after change value should be ok', () => {
123+
let outSetValue;
124+
125+
const Demo = () => {
126+
const [value, setValue] = React.useState<string | number>(1);
127+
outSetValue = setValue;
128+
return <InputNumber autoFocus value={value} onChange={val => setValue(val)} />;
129+
};
130+
131+
const wrapper = mount(<Demo />);
132+
wrapper.findInput().simulate('keyDown', { which: KeyCode.TAB });
133+
wrapper.blurInput();
134+
expect(wrapper.getInputValue()).toEqual('1');
135+
outSetValue(5);
136+
wrapper.focusInput();
137+
expect(wrapper.getInputValue()).toEqual('5');
138+
});
139+
122140
describe('empty on blur should trigger null', () => {
123141
it('basic', () => {
124142
const onChange = jest.fn();

0 commit comments

Comments
 (0)