Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated Remix Form Route with conform v1 #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the "remix-forge" extension will be documented in this file.

## [0.5.5]

- Updated Remix Form Route with conform v1

## [0.5.3]
- Added `clientLoader` generator
- Added `clientAction` generator
Expand Down
21 changes: 12 additions & 9 deletions src/forms/conform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { getConfig } from "../config";
export const generateConformForm = () => {
const runtimeDependency = getConfig().get("runtimeDependency") || "@remix-run/node";
return [
'import { useForm } from "@conform-to/react";',
'import { parse } from "@conform-to/zod";',
'import { getFormProps, useForm } from "@conform-to/react";',
'import { getZodConstraint, parseWithZod } from "@conform-to/zod";',
'import { Form, useActionData } from "@remix-run/react";',
`import { json } from "${runtimeDependency}";`,
`import type { ActionFunctionArgs } from "${runtimeDependency}";`,
Expand All @@ -16,27 +16,30 @@ export const generateConformForm = () => {
"",
"export async function action({ request }: ActionFunctionArgs) {",
" const formData = await request.formData();",
" const submission = parse(formData, { schema });",
" const submission = parseWithZod(formData, { schema });",
"",
" if (!submission.value || submission.intent !== 'submit') {",
" return json(submission, { status: 400 });",
' if (submission.status !== "success") {',
" return submission.reply();",
" }",
"",
" // Do something with the data",
" return json(submission);",
"}",
"",
"export default function FormRoute() {",
" const lastSubmission = useActionData<typeof action>();",
" const lastResult = useActionData<typeof action>();",
" const [form, fields] = useForm({",
" lastSubmission,",
" lastResult,",
" constraint: getZodConstraint(schema),",
" onValidate({ formData }) {",
" return parse(formData, { schema });",
" return parseWithZod(formData, { schema });",
" },",
" });",
"",
" return (",
' <Form method="post" {...form.props}>',
' <Form method="post" {...getFormProps(form)}>',
" <div id={form.errorId}>{form.errors}</div>",
"",
" {/** Your form elements here */}",
" <button>Submit</button>",
" </Form>",
Expand Down