import { useId, useState, type InputHTMLAttributes } from "react"; import { Eye, EyeOff } from "lucide-react"; export type PasswordFieldProps = Omit, "type" | "value" | "onChange"> & { value: string; onValueChange: (value: string) => void; saved?: boolean; savedPlaceholder?: string; revealLabel?: string; hideLabel?: string; inputClassName?: string; }; export default function PasswordField({ value, onValueChange, saved = false, savedPlaceholder = "••••••••", revealLabel = "Show password", hideLabel = "Hide password", placeholder, disabled = false, className = "", inputClassName = "", id, ...inputProps }: PasswordFieldProps) { const generatedId = useId(); const inputId = id ?? generatedId; const [visible, setVisible] = useState(false); const hasTypedPassword = value.length > 0; const showSavedPlaceholder = saved && !hasTypedPassword; const canReveal = hasTypedPassword && !disabled; const inputType = visible && canReveal ? "text" : "password"; return (
{ onValueChange(event.target.value); if (!event.target.value) setVisible(false); }} /> {canReveal && ( )}
); }