|
| 1 | +@php |
| 2 | + /** @var \Laravel\Boost\Install\GuidelineAssist $assist */ |
| 3 | +@endphp |
| 4 | +## Inertia v2 + 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 | + |
| 38 | +### Form with shadcn Field Components |
| 39 | +- Use Inertia's `<Form>` for state management and shadcn's `Field` components for styling. |
| 40 | +- Use Wayfinder's `.form()` method for type-safe action and method attributes. |
| 41 | +- Apply `data-invalid` to `Field` and `aria-invalid` to inputs when errors exist. |
| 42 | +- Use `FieldError` to display validation messages from Inertia's `errors` object. |
| 43 | + |
| 44 | + @boostsnippet("`<Form>` with Field Components", "react") |
| 45 | + import { Form } from '@inertiajs/react' |
| 46 | + import { store } from '@/actions/App/Http/Controllers/UserController' |
| 47 | + import { Input } from '@/components/ui/input' |
| 48 | + import { Button } from '@/components/ui/button' |
| 49 | + import { Spinner } from '@/components/ui/spinner' |
| 50 | + import { |
| 51 | + Field, |
| 52 | + FieldGroup, |
| 53 | + FieldLabel, |
| 54 | + FieldDescription, |
| 55 | + FieldError, |
| 56 | + } from '@/components/ui/field' |
| 57 | + |
| 58 | + export default () => ( |
| 59 | + <Form {...store.form()} disableWhileProcessing> |
| 60 | + {({ errors, processing }) => ( |
| 61 | + <FieldGroup> |
| 62 | + <Field data-invalid={!!errors.name}> |
| 63 | + <FieldLabel htmlFor="name">Full name</FieldLabel> |
| 64 | + <Input |
| 65 | + id="name" |
| 66 | + name="name" |
| 67 | + placeholder="John Doe" |
| 68 | + aria-invalid={!!errors.name} |
| 69 | + /> |
| 70 | + {errors.name && <FieldError>{errors.name}</FieldError>} |
| 71 | + </Field> |
| 72 | +
|
| 73 | + <Field data-invalid={!!errors.email}> |
| 74 | + <FieldLabel htmlFor="email">Email</FieldLabel> |
| 75 | + <Input |
| 76 | + id="email" |
| 77 | + name="email" |
| 78 | + type="email" |
| 79 | + placeholder="john@example.com" |
| 80 | + aria-invalid={!!errors.email} |
| 81 | + /> |
| 82 | + <FieldDescription>We'll never share your email.</FieldDescription> |
| 83 | + {errors.email && <FieldError>{errors.email}</FieldError>} |
| 84 | + </Field> |
| 85 | +
|
| 86 | + <Button type="submit" disabled={processing}> |
| 87 | + {processing && <Spinner />} |
| 88 | + Create User |
| 89 | + </Button> |
| 90 | + </FieldGroup> |
| 91 | + )} |
| 92 | + </Form> |
| 93 | + ) |
| 94 | + @endboostsnippet |
| 95 | +@endif |
| 96 | +
|
| 97 | +@if($assist->inertia()->hasFormComponent() === false) |
| 98 | + {{-- Inertia 2.0.x, not 2.1.0 or higher. So still need to use 'useForm' --}} |
| 99 | + @boostsnippet("Inertia React useForm Example", "react") |
| 100 | + import { useForm } from '@inertiajs/react' |
| 101 | +
|
| 102 | + const { data, setData, post, processing, errors } = useForm({ |
| 103 | + email: '', |
| 104 | + password: '', |
| 105 | + remember: false, |
| 106 | + }) |
| 107 | +
|
| 108 | + function submit(e) { |
| 109 | + e.preventDefault() |
| 110 | + post('/login') |
| 111 | + } |
| 112 | +
|
| 113 | + return ( |
| 114 | + <form onSubmit={submit}> |
| 115 | + <input type="text" value={data.email} onChange={e => setData('email', e.target.value)} /> |
| 116 | + {errors.email && <div>{errors.email}</div>} |
| 117 | + <input type="password" value={data.password} onChange={e => setData('password', e.target.value)} /> |
| 118 | + {errors.password && <div>{errors.password}</div>} |
| 119 | + <input type="checkbox" checked={data.remember} onChange={e => setData('remember', e.target.checked)} /> Remember Me |
| 120 | + <button type="submit" disabled={processing}>Login</button> |
| 121 | + </form> |
| 122 | + ) |
| 123 | + @endboostsnippet |
| 124 | +@endif |
| 125 | +
|
| 126 | +## shadcn/ui Button Component |
| 127 | +
|
| 128 | +### Icon Styling |
| 129 | +- Do NOT add margin classes (`mr-*`, `ml-*`, `mx-*`) to icons inside buttons. The button uses `gap-2` for spacing. |
| 130 | +- Do NOT add size classes (`w-*`, `h-*`, `size-*`) to icons unless a custom size is explicitly needed. The button sets `[&_svg:not([class*='size-'])]:size-4` as the default. |
| 131 | +- Icons inside buttons automatically inherit these styles from the button component: |
| 132 | + - `pointer-events-none` |
| 133 | + - `size-4` (default, ~16px) |
| 134 | + - `shrink-0` |
| 135 | +
|
| 136 | +### Examples |
| 137 | +@verbatim |
| 138 | +<code-snippet name="Correct Button with Icon" lang="tsx"> |
| 139 | +// Correct - no extra classes needed |
| 140 | +<Button> |
| 141 | + <Mail /> |
| 142 | + Send Email |
| 143 | +</Button> |
| 144 | +
|
| 145 | +// Correct - custom size when explicitly needed |
| 146 | +<Button> |
| 147 | + <Mail className="size-5" /> |
| 148 | + Send Email |
| 149 | +</Button> |
| 150 | +</code-snippet> |
| 151 | +
|
| 152 | +<code-snippet name="Incorrect Button with Icon" lang="tsx"> |
| 153 | +// Incorrect - unnecessary margin |
| 154 | +<Button> |
| 155 | + <Mail className="mr-2" /> |
| 156 | + Send Email |
| 157 | +</Button> |
| 158 | +
|
| 159 | +// Incorrect - unnecessary size (default is already size-4) |
| 160 | +<Button> |
| 161 | + <Mail className="h-4 w-4" /> |
| 162 | + Send Email |
| 163 | +</Button> |
| 164 | +</code-snippet> |
| 165 | +@endverbatim |
0 commit comments