From 5d1361f352c21ed8e1cb86414722e096a9f7f490 Mon Sep 17 00:00:00 2001 From: Dawid Tarasiuk Date: Mon, 5 Oct 2020 18:19:07 +0200 Subject: [PATCH] Prevent negative price setting in input --- .../ProductPricing/ProductPricing.tsx | 8 +++++++- .../ProductVariantPrice.tsx | 20 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/products/components/ProductPricing/ProductPricing.tsx b/src/products/components/ProductPricing/ProductPricing.tsx index 153ec3fd2..0c53611db 100644 --- a/src/products/components/ProductPricing/ProductPricing.tsx +++ b/src/products/components/ProductPricing/ProductPricing.tsx @@ -37,6 +37,12 @@ const ProductPricing: React.FC = props => { const formErrors = getFormErrors(["basePrice"], errors); + const handlePriceChange = event => { + if (/^\d*(\.\d+)?$/.test(event.target.value)) { + onChange(event); + } + }; + return ( = props => { name="basePrice" value={data.basePrice} currencySymbol={currency} - onChange={onChange} + onChange={handlePriceChange} InputProps={{ inputProps: { min: 0 diff --git a/src/products/components/ProductVariantPrice/ProductVariantPrice.tsx b/src/products/components/ProductVariantPrice/ProductVariantPrice.tsx index d39a927b0..d8092219c 100644 --- a/src/products/components/ProductVariantPrice/ProductVariantPrice.tsx +++ b/src/products/components/ProductVariantPrice/ProductVariantPrice.tsx @@ -36,6 +36,12 @@ const ProductVariantPrice: React.FC = props => { const formErrors = getFormErrors(["price", "cost_price"], errors); + const handlePriceChange = event => { + if (/^\d*(\.\d+)?$/.test(event.target.value)) { + onChange(event); + } + }; + return ( = props => { })} value={price} currencySymbol={currencySymbol} - onChange={onChange} + onChange={handlePriceChange} disabled={loading} + InputProps={{ + inputProps: { + min: "0" + } + }} />
@@ -76,8 +87,13 @@ const ProductVariantPrice: React.FC = props => { } value={costPrice} currencySymbol={currencySymbol} - onChange={onChange} + onChange={handlePriceChange} disabled={loading} + InputProps={{ + inputProps: { + min: "0" + } + }} />