import { useId, useState, type InputHTMLAttributes } from "react"; import { Dice5, Eye, EyeOff } from "lucide-react"; import PasswordGeneratorDialog from "./PasswordGeneratorDialog"; import type { PasswordGeneratorOptions } from "./passwordGenerator"; import { usePlatformLanguage } from "../i18n/LanguageContext"; export type PasswordFieldProps = Omit, "type" | "value" | "onChange"> & { value: string; onValueChange: (value: string) => void; saved?: boolean; savedPlaceholder?: string; revealLabel?: string; hideLabel?: string; generator?: boolean; generatorLabel?: string; generatorOptions?: Partial; inputClassName?: string; }; export default function PasswordField({ value, onValueChange, saved = false, savedPlaceholder = "••••••••", revealLabel = "i18n:govoplan-core.show_password.044b852f", hideLabel = "i18n:govoplan-core.hide_password.e40123b4", generator = false, generatorLabel = "i18n:govoplan-core.generate_password.bd5bede8", generatorOptions, placeholder, disabled = false, className = "", inputClassName = "", id, ...inputProps }: PasswordFieldProps) { const generatedId = useId(); const { translateText } = usePlatformLanguage(); const inputId = id ?? generatedId; const [visible, setVisible] = useState(false); const [generatorOpen, setGeneratorOpen] = useState(false); const hasTypedPassword = value.length > 0; const showSavedPlaceholder = saved && !hasTypedPassword; const canReveal = hasTypedPassword && !disabled; const canGenerate = generator && !disabled && !inputProps.readOnly; const inputType = visible && canReveal ? "text" : "password"; const translatedGeneratorLabel = translateText(generatorLabel); const translatedRevealLabel = translateText(revealLabel); const translatedHideLabel = translateText(hideLabel); return ( <>
{ onValueChange(event.target.value); if (!event.target.value) setVisible(false); }} /> {canReveal || canGenerate ? ( {canGenerate ? ( ) : null} {canReveal ? ( ) : null} ) : null}
{ onValueChange(password); setVisible(false); }} onClose={() => setGeneratorOpen(false)} /> ); }