← full-stack-fastapi-template / frontend/src/components/ui/password-input.tsx
| 1 | import * as React from "react" |
| 2 | import { Eye, EyeOff } from "lucide-react" |
| 3 | |
| 4 | import { cn } from "@/lib/utils" |
| 5 | import { Button } from "./button" |
| 6 | |
| 7 | interface PasswordInputProps extends React.ComponentProps<"input"> { |
| 8 | error?: string |
| 9 | } |
| 10 | |
| 11 | const PasswordInput = React.forwardRef<HTMLInputElement, PasswordInputProps>( |
| 12 | ({ className, error, ...props }, ref) => { |
| 13 | const [showPassword, setShowPassword] = React.useState(false) |
| 14 | |
| 15 | return ( |
| 16 | <div className="relative"> |
| 17 | <input |
| 18 | type={showPassword ? "text" : "password"} |
| 19 | data-slot="input" |
| 20 | className={cn( |
| 21 | "file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input h-9 w-full min-w-0 rounded-md border bg-transparent px-3 py-1 pr-10 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", |
| 22 | "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]", |
| 23 | "aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive", |
| 24 | className |
| 25 | )} |
| 26 | ref={ref} |
| 27 | aria-invalid={!!error} |
| 28 | {...props} |
| 29 | /> |
| 30 | <Button |
| 31 | type="button" |
| 32 | variant="ghost" |
| 33 | size="icon-sm" |
| 34 | className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent" |
| 35 | onClick={() => setShowPassword(!showPassword)} |
| 36 | aria-label={showPassword ? "Hide password" : "Show password"} |
| 37 | > |
| 38 | {showPassword ? ( |
| 39 | <EyeOff className="h-4 w-4 text-muted-foreground" /> |
| 40 | ) : ( |
| 41 | <Eye className="h-4 w-4 text-muted-foreground" /> |
| 42 | )} |
| 43 | </Button> |
| 44 | </div> |
| 45 | ) |
| 46 | } |
| 47 | ) |
| 48 | |
| 49 | PasswordInput.displayName = "PasswordInput" |
| 50 | |
| 51 | export { PasswordInput } |
| 52 |