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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4291: InputNumber respect PrimeReact.locale #4297

Merged
merged 2 commits into from
Jun 13, 2023
Merged
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
21 changes: 11 additions & 10 deletions components/lib/inputnumber/InputNumber.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { PrimeReactContext } from '../api/context';
import { useMountEffect, useUpdateEffect } from '../hooks/Hooks';
import { AngleDownIcon } from '../icons/angledown';
import { AngleUpIcon } from '../icons/angleup';
Expand All @@ -7,7 +8,6 @@ import { Ripple } from '../ripple/Ripple';
import { Tooltip } from '../tooltip/Tooltip';
import { DomHandler, IconUtils, ObjectUtils, classNames, mergeProps } from '../utils/Utils';
import { InputNumberBase } from './InputNumberBase';
import { PrimeReactContext } from '../api/context';

export const InputNumber = React.memo(
React.forwardRef((inProps, ref) => {
Expand Down Expand Up @@ -39,6 +39,7 @@ export const InputNumber = React.memo(
const _prefix = React.useRef(null);
const _index = React.useRef(null);

const _locale = props.locale || context.locale;
const stacked = props.showButtons && props.buttonLayout === 'stacked';
const horizontal = props.showButtons && props.buttonLayout === 'horizontal';
const vertical = props.showButtons && props.buttonLayout === 'vertical';
Expand All @@ -57,8 +58,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 @@ -76,28 +77,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 @@ -115,7 +116,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 @@ -127,7 +128,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 @@ -149,7 +150,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