Skip to content

Commit

Permalink
- Fixed #855.
Browse files Browse the repository at this point in the history
Handle caret position when fixedDecimalScale is used and user types after clearing input.
  • Loading branch information
s-yadav committed Sep 7, 2024
1 parent 69a5ed7 commit a163b7a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/numeric_format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ export function useNumericFormat<BaseType = InputAttributes>(
value === 0 &&
fixedDecimalScale &&
decimalScale &&
currentValue[to.start] === decimalSeparator &&
getDecimalSeparatorIndex(currentValue) < currentValueIndex &&
getDecimalSeparatorIndex(formattedValue) > formattedValueIndex
) {
Expand Down
10 changes: 10 additions & 0 deletions test/library/keypress_and_caret.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ describe('Test keypress and caret position changes', () => {
await waitFor(() => expect(input.value).toEqual('$0,00'));
});

it('should keep the caret position at proper position when fixedDecimalScale is used and user types after clearing input #855', async () => {
const { input, user } = await render(
<NumericFormat value={0} fixedDecimalScale decimalScale={2} />,
);
await simulateKeyInput(user, input, 'Backspace', 0, 4);

await simulateKeyInput(user, input, '1', 0, 0);
expect(input.selectionStart).toEqual(1);
});

describe('Test character insertion', () => {
it('should add any number properly when input is empty without format prop passed', async () => {
const { input, rerender, user } = await render(
Expand Down

0 comments on commit a163b7a

Please sign in to comment.