From f1d6696200f5f85799d79bec980572a2cdcbd486 Mon Sep 17 00:00:00 2001 From: Sudhanshu Date: Sun, 18 Sep 2022 15:40:14 +0530 Subject: [PATCH] Fixed bugs #669, #670 --- package.json | 2 +- src/number_format_base.tsx | 2 ++ src/utils.tsx | 4 ++-- test/library/input.spec.js | 28 ++++++----------------- test/library/input_numeric_format.spec.js | 7 ++++++ 5 files changed, 19 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index b4a0f175..933fd108 100755 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-number-format", "description": "React component to format number in an input or as a text.", - "version": "5.0.0-beta.4", + "version": "5.0.0-beta.5", "main": "dist/react-number-format.cjs.js", "module": "dist/react-number-format.es.js", "types": "types/index.d.ts", diff --git a/src/number_format_base.tsx b/src/number_format_base.tsx index 85c29c1c..51701510 100644 --- a/src/number_format_base.tsx +++ b/src/number_format_base.tsx @@ -259,6 +259,8 @@ export default function NumberFormatBase( setCaretPosition: true, input: event.target as HTMLInputElement, }); + + return true; }; const _onChange = (e: React.ChangeEvent) => { diff --git a/src/utils.tsx b/src/utils.tsx index c8be03f5..44da9765 100644 --- a/src/utils.tsx +++ b/src/utils.tsx @@ -161,7 +161,7 @@ export function roundToPrecision(numStr: string, scale: number, fixedDecimalScal //if number is empty don't do anything return empty string if (['', '-'].indexOf(numStr) !== -1) return numStr; - const shoudHaveDecimalSeparator = numStr.indexOf('.') !== -1 && scale; + const shouldHaveDecimalSeparator = (numStr.indexOf('.') !== -1 || fixedDecimalScale) && scale; const { beforeDecimal, afterDecimal, hasNegation } = splitDecimal(numStr); const floatValue = parseFloat(`0.${afterDecimal || '0'}`); const floatValueStr = @@ -182,7 +182,7 @@ export function roundToPrecision(numStr: string, scale: number, fixedDecimalScal const decimalPart = limitToScale(roundedDecimalParts[1] || '', scale, fixedDecimalScale); const negation = hasNegation ? '-' : ''; - const decimalSeparator = shoudHaveDecimalSeparator ? '.' : ''; + const decimalSeparator = shouldHaveDecimalSeparator ? '.' : ''; return `${negation}${intPart}${decimalSeparator}${decimalPart}`; } diff --git a/test/library/input.spec.js b/test/library/input.spec.js index 6e5db1a2..c5d570ee 100644 --- a/test/library/input.spec.js +++ b/test/library/input.spec.js @@ -25,27 +25,6 @@ describe('NumberFormat as input', () => { }); }); - // it('should accept and format custom numerals', () => { - // const wrapper = mount( - // , - // ); - // simulateKeyInput(wrapper.find('input'), '۱۲۳۴۵۶۷۸۹۰', 0); - - // expect(getInputValue(wrapper)).toEqual('1234567890'); - // }); - - // it('should accept and format custom numerals together with format input field', () => { - // const wrapper = mount( - // , - // ); - // simulateKeyInput(wrapper.find('input'), '۱۲۳۴۵۶۷۸۹۰', 0); - - // expect(getInputValue(wrapper)).toEqual('1234567890'); - // }); - it('should render input as type text by default', () => { const wrapper = mount(); expect(wrapper.find('input').instance().getAttribute('type')).toEqual('text'); @@ -553,6 +532,13 @@ describe('NumberFormat as input', () => { expect(spy).not.toHaveBeenCalled(); }); + it('should call onChange if value is changed or reset #669 ', () => { + const spy = jasmine.createSpy('onChange'); + const wrapper = mount(); + simulateKeyInput(wrapper.find('input'), 'Backspace', 1); + expect(spy).toHaveBeenCalled(); + }); + describe('Test masking', () => { it('should allow mask as string', () => { const wrapper = mount(); diff --git a/test/library/input_numeric_format.spec.js b/test/library/input_numeric_format.spec.js index dd0cae70..f86a2aad 100644 --- a/test/library/input_numeric_format.spec.js +++ b/test/library/input_numeric_format.spec.js @@ -224,6 +224,13 @@ describe('Test NumberFormat as input with numeric format options', () => { expect(getInputValue(wrapper)).toEqual('24.4568'); }); + it('should handle fixedDecimalScale correctly #670', () => { + const wrapper = mount( + , + ); + expect(getInputValue(wrapper)).toEqual('12.00'); + }); + it('should not round the initial if decimalScale is not provided', () => { const wrapper = mount(); expect(getInputValue(wrapper)).toEqual('123213.7535');