| 1 | /** |
| 2 | * PasswordInput - 密码输入框组件 |
| 3 | * 支持显示/隐藏密码切换功能 |
| 4 | */ |
| 5 | |
| 6 | 'use client' |
| 7 | |
| 8 | import { Eye, EyeOff } from 'lucide-react' |
| 9 | import * as React from 'react' |
| 10 | import { Input } from '@/components/ui/input' |
| 11 | import { cn } from '@/utils/className' |
| 12 | |
| 13 | export interface PasswordInputProps |
| 14 | extends Omit<React.ComponentProps<'input'>, 'type'> { |
| 15 | showToggle?: boolean |
| 16 | } |
| 17 | |
| 18 | const PasswordInput = React.forwardRef<HTMLInputElement, PasswordInputProps>( |
| 19 | ({ className, showToggle = true, ...props }, ref) => { |
| 20 | const [showPassword, setShowPassword] = React.useState(false) |
| 21 | |
| 22 | return ( |
| 23 | <div className="relative"> |
| 24 | <Input |
| 25 | type={showPassword ? 'text' : 'password'} |
| 26 | className={cn('pr-10', className)} |
| 27 | ref={ref} |
| 28 | {...props} |
| 29 | /> |
| 30 | {showToggle && ( |
| 31 | <button |
| 32 | type="button" |
| 33 | onClick={() => setShowPassword(!showPassword)} |
| 34 | className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground transition-colors cursor-pointer" |
| 35 | tabIndex={-1} |
| 36 | aria-label={showPassword ? 'Hide password' : 'Show password'} |
| 37 | > |
| 38 | {showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />} |
| 39 | </button> |
| 40 | )} |
| 41 | </div> |
| 42 | ) |
| 43 | }, |
| 44 | ) |
| 45 | PasswordInput.displayName = 'PasswordInput' |
| 46 | |
| 47 | export { PasswordInput } |
| 48 |