diff --git a/package.json b/package.json index bf0b62a..cbe79f4 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "lucide-react": "^0.367.0", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-hook-form": "^7.51.3", "tailwind-merge": "^2.2.2", "tailwindcss-animate": "^1.0.7" }, diff --git a/src/App.tsx b/src/App.tsx index 491c184..8b2d999 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,10 +1,34 @@ +import { useForm } from "react-hook-form"; import InputField from "./components/fields/InputField"; +import personalInfoSchema from "./schemas/personal_info.json"; function App() { + console.log({ personalInfoSchema }); + const schema = personalInfoSchema; + const { register, handleSubmit } = useForm(); + + const onSubmit = (data) => { + console.log("form Data = ", data); + }; + return (

JSON to Form

- + +
+ {/* TODO(Keyur): Make the below logic recursive*/} + {schema && + schema.fields.map((field) => ( + + ))} + +
); } diff --git a/src/components/fields/InputField.tsx b/src/components/fields/InputField.tsx index f87174a..8b48b04 100644 --- a/src/components/fields/InputField.tsx +++ b/src/components/fields/InputField.tsx @@ -1,18 +1,21 @@ +import { UseFormRegister } from "react-hook-form"; import { Input, InputProps } from "../ui/input"; import { Label } from "../ui/label"; +import { FieldType } from "@/interfaces/field"; interface InputFieldProps extends InputProps { labelText: string; htmlFor: string; + register: UseFormRegister; } const InputField = (props: InputFieldProps) => { - const { htmlFor, labelText, ...rest } = props; + const { htmlFor, register, labelText, ...rest } = props; return ( <>
- +
); diff --git a/src/components/ui/input.tsx b/src/components/ui/input.tsx index 677d05f..0dcfb1b 100644 --- a/src/components/ui/input.tsx +++ b/src/components/ui/input.tsx @@ -1,25 +1,25 @@ -import * as React from "react" +import * as React from "react"; -import { cn } from "@/lib/utils" +import { cn } from "@/lib/utils"; export interface InputProps - extends React.InputHTMLAttributes {} + extends React.InputHTMLAttributes {} const Input = React.forwardRef( - ({ className, type, ...props }, ref) => { - return ( - - ) - } -) -Input.displayName = "Input" + ({ className, type, ...props }, ref) => { + return ( + + ); + } +); +Input.displayName = "Input"; -export { Input } +export { Input }; diff --git a/src/schemas/personal_info.json b/src/schemas/personal_info.json new file mode 100644 index 0000000..d2309ef --- /dev/null +++ b/src/schemas/personal_info.json @@ -0,0 +1,23 @@ +{ + "type": "group", + "fields": [ + { + "type": "field", + "dataType": "text", + "fieldName": "First Name", + "accessorKey": "firstName" + }, + { + "type": "field", + "dataType": "text", + "fieldName": "Last Name", + "accessorKey": "lastName" + }, + { + "type": "field", + "dataType": "number", + "fieldName": "Mobile Number", + "accessorKey": "mobNo" + } + ] +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 82030a4..92abac0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1834,6 +1834,11 @@ react-dom@^18.2.0: loose-envify "^1.1.0" scheduler "^0.23.0" +react-hook-form@^7.51.3: + version "7.51.3" + resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.51.3.tgz#7486dd2d52280b6b28048c099a98d2545931cab3" + integrity sha512-cvJ/wbHdhYx8aviSWh28w9ImjmVsb5Y05n1+FW786vEZQJV5STNM0pW6ujS+oiBecb0ARBxJFyAnXj9+GHXACQ== + react-refresh@^0.14.0: version "0.14.0" resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.14.0.tgz#4e02825378a5f227079554d4284889354e5f553e"