2023-05-11 20:49:28 +02:00
|
|
|
import { useTranslationContext } from "@gnu-taler/web-util/browser";
|
2023-05-25 23:08:20 +02:00
|
|
|
import { ComponentChildren, Fragment, h } from "preact";
|
2023-05-15 16:45:23 +02:00
|
|
|
import { FlexibleForm } from "./forms/index.js";
|
|
|
|
import { FormProvider } from "./handlers/FormProvider.js";
|
|
|
|
import { RenderAllFieldsByUiConfig } from "./handlers/forms.js";
|
|
|
|
|
|
|
|
export function NiceForm<T extends object>({
|
|
|
|
initial,
|
|
|
|
onUpdate,
|
|
|
|
form,
|
2023-05-25 23:08:20 +02:00
|
|
|
onSubmit,
|
|
|
|
children,
|
2023-05-15 16:45:23 +02:00
|
|
|
}: {
|
2023-05-25 23:08:20 +02:00
|
|
|
children?: ComponentChildren;
|
2023-05-15 16:45:23 +02:00
|
|
|
initial: Partial<T>;
|
2023-05-25 23:08:20 +02:00
|
|
|
onSubmit?: (v: T) => void;
|
2023-05-15 16:45:23 +02:00
|
|
|
form: FlexibleForm<T>;
|
2023-05-25 23:08:20 +02:00
|
|
|
onUpdate?: (d: Partial<T>) => void;
|
2023-05-15 16:45:23 +02:00
|
|
|
}) {
|
2023-05-11 20:49:28 +02:00
|
|
|
return (
|
2023-05-15 20:38:04 +02:00
|
|
|
<FormProvider
|
|
|
|
initialValue={initial}
|
|
|
|
onUpdate={onUpdate}
|
2023-05-25 23:08:20 +02:00
|
|
|
onSubmit={onSubmit}
|
2023-05-15 20:38:04 +02:00
|
|
|
computeFormState={form.behavior}
|
|
|
|
>
|
|
|
|
<div class="space-y-10 divide-y -mt-5 divide-gray-900/10">
|
|
|
|
{form.design.map((section, i) => {
|
2023-05-25 23:08:20 +02:00
|
|
|
if (!section) return <Fragment />;
|
2023-05-15 20:38:04 +02:00
|
|
|
return (
|
|
|
|
<div class="grid grid-cols-1 gap-x-8 gap-y-8 pt-5 md:grid-cols-3">
|
|
|
|
<div class="px-4 sm:px-0">
|
|
|
|
<h2 class="text-base font-semibold leading-7 text-gray-900">
|
|
|
|
{section.title}
|
|
|
|
</h2>
|
|
|
|
{section.description && (
|
|
|
|
<p class="mt-1 text-sm leading-6 text-gray-600">
|
|
|
|
{section.description}
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div class="bg-white shadow-sm ring-1 ring-gray-900/5 rounded-md md:col-span-2">
|
|
|
|
<div class="p-3">
|
|
|
|
<div class="grid max-w-2xl grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
|
|
|
|
<RenderAllFieldsByUiConfig
|
|
|
|
key={i}
|
|
|
|
fields={section.fields}
|
|
|
|
/>
|
2023-05-11 20:49:28 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-05-15 20:38:04 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
2023-05-25 23:08:20 +02:00
|
|
|
{children}
|
2023-05-15 20:38:04 +02:00
|
|
|
</FormProvider>
|
2023-05-11 20:49:28 +02:00
|
|
|
);
|
|
|
|
}
|