-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-form): support Remix SSR (#1017)
* feat(react-form): support Remix SSR * update deps, update createServerValidate, fix ci errors * docs: add remix example to the docs sidebar --------- Co-authored-by: Leonardo Montini <lion.48m@gmail.com>
- Loading branch information
1 parent
ae97ec3
commit 56d065a
Showing
17 changed files
with
2,558 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
|
||
/.cache | ||
/build | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Welcome to Remix! | ||
|
||
- 📖 [Remix docs](https://remix.run/docs) | ||
|
||
## Development | ||
|
||
Run the dev server: | ||
|
||
```shellscript | ||
npm run dev | ||
``` | ||
|
||
## Deployment | ||
|
||
First, build your app for production: | ||
|
||
```sh | ||
npm run build | ||
``` | ||
|
||
Then run the app in production mode: | ||
|
||
```sh | ||
npm start | ||
``` | ||
|
||
Now you'll need to pick a host to deploy it to. | ||
|
||
### DIY | ||
|
||
If you're familiar with deploying Node applications, the built-in Remix app server is production-ready. | ||
|
||
Make sure to deploy the output of `npm run build` | ||
|
||
- `build/server` | ||
- `build/client` | ||
|
||
## Styling | ||
|
||
This template comes with [Tailwind CSS](https://tailwindcss.com/) already configured for a simple default starting experience. You can use whatever css framework you prefer. See the [Vite docs on css](https://vitejs.dev/guide/features.html#css) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { | ||
Links, | ||
Meta, | ||
Outlet, | ||
Scripts, | ||
ScrollRestoration, | ||
} from '@remix-run/react' | ||
|
||
export function Layout({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<meta charSet="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<Meta /> | ||
<Links /> | ||
</head> | ||
<body> | ||
{children} | ||
<ScrollRestoration /> | ||
<Scripts /> | ||
</body> | ||
</html> | ||
) | ||
} | ||
|
||
export default function App() { | ||
return <Outlet /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Form, useActionData } from '@remix-run/react' | ||
|
||
import { mergeForm, useForm, useTransform } from '@tanstack/react-form' | ||
import { | ||
ServerValidateError, | ||
createServerValidate, | ||
formOptions, | ||
initialFormState, | ||
} from '@tanstack/react-form/remix' | ||
|
||
import type { ActionFunctionArgs } from '@remix-run/node' | ||
|
||
const formOpts = formOptions({ | ||
defaultValues: { | ||
firstName: '', | ||
age: 0, | ||
}, | ||
}) | ||
|
||
const serverValidate = createServerValidate({ | ||
...formOpts, | ||
onServerValidate: ({ value }) => { | ||
if (value.age < 12) { | ||
return 'Server validation: You must be at least 12 to sign up' | ||
} | ||
}, | ||
}) | ||
|
||
export async function action({ request }: ActionFunctionArgs) { | ||
const formData = await request.formData() | ||
try { | ||
await serverValidate(formData) | ||
} catch (e) { | ||
if (e instanceof ServerValidateError) { | ||
return e.formState | ||
} | ||
|
||
// Some other error occurred while validating your form | ||
throw e | ||
} | ||
|
||
// Your form has successfully validated! | ||
} | ||
|
||
export default function Index() { | ||
const actionData = useActionData<typeof action>() | ||
|
||
const form = useForm({ | ||
...formOpts, | ||
transform: useTransform( | ||
(baseForm) => mergeForm(baseForm, actionData ?? initialFormState), | ||
[actionData], | ||
), | ||
}) | ||
const formErrors = form.useStore((formState) => formState.errors) | ||
|
||
return ( | ||
<Form method="post" onSubmit={() => form.handleSubmit()}> | ||
{formErrors.map((error) => ( | ||
<p key={error as string}>{error}</p> | ||
))} | ||
|
||
<form.Field | ||
name="age" | ||
validators={{ | ||
onChange: ({ value }) => | ||
value < 8 ? 'Client validation: You must be at least 8' : undefined, | ||
}} | ||
> | ||
{(field) => { | ||
return ( | ||
<div> | ||
<input | ||
name="age" | ||
type="number" | ||
value={field.state.value} | ||
onChange={(e) => field.handleChange(e.target.valueAsNumber)} | ||
/> | ||
{field.state.meta.errors.map((error) => ( | ||
<p key={error as string}>{error}</p> | ||
))} | ||
</div> | ||
) | ||
}} | ||
</form.Field> | ||
<form.Subscribe | ||
selector={(formState) => [formState.canSubmit, formState.isSubmitting]} | ||
> | ||
{([canSubmit, isSubmitting]) => ( | ||
<button type="submit" disabled={!canSubmit}> | ||
{isSubmitting ? '...' : 'Submit'} | ||
</button> | ||
)} | ||
</form.Subscribe> | ||
</Form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "@tanstack/form-example-remix", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"build": "remix vite:build", | ||
"dev": "remix vite:dev", | ||
"_test:types": "tsc" | ||
}, | ||
"dependencies": { | ||
"@remix-run/node": "^2.14.0", | ||
"@remix-run/react": "^2.14.0", | ||
"@remix-run/serve": "^2.14.0", | ||
"@tanstack/react-form": "^0.35.0", | ||
"isbot": "^4.1.0", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@remix-run/dev": "^2.11.2", | ||
"@types/react": "^18.3.3", | ||
"@types/react-dom": "^18.3.0", | ||
"typescript": "5.4.5", | ||
"vite": "^5.4.10", | ||
"vite-tsconfig-paths": "^5.1.2" | ||
}, | ||
"engines": { | ||
"node": ">=20.0.0" | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.