Skip to content

feat(ui): add password show input #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/(public)/login/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import { Label } from '@/components/ui/label';
import { login } from '@/app/auth/01-auth';
import Link from 'next/link';
import { useFormState, useFormStatus } from 'react-dom';
import { PasswordInput } from '@/components/ui/PasswordInput ';
import { useState } from 'react';

export function LoginForm() {
const [state, action] = useFormState(login, undefined);
const [password, setPassword] = useState('');

return (
<form action={action}>
Expand All @@ -32,7 +35,13 @@ export function LoginForm() {
Forgot your password?
</Link>
</div>
<Input id="password" type="password" name="password" />
<PasswordInput
value={password}
onChange={(e) => setPassword(e.target.value)}
id="password"
name="password"
placeholder="******"
/>
{state?.errors?.password && (
<p className="text-sm text-red-500">{state.errors.password}</p>
)}
Expand All @@ -51,7 +60,7 @@ export function LoginButton() {

return (
<Button aria-disabled={pending} type="submit" className="mt-4 w-full">
{pending ? 'Submitting...' : 'Sign up'}
{pending ? 'Submitting...' : 'Login'}
</Button>
);
}
13 changes: 11 additions & 2 deletions app/(public)/signup/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { signup } from '@/app/auth/01-auth';
import { useFormState, useFormStatus } from 'react-dom';
import { PasswordInput } from '@/components/ui/PasswordInput ';
import { useState } from 'react';

export function SignupForm() {
const [state, action] = useFormState(signup, undefined);
const [password, setPassword] = useState('');

return (
<form action={action}>
Expand All @@ -28,7 +31,13 @@ export function SignupForm() {
)}
<div>
<Label htmlFor="password">Password</Label>
<Input id="password" name="password" type="password" />
<PasswordInput
value={password}
onChange={(e) => setPassword(e.target.value)}
id="password"
name="password"
placeholder="******"
/>
</div>
{state?.errors?.password && (
<div className="text-sm text-red-500">
Expand All @@ -51,7 +60,7 @@ export function SignupButton() {

return (
<Button aria-disabled={pending} type="submit" className="mt-2 w-full">
{pending ? 'Submitting...' : 'Login'}
{pending ? 'Submitting...' : 'Sign up'}
</Button>
);
}
26 changes: 1 addition & 25 deletions app/dashboard/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,7 @@ export default async function Layout({
<span className="sr-only">Home</span>
</Link>
<h1 className="md:blobk hidden text-lg font-semibold">Dashboard</h1>
<div className="ml-auto flex items-center gap-4">
<form>
<div className="relative">
<Input
className="bg-gray-100/60 md:w-[200px]"
placeholder="Search orders..."
type="search"
/>
</div>
</form>
<Button className="rounded-full" size="icon" variant="ghost">
<Image
alt="Avatar"
className="rounded-full"
height="32"
src="/placeholder.svg"
style={{
aspectRatio: '32/32',
objectFit: 'cover',
}}
width="32"
/>
<span className="sr-only">View profile</span>
</Button>
</div>
<div className="ml-auto flex items-center gap-4">Welcome, {user?.name}!</div>
</header>
<main className="flex-1 p-4 md:p-6">{children}</main>
</div>
Expand Down
56 changes: 56 additions & 0 deletions components/ui/PasswordInput .tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"use client";

import { forwardRef, useState } from "react";
import { EyeIcon, EyeOffIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input, InputProps } from "@/components/ui/input";
import { cn } from "@/lib/utils";

const PasswordInput = forwardRef<HTMLInputElement, InputProps>(
({ className, ...props }, ref) => {
const [showPassword, setShowPassword] = useState(false);
const disabled =
props.value === "" || props.value === undefined || props.disabled;

return (
<div className="relative">
<Input
type={showPassword ? "text" : "password"}
className={cn("hide-password-toggle pr-10", className)}
ref={ref}
{...props}
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowPassword((prev) => !prev)}
disabled={disabled}
>
{showPassword && !disabled ? (
<EyeIcon className="h-4 w-4" aria-hidden="true" />
) : (
<EyeOffIcon className="h-4 w-4" aria-hidden="true" />
)}
<span className="sr-only">
{showPassword ? "Hide password" : "Show password"}
</span>
</Button>

{/* hides browsers password toggles */}
<style>{`
.hide-password-toggle::-ms-reveal,
.hide-password-toggle::-ms-clear {
visibility: hidden;
pointer-events: none;
display: none;
}
`}</style>
</div>
);
},
);
PasswordInput.displayName = "PasswordInput";

export { PasswordInput };