import { forwardRef, useImperativeHandle, useRef, useState, useEffect } from "react"; import { motion } from "motion/react"; import styles from "./auth.module.scss"; export interface AuthTextFieldHandle { value: string; focus: () => void; blur: () => void; } export interface AuthTextFieldProps { label: string; name?: string; type?: string; icon?: string; autocomplete?: string; required?: boolean; maxlength?: number; counter?: boolean; "toggle-password"?: boolean; defaultValue?: string; value?: string; onChange?: (value: string) => void; className?: string; } export const AuthTextField = forwardRef( ({ label, name, type = "text", icon, autocomplete, required = false, maxlength, counter = false, "toggle-password": togglePassword = false, defaultValue = "", value: controlledValue, onChange, className = "" }, ref) => { const [internalValue, setInternalValue] = useState(defaultValue); const [isFocused, setIsFocused] = useState(false); const [showPassword, setShowPassword] = useState(false); const [charCount, setCharCount] = useState(0); const inputRef = useRef(null); const isControlled = controlledValue !== undefined; const value = isControlled ? controlledValue : internalValue; const displayType = togglePassword && type === "password" ? (showPassword ? "text" : "password") : type; useEffect(() => { if (!isControlled) { setInternalValue(defaultValue); } }, [defaultValue, isControlled]); useEffect(() => { setCharCount(value.length); }, [value]); useImperativeHandle(ref, () => ({ get value() { return value; }, focus: () => { inputRef.current?.focus(); }, blur: () => { inputRef.current?.blur(); } })); const handleChange = (e: React.ChangeEvent) => { const newValue = e.target.value; if (!isControlled) { setInternalValue(newValue); } onChange?.(newValue); }; const hasError = false; // Can be extended for validation return (
{icon && ( {icon.replace("--filled", "").replace("--outlined", "")} )}
setIsFocused(true)} onBlur={() => setIsFocused(false)} autoComplete={autocomplete} required={required} maxLength={maxlength} placeholder={label + (required ? " *" : "")} className={styles.input} />
{togglePassword && type === "password" && ( )}
{counter && maxlength && (
{charCount} / {maxlength}
)}
); } ); AuthTextField.displayName = "AuthTextField";