Create non negative value change handler
This commit is contained in:
parent
a10fc62819
commit
66a049b2dd
3 changed files with 15 additions and 10 deletions
|
@ -5,6 +5,7 @@ import CardTitle from "@saleor/components/CardTitle";
|
||||||
import PriceField from "@saleor/components/PriceField";
|
import PriceField from "@saleor/components/PriceField";
|
||||||
import { ProductErrorFragment } from "@saleor/fragments/types/ProductErrorFragment";
|
import { ProductErrorFragment } from "@saleor/fragments/types/ProductErrorFragment";
|
||||||
import { getFormErrors, getProductErrorMessage } from "@saleor/utils/errors";
|
import { getFormErrors, getProductErrorMessage } from "@saleor/utils/errors";
|
||||||
|
import createNonNegativeValueChangeHandler from "@saleor/utils/handlers/nonNegativeValueChangeHandler";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { useIntl } from "react-intl";
|
import { useIntl } from "react-intl";
|
||||||
|
|
||||||
|
@ -37,11 +38,7 @@ const ProductPricing: React.FC<ProductPricingProps> = props => {
|
||||||
|
|
||||||
const formErrors = getFormErrors(["basePrice"], errors);
|
const formErrors = getFormErrors(["basePrice"], errors);
|
||||||
|
|
||||||
const handlePriceChange = event => {
|
const handlePriceChange = createNonNegativeValueChangeHandler(onChange);
|
||||||
if (/^\d*(\.\d+)?$/.test(event.target.value)) {
|
|
||||||
onChange(event);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
|
|
|
@ -5,6 +5,7 @@ import CardTitle from "@saleor/components/CardTitle";
|
||||||
import PriceField from "@saleor/components/PriceField";
|
import PriceField from "@saleor/components/PriceField";
|
||||||
import { ProductErrorFragment } from "@saleor/fragments/types/ProductErrorFragment";
|
import { ProductErrorFragment } from "@saleor/fragments/types/ProductErrorFragment";
|
||||||
import { getFormErrors, getProductErrorMessage } from "@saleor/utils/errors";
|
import { getFormErrors, getProductErrorMessage } from "@saleor/utils/errors";
|
||||||
|
import createNonNegativeValueChangeHandler from "@saleor/utils/handlers/nonNegativeValueChangeHandler";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { useIntl } from "react-intl";
|
import { useIntl } from "react-intl";
|
||||||
|
|
||||||
|
@ -36,11 +37,7 @@ const ProductVariantPrice: React.FC<ProductVariantPriceProps> = props => {
|
||||||
|
|
||||||
const formErrors = getFormErrors(["price", "cost_price"], errors);
|
const formErrors = getFormErrors(["price", "cost_price"], errors);
|
||||||
|
|
||||||
const handlePriceChange = event => {
|
const handlePriceChange = createNonNegativeValueChangeHandler(onChange);
|
||||||
if (/^\d*(\.\d+)?$/.test(event.target.value)) {
|
|
||||||
onChange(event);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
|
|
11
src/utils/handlers/nonNegativeValueChangeHandler.ts
Normal file
11
src/utils/handlers/nonNegativeValueChangeHandler.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import { FormChange } from "@saleor/hooks/useForm";
|
||||||
|
|
||||||
|
function createNonNegativeValueChangeHandler(change: FormChange) {
|
||||||
|
return (event: React.ChangeEvent<any>) => {
|
||||||
|
if (/^\d*(\.\d+)?$/.test(event.target.value)) {
|
||||||
|
change(event);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default createNonNegativeValueChangeHandler;
|
Loading…
Reference in a new issue