Fix event spread in PriceField (#1996)

* Fix event spread in PriceField

* Use synthetic event

Co-authored-by: Dominik Żegleń <flesz3@o2.pl>
This commit is contained in:
Jonatan Witoszek 2022-04-15 16:12:13 +02:00 committed by GitHub
parent 207ce6d031
commit 0f237ad2e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,6 @@
import { InputAdornment, TextField, TextFieldProps } from "@material-ui/core";
import { InputProps } from "@material-ui/core/Input";
import { FormChange } from "@saleor/hooks/useForm";
import { makeStyles } from "@saleor/macaw-ui";
import React, { useMemo } from "react";
import { FormattedMessage } from "react-intl";
@ -70,22 +71,27 @@ export const PriceField: React.FC<PriceFieldProps> = props => {
[currencySymbol]
);
const handleChange: React.ChangeEventHandler<HTMLInputElement> = e => {
let newValue = e.target.value;
const splitCharacter = findPriceSeparator(newValue);
const [integerPart, decimalPart] = e.target.value.split(splitCharacter);
const handleChange: FormChange = e => {
let value = e.target.value;
const splitCharacter = findPriceSeparator(value);
const [integerPart, decimalPart] = value.split(splitCharacter);
if (maxDecimalLength === 0 && decimalPart) {
// this shouldn't happen - decimal character should be ignored
newValue = integerPart;
value = integerPart;
}
if (decimalPart?.length > maxDecimalLength) {
const shortenedDecimalPart = decimalPart.slice(0, maxDecimalLength);
newValue = `${integerPart}${splitCharacter}${shortenedDecimalPart}`;
value = `${integerPart}${splitCharacter}${shortenedDecimalPart}`;
}
onChange({ ...e, target: { ...e.target, value: newValue } });
onChange({
target: {
name: e.target.name,
value
}
});
};
const handleKeyPress: TextFieldProps["onKeyDown"] = e => {