diff --git a/src/App.tsx b/src/App.tsx index 7c90e08..e309fff 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import { useForm } from "react-hook-form"; -import InputField from "./components/fields/InputField"; import personalInfoSchema from "./schemas/forms/personal_info.json"; import { SchemaDef } from "./interfaces/schema"; +import SchemaRenderer from "./components/ui/SchemaRenderer"; const schema = SchemaDef.parse(personalInfoSchema); @@ -12,7 +12,7 @@ const FormValues = schema.fields.reduce((acc, currValue) => { return acc; }, {} as Record); -type TFormValues = typeof FormValues; +export type TFormValues = typeof FormValues; function App() { const { @@ -30,22 +30,7 @@ function App() {

JSON to Form

- {/* TODO(Keyur): Make the below logic recursive*/} - {schema && - schema.fields.map( - (field) => - field.type === "field" && ( - - ) - )} + diff --git a/src/components/fields/InputField.tsx b/src/components/fields/InputField.tsx index f869bad..022b6ea 100644 --- a/src/components/fields/InputField.tsx +++ b/src/components/fields/InputField.tsx @@ -2,11 +2,12 @@ import { FieldError, UseFormRegister } from "react-hook-form"; import { Input, InputProps } from "../ui/input"; import { Label } from "../ui/label"; import { TField } from "@/interfaces/field"; +import { TFormValues } from "@/App"; interface InputFieldProps extends InputProps { labelText: string; htmlFor: string; - register: UseFormRegister; + register: UseFormRegister; validation?: TField["validation"]; error?: FieldError; } diff --git a/src/components/ui/SchemaRenderer.tsx b/src/components/ui/SchemaRenderer.tsx new file mode 100644 index 0000000..29f5e9b --- /dev/null +++ b/src/components/ui/SchemaRenderer.tsx @@ -0,0 +1,36 @@ +import { SchemaType } from "@/interfaces/schema"; +import InputField from "../fields/InputField"; +import { FieldErrors, UseFormRegister } from "react-hook-form"; +import { TFormValues } from "@/App"; + +type SchemaRendererProps = { + schema: SchemaType; + errors: FieldErrors>; + register: UseFormRegister; +}; + +const SchemaRenderer = (props: SchemaRendererProps) => { + const { schema, errors, register } = props; + + return schema.fields.map((field) => { + if (field.type === "field") { + return ( + + ); + } + + return ( + + ); + }); +}; + +export default SchemaRenderer; diff --git a/src/schemas/forms/personal_info.json b/src/schemas/forms/personal_info.json index 3133044..4666aca 100644 --- a/src/schemas/forms/personal_info.json +++ b/src/schemas/forms/personal_info.json @@ -30,6 +30,29 @@ "message": "Email Address is required" } } + }, + { + "type": "group", + "fields": [ + { + "type": "field", + "dataType": "text", + "fieldName": "UAN", + "accessorKey": "uan" + }, + { + "type": "field", + "dataType": "text", + "fieldName": "PAN", + "accessorKey": "pan" + }, + { + "type": "field", + "dataType": "number", + "fieldName": "Aadhar card number", + "accessorKey": "aadharCardNum" + } + ] } ] } \ No newline at end of file