Skip to content

Commit abd2ca5

Browse files
committed
guidelines: inertia-react: v0.1 of 'forms' guidelines
1 parent dd86b92 commit abd2ca5

File tree

13 files changed

+272
-62
lines changed

13 files changed

+272
-62
lines changed

.ai/inertia-laravel/2/core.blade.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@
1111

1212
### Deferred Props & Empty States
1313
- When using deferred props on the frontend, you should add a nice empty state with pulsing / animated skeleton.
14+
15+
## Inertia Forms Core
16+
@if($assist->inertia()->hasFormComponent())
17+
- The recommended way to build forms when using Inertia is with the `<Form>` component, a useful example is below. Use `search-docs` with the `form component` query for guidance.
18+
- Forms can also be built using the `useForm` helper for more programmatic control, or to follow existing conventions. Use `search-docs` with the `useForm helper` query for guidance.
19+
@if($assist->inertia()->hasFormComponentResets())
20+
- `resetOnError`, `resetOnSuccess`, and `setDefaultsOnSuccess` are available on the `<Form>` component. Use `search-docs` with 'form component resetting' for explicit guidance.
21+
@else
22+
- This version of Inertia does NOT support `resetOnError`, `resetOnSuccess`, or `setDefaultsOnSuccess` on the `<Form>` component. Using these will cause errors.
23+
@endif
24+
@endif

.ai/inertia-laravel/core.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Inertia.js components should be placed in the `resources/js/Pages` directory unless specified differently in the JS bundler (vite.config.js).
44
- Use `Inertia::render()` for server-side routing instead of traditional Blade views.
5+
- Use `search-docs` for accurate guidance on all things Inertia.
56

67
<code-snippet lang="php" name="Inertia::render Example">
78
// routes/web.php example

.ai/inertia-react/1/.gitkeep

Whitespace-only changes.

.ai/inertia-react/1/forms.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Forms in Inertia
22

3-
- For form handling, use `router.post` and related methods. Do not use regular forms.
3+
- For form handling in Inertia pages, use `router.post` and related methods. Do not use regular forms.
44

55
@verbatim
66
<code-snippet name="Inertia React Form Example" lang="react">
Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
@php
22
/** @var \Laravel\Boost\Install\GuidelineAssist $assist */
33
@endphp
4-
54
## Forms in Inertia
65

76
@if($assist->inertia()->hasFormComponent())
8-
- The recommended way to build forms when using Inertia is with the `<Form>` component. Use `search-docs` with `form component` for guidance.
9-
- Forms can also be built using the `useForm` helper for more programmatic control, or to follow existing conventions. Use `search-docs` with `useForm helper` for guidance.
10-
117
@boostsnippet("Example form using the `<Form>` component", "react")
128
import { Form } from '@inertiajs/react'
139

