Skip to content

Commit

Permalink
fix: windows should delay replace 。 with . (#361)
Browse files Browse the repository at this point in the history
* fix: fix 。change

* test: Fix test case

* test: fix test case
  • Loading branch information
zombieJ committed Sep 24, 2021
1 parent a1e9630 commit eb112e4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
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

1 comment on commit eb112e4

@vercel
Copy link

@vercel vercel bot commented on eb112e4 Sep 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.