Skip to content

Commit

Permalink
fix(InputMasked): change value when leading zero is 0 initially
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Nov 1, 2021
1 parent b3c4534 commit 04cca2e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Component
value={0.1}
number_mask={{
allowDecimal: true,
}}
/>
)

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()
Expand Down

0 comments on commit 04cca2e

Please sign in to comment.