|
| 1 | +@php |
| 2 | +/** @var \Laravel\Boost\Install\GuidelineAssist $assist */ |
| 3 | +@endphp |
| 4 | +## Inertia + React Forms |
| 5 | + |
| 6 | +@if($assist->inertia()->hasFormComponent()) |
| 7 | +@boostsnippet("`<Form>` Component Example", "react") |
| 8 | +import { Form } from '@inertiajs/react' |
| 9 | + |
| 10 | +export default () => ( |
| 11 | + <Form action="/users" method="post"> |
| 12 | + {({ |
| 13 | + errors, |
| 14 | + hasErrors, |
| 15 | + processing, |
| 16 | + wasSuccessful, |
| 17 | + recentlySuccessful, |
| 18 | + clearErrors, |
| 19 | + resetAndClearErrors, |
| 20 | + defaults |
| 21 | + }) => ( |
| 22 | + <> |
| 23 | + <input type="text" name="name" /> |
| 24 | + |
| 25 | + {errors.name && <div>{errors.name}</div>} |
| 26 | + |
| 27 | + <button type="submit" disabled={processing}> |
| 28 | + {processing ? 'Creating...' : 'Create User'} |
| 29 | + </button> |
| 30 | + |
| 31 | + {wasSuccessful && <div>User created successfully!</div>} |
| 32 | + </> |
| 33 | + )} |
| 34 | + </Form> |
| 35 | +) |
| 36 | +@endboostsnippet |
| 37 | +@endif |
| 38 | + |
| 39 | +@if($assist->inertia()->hasFormComponent() === false) |
| 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 |
| 66 | +@endif |
0 commit comments