()
| 10 | type FormFields = z.infer<typeof schema>; |
| 11 | |
| 12 | const App = () => { |
| 13 | const { |
| 14 | register, |
| 15 | handleSubmit, |
| 16 | setError, |
| 17 | formState: { errors, isSubmitting }, |
| 18 | } = useForm<FormFields>({ |
| 19 | defaultValues: { |
| 20 | email: "test@email.com", |
| 21 | }, |
| 22 | resolver: zodResolver(schema), |
| 23 | }); |
| 24 | |
| 25 | const onSubmit: SubmitHandler<FormFields> = async (data) => { |
| 26 | try { |
| 27 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 28 | console.log(data); |
| 29 | } catch (error) { |
| 30 | setError("root", { |
| 31 | message: "This email is already taken", |
| 32 | }); |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | return ( |
| 37 | <form className="tutorial gap-2" onSubmit={handleSubmit(onSubmit)}> |
| 38 | <input {...register("email")} type="text" placeholder="Email" /> |
| 39 | {errors.email && ( |
| 40 | <div className="text-red-500">{errors.email.message}</div> |
| 41 | )} |
| 42 | <input {...register("password")} type="password" placeholder="Password" /> |
| 43 | {errors.password && ( |
| 44 | <div className="text-red-500">{errors.password.message}</div> |
| 45 | )} |
| 46 | <button disabled={isSubmitting} type="submit"> |
| 47 | {isSubmitting ? "Loading..." : "Submit"} |
| 48 | </button> |
| 49 | {errors.root && <div className="text-red-500">{errors.root.message}</div>} |
| 50 | </form> |
| 51 | ); |
| 52 | }; |
| 53 | |
| 54 | export default App; |
nothing calls this directly
no test coverage detected