Skip to content

Commit

Permalink
fix(autoDecimalDigits): use the maximumFractionDigits of the number f…
Browse files Browse the repository at this point in the history
…ormat
  • Loading branch information
dm4t2 committed Nov 3, 2021
1 parent 2b2f8c3 commit 18cfb28
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/numberInput.ts
@@ -1,5 +1,5 @@
import { DECIMAL_SEPARATORS, NumberFormat } from './numberFormat'
import { AutoDecimalModeNumberMask, DefaultNumberMask, NumberMask } from './numberMask'
import { AutoDecimalDigitsNumberMask, DefaultNumberMask, NumberMask } from './numberMask'
import { NumberFormatStyle, NumberInputConstructorArgs, NumberInputOptions, NumberInputValue } from './api'
import { count } from './utils'

Expand Down Expand Up @@ -67,7 +67,7 @@ export class NumberInput {
this.el.setAttribute('inputmode', 'decimal')
}
this.numberFormat = new NumberFormat(this.options)
this.numberMask = this.options.autoDecimalDigits ? new AutoDecimalModeNumberMask(this.numberFormat) : new DefaultNumberMask(this.numberFormat)
this.numberMask = this.options.autoDecimalDigits ? new AutoDecimalDigitsNumberMask(this.numberFormat) : new DefaultNumberMask(this.numberFormat)
this.step = options.step && options.step > 0 ? Math.max(options.step, this.toFloat(1)) : this.toFloat(1)
this.minValue = this.getMinValue()
this.maxValue = this.getMaxValue()
Expand Down
6 changes: 3 additions & 3 deletions src/numberMask.ts
Expand Up @@ -58,7 +58,7 @@ export class DefaultNumberMask extends AbstractNumberMask implements NumberMask
}
}

export class AutoDecimalModeNumberMask extends AbstractNumberMask implements NumberMask {
export class AutoDecimalDigitsNumberMask extends AbstractNumberMask implements NumberMask {
conformToMask(str: string, previousConformedValue = ''): string | { fractionDigits: string; numberValue: number } {
if (
str === '' ||
Expand All @@ -71,10 +71,10 @@ export class AutoDecimalModeNumberMask extends AbstractNumberMask implements Num
const numberValue =
this.numberFormat.stripMinusSymbol(str) === ''
? -0
: Number(`${negative ? '-' : ''}${removeLeadingZeros(this.numberFormat.onlyDigits(str))}`) / Math.pow(10, this.numberFormat.minimumFractionDigits)
: Number(`${negative ? '-' : ''}${removeLeadingZeros(this.numberFormat.onlyDigits(str))}`) / Math.pow(10, this.numberFormat.maximumFractionDigits)
return {
numberValue,
fractionDigits: numberValue.toFixed(this.numberFormat.minimumFractionDigits).slice(-this.numberFormat.minimumFractionDigits)
fractionDigits: numberValue.toFixed(this.numberFormat.maximumFractionDigits).slice(-this.numberFormat.maximumFractionDigits)
}
}
}
12 changes: 12 additions & 0 deletions tests/unit/numberMask.spec.ts
@@ -0,0 +1,12 @@
import { AutoDecimalDigitsNumberMask } from '../../src/numberMask'
import { NumberFormat } from '../../src/numberFormat'
import { NumberFormatStyle } from '../../src'

describe('AutoDecimalDigitsNumberMask', () => {
it('should use the maximumFractionDigits of the number format', () => {
const numberFormat = new NumberFormat({ formatStyle: NumberFormatStyle.Decimal, locale: 'en' })
const autoDecimalDigitsNumberMask = new AutoDecimalDigitsNumberMask(numberFormat)

expect(autoDecimalDigitsNumberMask.conformToMask('1000')).toEqual({ fractionDigits: '000', numberValue: 1 })
})
})

0 comments on commit 18cfb28

Please sign in to comment.