Skip to content
Merged
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
32 changes: 20 additions & 12 deletions src/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import StepHandler from './StepHandler';
import { getNumberPrecision, num2str, validateNumber } from './utils/numberUtil';
import useCursor from './hooks/useCursor';
import useUpdateEffect from './hooks/useUpdateEffect';
import useFrame from './hooks/useFrame';

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

// ========================== User Input ==========================
const onNextPromise = useFrame();

// >>> Collect input value
const collectInputValue = (inputStr: string) => {
recordCursor();
Expand All @@ -338,6 +341,22 @@ const InputNumber = React.forwardRef(
triggerValueUpdate(finalDecimal, true);
}
}

// Trigger onInput later to let user customize value if they want do handle something after onChange
onInput?.(inputStr);

// optimize for chinese input experience
// https://github.com/ant-design/ant-design/issues/8196
onNextPromise(() => {
let nextInputStr = inputStr;
if (!parser) {
nextInputStr = inputStr.replace(/。/g, '.');
}

if (nextInputStr !== inputStr) {
collectInputValue(nextInputStr);
}
});
};

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

// >>> Input
const onInternalInput: React.ChangeEventHandler<HTMLInputElement> = (e) => {
let inputStr = e.target.value;

// optimize for chinese input experience
// https://github.com/ant-design/ant-design/issues/8196
if (!parser) {
inputStr = inputStr.replace(/。/g, '.');
}

collectInputValue(inputStr);

// Trigger onInput later to let user customize value if they want do handle something after onChange
onInput?.(inputStr);
collectInputValue(e.target.value);
};

// ============================= Step =============================
Expand Down
23 changes: 23 additions & 0 deletions src/hooks/useFrame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useRef, useEffect } from 'react';
import raf from 'rc-util/lib/raf';

/**
* Always trigger latest once when call multiple time
*/
export default () => {
const idRef = useRef(0);

const cleanUp = () => {
raf.cancel(idRef.current);
};

useEffect(() => cleanUp, []);

return (callback: () => void) => {
cleanUp();

idRef.current = raf(() => {
callback();
});
};
};
9 changes: 9 additions & 0 deletions tests/github.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('InputNumber.Github', () => {
});

afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});

Expand Down Expand Up @@ -340,6 +341,14 @@ describe('InputNumber.Github', () => {
const onChange = jest.fn();
const wrapper = mount(<InputNumber min={1} max={10} onChange={onChange} />);
wrapper.changeValue('8。1');

act(() => {
jest.runAllTimers();
wrapper.update();
});

wrapper.update();

expect(wrapper.getInputValue()).toEqual('8.1');
expect(onChange).toHaveBeenCalledWith(8.1);
});
Expand Down