Skip to content

Commit eb112e4

Browse files
authored
fix: windows should delay replace 。 with . (#361)
* fix: fix 。change * test: Fix test case * test: fix test case
1 parent a1e9630 commit eb112e4

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

src/InputNumber.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import StepHandler from './StepHandler';
77
import { getNumberPrecision, num2str, validateNumber } from './utils/numberUtil';
88
import useCursor from './hooks/useCursor';
99
import useUpdateEffect from './hooks/useUpdateEffect';
10+
import useFrame from './hooks/useFrame';
1011

1112
/**
1213
* We support `stringMode` which need handle correct type when user call in onChange
@@ -323,6 +324,8 @@ const InputNumber = React.forwardRef(
323324
};
324325

325326
// ========================== User Input ==========================
327+
const onNextPromise = useFrame();
328+
326329
// >>> Collect input value
327330
const collectInputValue = (inputStr: string) => {
328331
recordCursor();
@@ -338,6 +341,22 @@ const InputNumber = React.forwardRef(
338341
triggerValueUpdate(finalDecimal, true);
339342
}
340343
}
344+
345+
// Trigger onInput later to let user customize value if they want do handle something after onChange
346+
onInput?.(inputStr);
347+
348+
// optimize for chinese input experience
349+
// https://github.com/ant-design/ant-design/issues/8196
350+
onNextPromise(() => {
351+
let nextInputStr = inputStr;
352+
if (!parser) {
353+
nextInputStr = inputStr.replace(//g, '.');
354+
}
355+
356+
if (nextInputStr !== inputStr) {
357+
collectInputValue(nextInputStr);
358+
}
359+
});
341360
};
342361

343362
// >>> Composition
@@ -353,18 +372,7 @@ const InputNumber = React.forwardRef(
353372

354373
// >>> Input
355374
const onInternalInput: React.ChangeEventHandler<HTMLInputElement> = (e) => {
356-
let inputStr = e.target.value;
357-
358-
// optimize for chinese input experience
359-
// https://github.com/ant-design/ant-design/issues/8196
360-
if (!parser) {
361-
inputStr = inputStr.replace(//g, '.');
362-
}
363-
364-
collectInputValue(inputStr);
365-
366-
// Trigger onInput later to let user customize value if they want do handle something after onChange
367-
onInput?.(inputStr);
375+
collectInputValue(e.target.value);
368376
};
369377

370378
// ============================= Step =============================

src/hooks/useFrame.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useRef, useEffect } from 'react';
2+
import raf from 'rc-util/lib/raf';
3+
4+
/**
5+
* Always trigger latest once when call multiple time
6+
*/
7+
export default () => {
8+
const idRef = useRef(0);
9+
10+
const cleanUp = () => {
11+
raf.cancel(idRef.current);
12+
};
13+
14+
useEffect(() => cleanUp, []);
15+
16+
return (callback: () => void) => {
17+
cleanUp();
18+
19+
idRef.current = raf(() => {
20+
callback();
21+
});
22+
};
23+
};

tests/github.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('InputNumber.Github', () => {
1111
});
1212

1313
afterEach(() => {
14+
jest.clearAllTimers();
1415
jest.useRealTimers();
1516
});
1617

@@ -340,6 +341,14 @@ describe('InputNumber.Github', () => {
340341
const onChange = jest.fn();
341342
const wrapper = mount(<InputNumber min={1} max={10} onChange={onChange} />);
342343
wrapper.changeValue('8。1');
344+
345+
act(() => {
346+
jest.runAllTimers();
347+
wrapper.update();
348+
});
349+
350+
wrapper.update();
351+
343352
expect(wrapper.getInputValue()).toEqual('8.1');
344353
expect(onChange).toHaveBeenCalledWith(8.1);
345354
});

0 commit comments

Comments
 (0)