40 lines
1.8 KiB
TypeScript
40 lines
1.8 KiB
TypeScript
// src/blocks/FormBlock/Component.tsx — SERVER part: rich text from the panel + bound action.
|
|
// Block config (src/blocks/FormBlock/config.ts): slug 'formBlock', fields
|
|
// { name: 'form', type: 'relationship', relationTo: 'forms', required: true } + the
|
|
// shared section fields (eyebrow/heading/intro, anchor, background) — ipal-block.
|
|
// Page query depth ≥ 1, otherwise `form` is just an ID. Wrap the output in the
|
|
// project's <Section>/<SectionHeader> like every other block.
|
|
// Blocks never import lib/* or app/* (AGENT.md §4.10) — texts and the server action
|
|
// come in through enhanceProps from the page route (SKILL.md, step 4).
|
|
import type { ReactNode } from 'react'
|
|
import type { FormNotificationTexts } from '@intecion/ipal-kit'
|
|
import { RichText } from '@/components/RichText'
|
|
import { FormRenderer, type BuilderForm, type FormAction } from '@/components/forms/FormRenderer'
|
|
|
|
type Props = {
|
|
form?: BuilderForm | string | number | null
|
|
locale: string
|
|
formTexts?: FormNotificationTexts
|
|
sendForm?: (formId: string, locale: string, values: Record<string, unknown>, token?: string) => ReturnType<FormAction>
|
|
}
|
|
|
|
export function FormBlock({ form, locale, formTexts, sendForm }: Props) {
|
|
if (!form || typeof form !== 'object' || !formTexts || !sendForm) return null // never a fake/hardcoded form
|
|
|
|
const messages: Record<number, ReactNode> = {}
|
|
form.fields?.forEach((f, i) => {
|
|
if (f.blockType === 'message' && f.message) messages[i] = <RichText data={f.message as never} />
|
|
})
|
|
const confirmation = form.confirmationMessage ? <RichText data={form.confirmationMessage as never} /> : null
|
|
|
|
return (
|
|
<FormRenderer
|
|
form={form}
|
|
messages={messages}
|
|
confirmation={confirmation}
|
|
validationText={formTexts.validation}
|
|
action={sendForm.bind(null, String(form.id), locale)}
|
|
/>
|
|
)
|
|
}
|