Skip to content

Commit 1f4e775

Browse files
authored
Add submit (next page) simulation on form preview (#28)
Fixes #26. Revisit this with #29.
1 parent 06176d1 commit 1f4e775

2 files changed

Lines changed: 63 additions & 8 deletions

File tree

packages/design/src/Form/Form.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ export default function Form({
7878

7979
const formMethods = useForm<Record<string, string>>({});
8080

81+
// Reset React Hook Form when route changes (e.g., page navigation in FormPreview)
82+
// This prevents field values from bleeding across pages
83+
// Only reset when we have an onSubmit handler (FormPreview mode), not in FormEdit mode
84+
const hasOnSubmit = !!onSubmit;
85+
useEffect(() => {
86+
if (isPreview && hasOnSubmit) {
87+
formMethods.reset({});
88+
}
89+
}, [session.route?.params.page, isPreview, hasOnSubmit]);
90+
8191
return (
8292
<FormProvider {...formMethods}>
8393
<div className="preview grid-container">
@@ -114,6 +124,19 @@ export default function Form({
114124
>
115125
<FormContents context={context} prompt={prompt} />
116126
</form>
127+
) : onSubmit ? (
128+
<form
129+
className="usa-form margin-bottom-3 maxw-full formContentWrapper"
130+
encType="multipart/form-data"
131+
onSubmit={formMethods.handleSubmit(async (data, event) => {
132+
event?.preventDefault();
133+
onSubmit(data);
134+
})}
135+
method="POST"
136+
aria-label={session.form.summary.title || 'Form'}
137+
>
138+
<FormContents context={context} prompt={prompt} />
139+
</form>
117140
) : (
118141
<div className="formContentWrapper">
119142
<FormContents context={context} prompt={prompt} />
@@ -134,10 +157,8 @@ const FormContents = ({
134157
prompt: Prompt;
135158
}) => {
136159
return (
137-
<>
138-
<fieldset className="usa-fieldset width-full">
139-
{renderPromptComponents(context, prompt.components)}
140-
</fieldset>
141-
</>
160+
<fieldset className="usa-fieldset width-full">
161+
{renderPromptComponents(context, prompt.components)}
162+
</fieldset>
142163
);
143164
};

packages/design/src/FormManager/FormPreview/FormPreview.tsx

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useEffect } from 'react';
2+
import { useNavigate } from 'react-router-dom';
23

3-
import { mergeSession } from '@flexion/forms-core';
4+
import { applyPromptResponse, mergeSession } from '@flexion/forms-core';
45

56
import Form from '../../Form/Form.js';
67
import { useRouteParams } from '../hooks.js';
@@ -12,7 +13,8 @@ export const FormPreview = () => {
1213
setSession: state.setSession,
1314
}));
1415
const session = useFormManagerStore(state => state.session);
15-
const { routeParams } = useRouteParams();
16+
const { routeParams, pathname } = useRouteParams();
17+
const navigate = useNavigate();
1618

1719
useEffect(() => {
1820
if (routeParams.page !== session.route?.params.page) {
@@ -27,5 +29,37 @@ export const FormPreview = () => {
2729
}
2830
}, [routeParams.page]);
2931

30-
return <Form isPreview={true} context={context} session={session} />;
32+
const handleSubmit = (data: Record<string, string>) => {
33+
// Validate and update session with form data
34+
const result = applyPromptResponse(context.config, session, {
35+
action: 'submit',
36+
data,
37+
});
38+
39+
if (!result.success) {
40+
console.warn('Error applying prompt response in preview...', result.error);
41+
return;
42+
}
43+
44+
// Update session with validated data
45+
setSession(result.data);
46+
47+
// Navigate to next page
48+
const currentPage = Number(routeParams.page) || 0;
49+
const nextPage = currentPage + 1;
50+
const newParams = new URLSearchParams({
51+
...routeParams,
52+
page: nextPage.toString(),
53+
});
54+
navigate(`${pathname}?${newParams.toString()}`);
55+
};
56+
57+
return (
58+
<Form
59+
isPreview={true}
60+
context={context}
61+
session={session}
62+
onSubmit={handleSubmit}
63+
/>
64+
);
3165
};

0 commit comments

Comments
 (0)