From 056e8faada7d7dbbaa5d51e20f0aa87449b9a4cb Mon Sep 17 00:00:00 2001 From: mertsincan Date: Tue, 1 Sep 2020 11:43:16 +0300 Subject: [PATCH] Fixed #455 - InputNumber with dynamic fraction digits doesn't work as expected --- src/components/inputnumber/InputNumber.vue | 62 ++++++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/src/components/inputnumber/InputNumber.vue b/src/components/inputnumber/InputNumber.vue index 2c5e96354..a32300699 100755 --- a/src/components/inputnumber/InputNumber.vue +++ b/src/components/inputnumber/InputNumber.vue @@ -119,18 +119,40 @@ export default { focused: false } }, + watch: { + locale(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + }, + localeMatcher(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + }, + mode(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + }, + currency(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + }, + currencyDisplay(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + }, + useGrouping(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + }, + minFractionDigits(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + }, + maxFractionDigits(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + }, + suffix(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + }, + prefix(newValue, oldValue) { + this.updateConstructParser(newValue, oldValue); + } + }, created() { - this.numberFormat = new Intl.NumberFormat(this.locale, this.getOptions()); - const numerals = [...new Intl.NumberFormat(this.locale, {useGrouping: false}).format(9876543210)].reverse(); - const index = new Map(numerals.map((d, i) => [d, i])); - this._numeral = new RegExp(`[${numerals.join('')}]`, 'g'); - this._decimal = this.getDecimalExpression(); - this._group = this.getGroupingExpression(); - this._minusSign = this.getMinusSignExpression(); - this._currency = this.getCurrencyExpression(); - this._suffix = new RegExp(`[${this.suffix||''}]`, 'g'); - this._prefix = new RegExp(`[${this.prefix||''}]`, 'g'); - this._index = d => index.get(d); + this.constructParser(); }, methods: { getOptions() { @@ -144,6 +166,24 @@ export default { maximumFractionDigits: this.maxFractionDigits }; }, + constructParser() { + this.numberFormat = new Intl.NumberFormat(this.locale, this.getOptions()); + const numerals = [...new Intl.NumberFormat(this.locale, {useGrouping: false}).format(9876543210)].reverse(); + const index = new Map(numerals.map((d, i) => [d, i])); + this._numeral = new RegExp(`[${numerals.join('')}]`, 'g'); + this._decimal = this.getDecimalExpression(); + this._group = this.getGroupingExpression(); + this._minusSign = this.getMinusSignExpression(); + this._currency = this.getCurrencyExpression(); + this._suffix = new RegExp(`[${this.suffix||''}]`, 'g'); + this._prefix = new RegExp(`[${this.prefix||''}]`, 'g'); + this._index = d => index.get(d); + }, + updateConstructParser(newValue, oldValue) { + if (newValue !== oldValue) { + this.constructParser(); + } + }, getDecimalExpression() { const formatter = new Intl.NumberFormat(this.locale, {useGrouping: false}); return new RegExp(`[${formatter.format(1.1).trim().replace(this._numeral, '')}]`, 'g');