Skip to content

Commit dcc1bb7

Browse files
authored
fix: Safari of input IME (#512)
1 parent fdb1cf5 commit dcc1bb7

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
},
4545
"devDependencies": {
4646
"@testing-library/jest-dom": "^5.16.4",
47-
"@testing-library/react": "13.3.0",
47+
"@testing-library/react": "^13.3.0",
4848
"@types/classnames": "^2.2.9",
4949
"@types/enzyme": "^3.10.8",
5050
"@types/jest": "^26.0.0",

src/InputNumber.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ const InputNumber = React.forwardRef(
444444
}
445445
};
446446

447+
// Solve the issue of the event triggering sequence when entering numbers in chinese input (Safari)
448+
const onBeforeInput = () => {
449+
userTypingRef.current = true;
450+
};
451+
447452
const onKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (event) => {
448453
const { which, shiftKey } = event;
449454
userTypingRef.current = true;
@@ -536,6 +541,7 @@ const InputNumber = React.forwardRef(
536541
onKeyUp={onKeyUp}
537542
onCompositionStart={onCompositionStart}
538543
onCompositionEnd={onCompositionEnd}
544+
onBeforeInput={onBeforeInput}
539545
>
540546
{controls && (
541547
<StepHandler

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import InputNumber, { InputNumberProps } from './InputNumber';
22

3-
export { InputNumberProps };
3+
export type { InputNumberProps };
44

55
export default InputNumber;

tests/precision.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import '@testing-library/jest-dom';
3+
import { render, fireEvent } from '@testing-library/react';
4+
import KeyCode from 'rc-util/lib/KeyCode';
5+
import InputNumber from '../src';
6+
7+
describe('InputNumber.Precision', () => {
8+
// https://github.com/react-component/input-number/issues/506
9+
it('Safari bug of input', async () => {
10+
const Demo = () => {
11+
const [value, setValue] = React.useState<string | number>(null);
12+
13+
return <InputNumber precision={2} value={value} onChange={setValue} />;
14+
};
15+
16+
const { container } = render(<Demo />);
17+
const input = container.querySelector('input');
18+
19+
// React use SyntheticEvent to handle `onBeforeInput`, let's mock this
20+
fireEvent.keyPress(input, {
21+
which: KeyCode.TWO,
22+
keyCode: KeyCode.TWO,
23+
char: '2',
24+
});
25+
26+
fireEvent.change(input, {
27+
target: {
28+
value: '2',
29+
},
30+
});
31+
32+
expect(input.value).toEqual('2');
33+
});
34+
});

0 commit comments

Comments
 (0)