Skip to content

Commit

Permalink
feat: added initial parsing of schema
Browse files Browse the repository at this point in the history
  • Loading branch information
keyurparalkar committed Apr 12, 2024
1 parent 69db0d6 commit c8304af
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
26 changes: 25 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="container mx-auto flex flex-col items-center">
<h1 className="text-4xl font-bold tracking-tight mb-5">JSON to Form</h1>
<InputField type="text" labelText="First Name" htmlFor="firstName" />

<form onSubmit={handleSubmit(onSubmit)}>
{/* TODO(Keyur): Make the below logic recursive*/}
{schema &&
schema.fields.map((field) => (
<InputField
key={`key-${field.accessorKey}`}
register={register}
labelText={field.fieldName}
htmlFor={field.accessorKey}
type={field.type}
/>
))}
<input type="submit" />
</form>
</div>
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/components/fields/InputField.tsx
Original file line number Diff line number Diff line change
@@ -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<any>;
}

const InputField = (props: InputFieldProps) => {
const { htmlFor, labelText, ...rest } = props;
const { htmlFor, register, labelText, ...rest } = props;
return (
<>
<div className="flex flex-col space-y-2">
<Label htmlFor={htmlFor}>{labelText}</Label>
<Input id="htmlFor" {...rest} />
<Input id="htmlFor" {...rest} {...register(htmlFor)} />
</div>
</>
);
Expand Down
38 changes: 19 additions & 19 deletions src/components/ui/input.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLInputElement> {}
extends React.InputHTMLAttributes<HTMLInputElement> {}

const Input = React.forwardRef<HTMLInputElement, InputProps>(
({ className, type, ...props }, ref) => {
return (
<input
type={type}
className={cn(
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
{...props}
/>
)
}
)
Input.displayName = "Input"
({ className, type, ...props }, ref) => {
return (
<input
type={type}
className={cn(
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
{...props}
/>
);
}
);
Input.displayName = "Input";

export { Input }
export { Input };
23 changes: 23 additions & 0 deletions src/schemas/personal_info.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c8304af

Please sign in to comment.