From ff0e79b608a0470a92db5110b68219b472e1a85e Mon Sep 17 00:00:00 2001 From: melloware Date: Thu, 27 Apr 2023 13:39:04 -0400 Subject: [PATCH] Fix #4291: InputNumber respect PrimeReact.locale --- components/lib/inputnumber/InputNumber.js | 26 ++++++++++++----------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/components/lib/inputnumber/InputNumber.js b/components/lib/inputnumber/InputNumber.js index 1d351df275..e1d625cc82 100644 --- a/components/lib/inputnumber/InputNumber.js +++ b/components/lib/inputnumber/InputNumber.js @@ -1,12 +1,13 @@ import * as React from 'react'; +import PrimeReact from '../api/Api'; import { useMountEffect, useUpdateEffect } from '../hooks/Hooks'; +import { AngleDownIcon } from '../icons/angledown'; +import { AngleUpIcon } from '../icons/angleup'; import { InputText } from '../inputtext/InputText'; import { Ripple } from '../ripple/Ripple'; import { Tooltip } from '../tooltip/Tooltip'; -import { classNames, DomHandler, ObjectUtils, IconUtils } from '../utils/Utils'; +import { DomHandler, IconUtils, ObjectUtils, classNames } from '../utils/Utils'; import { InputNumberBase } from './InputNumberBase'; -import { AngleUpIcon } from '../icons/angleup'; -import { AngleDownIcon } from '../icons/angledown'; export const InputNumber = React.memo( React.forwardRef((inProps, ref) => { @@ -32,6 +33,7 @@ export const InputNumber = React.memo( const _prefix = React.useRef(null); const _index = React.useRef(null); + const _locale = props.locale || PrimeReact.locale; const stacked = props.showButtons && props.buttonLayout === 'stacked'; const horizontal = props.showButtons && props.buttonLayout === 'horizontal'; const vertical = props.showButtons && props.buttonLayout === 'vertical'; @@ -50,8 +52,8 @@ export const InputNumber = React.memo( }; const constructParser = () => { - numberFormat.current = new Intl.NumberFormat(props.locale, getOptions()); - const numerals = [...new Intl.NumberFormat(props.locale, { useGrouping: false }).format(9876543210)].reverse(); + numberFormat.current = new Intl.NumberFormat(_locale, getOptions()); + const numerals = [...new Intl.NumberFormat(_locale, { useGrouping: false }).format(9876543210)].reverse(); const index = new Map(numerals.map((d, i) => [d, i])); _numeral.current = new RegExp(`[${numerals.join('')}]`, 'g'); @@ -69,13 +71,13 @@ export const InputNumber = React.memo( }; const getDecimalExpression = () => { - const formatter = new Intl.NumberFormat(props.locale, { ...getOptions(), useGrouping: false }); + const formatter = new Intl.NumberFormat(_locale, { ...getOptions(), useGrouping: false }); return new RegExp(`[${formatter.format(1.1).replace(_currency.current, '').trim().replace(_numeral.current, '')}]`, 'g'); }; const getGroupingExpression = () => { - const formatter = new Intl.NumberFormat(props.locale, { useGrouping: true }); + const formatter = new Intl.NumberFormat(_locale, { useGrouping: true }); groupChar.current = formatter.format(1000000).trim().replace(_numeral.current, '').charAt(0); @@ -83,14 +85,14 @@ export const InputNumber = React.memo( }; const getMinusSignExpression = () => { - const formatter = new Intl.NumberFormat(props.locale, { useGrouping: false }); + const formatter = new Intl.NumberFormat(_locale, { useGrouping: false }); return new RegExp(`[${formatter.format(-1).trim().replace(_numeral.current, '')}]`, 'g'); }; const getCurrencyExpression = () => { if (props.currency) { - const formatter = new Intl.NumberFormat(props.locale, { + const formatter = new Intl.NumberFormat(_locale, { style: 'currency', currency: props.currency, currencyDisplay: props.currencyDisplay, @@ -108,7 +110,7 @@ export const InputNumber = React.memo( if (props.prefix) { prefixChar.current = props.prefix; } else { - const formatter = new Intl.NumberFormat(props.locale, { style: props.mode, currency: props.currency, currencyDisplay: props.currencyDisplay }); + const formatter = new Intl.NumberFormat(_locale, { style: props.mode, currency: props.currency, currencyDisplay: props.currencyDisplay }); prefixChar.current = formatter.format(1).split('1')[0]; } @@ -120,7 +122,7 @@ export const InputNumber = React.memo( if (props.suffix) { suffixChar.current = props.suffix; } else { - const formatter = new Intl.NumberFormat(props.locale, { + const formatter = new Intl.NumberFormat(_locale, { style: props.mode, currency: props.currency, currencyDisplay: props.currencyDisplay, @@ -142,7 +144,7 @@ export const InputNumber = React.memo( } if (props.format) { - let formatter = new Intl.NumberFormat(props.locale, getOptions()); + let formatter = new Intl.NumberFormat(_locale, getOptions()); let _formattedValue = formatter.format(value); if (props.prefix) {