From 9009cf15e52d0e1cf18ebaaf3316fa9b80d781c6 Mon Sep 17 00:00:00 2001 From: roykho Date: Mon, 30 Jan 2023 07:14:41 -0800 Subject: [PATCH 1/2] Check if object exists otherwise return default --- assets/js/base/components/formatted-monetary-amount/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/base/components/formatted-monetary-amount/index.tsx b/assets/js/base/components/formatted-monetary-amount/index.tsx index f13aa03da01..2c8953f9a95 100644 --- a/assets/js/base/components/formatted-monetary-amount/index.tsx +++ b/assets/js/base/components/formatted-monetary-amount/index.tsx @@ -72,7 +72,7 @@ const FormattedMonetaryAmount = ( { return null; } - const priceValue = value / 10 ** currency.minorUnit; + const priceValue = value / 10 ** ( currency?.minorUnit || 2 ); if ( ! Number.isFinite( priceValue ) ) { return null; From 1161d76c3ea094d671b6a535e4e358739016f687 Mon Sep 17 00:00:00 2001 From: roykho Date: Fri, 3 Feb 2023 06:04:45 -0800 Subject: [PATCH 2/2] Set default value for Storybook currency control --- .../js/base/components/formatted-monetary-amount/index.tsx | 7 ++----- storybook/custom-controls/currency.ts | 1 + 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/assets/js/base/components/formatted-monetary-amount/index.tsx b/assets/js/base/components/formatted-monetary-amount/index.tsx index 39cf2677790..f13aa03da01 100644 --- a/assets/js/base/components/formatted-monetary-amount/index.tsx +++ b/assets/js/base/components/formatted-monetary-amount/index.tsx @@ -72,10 +72,7 @@ const FormattedMonetaryAmount = ( { return null; } - const minorUnit = - typeof currency?.minorUnit === 'undefined' ? 2 : currency.minorUnit; - - const priceValue = value / 10 ** minorUnit; + const priceValue = value / 10 ** currency.minorUnit; if ( ! Number.isFinite( priceValue ) ) { return null; @@ -97,7 +94,7 @@ const FormattedMonetaryAmount = ( { // Wrapper for NumberFormat onValueChange which handles subunit conversion. const onValueChangeWrapper = onValueChange ? ( values: NumberFormatValues ) => { - const minorUnitValue = +values.value * 10 ** minorUnit; + const minorUnitValue = +values.value * 10 ** currency.minorUnit; onValueChange( minorUnitValue ); } : () => void 0; diff --git a/storybook/custom-controls/currency.ts b/storybook/custom-controls/currency.ts index 2c1d39e8f1f..f012121791a 100644 --- a/storybook/custom-controls/currency.ts +++ b/storybook/custom-controls/currency.ts @@ -37,4 +37,5 @@ export const currencyControl = { control: 'select', options: currencies, mapping: Object.keys( currencies ), + defaultValue: currencies.USD, };