From d5b6dc31cdfce87b32d7f1e106449955a94e0f25 Mon Sep 17 00:00:00 2001 From: Corey Martin Date: Thu, 16 Jan 2025 11:19:09 -0800 Subject: [PATCH] [core] Hide units by default when unit is current locale currency unit (#14909) Also start defaulting to non lowercase, non plural appended units everywhere. This should have already been the default. GitOrigin-RevId: 4f5db9dc9098b257daaf54ca14220397691cd653 --- packages/core/src/utils/currency.ts | 11 +++++++++++ packages/ui/src/components/CurrencyAmount.tsx | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/core/src/utils/currency.ts b/packages/core/src/utils/currency.ts index 37e64c71..4e9c658b 100644 --- a/packages/core/src/utils/currency.ts +++ b/packages/core/src/utils/currency.ts @@ -577,6 +577,9 @@ const defaultOptions = { export type AppendUnitsOptions = { plural?: boolean | undefined; lowercase?: boolean | undefined; + /* Default behavior for built in toLocaleString is to not show the unit when it's + the default unit in the locale (when using default currencyDisplay). We'll do the same. */ + showForCurrentLocaleUnit?: boolean | undefined; }; type FormatCurrencyStrOptions = { @@ -675,6 +678,14 @@ export function formatCurrencyStr( } if (options?.appendUnits) { + const localeCurrencyCode = localeToCurrencyCode(currentLocale); + if ( + unit === localeCurrencyCode && + !options.appendUnits.showForCurrentLocaleUnit + ) { + return formattedStr; + } + const unitStr = abbrCurrencyUnit(unit); const unitSuffix = options.appendUnits.plural && num > 1 ? "s" : ""; const unitStrWithSuffix = `${unitStr}${unitSuffix}`; diff --git a/packages/ui/src/components/CurrencyAmount.tsx b/packages/ui/src/components/CurrencyAmount.tsx index efad9500..9914ff09 100644 --- a/packages/ui/src/components/CurrencyAmount.tsx +++ b/packages/ui/src/components/CurrencyAmount.tsx @@ -65,8 +65,9 @@ export function CurrencyAmount({ ? undefined : showUnits === true ? ({ - plural: true, - lowercase: true, + plural: false, + lowercase: false, + showForCurrentLocaleUnit: false, } as const) : showUnits;