2023-12-18 15:04:09 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { Box, FormControl, TextField, Typography, useTheme } 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>;
|
2023-10-12 15:51:39 +00:00
|
|
|
InputProps?: Partial<InputProps>;
|
2023-03-10 01:38:31 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 09:56:15 +00:00
|
|
|
export default function CustomTextField({
|
|
|
|
placeholder,
|
2023-09-12 14:36:22 +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-09-12 09:56:15 +00:00
|
|
|
}: CustomTextFieldProps) {
|
|
|
|
const theme = useTheme();
|
2023-04-11 18:29:38 +00:00
|
|
|
|
2023-12-19 20:51:13 +00:00
|
|
|
const [inputValue, setInputValue] = useState(value || text || "");
|
2023-12-18 15:04:09 +00:00
|
|
|
const [isInputActive, setIsInputActive] = useState(false);
|
|
|
|
|
|
|
|
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const inputValue = event.target.value;
|
|
|
|
setInputValue(inputValue);
|
|
|
|
|
|
|
|
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 (
|
|
|
|
<FormControl fullWidth variant="standard" sx={{ p: 0 }}>
|
|
|
|
<TextField
|
|
|
|
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-09-12 15:57:48 +00:00
|
|
|
label={error}
|
2023-12-18 15:04:09 +00:00
|
|
|
onFocus={handleInputFocus}
|
|
|
|
onBlur={handleInputBlur}
|
2023-09-12 09:56:15 +00:00
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
sx={{
|
|
|
|
"& .MuiInputBase-root": {
|
|
|
|
backgroundColor: theme.palette.background.default,
|
|
|
|
height: "48px",
|
|
|
|
borderRadius: "10px",
|
|
|
|
},
|
2023-09-12 15:57:48 +00:00
|
|
|
"& .MuiInputLabel-root": {
|
|
|
|
fontSize: "13.5px",
|
|
|
|
marginTop: "3px",
|
|
|
|
},
|
2023-09-12 09:56:15 +00:00
|
|
|
}}
|
2023-10-12 15:51:39 +00:00
|
|
|
InputProps={InputProps}
|
2023-09-12 09:56:15 +00:00
|
|
|
inputProps={{
|
2023-12-18 15:04:09 +00:00
|
|
|
maxLength: maxLength,
|
2023-09-12 09:56:15 +00:00
|
|
|
sx: {
|
|
|
|
borderRadius: "10px",
|
|
|
|
fontSize: "18px",
|
|
|
|
lineHeight: "21px",
|
|
|
|
py: 0,
|
|
|
|
},
|
2023-12-18 15:04:09 +00:00
|
|
|
...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>
|
|
|
|
);
|
|
|
|
}
|