Skip to content

Commit

Permalink
- Fixed #725
Browse files Browse the repository at this point in the history
The cursor position not being correct when allowed decimal separator is typed
  • Loading branch information
s-yadav committed May 14, 2023
1 parent 477195b commit 8de6788
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,15 @@ export function getCaretPosition(
boundary: boolean[],
isValidInputCharacter: (char: string) => boolean,
) {
const changeRange = findChangeRange(curValue, newFormattedValue);
const { from, to } = changeRange;

// if only last typed character is changed in the
if (from.end - from.start === 1 && from.end === to.end && to.end === curCaretPos) {
// don't do anything
return curCaretPos;
}

/**
* if something got inserted on empty value, add the formatted character before the current value,
* This is to avoid the case where typed character is present on format characters
Expand Down
15 changes: 15 additions & 0 deletions test/library/keypress_and_caret.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ describe('Test keypress and caret position changes', () => {
expect(input.selectionStart).toEqual(2);
});

it('should handle caret position correctly when suffix starts with space and allowed decimal separator is pressed. #725', async () => {
const { input } = await render(
<NumericFormat
value={0}
decimalSeparator=","
allowedDecimalSeparators={['%', '.']}
decimalScale={2}
suffix=" €"
/>,
);

simulateNativeKeyInput(input, '.', 1, 1);
expect(input.selectionStart).toEqual(2);
});

describe('Test character insertion', () => {
it('should add any number properly when input is empty without format prop passed', async () => {
const { input } = await render(<NumericFormat thousandSeparator={true} prefix={'$'} />);
Expand Down

0 comments on commit 8de6788

Please sign in to comment.