-
Notifications
You must be signed in to change notification settings - Fork 0
/
login-form.tsx
60 lines (55 loc) · 1.52 KB
/
login-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
58
59
60
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useHookFormAction } from "@next-safe-action/adapter-react-hook-form/hooks";
import { loginSchema } from "./login-validation";
import { loginAction } from "./login-action";
export function LoginForm() {
const { form, action, handleSubmitWithAction, resetFormAndAction } =
useHookFormAction(loginAction, zodResolver(loginSchema), {
formProps: {
mode: "onChange",
},
actionProps: {
onSuccess: () => {
window.alert("Logged in successfully!");
resetFormAndAction();
},
},
});
return (
<form onSubmit={handleSubmitWithAction} className="flex flex-col space-y-4">
<input
defaultValue=""
placeholder="Username"
className="border px-2 py-1 rounded-lg"
{...form.register("username")}
/>
{form.formState.errors.username ? (
<p>{form.formState.errors.username.message}</p>
) : null}
<input
defaultValue=""
placeholder="Password"
className="border px-2 py-1 rounded-lg"
{...form.register("password")}
/>
{form.formState.errors.password ? (
<p>{form.formState.errors.password.message}</p>
) : null}
<button
type="submit"
className="bg-black text-white rounded-lg px-3 py-2">
Login
</button>
<button
onClick={resetFormAndAction}
type="button"
className="bg-black text-white rounded-lg px-3 py-2">
Reset form and action state
</button>
{form.formState.errors.root ? (
<p>{form.formState.errors.root.message}</p>
) : null}
</form>
);
}