Skip to content

Commit

Permalink
Fix primefaces#4291: InputNumber respect PrimeReact.locale
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed May 10, 2023
1 parent 3fdccf0 commit 35ef301
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions components/lib/inputnumber/InputNumber.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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';
Expand All @@ -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');
Expand All @@ -69,28 +71,28 @@ 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);

return new RegExp(`[${groupChar.current}]`, 'g');
};

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,
Expand All @@ -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];
}
Expand All @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit 35ef301

Please sign in to comment.