s-yadav / react-number-format

React component to format numbers in an input or as a text.
MIT License
3.87k stars 409 forks source link

The caret position isn't set properly when I use the `removeFormatting` function to use the Persian numbers with the `thousandSeparator` prop #834

Open hamiidrahmani opened 6 months ago

hamiidrahmani commented 6 months ago

Describe the issue and the actual behavior

The caret position isn't set properly when I use the removeFormatting function to use the Persian numbers with the thousandSeparator prop, It should be fixed at the last item of input when I'm writing.

Describe the expected behavior

I think it should be considered to detect automatically where the caret position is when I'm typing.

Provide a CodeSandbox link illustrating the issue

https://codesandbox.io/p/sandbox/custom-numeral-numer-format-forked-qsy7ft?file=%2Fsrc%2FApp.js%3A12%2C31 Also, I provide a video to give a good understanding:

https://github.com/s-yadav/react-number-format/assets/46620517/e1e309f0-0b40-40c6-8cba-55c265a28190

Provide steps to reproduce this issue

Please check the browsers where the issue is seen

hamiidrahmani commented 6 months ago

I traced back the issue, It seems that we need to modify the isCharacterSame function, because currentValue and formattedValue differ in the last character position for Persian input.

const _isCharacterSame: IsCharacterSame = (ref) => {
    ref.currentValue = persianToEnglishNum(ref.currentValue);
    return isCharacterSame!(ref);
  };
Melancholism commented 3 months ago

@hamiidrahmani Could you please share the complete code.

hamiidrahmani commented 3 months ago

@hamiidrahmani Could you please share the complete code.

Only pass the function to NumberFormatBase, something like this:

const {  isCharacterSame, ...numericFormatProps } = useNumericFormat();

const _isCharacterSame: IsCharacterSame = (ref) => {
    ref.currentValue = persianToEnglishNum(ref.currentValue);
    return isCharacterSame!(ref);
  };

<ReactNumberFormatBase
      {...numericFormatProps}
      isCharacterSame={_isCharacterSame}
    />