Skip to content

Commit

Permalink
[core] Hide units by default when unit is current locale currency uni…
Browse files Browse the repository at this point in the history
…t (#14909)

Also start defaulting to non lowercase, non plural appended units
everywhere. This should have already been the default.

GitOrigin-RevId: 4f5db9dc9098b257daaf54ca14220397691cd653
  • Loading branch information
coreymartin authored and Lightspark Eng committed Jan 16, 2025
1 parent 0b4a802 commit d5b6dc3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/core/src/utils/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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}`;
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/src/components/CurrencyAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ export function CurrencyAmount({
? undefined
: showUnits === true
? ({
plural: true,
lowercase: true,
plural: false,
lowercase: false,
showForCurrentLocaleUnit: false,
} as const)
: showUnits;

Expand Down

0 comments on commit d5b6dc3

Please sign in to comment.