Skip to content

Commit

Permalink
types: added form values for schema
Browse files Browse the repository at this point in the history
  • Loading branch information
keyurparalkar committed Apr 13, 2024
1 parent 838a7ee commit 872b514
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
42 changes: 30 additions & 12 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
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";

const schema = SchemaDef.parse(personalInfoSchema);

// type FormValues<T extends typeof schema> = {
// [P in keyof T]: T[P];
// };
const FormValues = schema.fields.reduce((acc, currValue) => {
if (currValue.type === "field") {
acc[currValue.accessorKey] = "";
}
return acc;
}, {} as Record<string, string>);

type TFormValues = typeof FormValues;

function App() {
const schema = personalInfoSchema;
const { register, handleSubmit } = useForm();
console.log({ schema });
const { register, handleSubmit } = useForm<TFormValues>();

const onSubmit = (data) => {
const onSubmit = (data: TFormValues) => {
console.log("form Data = ", data);
};

Expand All @@ -17,15 +32,18 @@ function App() {
<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}
/>
))}
schema.fields.map(
(field) =>
field.type === "field" && (
<InputField
key={`key-${field.accessorKey}`}
register={register}
labelText={field.fieldName}
htmlFor={field.accessorKey}
type={field.type}
/>
)
)}
<input type="submit" />
</form>
</div>
Expand Down
19 changes: 7 additions & 12 deletions src/interfaces/field.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
export type FieldType = {
type: "field";
dataType: FIELD_DATA_TYPE;
fieldName: string;
accessorKey: string;
constraints?: Partial<Record<string, unknown>>;
};
import { z } from "zod";

export enum FIELD_DATA_TYPE {
TEXT = "text",
NUMBER = "number",
EMAIL = "email",
}
export const FieldTypeDef = z.object({
type: z.literal("field"),
dataType: z.enum(["text", "number", "email"]),
fieldName: z.string(),
accessorKey: z.string(),
});
12 changes: 9 additions & 3 deletions src/interfaces/group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { FieldType } from "./field";
import { z } from "zod";
import { FieldTypeDef } from "./field";

export type GroupField = {
type GroupFieldType = {
type: "group";
fields: (FieldType | GroupField)[];
fields: (z.infer<typeof FieldTypeDef> | GroupFieldType)[];
};

export const GroupTypeDef: z.ZodType<GroupFieldType> = z.object({
type: z.literal("group"),
fields: z.array(z.union([FieldTypeDef, z.lazy(() => GroupTypeDef)])),
});
18 changes: 1 addition & 17 deletions src/interfaces/schema.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
import { z } from "zod";

export const FieldTypeDef = z.object({
type: z.literal("field"),
dataType: z.enum(["text", "number", "email"]),
fieldName: z.string(),
accessorKey: z.string(),
});

type GroupFieldType = {
type: "group";
fields: (z.infer<typeof FieldTypeDef> | GroupFieldType)[];
};

export const GroupTypeDef: z.ZodType<GroupFieldType> = z.object({
type: z.literal("group"),
fields: z.array(z.union([FieldTypeDef, z.lazy(() => GroupTypeDef)])),
});
import { GroupTypeDef } from "./group";

export const SchemaDef = GroupTypeDef;

Expand Down

0 comments on commit 872b514

Please sign in to comment.