MCPcopy Create free account
hub / github.com/code-forge-io/Remix-Forge / generateConformForm

Function generateConformForm

src/forms/conform.ts:3–46  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1import { getConfig } from "../config";
2
3export const generateConformForm = () => {
4 const runtimeDependency = getConfig().get("runtimeDependency") || "@remix-run/node";
5 return [
6 'import { useForm } from "@conform-to/react";',
7 'import { parse } from "@conform-to/zod";',
8 'import { Form, useActionData } from "@remix-run/react";',
9 `import { json } from "${runtimeDependency}";`,
10 `import type { ActionFunctionArgs } from "${runtimeDependency}";`,
11 'import { z } from "zod";',
12 "",
13 "const schema = z.object({",
14 " // Add your schema here",
15 "});",
16 "",
17 "export async function action({ request }: ActionFunctionArgs) {",
18 " const formData = await request.formData();",
19 " const submission = parse(formData, { schema });",
20 "",
21 " if (!submission.value || submission.intent !== 'submit') {",
22 " return json(submission, { status: 400 });",
23 " }",
24 "",
25 " // Do something with the data",
26 " return json(submission);",
27 "}",
28 "",
29 "export default function FormRoute() {",
30 " const lastSubmission = useActionData<typeof action>();",
31 " const [form, fields] = useForm({",
32 " lastSubmission,",
33 " onValidate({ formData }) {",
34 " return parse(formData, { schema });",
35 " },",
36 " });",
37 "",
38 " return (",
39 ' <Form method="post" {...form.props}>',
40 " {/** Your form elements here */}",
41 " <button>Submit</button>",
42 " </Form>",
43 " );",
44 "}",
45 ].join("\n");
46};

Callers 1

generateRemixFormRouteFunction · 0.90

Calls 1

getConfigFunction · 0.90

Tested by

no test coverage detected