1410
export default () => (
1511
<Form action="/users" method="post">
1612
{({
17-
errors,
18-
hasErrors,
19-
processing,
20-
progress,
21-
wasSuccessful,
22-
recentlySuccessful,
23-
setError,
24-
clearErrors,
25-
resetAndClearErrors,
26-
defaults,
27-
isDirty,
28-
reset,
29-
submit,
13+
errors,
14+
hasErrors,
15+
processing,
16+
wasSuccessful,
17+
recentlySuccessful,
18+
clearErrors,
19+
resetAndClearErrors,
20+
defaults
3021
}) => (
3122
<>
3223
<input type="text" name="name" />
@@ -43,14 +34,33 @@
4334
</Form>
4435
)
4536
@endboostsnippet
46-
47-
@if($assist->inertia()->hasFormComponentResets())
48-
- Added `resetOnError`, `resetOnSuccess`, and `setDefaultsOnSuccess` to the `<Form>` component. Use `search-docs` with 'form component resetting' for explicit guidance.
49-
@else
50-
- This version of Inertia does NOT support `resetOnError`, `resetOnSuccess`, or `setDefaultsOnSuccess` on the `<Form>` component.
51-
@endif
5237
@endif
5338

5439
@if($assist->inertia()->hasFormComponent() === false)
55-
- Forms can be built using the `useForm` helper. Use `search-docs` with `useForm helper` for guidance.
40+
{{-- Inertia 2.0.x, not 2.1.0 or higher. So they still need to use 'useForm' --}}
41+
@boostsnippet("Inertia React useForm example", "react")
42+
import { useForm } from '@inertiajs/react'
43+
44+
const { data, setData, post, processing, errors } = useForm({
45+
email: '',
46+
password: '',
47+
remember: false,
48+
})
49+
50+
function submit(e) {
51+
e.preventDefault()
52+
post('/login')
53+
}
54+
55+
return (
56+
<form onSubmit={submit}>
57+
<input type="text" value={data.email} onChange={e => setData('email', e.target.value)} />
58+
{errors.email && <div>{errors.email}</div>}
59+
<input type="password" value={data.password} onChange={e => setData('password', e.target.value)} />
60+
{errors.password && <div>{errors.password}</div>}
61+
<input type="checkbox" checked={data.remember} onChange={e => setData('remember', e.target.checked)} /> Remember Me
62+
<button type="submit" disabled={processing}>Login</button>
63+
</form>
64+
)
65+
@endboostsnippet
5666
@endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Forms in Inertia
2+
3+
- For form handling, use `router.post` and related methods. Do not use regular forms.
4+
5+
@verbatim
6+
<code-snippet lang="svelte" name="Inertia Form Example">
7+
<script>
8+
import { router } from '@inertiajs/svelte'
9+
10+
let values = {
11+
first_name: null,
12+
last_name: null,
13+
email: null,
14+
}
15+
16+
function handleSubmit() {
17+
router.post('/users', values)
18+
}
19+
</script>
20+
21+
<form on:submit|preventDefault={handleSubmit}>
22+
<label for="first_name">First name:</label>
23+
<input id="first_name" bind:value={values.first_name}>
24+
25+
<label for="last_name">Last name:</label>
26+
<input id="last_name" bind:value={values.last_name}>
27+
28+
<label for="email">Email:</label>
29+
<input id="email" bind:value={values.email}>
30+
31+
<button type="submit">Submit</button>
32+
</form>
33+
</code-snippet>
34+
@endverbatim
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
@php
2+
/** @var \Laravel\Boost\Install\GuidelineAssist $assist */
3+
@endphp
4+
## Forms in Inertia
5+
- There are critical differences between Svelte 4 and 5, use the `search-docs` tool for up-to-date guidance.
6+
@if($assist->inertia()->hasFormComponent())
7+
@boostsnippet("Example form using the `<Form>` component", "svelte5")
8+
<Form action="/users" method="post">
9+
{#snippet children({
10+
errors,
11+
hasErrors,
12+
processing,
13+
progress,
14+
wasSuccessful,
15+
recentlySuccessful,
16+
setError,
17+
clearErrors,
18+
resetAndClearErrors,
19+
defaults,
20+
isDirty,
21+
reset,
22+
submit,
23+
})}
24+
<input type="text" name="name" />
25+
26+
{#if errors.name}
27+
<div>{errors.name}</div>
28+
{/if}
29+
30+
<button type="submit" disabled={processing}>
31+
{processing ? 'Creating...' : 'Create User'}
32+
</button>
33+
34+
{#if wasSuccessful}
35+
<div>User created successfully!</div>
36+
{/if}
37+
{/snippet}
38+
</Form>
39+
@endboostsnippet
40+
@endif
41+
42+
@if($assist->inertia()->hasFormComponent() === false)
43+
{{-- Inertia 2.0.x, not 2.1.0 or higher. So they still need to use 'useForm' --}}
44+
@boostsnippet("Inertia React useForm example", "svelte")
45+
<script>
46+
import { useForm } from '@inertiajs/svelte'
47+
48+
const form = useForm({
49+
email: null,
50+
password: null,
51+
remember: false,
52+
})
53+
54+
function submit(e) {
55+
e.preventDefault() /* Only required with Svelte 5 */
56+
$form.post('/login')
57+
}
58+
</script>
59+
60+
<form onsubmit={submit}>
61+
<input type="text" bind:value={$form.email} />
62+
{#if $form.errors.email}
63+
<div class="form-error">{$form.errors.email}</div>
64+
{/if}
65+
<input type="password" bind:value={$form.password} />
66+
{#if $form.errors.password}
67+
<div class="form-error">{$form.errors.password}</div>
68+
{/if}
69+
<input type="checkbox" bind:checked={$form.remember} /> Remember Me
70+
<button type="submit" disabled={$form.processing}>Submit</button>
71+
</form>
72+
@endboostsnippet
73+
@endif

.ai/inertia-svelte/core.blade.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Inertia + Svelte
2+
3+
- Use `router.visit()` or `<Link>` for navigation instead of traditional links.
4+
5+
@boostsnippet("Inertia Client Navigation", "svelte")
6+
import { inertia, Link } from '@inertiajs/svelte'
7+
8+
<a href="/" use:inertia>Home</a>
9+
<Link href="/">Home</Link>
10+
@endboostsnippet

.ai/inertia-vue/1/.gitkeep

Whitespace-only changes.

.ai/inertia-vue/1/forms.blade.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Forms in Inertia
2+
3+
- For form handling in Inertia pages, use `router.post` and related methods. Do not use regular forms.
4+
5+
@verbatim
6+
<code-snippet lang="vue" name="Inertia Vue Form Example">
7+
<script setup>
8+
import { reactive } from 'vue'
9+
import { router } from '@inertiajs/vue3'
10+
import { usePage } from '@inertiajs/vue3'
11+
12+
const page = usePage()
13+
14+
const form = reactive({
15+
first_name: null,
16+
last_name: null,
17+
email: null,
18+
})
19+
20+
function submit() {
21+
router.post('/users', form)
22+
}
23+
</script>
24+
25+
<template>
26+
<h1>Create {{ page.modelName }}</h1>
27+
<form @submit.prevent="submit">
28+
<label for="first_name">First name:</label>
29+
<input id="first_name" v-model="form.first_name" />
30+
<label for="last_name">Last name:</label>
31+
<input id="last_name" v-model="form.last_name" />
32+
<label for="email">Email:</label>
33+
<input id="email" v-model="form.email" />
34+
<button type="submit">Submit</button>
35+
</form>
36+
</template>
37+
</code-snippet>
38+
@endverbatim

0 commit comments

Comments
 (0)