2023-12-25 12:02:35 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
FormControl,
|
|
|
|
TextField,
|
|
|
|
Typography,
|
|
|
|
useTheme,
|
2024-01-02 01:09:52 +00:00
|
|
|
Input,
|
2024-01-02 01:31:47 +00:00
|
|
|
InputLabel,
|
2023-12-25 12:02:35 +00:00
|
|
|
} from "@mui/material";
|
2023-09-12 09:56:15 +00:00
|
|
|
import type { ChangeEvent, KeyboardEvent, FocusEvent } from "react";
|
2023-12-18 15:04:09 +00:00
|
|
|
import type { InputProps, SxProps, Theme } from "@mui/material";
|
2023-03-10 01:38:31 +00:00
|
|
|
|
|
|
|
interface CustomTextFieldProps {
|
2023-09-12 09:56:15 +00:00
|
|
|
placeholder: string;
|
2023-09-12 14:36:22 +00:00
|
|
|
value?: string;
|
2023-09-12 15:57:48 +00:00
|
|
|
error?: string;
|
2023-12-19 11:23:09 +00:00
|
|
|
emptyError?: boolean;
|
2023-09-12 09:56:15 +00:00
|
|
|
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
|
|
|
|
onKeyDown?: (event: KeyboardEvent<HTMLInputElement>) => void;
|
|
|
|
onBlur?: (event: FocusEvent<HTMLInputElement>) => void;
|
|
|
|
text?: string;
|
2023-12-18 15:04:09 +00:00
|
|
|
maxLength?: number;
|
2023-09-12 09:56:15 +00:00
|
|
|
sx?: SxProps<Theme>;
|
2024-01-02 01:09:52 +00:00
|
|
|
sxForm?: SxProps<Theme>;
|
2023-10-12 15:51:39 +00:00
|
|
|
InputProps?: Partial<InputProps>;
|
2023-12-31 02:53:25 +00:00
|
|
|
type?: string;
|
2024-01-02 01:09:52 +00:00
|
|
|
rows?: number;
|
2024-01-09 11:36:00 +00:00
|
|
|
className?: string;
|
2023-03-10 01:38:31 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 09:56:15 +00:00
|
|
|
export default function CustomTextField({
|
|
|
|
placeholder,
|
2023-12-25 12:02:35 +00:00
|
|
|
value = "",
|
2023-09-12 09:56:15 +00:00
|
|
|
onChange,
|
|
|
|
onKeyDown,
|
|
|
|
onBlur,
|
2023-12-18 15:04:09 +00:00
|
|
|
text,
|
|
|
|
sx,
|
|
|
|
error,
|
2023-12-19 11:23:09 +00:00
|
|
|
emptyError,
|
2023-10-12 15:51:39 +00:00
|
|
|
InputProps,
|
2023-12-18 15:04:09 +00:00
|
|
|
maxLength = 200,
|
2023-12-31 02:53:25 +00:00
|
|
|
type = "",
|
2024-01-02 01:09:52 +00:00
|
|
|
rows = 0,
|
2024-01-02 01:31:47 +00:00
|
|
|
sxForm,
|
2024-01-09 11:36:00 +00:00
|
|
|
className,
|
2023-09-12 09:56:15 +00:00
|
|
|
}: CustomTextFieldProps) {
|
|
|
|
const theme = useTheme();
|
2023-04-11 18:29:38 +00:00
|
|
|
|
2023-12-25 12:02:35 +00:00
|
|
|
const [inputValue, setInputValue] = useState("");
|
2023-12-18 15:04:09 +00:00
|
|
|
const [isInputActive, setIsInputActive] = useState(false);
|
|
|
|
|
2023-12-25 12:02:35 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setInputValue(value);
|
|
|
|
}, [value]);
|
|
|
|
|
2023-12-18 15:04:09 +00:00
|
|
|
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const inputValue = event.target.value;
|
2023-12-28 20:08:41 +00:00
|
|
|
|
2023-12-31 02:53:25 +00:00
|
|
|
if (type === "number") {
|
|
|
|
setInputValue(inputValue.replace(/\D/g, ""));
|
2023-12-28 20:08:41 +00:00
|
|
|
} else {
|
|
|
|
setInputValue(inputValue);
|
|
|
|
}
|
|
|
|
|
2023-12-18 15:04:09 +00:00
|
|
|
if (onChange) {
|
|
|
|
onChange(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleInputFocus = () => {
|
|
|
|
setIsInputActive(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleInputBlur = (event: React.FocusEvent<HTMLInputElement>) => {
|
|
|
|
setIsInputActive(false);
|
|
|
|
|
|
|
|
if (onBlur) {
|
|
|
|
onBlur(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-12 09:56:15 +00:00
|
|
|
return (
|
2024-01-09 11:36:00 +00:00
|
|
|
<FormControl
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
sx={{ p: 0, ...sxForm }}
|
|
|
|
className={className || ""}
|
|
|
|
>
|
2024-01-02 01:31:47 +00:00
|
|
|
{error && (
|
|
|
|
<InputLabel
|
|
|
|
sx={{
|
2024-01-02 01:09:52 +00:00
|
|
|
fontSize: "13.5px",
|
|
|
|
marginTop: "3px",
|
2024-01-02 01:31:47 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{error}
|
|
|
|
</InputLabel>
|
|
|
|
)}
|
2024-01-02 01:09:52 +00:00
|
|
|
<Input
|
2023-09-12 09:56:15 +00:00
|
|
|
defaultValue={text}
|
|
|
|
fullWidth
|
2023-12-18 15:04:09 +00:00
|
|
|
value={inputValue}
|
2023-09-12 09:56:15 +00:00
|
|
|
placeholder={placeholder}
|
2023-12-18 15:04:09 +00:00
|
|
|
onChange={handleInputChange}
|
2023-12-19 11:23:09 +00:00
|
|
|
error={!!error || emptyError}
|
2023-12-18 15:04:09 +00:00
|
|
|
onFocus={handleInputFocus}
|
|
|
|
onBlur={handleInputBlur}
|
2023-09-12 09:56:15 +00:00
|
|
|
onKeyDown={onKeyDown}
|
2024-01-02 01:09:52 +00:00
|
|
|
multiline={rows > 0}
|
|
|
|
rows={rows}
|
|
|
|
disableUnderline
|
2023-09-12 09:56:15 +00:00
|
|
|
sx={{
|
2023-12-18 15:04:09 +00:00
|
|
|
maxLength: maxLength,
|
2024-01-02 01:31:47 +00:00
|
|
|
borderRadius: "10px",
|
|
|
|
fontSize: "18px",
|
|
|
|
lineHeight: "21px",
|
|
|
|
p: "13px",
|
|
|
|
border: `${isInputActive ? "black 2px" : "#9A9AAF 1px"} solid`,
|
|
|
|
backgroundColor: theme.palette.background.default,
|
|
|
|
height: "48px",
|
|
|
|
...sx,
|
2023-09-12 09:56:15 +00:00
|
|
|
}}
|
2023-11-06 14:41:50 +00:00
|
|
|
data-cy="textfield"
|
2023-09-12 09:56:15 +00:00
|
|
|
/>
|
2023-12-18 15:04:09 +00:00
|
|
|
{isInputActive && inputValue.length >= maxLength - 7 && (
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
marginTop: "5px",
|
|
|
|
marginLeft: "auto",
|
|
|
|
position: "absolute",
|
|
|
|
bottom: "-25px",
|
|
|
|
right: "0",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Typography fontSize="14px">{inputValue.length}</Typography>
|
|
|
|
<span>/</span>
|
|
|
|
<Typography fontSize="14px">{maxLength}</Typography>
|
|
|
|
</Box>
|
|
|
|
)}
|
2023-09-12 09:56:15 +00:00
|
|
|
</FormControl>
|
|
|
|
);
|
|
|
|
}
|