From dc9a0c821f8381ea78e4515c84123e707eaed16d Mon Sep 17 00:00:00 2001 From: Sudhanshu Date: Sun, 3 Sep 2023 21:32:33 +0530 Subject: [PATCH] - Fix for delete of decimalSeparator happening when fixeDecimalScale is set. #789 --- src/numeric_format.tsx | 15 +++++++-------- test/library/input_numeric_format.spec.js | 10 ++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/numeric_format.tsx b/src/numeric_format.tsx index aa35cc98..6daba3ca 100644 --- a/src/numeric_format.tsx +++ b/src/numeric_format.tsx @@ -426,14 +426,13 @@ export function useNumericFormat( } // 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 diff --git a/test/library/input_numeric_format.spec.js b/test/library/input_numeric_format.spec.js index ff6278b1..3a437945 100644 --- a/test/library/input_numeric_format.spec.js +++ b/test/library/input_numeric_format.spec.js @@ -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( + , + ); + + 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();