Skip to content

Commit f03f4f6

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

File tree

4 files changed

+102
-43
lines changed

4 files changed

+102
-43
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 name="Inertia React Form Example" lang="react">
7+
import { useState } from 'react'
8+
import { router } from '@inertiajs/react'
9+
10+
export default function Edit() {
11+
const [values, setValues] = useState({
12+
first_name: "",
13+
last_name: "",
14+
email: "",
15+
})
16+
17+
function handleChange(e) {
18+
const key = e.target.id;
19+
const value = e.target.value
20+
21+
setValues(values => ({
22+
...values,
23+
[key]: value,
24+
}))
25+
}
26+
27+
function handleSubmit(e) {
28+
e.preventDefault()
29+
30+
router.post('/users', values)
31+
}
32+
33+
return (
34+
<form onSubmit={handleSubmit}>
35+
<label htmlFor="first_name">First name:</label>
36+
<input id="first_name" value={values.first_name} onChange={handleChange} />
37+
<label htmlFor="last_name">Last name:</label>
38+
<input id="last_name" value={values.last_name} onChange={handleChange} />
39+
<label htmlFor="email">Email:</label>
40+
<input id="email" value={values.email} onChange={handleChange} />
41+
<button type="submit">Submit</button>
42+
</form>
43+
)
44+
}
45+
</code-snippet>
46+
@endverbatim

.ai/inertia-react/2/.gitkeep

Whitespace-only changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
@php
2+
/** @var \Laravel\Boost\Install\GuidelineAssist $assist */
3+
@endphp
4+
5+
## Forms in Inertia
6+
7+
@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+
11+
@boostsnippet("Example form using the `<Form>` component", "react")
12+
import { Form } from '@inertiajs/react'
13+
14+
export default () => (
15+
<Form action="/users" method="post">
16+
{({
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,
30+
}) => (
31+
<>
32+
<input type="text" name="name" />
33+
34+
{errors.name && <div>{errors.name}</div>}
35+
36+
<button type="submit" disabled={processing}>
37+
{processing ? 'Creating...' : 'Create User'}
38+
</button>
39+
40+
{wasSuccessful && <div>User created successfully!</div>}
41+
</>
42+
)}
43+
</Form>
44+
)
45+
@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
52+
@endif
53+
54+
@if($assist->inertia()->hasFormComponent() === false)
55+
- Forms can be built using the `useForm` helper. Use `search-docs` with `useForm helper` for guidance.
56+
@endif

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

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,3 @@
77

88
<Link href="/">Home</Link>
99
</code-snippet>
10-
11-
- For form handling, use `router.post` and related methods. Do not use regular forms.
12-
13-
<code-snippet lang="react" name="Inertia React Form Example">
14-
import { useState } from 'react'
15-
import { router } from '@inertiajs/react'
16-
17-
export default function Edit() {
18-
const [values, setValues] = useState({
19-
first_name: "",
20-
last_name: "",
21-
email: "",
22-
})
23-
24-
function handleChange(e) {
25-
const key = e.target.id;
26-
const value = e.target.value
27-
28-
setValues(values => ({
29-
...values,
30-
[key]: value,
31-
}))
32-
}
33-
34-
function handleSubmit(e) {
35-
e.preventDefault()
36-
37-
router.post('/users', values)
38-
}
39-
40-
return (
41-
<form onSubmit={handleSubmit}>
42-
<label htmlFor="first_name">First name:</label>
43-
<input id="first_name" value={values.first_name} onChange={handleChange} />
44-
<label htmlFor="last_name">Last name:</label>
45-
<input id="last_name" value={values.last_name} onChange={handleChange} />
46-
<label htmlFor="email">Email:</label>
47-
<input id="email" value={values.email} onChange={handleChange} />
48-
<button type="submit">Submit</button>
49-
</form>
50-
)
51-
}
52-
</code-snippet>

0 commit comments

Comments
 (0)