Skip to content

Commit

Permalink
- Fix for delete of decimalSeparator happening when fixeDecimalScale …
Browse files Browse the repository at this point in the history
…is set. #789
  • Loading branch information
s-yadav committed Sep 3, 2023
1 parent 3bdacf9 commit fd9b131
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/numeric_format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,13 @@ export function useNumericFormat<BaseType = InputAttributes>(
}

// don't allow user to delete decimal separator when decimalScale and fixedDecimalScale is set
if (
key === 'Backspace' &&
value[selectionStart - 1] === decimalSeparator &&
decimalScale &&
fixedDecimalScale
) {
setCaretPosition(el, selectionStart - 1);
e.preventDefault();
if (decimalScale && fixedDecimalScale) {
if (key === 'Backspace' && value[selectionStart - 1] === decimalSeparator) {
setCaretPosition(el, selectionStart - 1);
e.preventDefault();
} else if (key === 'Delete' && value[selectionStart] === decimalSeparator) {
e.preventDefault();
}
}

// if user presses the allowed decimal separator before the separator, move the cursor after the separator
Expand Down
10 changes: 10 additions & 0 deletions test/library/input_numeric_format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,16 @@ describe('Test NumberFormat as input with numeric format options', () => {
expect(input.value).toEqual('2,00');
});

it('should not delete decimal separator if delete key is pressed before decimal separator when fixedDecimalScale is provided. #789', async () => {
const { input } = await render(
<NumericFormat value={'123.000'} decimalScale={3} fixedDecimalScale />,
);

simulateNativeKeyInput(input, '{delete}', 3, 3);

expect(input.value).toEqual('123.000');
});

describe('should allow typing number if prefix or suffix is just an number #691', () => {
it('when prefix is number', async () => {
const { input } = await render(<NumericFormat prefix="1" />);
Expand Down

0 comments on commit fd9b131

Please sign in to comment.