import { useState } from 'react'; import { Clipboard, Eye, EyeOff, RotateCcwKey } from 'lucide-react'; import Button from '@/components/elements/Button'; import CopyOnClick from '@/components/elements/CopyOnClick'; import Icon from '@/components/elements/Icon'; import Tooltip from '@/components/elements/tooltip/Tooltip'; import { TextInput, type TextInputProps } from '@/components/form/controls'; import { cn } from '@/lib/cn'; import { generateSecurePassword } from '@/lib/passwords'; export type PasswordInputProps = Omit & { value: string; onValueChange: (value: string) => void; /** Called with a generated password after it is set, e.g. to fill a confirmation field. */ onGenerate?: (value: string) => void; /** What the show and hide button names, e.g. "new password". */ noun?: string; }; /** A password input with buttons to generate a password, show or hide it, and copy one it generated. */ export function PasswordInput({ value, onValueChange, onGenerate, noun = 'password', className, ...props }: PasswordInputProps) { const [visible, setVisible] = useState(false); const [generated, setGenerated] = useState(false); const toggleLabel = `${visible ? 'Hide' : 'Show'} ${noun}`; const generate = () => { const password = generateSecurePassword(); onValueChange(password); onGenerate?.(password); setGenerated(true); setVisible(true); }; return (
{ onValueChange(next); setGenerated(false); }} className={cn('pr-24', className)} />
setVisible((current) => !current)} > {generated && ( )}
); }