import { DynamicFormField, type FieldProps, } from "@components/Form/DynamicFormField.js"; import { FieldWrapper } from "@components/Form/FormWrapper.js"; import { Button } from "@components/UI/Button.js"; import { H4 } from "@components/UI/Typography/H4.js"; import { Subtle } from "@components/UI/Typography/Subtle.js"; import { type Control, type DefaultValues, type FieldValues, type Path, type SubmitHandler, useForm, } from "react-hook-form"; interface DisabledBy { fieldName: Path; selector?: number; invert?: boolean; } export interface BaseFormBuilderProps { name: Path; disabled?: boolean; disabledBy?: DisabledBy[]; label: string; description?: string; validationText?: string; properties?: Record; } export interface GenericFormElementProps { control: Control; disabled?: boolean; field: Y; } export interface DynamicFormProps { onSubmit: SubmitHandler; submitType?: "onChange" | "onSubmit"; hasSubmitButton?: boolean; defaultValues?: DefaultValues; fieldGroups: { label: string; description: string; valid?: boolean; validationText?: string; fields: FieldProps[]; }[]; } export function DynamicForm({ onSubmit, submitType = "onChange", hasSubmitButton, defaultValues, fieldGroups, }: DynamicFormProps) { const { handleSubmit, control, getValues } = useForm({ mode: submitType, defaultValues: defaultValues, }); const isDisabled = ( disabledBy?: DisabledBy[], disabled?: boolean, ): boolean => { if (disabled) return true; if (!disabledBy) return false; return disabledBy.some((field) => { const value = getValues(field.fieldName); if (value === "always") return true; if (typeof value === "boolean") return field.invert ? value : !value; if (typeof value === "number") return field.invert ? field.selector !== value : field.selector === value; return false; }); }; return (
{fieldGroups.map((fieldGroup) => (

{fieldGroup.label}

{fieldGroup.description}
{fieldGroup.fields.map((field) => ( ))}
))} {hasSubmitButton && }
); }