From dde6e22db476ce880f243a68377306c319d8117b Mon Sep 17 00:00:00 2001 From: "HanuCh@udhary" <137854084+hanuchaudhary@users.noreply.github.com> Date: Thu, 9 Jan 2025 18:28:49 +0530 Subject: [PATCH 1/2] fix --- components/AuthComponent/SignupForm.tsx | 213 ++++++++++++------------ components/ui/form.tsx | 178 ++++++++++++++++++++ package-lock.json | 28 +++- package.json | 2 + store/AuthStore/useAuthStore.ts | 2 +- validations/validation.ts | 31 +++- 6 files changed, 342 insertions(+), 112 deletions(-) create mode 100644 components/ui/form.tsx diff --git a/components/AuthComponent/SignupForm.tsx b/components/AuthComponent/SignupForm.tsx index e3e8963..8b93fe1 100644 --- a/components/AuthComponent/SignupForm.tsx +++ b/components/AuthComponent/SignupForm.tsx @@ -1,17 +1,23 @@ "use client"; -import React, { useState } from "react"; +import React from "react"; import { useRouter } from "next/navigation"; -import Link from "next/link"; -import { Button } from "@/components/ui/button"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; +import * as z from "zod"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; import { Input } from "@/components/ui/input"; -import { Label } from "@/components/ui/label"; import { Select, SelectContent, - SelectGroup, SelectItem, - SelectLabel, SelectTrigger, SelectValue, } from "@/components/ui/select"; @@ -24,33 +30,29 @@ import { CardTitle, } from "@/components/ui/card"; import { Alert, AlertDescription } from "@/components/ui/alert"; -import { UserType } from "@/types/typeInterfaces"; import { useAuthStore } from "@/store/AuthStore/useAuthStore"; import AuthBottom from "./AuthBottom"; import LoadingButton from "../Buttons/LoadingButton"; +import { signupSchema } from "@/validations/validation"; +type SignupFormValues = z.infer; export default function SignupForm() { const { isSigningUp, signup, signupError } = useAuthStore(); const router = useRouter(); - const [formData, setFormData] = useState({ - fullName: "", - email: "", - password: "", - leetcodeUsername: "", - gender: "", - }); - - const handleChange = (e: React.ChangeEvent) => { - setFormData({ ...formData, [e.target.name]: e.target.value }); - }; - const handleSelectChange = (value: string) => { - setFormData({ ...formData, gender: value }); - }; + const form = useForm({ + resolver: zodResolver(signupSchema), + defaultValues: { + fullName: "", + email: "", + password: "", + leetcodeUsername: "", + gender: "", + }, + }); - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - signup(formData, router); + const onSubmit = (data: SignupFormValues) => { + signup(data, router); }; return ( @@ -65,93 +67,98 @@ export default function SignupForm() { -
-
- - + + ( + + Full Name + + + + + + )} /> -
- -
- - ( + + Email + + + + + + )} /> -
- -
- - ( + + Password + + + + + + )} /> -
- -
- - ( + + LeetCode Username + + + + + + )} /> -
- -
- - -
- - {signupError && ( - - {signupError} - - )} - - - + ( + + Gender + + + + )} + /> + {signupError && ( + + {signupError} + + )} + + +
= FieldPath +> = { + name: TName +} + +const FormFieldContext = React.createContext( + {} as FormFieldContextValue +) + +const FormField = < + TFieldValues extends FieldValues = FieldValues, + TName extends FieldPath = FieldPath +>({ + ...props +}: ControllerProps) => { + return ( + + + + ) +} + +const useFormField = () => { + const fieldContext = React.useContext(FormFieldContext) + const itemContext = React.useContext(FormItemContext) + const { getFieldState, formState } = useFormContext() + + const fieldState = getFieldState(fieldContext.name, formState) + + if (!fieldContext) { + throw new Error("useFormField should be used within ") + } + + const { id } = itemContext + + return { + id, + name: fieldContext.name, + formItemId: `${id}-form-item`, + formDescriptionId: `${id}-form-item-description`, + formMessageId: `${id}-form-item-message`, + ...fieldState, + } +} + +type FormItemContextValue = { + id: string +} + +const FormItemContext = React.createContext( + {} as FormItemContextValue +) + +const FormItem = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => { + const id = React.useId() + + return ( + +
+ + ) +}) +FormItem.displayName = "FormItem" + +const FormLabel = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => { + const { error, formItemId } = useFormField() + + return ( +