Skip to content

Commit

Permalink
feat: added orientation prop to the json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
keyurparalkar committed Apr 17, 2024
1 parent ba16360 commit fd0a980
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/components/ui/SchemaRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,39 @@ type SchemaRendererProps = {
const SchemaRenderer = (props: SchemaRendererProps) => {
const { schema, errors, register } = props;

return schema.fields.map((field, index) => {
if (field.type === "field") {
return (
<InputField
key={`key-${field.accessorKey}`}
register={register}
labelText={field.fieldName}
htmlFor={field.accessorKey}
type={field.dataType}
validation={field.validation}
error={errors[field.accessorKey]}
/>
);
}
return (
<div
id="group-container"
className={`grid ${
schema.orientation === "horizontal" ? "grid-cols-3" : "grid-rows-1"
} gap-[10px]`}
>
{schema.fields.map((field, index) => {
if (field.type === "field") {
return (
<InputField
key={`key-${field.accessorKey}`}
register={register}
labelText={field.fieldName}
htmlFor={field.accessorKey}
type={field.dataType}
validation={field.validation}
error={errors[field.accessorKey]}
/>
);
}

return (
<SchemaRenderer
key={`key-${field.type}-${index}`}
schema={field}
errors={errors}
register={register}
/>
);
});
return (
<SchemaRenderer
schema={field}
errors={errors}
register={register}
key={`group-container-${index}`}
/>
);
})}
</div>
);
};

export default SchemaRenderer;
2 changes: 2 additions & 0 deletions src/interfaces/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { FieldTypeDef } from "./field";

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

export const GroupTypeDef: z.ZodType<GroupFieldType> = z.object({
type: z.literal("group"),
orientation: z.union([z.literal("horizontal"), z.literal("vertical")]),
fields: z.array(z.union([FieldTypeDef, z.lazy(() => GroupTypeDef)])),
});
2 changes: 2 additions & 0 deletions src/schemas/forms/personal_info.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"type": "group",
"orientation": "horizontal",
"fields": [
{
"type": "field",
Expand Down Expand Up @@ -33,6 +34,7 @@
},
{
"type": "group",
"orientation": "vertical",
"fields": [
{
"type": "field",
Expand Down

0 comments on commit fd0a980

Please sign in to comment.