From 4dbe1c96e7ab21c82c25b9e34901feafb8cd29ee Mon Sep 17 00:00:00 2001 From: Nathan Silveira Date: Tue, 16 Jul 2024 10:22:44 -0300 Subject: [PATCH] Prevent high numbers from breaking currency inputs (#49489) --- .../changelog/fix-high-number-currency-round | 5 +++++ .../product-editor/src/hooks/use-currency-input-props.ts | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 packages/js/product-editor/changelog/fix-high-number-currency-round diff --git a/packages/js/product-editor/changelog/fix-high-number-currency-round b/packages/js/product-editor/changelog/fix-high-number-currency-round new file mode 100644 index 0000000000000..710258afc0951 --- /dev/null +++ b/packages/js/product-editor/changelog/fix-high-number-currency-round @@ -0,0 +1,5 @@ +Significance: patch +Type: fix +Comment: Prevent high numbers from breaking currency inputs + + diff --git a/packages/js/product-editor/src/hooks/use-currency-input-props.ts b/packages/js/product-editor/src/hooks/use-currency-input-props.ts index d3716ed1ccc88..20647703622dd 100644 --- a/packages/js/product-editor/src/hooks/use-currency-input-props.ts +++ b/packages/js/product-editor/src/hooks/use-currency-input-props.ts @@ -27,6 +27,8 @@ type Props = { onKeyUp?: ( event: React.KeyboardEvent< HTMLInputElement > ) => void; }; +const CURRENCY_INPUT_MAX = 1_000_000_000_000_000_000.0; + export const useCurrencyInputProps = ( { value, onChange, @@ -72,7 +74,11 @@ export const useCurrencyInputProps = ( { onChange( newValue: string ) { const sanitizeValue = sanitizePrice( newValue ); if ( onChange ) { - onChange( sanitizeValue ); + onChange( + Number( sanitizeValue ) <= CURRENCY_INPUT_MAX + ? sanitizeValue + : String( CURRENCY_INPUT_MAX ) + ); } }, };