2023-09-12 14:36:22 +00:00
|
|
|
import CustomTextField from "./CustomTextField";
|
|
|
|
|
|
|
|
import type { ChangeEvent, KeyboardEvent, FocusEvent } from "react";
|
|
|
|
import type { SxProps, Theme } from "@mui/material";
|
|
|
|
|
|
|
|
interface CustomNumberFieldProps {
|
|
|
|
placeholder: string;
|
|
|
|
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
|
|
|
|
onKeyDown?: (event: KeyboardEvent<HTMLInputElement>) => void;
|
|
|
|
onBlur?: (event: FocusEvent<HTMLInputElement>) => void;
|
2023-09-12 15:57:48 +00:00
|
|
|
error?: string;
|
2023-12-19 11:23:09 +00:00
|
|
|
emptyError?: boolean;
|
2023-09-12 15:23:56 +00:00
|
|
|
value: string;
|
2023-09-12 14:36:22 +00:00
|
|
|
sx?: SxProps<Theme>;
|
|
|
|
min?: number;
|
|
|
|
max?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function CustomNumberField({
|
|
|
|
placeholder,
|
2023-09-12 15:23:56 +00:00
|
|
|
value,
|
2023-09-12 14:36:22 +00:00
|
|
|
sx,
|
2023-09-12 15:57:48 +00:00
|
|
|
error,
|
2023-12-19 11:23:09 +00:00
|
|
|
emptyError,
|
2023-09-12 14:36:22 +00:00
|
|
|
onChange,
|
|
|
|
onKeyDown,
|
|
|
|
onBlur,
|
|
|
|
min = -999999999,
|
|
|
|
max = 999999999,
|
|
|
|
}: CustomNumberFieldProps) {
|
|
|
|
const onInputChange = (event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const inputValue = event.target.value;
|
|
|
|
|
|
|
|
if (
|
2023-09-13 08:15:02 +00:00
|
|
|
inputValue === "" ||
|
|
|
|
inputValue.match(/^\d*$/) ||
|
|
|
|
(inputValue[0] === "-" && inputValue.slice(1).match(/^\d*$/))
|
2023-09-12 14:36:22 +00:00
|
|
|
) {
|
2023-12-19 11:23:09 +00:00
|
|
|
console.log("inputValue", inputValue);
|
2023-09-12 14:36:22 +00:00
|
|
|
onChange?.({ ...event, target: { ...event.target, value: inputValue } });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CustomTextField
|
|
|
|
placeholder={placeholder}
|
|
|
|
sx={sx}
|
2023-09-12 15:57:48 +00:00
|
|
|
error={error}
|
2023-12-19 11:23:09 +00:00
|
|
|
emptyError={emptyError}
|
2023-09-12 14:36:22 +00:00
|
|
|
onChange={onInputChange}
|
|
|
|
onKeyDown={onKeyDown}
|
2023-12-19 11:23:09 +00:00
|
|
|
onBlur={onBlur}
|
2023-09-12 14:36:22 +00:00
|
|
|
value={value}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|