diff --git a/packages/dnb-eufemia/src/components/input-masked/InputMaskedUtils.js b/packages/dnb-eufemia/src/components/input-masked/InputMaskedUtils.js index 2c7bd1e551c..212ab201d76 100644 --- a/packages/dnb-eufemia/src/components/input-masked/InputMaskedUtils.js +++ b/packages/dnb-eufemia/src/components/input-masked/InputMaskedUtils.js @@ -104,8 +104,24 @@ export const correctNumberValue = ({ if (localValue !== null) { const localNumberValue = localValue.replace(/[^\d,.]/g, '') - - if (localNumberValue.startsWith('0')) { + const numberValue = value.replace(/[^\d,.]/g, '') + + /** + * If the user removes a leading digit and we have left a leading zero. + * + * The users enters these steps: + * Step 1. 1012 + * Step 2. 012 -> user removes 1, now use "localValue" + * Step 3. 2012 + * + * If a dev listens on_change and sends the number value back in, + * for this, we also ensure that "numberValue" and "localNumberValue" is the same. + */ + if ( + localNumberValue !== '0' && + localNumberValue.startsWith('0') && + parseFloat(numberValue) === parseFloat(localNumberValue) + ) { value = localValue } diff --git a/packages/dnb-eufemia/src/components/input-masked/__tests__/InputMasked.test.js b/packages/dnb-eufemia/src/components/input-masked/__tests__/InputMasked.test.js index 320b3f79ab9..397a25ed718 100644 --- a/packages/dnb-eufemia/src/components/input-masked/__tests__/InputMasked.test.js +++ b/packages/dnb-eufemia/src/components/input-masked/__tests__/InputMasked.test.js @@ -282,6 +282,23 @@ describe('InputMasked component', () => { expect(Comp.find('input').instance().value).toBe('NOK 0 012,01 kr') }) + it('should update value when value has leading zero', () => { + const Comp = mount( + + ) + + expect(Comp.find('input').instance().value).toBe('0,1') + + Comp.setProps({ value: 1234 }) + + expect(Comp.find('input').instance().value).toBe('1 234') + }) + it('should not set leading zero when entering decimal separator', () => { const onKeyDown = jest.fn() const preventDefault = jest.fn()