Files
knowledge-governance/web/src/PasswordInput.tsx
T

30 lines
1.1 KiB
TypeScript

import { useState } from 'react'
import type { InputHTMLAttributes } from 'react'
// PasswordInput renders a password field with an inline visibility toggle so
// users can verify what they typed before submitting. The toggle is a
// presentation-only button (type=button) nested in a relative wrapper; it
// never submits the surrounding form.
interface PasswordInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'type'> {
autoComplete: string
initiallyRevealed?: boolean
}
export function PasswordInput({ autoComplete, initiallyRevealed = false, ...inputProps }: PasswordInputProps) {
const [revealed, setRevealed] = useState(initiallyRevealed)
return (
<span className="password-field">
<input {...inputProps} type={revealed ? 'text' : 'password'} autoComplete={autoComplete} />
<button
type="button"
className="password-toggle"
aria-label={revealed ? '隐藏密码' : '显示密码'}
aria-pressed={revealed}
onClick={() => setRevealed((value) => !value)}
>
{revealed ? '隐藏' : '显示'}
</button>
</span>
)
}