From c99618f73751e2562065bced7131218f3b10219e Mon Sep 17 00:00:00 2001 From: Markus Thielker Date: Sat, 28 Dec 2024 23:43:13 +0100 Subject: [PATCH] NORY-47: create new dynamic-form component --- dashboard/src/components/dynamic-form.tsx | 99 +++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 dashboard/src/components/dynamic-form.tsx diff --git a/dashboard/src/components/dynamic-form.tsx b/dashboard/src/components/dynamic-form.tsx new file mode 100644 index 0000000..9020d46 --- /dev/null +++ b/dashboard/src/components/dynamic-form.tsx @@ -0,0 +1,99 @@ +'use client'; + +import { FieldValues, Path, SubmitErrorHandler, SubmitHandler, UseFormReturn } from 'react-hook-form'; +import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel } from '@/components/ui/form'; +import { Input } from '@/components/ui/input'; +import { Button } from '@/components/ui/button'; +import React, { ReactNode } from 'react'; +import { Checkbox } from '@/components/ui/checkbox'; +import { KratosSchemaProperties } from '@/lib/forms/identity-form'; + +interface DynamicFormProps { + children?: ReactNode, + form: UseFormReturn, + properties: KratosSchemaProperties, + onValid: SubmitHandler, + onInvalid: SubmitErrorHandler, + submitLabel?: string, +} + +export function DynamicForm( + { + children, + form, + properties, + onValid, + onInvalid, + submitLabel, + }: DynamicFormProps, +) { + + const generateFormFields = (data: KratosSchemaProperties, prefix = '') => { + return ( + + { + data && Object.entries(data).map(([key, value]) => { + + const fullFieldName = prefix ? `${prefix}.${key}` : key; + + if (value.type === 'object') { + + return generateFormFields(value.properties!, fullFieldName); + + } else if (value.type === 'boolean') { + + return ( + )} + key={fullFieldName} + render={({ field }) => ( + + + {key} + + )} + /> + ); + + } else { + + return ( + )} + key={fullFieldName} + render={({ field }) => ( + + {value.title} + + + + {value.description} + + )} + /> + ); + } + }) + } + + ); + }; + + return ( +
+ + {generateFormFields(properties)} + {children} + +
+ + ); +} + +export default DynamicForm;