Skip to content

Commit dc9a0c8

Browse files
committed
- Fix for delete of decimalSeparator happening when fixeDecimalScale is set. #789
1 parent 424c750 commit dc9a0c8

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/numeric_format.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,13 @@ export function useNumericFormat<BaseType = InputAttributes>(
426426
}
427427

428428
// don't allow user to delete decimal separator when decimalScale and fixedDecimalScale is set
429-
if (
430-
key === 'Backspace' &&
431-
value[selectionStart - 1] === decimalSeparator &&
432-
decimalScale &&
433-
fixedDecimalScale
434-
) {
435-
setCaretPosition(el, selectionStart - 1);
436-
e.preventDefault();
429+
if (decimalScale && fixedDecimalScale) {
430+
if (key === 'Backspace' && value[selectionStart - 1] === decimalSeparator) {
431+
setCaretPosition(el, selectionStart - 1);
432+
e.preventDefault();
433+
} else if (key === 'Delete' && value[selectionStart] === decimalSeparator) {
434+
e.preventDefault();
435+
}
437436
}
438437

439438
// if user presses the allowed decimal separator before the separator, move the cursor after the separator

test/library/input_numeric_format.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,16 @@ describe('Test NumberFormat as input with numeric format options', () => {
731731
expect(input.value).toEqual('2,00');
732732
});
733733

734+
it('should not delete decimal separator if delete key is pressed before decimal separator when fixedDecimalScale is provided. #789', async () => {
735+
const { input } = await render(
736+
<NumericFormat value={'123.000'} decimalScale={3} fixedDecimalScale />,
737+
);
738+
739+
simulateNativeKeyInput(input, '{delete}', 3, 3);
740+
741+
expect(input.value).toEqual('123.000');
742+
});
743+
734744
describe('should allow typing number if prefix or suffix is just an number #691', () => {
735745
it('when prefix is number', async () => {
736746
const { input } = await render(<NumericFormat prefix="1" />);

0 commit comments

Comments
 (0)