Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 fix(bal-number-input): German format with decimals #1401

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-avocados-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baloise/ds-core': patch
---

**number-input**: handle inputs for Germany
26 changes: 26 additions & 0 deletions e2e/cypress/component/bal-number-input.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,30 @@ describe('bal-number-input', () => {
cy.get('@balFocus').should('have.been.calledTwice')
cy.get('@balBlur').should('have.been.calledTwice')
})

it('should update german numbers with decimal', () => {
cy.window().then((win: any) => (win.BaloiseDesignSystem.config.region = 'DE'))
cy.get('bal-number-input').waitForComponents().invoke('attr', 'decimal', 2)

cy.get('bal-number-input').find('input').type('1').blur()

cy.get('bal-number-input').find('input').should('have.value', '1,00')

cy.get('bal-number-input').find('input').click().blur()
cy.get('bal-number-input').find('input').should('have.value', '1,00')
cy.get('@balChange').should('have.been.calledOnce')
})

it('should update german numbers with thousand seperator', () => {
cy.window().then((win: any) => (win.BaloiseDesignSystem.config.region = 'DE'))
cy.get('bal-number-input').waitForComponents().invoke('attr', 'decimal', 2)

cy.get('bal-number-input').find('input').type('1000,42').blur()

cy.get('bal-number-input').find('input').should('have.value', '1.000,42')

cy.get('bal-number-input').find('input').click().blur()
cy.get('bal-number-input').find('input').should('have.value', '1.000,42')
cy.get('@balChange').should('have.been.calledOnce')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { ACTION_KEYS, NUMBER_KEYS } from '../../utils/constants/keys.constant'
import { formatLocaleNumber, getDecimalSeparator, getNegativeSymbol, getThousandSeparator } from '../../utils/number'
import isNaN from 'lodash.isnan'

function checkIfValueIsStringAndDoesNotHaveGermanFormat(val: any): boolean {
return typeof val === 'string' && getThousandSeparator() !== '.'
}

export function isNumber(value: any): boolean {
const num = parseFloat(value)
return typeof num === 'number' && !isNaN(num)
Expand All @@ -26,7 +30,7 @@ export function toNumber(value: any, decimalPoints = 0): number | undefined {
return undefined
}

if (typeof val === 'string') {
if (checkIfValueIsStringAndDoesNotHaveGermanFormat(val)) {
val = val.split(getThousandSeparator()).join('').split('`').join('').split("'").join('')
hirsch88 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -39,7 +43,7 @@ export function toFixedNumber(value: string, decimalPoints = 0): string {
return ''
}

if (typeof val === 'string') {
if (checkIfValueIsStringAndDoesNotHaveGermanFormat(val)) {
val = val.split(getThousandSeparator()).join('').split('`').join('').split("'").join('')
hirsch88 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down