import type { ChangeEvent, FocusEvent, KeyboardEvent } from "react"; import React, { useEffect, useState } from "react"; import type { InputProps, SxProps, Theme } from "@mui/material"; import { Box, FormControl, Input, InputLabel, Typography, useTheme, } from "@mui/material"; interface CustomTextFieldProps { placeholder: string; id?: string; value?: string; error?: string; emptyError?: boolean; onChange?: (event: ChangeEvent) => void; onKeyDown?: (event: KeyboardEvent) => void; onBlur?: (event: FocusEvent) => void; text?: string; maxLength?: number; sx?: SxProps; sxForm?: SxProps; InputProps?: Partial; type?: string; rows?: number; className?: string; disabled?: boolean; } export default function CustomTextField({ placeholder, id, value = "", onChange, onKeyDown, onBlur, text, sx, error, emptyError, InputProps, maxLength = 200, type = "", rows = 0, sxForm, className, disabled, }: CustomTextFieldProps) { const theme = useTheme(); const [inputValue, setInputValue] = useState(""); const [isInputActive, setIsInputActive] = useState(false); useEffect(() => { setInputValue(value); }, [value]); const handleInputChange = (event: React.ChangeEvent) => { if (event.target.value.length <= maxLength) { const inputValue = event.target.value; if (type === "number") { setInputValue(inputValue.replace(/\D/g, "")); } else { setInputValue(inputValue); } if (onChange) { onChange(event); } } }; const handleInputFocus = () => { setIsInputActive(true); }; const handleInputBlur = (event: React.FocusEvent) => { setIsInputActive(false); if (onBlur) { onBlur(event); } }; return ( {error && ( {error} )} 0} rows={rows} disabled={disabled} disableUnderline sx={{ maxLength: maxLength, borderRadius: "10px", fontSize: "18px", lineHeight: "21px", p: "13px", border: `${isInputActive ? "black 2px" : "#9A9AAF 1px"} solid`, backgroundColor: theme.palette.background.default, height: "48px", ...sx, }} data-cy="textfield" /> {isInputActive && inputValue.length >= maxLength - 7 && ( {inputValue.length} / {maxLength} )} ); }