From a2e0a65b2599267efe94d665d6305f59b225bbd5 Mon Sep 17 00:00:00 2001 From: Biswa Kalyan Bhuyan Date: Tue, 29 Apr 2025 10:47:43 +0530 Subject: feat: added initlaized the frontend and backend --- frontend/src/components/ui/form.jsx | 144 ++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 frontend/src/components/ui/form.jsx (limited to 'frontend/src/components/ui/form.jsx') diff --git a/frontend/src/components/ui/form.jsx b/frontend/src/components/ui/form.jsx new file mode 100644 index 0000000..728ea87 --- /dev/null +++ b/frontend/src/components/ui/form.jsx @@ -0,0 +1,144 @@ +"use client"; +import * as React from "react" +import { Slot } from "@radix-ui/react-slot" +import { Controller, FormProvider, useFormContext, useFormState } from "react-hook-form"; + +import { cn } from "@/lib/utils" +import { Label } from "@/components/ui/label" + +const Form = FormProvider + +const FormFieldContext = React.createContext({}) + +const FormField = ( + { + ...props + } +) => { + return ( + + + + ); +} + +const useFormField = () => { + const fieldContext = React.useContext(FormFieldContext) + const itemContext = React.useContext(FormItemContext) + const { getFieldState } = useFormContext() + const formState = useFormState({ name: fieldContext.name }) + 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, + } +} + +const FormItemContext = React.createContext({}) + +function FormItem({ + className, + ...props +}) { + const id = React.useId() + + return ( + +
+ + ); +} + +function FormLabel({ + className, + ...props +}) { + const { error, formItemId } = useFormField() + + return ( +