|
1
|
--- |
|
2
|
sidebar_label: "Route Drawer" |
|
3
|
--- |
|
4
|
|
|
5
|
import { Table, TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `RouteDrawer - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
This component is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
The `RouteDrawer` component renders a drawer that slides in from the side of the screen. The Medusa Admin uses it for edit forms, such as the form that edits a customer's details. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { RouteDrawer } from "@medusajs/dashboard/components" |
|
23
|
``` |
|
24
|
|
|
25
|
--- |
|
26
|
|
|
27
|
## Route Modals |
|
28
|
|
|
29
|
`RouteDrawer` is tied to a route, not to a trigger button. You render it as the root element of a [UI route](!docs!/learn/fundamentals/admin/ui-routes) that is nested under the page it edits. When the user navigates to that route, the drawer opens. When the user closes the drawer, `RouteDrawer` navigates back to the previous route. |
|
30
|
|
|
31
|
So, to open the drawer, link the user to the drawer's route. For example, link to `/brands/:id/edit` from the brand's details page. |
|
32
|
|
|
33
|
--- |
|
34
|
|
|
35
|
## Example |
|
36
|
|
|
37
|
Consider a UI route at `src/admin/routes/brands/[id]/edit/page.tsx` that edits a brand: |
|
38
|
|
|
39
|
```tsx title="src/admin/routes/brands/[id]/edit/page.tsx" |
|
40
|
import { Heading } from "@medusajs/ui" |
|
41
|
import { RouteDrawer } from "@medusajs/dashboard/components" |
|
42
|
import { useParams } from "react-router-dom" |
|
43
|
|
|
44
|
const BrandEdit = () => { |
|
45
|
const { id } = useParams() |
|
46
|
|
|
47
|
return ( |
|
48
|
<RouteDrawer> |
|
49
|
<RouteDrawer.Header> |
|
50
|
<RouteDrawer.Title asChild> |
|
51
|
<Heading>Edit Brand</Heading> |
|
52
|
</RouteDrawer.Title> |
|
53
|
</RouteDrawer.Header> |
|
54
|
<EditBrandForm id={id!} /> |
|
55
|
</RouteDrawer> |
|
56
|
) |
|
57
|
} |
|
58
|
|
|
59
|
export default BrandEdit |
|
60
|
``` |
|
61
|
|
|
62
|
`EditBrandForm` renders the form's fields, as explained in the [useRouteModal](#useroutemodal) section. |
|
63
|
|
|
64
|
--- |
|
65
|
|
|
66
|
## Props |
|
67
|
|
|
68
|
`RouteDrawer` accepts the following props, in addition to its children: |
|
69
|
|
|
70
|
<TypeList |
|
71
|
types={[ |
|
72
|
{ |
|
73
|
name: "prev", |
|
74
|
type: "`string` \\| `object` \\| `number`", |
|
75
|
optional: true, |
|
76
|
defaultValue: "..", |
|
77
|
description: |
|
78
|
"The route to navigate to when the drawer closes. Pass a string, such as `\"../..\"`, or an object with the path's parts. You can also pass a number, such as `-1`, to go back that many entries in the browser's history instead of navigating to a route.", |
|
79
|
children: [ |
|
80
|
{ |
|
81
|
name: "pathname", |
|
82
|
type: "`string`", |
|
83
|
optional: true, |
|
84
|
description: "The path to navigate to, such as `/brands`.", |
|
85
|
}, |
|
86
|
{ |
|
87
|
name: "search", |
|
88
|
type: "`string`", |
|
89
|
optional: true, |
|
90
|
description: |
|
91
|
"The query string to append to the path, such as `?order=created_at`.", |
|
92
|
}, |
|
93
|
{ |
|
94
|
name: "hash", |
|
95
|
type: "`string`", |
|
96
|
optional: true, |
|
97
|
description: "The hash to append to the path, such as `#details`.", |
|
98
|
}, |
|
99
|
], |
|
100
|
}, |
|
101
|
]} |
|
102
|
/> |
|
103
|
|
|
104
|
--- |
|
105
|
|
|
106
|
## Sub-components |
|
107
|
|
|
108
|
<Table> |
|
109
|
<Table.Header> |
|
110
|
<Table.Row> |
|
111
|
<Table.HeaderCell> |
|
112
|
Component |
|
113
|
</Table.HeaderCell> |
|
114
|
<Table.HeaderCell> |
|
115
|
Description |
|
116
|
</Table.HeaderCell> |
|
117
|
</Table.Row> |
|
118
|
</Table.Header> |
|
119
|
<Table.Body> |
|
120
|
<Table.Row> |
|
121
|
<Table.Cell> |
|
122
|
|
|
123
|
`RouteDrawer.Header` |
|
124
|
|
|
125
|
</Table.Cell> |
|
126
|
<Table.Cell> |
|
127
|
|
|
128
|
The drawer's header. Place the title and description in it. |
|
129
|
|
|
130
|
</Table.Cell> |
|
131
|
</Table.Row> |
|
132
|
<Table.Row> |
|
133
|
<Table.Cell> |
|
134
|
|
|
135
|
`RouteDrawer.Title` |
|
136
|
|
|
137
|
</Table.Cell> |
|
138
|
<Table.Cell> |
|
139
|
|
|
140
|
The drawer's title. Pass `asChild` to render your own heading element. |
|
141
|
|
|
142
|
</Table.Cell> |
|
143
|
</Table.Row> |
|
144
|
<Table.Row> |
|
145
|
<Table.Cell> |
|
146
|
|
|
147
|
`RouteDrawer.Description` |
|
148
|
|
|
149
|
</Table.Cell> |
|
150
|
<Table.Cell> |
|
151
|
|
|
152
|
An accessible description of the drawer's purpose. |
|
153
|
|
|
154
|
</Table.Cell> |
|
155
|
</Table.Row> |
|
156
|
<Table.Row> |
|
157
|
<Table.Cell> |
|
158
|
|
|
159
|
`RouteDrawer.Body` |
|
160
|
|
|
161
|
</Table.Cell> |
|
162
|
<Table.Cell> |
|
163
|
|
|
164
|
The drawer's scrollable content area. |
|
165
|
|
|
166
|
</Table.Cell> |
|
167
|
</Table.Row> |
|
168
|
<Table.Row> |
|
169
|
<Table.Cell> |
|
170
|
|
|
171
|
`RouteDrawer.Footer` |
|
172
|
|
|
173
|
</Table.Cell> |
|
174
|
<Table.Cell> |
|
175
|
|
|
176
|
The drawer's footer. Place the cancel and submit buttons in it. |
|
177
|
|
|
178
|
</Table.Cell> |
|
179
|
</Table.Row> |
|
180
|
<Table.Row> |
|
181
|
<Table.Cell> |
|
182
|
|
|
183
|
`RouteDrawer.Close` |
|
184
|
|
|
185
|
</Table.Cell> |
|
186
|
<Table.Cell> |
|
187
|
|
|
188
|
Closes the drawer when clicked, which navigates back to the `prev` route. Pass `asChild` to use your own button. |
|
189
|
|
|
190
|
</Table.Cell> |
|
191
|
</Table.Row> |
|
192
|
<Table.Row> |
|
193
|
<Table.Cell> |
|
194
|
|
|
195
|
`RouteDrawer.Form` |
|
196
|
|
|
197
|
</Table.Cell> |
|
198
|
<Table.Cell> |
|
199
|
|
|
200
|
Wraps a [React Hook Form](https://react-hook-form.com/) form. It provides the form's context to the fields, and shows a confirmation prompt if the user leaves the route while the form has unsaved changes. |
|
201
|
|
|
202
|
</Table.Cell> |
|
203
|
</Table.Row> |
|
204
|
</Table.Body> |
|
205
|
</Table> |
|
206
|
|
|
207
|
### RouteDrawer.Form Props |
|
208
|
|
|
209
|
<TypeList |
|
210
|
types={[ |
|
211
|
{ |
|
212
|
name: "form", |
|
213
|
type: "`object`", |
|
214
|
optional: false, |
|
215
|
description: `The form instance returned by the \`useForm\` hook of [React Hook Form](https://react-hook-form.com/). Refer to [React Hook Form's documentation](https://react-hook-form.com/docs/useform) for all of its properties.`, |
|
216
|
children: [ |
|
217
|
{ |
|
218
|
name: "control", |
|
219
|
type: "`object`", |
|
220
|
optional: false, |
|
221
|
description: |
|
222
|
"The object that registers the form's fields and manages their state. Pass it to the fields you render in the form.", |
|
223
|
}, |
|
224
|
{ |
|
225
|
name: "formState", |
|
226
|
type: "`object`", |
|
227
|
optional: false, |
|
228
|
description: |
|
229
|
"The form's state, including whether it has unsaved changes. The modal uses it to decide whether to show the unsaved-changes prompt.", |
|
230
|
children: [ |
|
231
|
{ |
|
232
|
name: "isDirty", |
|
233
|
type: "`boolean`", |
|
234
|
optional: false, |
|
235
|
description: |
|
236
|
"Whether the user changed any of the form's fields.", |
|
237
|
}, |
|
238
|
], |
|
239
|
}, |
|
240
|
{ |
|
241
|
name: "handleSubmit", |
|
242
|
type: "`(onValid: (data: object) => unknown, onInvalid?: (errors: object) => unknown) => (event?: React.BaseSyntheticEvent) => Promise<void>`", |
|
243
|
optional: false, |
|
244
|
description: |
|
245
|
"Creates the submit handler that you pass to the `onSubmit` prop of the `form` element. It validates the fields before running your callback.", |
|
246
|
}, |
|
247
|
{ |
|
248
|
name: "register", |
|
249
|
type: "`(name: string, options?: object) => object`", |
|
250
|
optional: false, |
|
251
|
description: |
|
252
|
"Registers an input in the form. Spread its returned value on the input you render.", |
|
253
|
}, |
|
254
|
{ |
|
255
|
name: "watch", |
|
256
|
type: "`(name?: string) => unknown`", |
|
257
|
optional: false, |
|
258
|
description: |
|
259
|
"Returns the current value of a field, or of the entire form if you don't pass a name.", |
|
260
|
}, |
|
261
|
{ |
|
262
|
name: "setValue", |
|
263
|
type: "`(name: string, value: unknown, options?: object) => void`", |
|
264
|
optional: false, |
|
265
|
description: "Sets a field's value programmatically.", |
|
266
|
}, |
|
267
|
{ |
|
268
|
name: "reset", |
|
269
|
type: "`(values?: object, options?: object) => void`", |
|
270
|
optional: false, |
|
271
|
description: |
|
272
|
"Resets the form's fields to the specified values, or to their default values.", |
|
273
|
}, |
|
274
|
], |
|
275
|
}, |
|
276
|
{ |
|
277
|
name: "blockSearchParams", |
|
278
|
type: "`boolean`", |
|
279
|
optional: true, |
|
280
|
defaultValue: "false", |
|
281
|
description: |
|
282
|
"Whether to also show the unsaved-changes prompt when only the search parameters of the route change.", |
|
283
|
}, |
|
284
|
{ |
|
285
|
name: "onClose", |
|
286
|
type: "`(isSubmitSuccessful: boolean) => void`", |
|
287
|
optional: true, |
|
288
|
description: |
|
289
|
"A function to run when the user leaves the form's route. It receives whether the form was submitted successfully.", |
|
290
|
}, |
|
291
|
]} |
|
292
|
/> |
|
293
|
|
|
294
|
--- |
|
295
|
|
|
296
|
## useRouteModal |
|
297
|
|
|
298
|
Use the `useRouteModal` hook in components rendered inside a `RouteDrawer` or a [RouteFocusModal](../route-focus-modal/page.mdx). It gives you access to the modal's route context. |
|
299
|
|
|
300
|
```tsx |
|
301
|
import { useRouteModal } from "@medusajs/dashboard/components" |
|
302
|
``` |
|
303
|
|
|
304
|
The hook throws an error if you use it outside of a route modal. |
|
305
|
|
|
306
|
### Returned Values |
|
307
|
|
|
308
|
<TypeList |
|
309
|
types={[ |
|
310
|
{ |
|
311
|
name: "handleSuccess", |
|
312
|
type: "`(path?: string) => void`", |
|
313
|
optional: false, |
|
314
|
description: |
|
315
|
"Closes the modal after a successful submission, without showing the unsaved-changes prompt. Pass a path to navigate to a route other than `prev`.", |
|
316
|
}, |
|
317
|
{ |
|
318
|
name: "setCloseOnEscape", |
|
319
|
type: "`(value: boolean) => void`", |
|
320
|
optional: false, |
|
321
|
description: |
|
322
|
"Controls whether the `Escape` key closes the modal. Pass `false` while the user edits an inline element, such as a cell in a data grid, then pass `true` again.", |
|
323
|
}, |
|
324
|
]} |
|
325
|
/> |
|
326
|
|
|
327
|
### Example |
|
328
|
|
|
329
|
The form component uses `handleSuccess` to close the drawer after the brand is updated: |
|
330
|
|
|
331
|
```tsx title="src/admin/routes/brands/[id]/edit/page.tsx" |
|
332
|
import { Button, Input, toast } from "@medusajs/ui" |
|
333
|
import { |
|
334
|
RouteDrawer, |
|
335
|
useRouteModal, |
|
336
|
} from "@medusajs/dashboard/components" |
|
337
|
import { useForm } from "react-hook-form" |
|
338
|
|
|
339
|
const EditBrandForm = ({ id }: { id: string }) => { |
|
340
|
const { handleSuccess } = useRouteModal() |
|
341
|
const form = useForm({ |
|
342
|
defaultValues: { |
|
343
|
name: "", |
|
344
|
}, |
|
345
|
}) |
|
346
|
|
|
347
|
const handleSubmit = form.handleSubmit(async (data) => { |
|
348
|
await updateBrand(id, data) |
|
349
|
toast.success("Brand updated") |
|
350
|
handleSuccess() |
|
351
|
}) |
|
352
|
|
|
353
|
return ( |
|
354
|
<RouteDrawer.Form form={form}> |
|
355
|
<form onSubmit={handleSubmit}> |
|
356
|
<RouteDrawer.Body> |
|
357
|
<Input {...form.register("name")} /> |
|
358
|
</RouteDrawer.Body> |
|
359
|
<RouteDrawer.Footer> |
|
360
|
<RouteDrawer.Close asChild> |
|
361
|
<Button variant="secondary" size="small"> |
|
362
|
Cancel |
|
363
|
</Button> |
|
364
|
</RouteDrawer.Close> |
|
365
|
<Button type="submit" size="small"> |
|
366
|
Save |
|
367
|
</Button> |
|
368
|
</RouteDrawer.Footer> |
|
369
|
</form> |
|
370
|
</RouteDrawer.Form> |
|
371
|
) |
|
372
|
} |
|
373
|
``` |