Skip to content

Commit 6276119

Browse files
authored
✨ feat: Add webform resolver for nextjs (#158)
1 parent 5603c84 commit 6276119

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

starters/next/integration/forms/ContactForm/ContactForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import { contactFormSchema } from '@/integration/forms/ContactForm/schema'
1010
import { useActionState } from 'react'
1111
import { submitContactFormAction } from '@/integration/forms/ContactForm/action'
1212

13-
export const ContactForm = () => {
13+
export const ContactForm = ({ id }: { id: string }) => {
1414
const [state, action] = useActionState(submitContactFormAction, undefined)
1515
const [form, fields] = useForm({
16-
id: 'contact-form',
16+
id,
1717
lastResult: state?.reply,
1818
constraint: getZodConstraint(contactFormSchema),
1919
onValidate({ formData }) {

starters/next/integration/resolvers/ParagraphWebformResolver.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@ export const ParagraphWebformFragment = graphql(
2626
[WebformFragment]
2727
)
2828

29+
type FormComponents = React.FC<{ id: string }>
30+
31+
const WebformComponentResolver: Record<string, FormComponents> = {
32+
contact_form: ContactForm,
33+
}
34+
2935
export const ParagraphWebformResolver = ({
3036
paragraph,
3137
}: ParagraphWebformProps) => {
3238
const { heading, subheadingOptional, descriptionOptional, form } =
3339
readFragment(ParagraphWebformFragment, paragraph)
3440

41+
if (!form || !form.id) {
42+
return null
43+
}
44+
45+
const Webform = WebformComponentResolver[form.id]
3546
return (
3647
<div className="container mx-auto py-8 md:py-16 lg:py-24">
3748
<div>
@@ -47,11 +58,9 @@ export const ParagraphWebformResolver = ({
4758
dangerouslySetInnerHTML={{ __html: descriptionOptional }}
4859
/>
4960
)}
50-
{form && (
51-
<div className="py-8 md:py-16 lg:py-24">
52-
<ContactForm />
53-
</div>
54-
)}
61+
<div className="py-8 md:py-16 lg:py-24">
62+
<Webform id={form.id} />
63+
</div>
5564
</div>
5665
</div>
5766
)

0 commit comments

Comments
 (0)