Skip to content

An AI-powered search engine with a generative UI

feat(components): add PasswordInput component with visibility toggle

vishnu vc7c713680cf2
diff --git a/components/login-form.tsx b/components/login-form.tsx
index 8c592cf5..6693d450 100644
--- a/components/login-form.tsx
+++ b/components/login-form.tsx
@@ -19,6 +19,8 @@ import { IconLogo } from '@/components/ui/icons'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
+import { PasswordInput } from './ui/password-input'
+
export function LoginForm({
className,
...props
@@ -129,7 +131,7 @@ export function LoginForm({
Forgot password?
</Link>
</div>
- <Input
+ <PasswordInput
id="password"
type="password"
placeholder="********"
diff --git a/components/sign-up-form.tsx b/components/sign-up-form.tsx
index 75173ef6..1042ac4b 100644
--- a/components/sign-up-form.tsx
+++ b/components/sign-up-form.tsx
@@ -18,6 +18,7 @@ import {
import { IconLogo } from '@/components/ui/icons'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
+import { PasswordInput } from '@/components/ui/password-input'
export function SignUpForm({
className,
@@ -92,9 +93,8 @@ export function SignUpForm({
<div className="flex items-center">
<Label htmlFor="password">Password</Label>
</div>
- <Input
+ <PasswordInput
id="password"
- type="password"
placeholder="********"
required
value={password}
@@ -105,7 +105,7 @@ export function SignUpForm({
<div className="flex items-center">
<Label htmlFor="repeat-password">Repeat Password</Label>
</div>
- <Input
+ <PasswordInput
id="repeat-password"
type="password"
placeholder="********"
diff --git a/components/ui/input.tsx b/components/ui/input.tsx
index d2511deb..ed81cc9f 100644
--- a/components/ui/input.tsx
+++ b/components/ui/input.tsx
@@ -3,20 +3,25 @@ import * as React from 'react'
import { cn } from '@/lib/utils/index'
export interface InputProps
- extends React.InputHTMLAttributes<HTMLInputElement> {}
+ extends React.InputHTMLAttributes<HTMLInputElement> {
+ rightElement?: React.ReactNode
+}
const Input = React.forwardRef<HTMLInputElement, InputProps>(
- ({ className, type, ...props }, ref) => {
+ ({ className, type, rightElement, ...props }, ref) => {
return (
- <input
- type={type}
- className={cn(
- 'flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
- className
- )}
- ref={ref}
- {...props}
- />
+ <div className="relative flex items-center">
+ <input
+ type={type}
+ className={cn(
+ 'flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
+ className
+ )}
+ ref={ref}
+ {...props}
+ />
+ {rightElement && <div className="absolute right-3">{rightElement}</div>}
+ </div>
)
}
)
diff --git a/components/ui/password-input.tsx b/components/ui/password-input.tsx
new file mode 100644
index 00000000..6b35d31d
--- /dev/null
+++ b/components/ui/password-input.tsx
@@ -0,0 +1,41 @@
+import * as React from 'react'
+import { useState } from 'react'
+
+import { Eye, EyeOff } from 'lucide-react'
+
+import { Input, InputProps } from '@/components/ui/input'
+
+import { Button } from './button'
+
+const PasswordInput = React.forwardRef<HTMLInputElement, InputProps>(
+ ({ className, type = 'password', rightElement, ...props }, ref) => {
+ const [showPassword, setShowPassword] = useState(false)
+ return (
+ <Input
+ type={showPassword ? 'text' : type}
+ className={className}
+ ref={ref}
+ {...props}
+ rightElement={
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon"
+ className="h-full px-3 py-2 hover:bg-transparent"
+ onClick={() => setShowPassword(!showPassword)}
+ >
+ {showPassword ? (
+ <EyeOff className="h-4 w-4" />
+ ) : (
+ <Eye className="h-4 w-4" />
+ )}
+ </Button>
+ }
+ />
+ )
+ }
+)
+
+PasswordInput.displayName = 'PasswordInput'
+
+export { PasswordInput }