-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathform.tsx
57 lines (52 loc) · 1.64 KB
/
form.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use client';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { login } from '@/app/auth/01-auth';
import Link from 'next/link';
import { useFormState, useFormStatus } from 'react-dom';
export function LoginForm() {
const [state, action] = useFormState(login, undefined);
return (
<form action={action}>
<div className="flex flex-col gap-2">
<div>
<Label htmlFor="email">Email</Label>
<Input
id="email"
name="email"
placeholder="[email protected]"
type="email"
/>
{state?.errors?.email && (
<p className="text-sm text-red-500">{state.errors.email}</p>
)}
</div>
<div className="mt-4">
<div className="flex items-center justify-between">
<Label htmlFor="password">Password</Label>
<Link className="text-sm underline" href="#">
Forgot your password?
</Link>
</div>
<Input id="password" type="password" name="password" />
{state?.errors?.password && (
<p className="text-sm text-red-500">{state.errors.password}</p>
)}
</div>
{state?.message && (
<p className="text-sm text-red-500">{state.message}</p>
)}
<LoginButton />
</div>
</form>
);
}
export function LoginButton() {
const { pending } = useFormStatus();
return (
<Button aria-disabled={pending} type="submit" className="mt-4 w-full">
{pending ? 'Submitting...' : 'Login'}
</Button>
);
}