Raw
1 ---
2 sidebar_label: "Data Grid"
3 ---
4
5 import { Table, TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `DataGrid - 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 `DataGrid` component shows an editable spreadsheet-like grid whose values are bound to a [react-hook-form](https://react-hook-form.com/) form. The Medusa Admin uses it to bulk-edit variant prices and inventory quantities.
20
21 ```tsx
22 import {
23 DataGrid,
24 createDataGridHelper,
25 } from "@medusajs/dashboard/components"
26 ```
27
28 The grid supports keyboard navigation, range selection, copying and pasting across cells, and undo and redo.
29
30 ---
31
32 ## Example
33
34 Create the grid's columns with `createDataGridHelper`, then pass your form to the grid's `state` prop:
35
36 ```tsx title="src/admin/routes/variants/page.tsx"
37 import {
38 DataGrid,
39 createDataGridHelper,
40 } from "@medusajs/dashboard/components"
41 import { useForm } from "react-hook-form"
42
43 const columnHelper = createDataGridHelper<
44 Variant,
45 VariantsFormValues
46 >()
47
48 const VariantsPage = ({ variants }) => {
49 const form = useForm<VariantsFormValues>({
50 defaultValues: { variants },
51 })
52
53 const columns = [
54 columnHelper.column({
55 id: "title",
56 name: "Title",
57 header: "Title",
58 cell: (context) => (
59 <DataGrid.ReadonlyCell context={context}>
60 {context.row.original.title}
61 </DataGrid.ReadonlyCell>
62 ),
63 }),
64 columnHelper.column({
65 id: "sku",
66 name: "SKU",
67 header: "SKU",
68 type: "text",
69 field: (context) =>
70 `variants.${context.row.index}.sku`,
71 cell: (context) => (
72 <DataGrid.TextCell context={context} />
73 ),
74 }),
75 ]
76
77 return (
78 <DataGrid
79 columns={columns}
80 data={variants}
81 state={form}
82 />
83 )
84 }
85
86 export default VariantsPage
87 ```
88
89 The `field` function returns the path of the form field that the cell edits. If it returns `null`, the cell isn't editable.
90
91 ---
92
93 ## Type Arguments
94
95 `DataGrid` accepts two type arguments:
96
97 - `TData`: the type of a single row in the grid.
98 - `TFieldValues`: the shape of the values in the [react-hook-form](https://react-hook-form.com/) form that the grid edits. It defaults to `FieldValues`.
99
100 You rarely set them explicitly. TypeScript infers `TData` from the `columns` and `data` props, and `TFieldValues` from the `state` prop:
101
102 ```tsx
103 import { useForm } from "react-hook-form"
104 import { HttpTypes } from "@medusajs/framework/types"
105
106 type PricesForm = {
107 variants: Record<string, Record<string, number>>
108 }
109
110 const form = useForm<PricesForm>()
111 ```
112
113 Passing that form as `state` sets `TFieldValues` to `PricesForm`, and typing the columns sets `TData`:
114
115 ```tsx
116 const columnHelper = createDataGridHelper<
117 HttpTypes.AdminProductVariant,
118 PricesForm
119 >()
120 ```
121
122 `createDataGridHelper` takes the same two type arguments, so set them there and the grid infers the rest:
123
124 ```tsx
125 <DataGrid
126 columns={columns}
127 data={variants}
128 state={form}
129 />
130 ```
131
132 To set the type arguments explicitly, pass them when you use the component:
133
134 ```tsx
135 <DataGrid<HttpTypes.AdminProductVariant, PricesForm>
136 columns={columns}
137 data={variants}
138 state={form}
139 />
140 ```
141
142 ---
143
144 ## Props
145
146 `DataGrid` accepts the `DataGridProps` type, which extends `DataGridRootProps`:
147
148 <TypeList
149 types={[
150 {
151 name: "columns",
152 type: "`ColumnDef<TData>[]`",
153 optional: false,
154 description: "The grid's columns. Create them with `createDataGridHelper` or `createDataGridPriceColumns`, which also set the [`TData` and `TFieldValues` type arguments](#type-arguments).",
155 },
156 {
157 name: "data",
158 type: "`TData[]`",
159 optional: true,
160 description: `The records to show in the grid. \`TData\` is the [row type](#type-arguments).`,
161 },
162 {
163 name: "state",
164 type: "`UseFormReturn<TFieldValues>`",
165 optional: false,
166 description: `The [react-hook-form](https://react-hook-form.com/) form that holds the grid's values. \`TFieldValues\` is the [form's value type](#type-arguments).`,
167 },
168 {
169 name: "isLoading",
170 type: "`boolean`",
171 optional: true,
172 description: "Whether the grid shows its loading skeleton instead of the grid.",
173 },
174 {
175 name: "getSubRows",
176 type: "`(row: TData) => TData[] | undefined`",
177 optional: true,
178 description: `Returns the sub-rows of a row, such as a product's variants, where \`TData\` is the [row type](#type-arguments).`,
179 },
180 {
181 name: "onEditingChange",
182 type: "`(isEditing: boolean) => void`",
183 optional: true,
184 description: "A function that runs when the user starts or stops editing a cell. Use it to prevent a modal from closing while the user edits a cell.",
185 },
186 {
187 name: "disableInteractions",
188 type: "`boolean`",
189 optional: true,
190 description: "Whether to disable the grid's keyboard navigation and selection.",
191 },
192 {
193 name: "multiColumnSelection",
194 type: "`boolean`",
195 optional: true,
196 description: "Whether the user can select cells across more than one column.",
197 },
198 {
199 name: "showColumnsDropdown",
200 type: "`boolean`",
201 optional: true,
202 description: "Whether to show the dropdown that toggles the grid's columns.",
203 },
204 {
205 name: "headerContent",
206 type: "`ReactNode`",
207 optional: true,
208 description: "Custom content shown in the grid's header, between the column controls and the error section.",
209 },
210 {
211 name: "totalRowCount",
212 type: "`number`",
213 optional: true,
214 description: "The total number of rows. Pass it to enable lazy loading, where the grid fetches more rows as the user scrolls.",
215 },
216 {
217 name: "onFetchMore",
218 type: "`() => void`",
219 optional: true,
220 description: "A function that runs when the grid needs to fetch more rows in lazy loading mode.",
221 },
222 {
223 name: "isFetchingMore",
224 type: "`boolean`",
225 optional: true,
226 description: "Whether the grid is currently fetching more rows.",
227 },
228 {
229 name: "hasNextPage",
230 type: "`boolean`",
231 optional: true,
232 description: "Whether there are more rows to fetch.",
233 },
234 ]}
235 />
236
237 ---
238
239 ## Cell Components
240
241 `DataGrid` has the following sub-components to render a column's cells. Each of them accepts a `context` prop, which is the cell context that the column's `cell` function receives:
242
243 <Table>
244 <Table.Header>
245 <Table.Row>
246 <Table.HeaderCell>
247 Component
248 </Table.HeaderCell>
249 <Table.HeaderCell>
250 Description
251 </Table.HeaderCell>
252 </Table.Row>
253 </Table.Header>
254 <Table.Body>
255 <Table.Row>
256 <Table.Cell>
257
258 `DataGrid.TextCell`
259
260 </Table.Cell>
261 <Table.Cell>
262
263 Edits the cell's value in a single-line text input.
264
265 </Table.Cell>
266 </Table.Row>
267 <Table.Row>
268 <Table.Cell>
269
270 `DataGrid.MultilineCell`
271
272 </Table.Cell>
273 <Table.Cell>
274
275 Edits the cell's value in a multi-line text input.
276
277 </Table.Cell>
278 </Table.Row>
279 <Table.Row>
280 <Table.Cell>
281
282 `DataGrid.ExpandableTextCell`
283
284 </Table.Cell>
285 <Table.Cell>
286
287 Edits long text. The user can expand the cell into a modal.
288
289 </Table.Cell>
290 </Table.Row>
291 <Table.Row>
292 <Table.Cell>
293
294 `DataGrid.NumberCell`
295
296 </Table.Cell>
297 <Table.Cell>
298
299 Edits the cell's value in a number input. It also accepts the `min` and `max` props.
300
301 </Table.Cell>
302 </Table.Row>
303 <Table.Row>
304 <Table.Cell>
305
306 `DataGrid.CurrencyCell`
307
308 </Table.Cell>
309 <Table.Cell>
310
311 Edits a price. It also accepts a `code` prop, which is the currency's code.
312
313 </Table.Cell>
314 </Table.Row>
315 <Table.Row>
316 <Table.Cell>
317
318 `DataGrid.BooleanCell`
319
320 </Table.Cell>
321 <Table.Cell>
322
323 Edits the cell's value with a checkbox. It also accepts a `disabled` prop.
324
325 </Table.Cell>
326 </Table.Row>
327 <Table.Row>
328 <Table.Cell>
329
330 `DataGrid.ReadonlyCell`
331
332 </Table.Cell>
333 <Table.Cell>
334
335 Shows its `children` in a cell that the user can't edit.
336
337 </Table.Cell>
338 </Table.Row>
339 </Table.Body>
340 </Table>
341
342 ---
343
344 ## createDataGridHelper
345
346 `createDataGridHelper` returns a typed helper whose `column` method defines a grid column. It accepts two type parameters: the type of a row, and the type of the form's values.
347
348 ```tsx
349 const columnHelper = createDataGridHelper<
350 Variant,
351 VariantsFormValues
352 >()
353 ```
354
355 For example, the following column shows a variant's title in an editable text cell:
356
357 ```tsx
358 columnHelper.column({
359 id: "title",
360 name: "Title",
361 header: "Title",
362 field: (context) =>
363 `variants.${context.row.index}.title`,
364 type: "text",
365 cell: (context) => (
366 <DataGrid.TextCell context={context} />
367 ),
368 })
369 ```
370
371 The `header` and `cell` properties accept either a string or a function. The function receives a context object that you pass to the cell component, which uses it to read and write the cell's value.
372
373 The `column` method accepts an object with the following properties:
374
375 <TypeList
376 types={[
377 {
378 name: "id",
379 type: "`string`",
380 optional: false,
381 description: "The column's ID.",
382 },
383 {
384 name: "header",
385 type: "`string` \\| `(context) => ReactNode`",
386 optional: false,
387 description: "The column's header. Pass a string to show it as plain text, or a function to render custom content.",
388 children: [
389 {
390 name: "context",
391 type: "`object`",
392 optional: false,
393 description: `The header's context, which is TanStack Table's [HeaderContext](https://tanstack.com/table/latest/docs/api/core/header). It is passed to the header's rendering function.`,
394 children: [
395 {
396 name: "column",
397 type: "`Column<TData, unknown>`",
398 optional: false,
399 description: "The column that the header belongs to. `TData` is the [row type](#type-arguments).",
400 },
401 {
402 name: "header",
403 type: "`Header<TData, unknown>`",
404 optional: false,
405 description: "The header's instance, which holds its ID and size.",
406 },
407 {
408 name: "table",
409 type: "`Table<TData>`",
410 optional: false,
411 description: "The grid's table instance.",
412 },
413 ],
414 },
415 ],
416 },
417 {
418 name: "cell",
419 type: "`string` \\| `(context) => ReactNode`",
420 optional: false,
421 description: "The cell's content. Pass a function that returns one of the [cell components](#cell-components), and pass the function's `context` to that component so it can read and edit the cell's value.",
422 children: [
423 {
424 name: "context",
425 type: "`object`",
426 optional: false,
427 description: `The cell's context, which is TanStack Table's [CellContext](https://tanstack.com/table/latest/docs/api/core/cell). Pass it as-is to a cell component.`,
428 children: [
429 {
430 name: "row",
431 type: "`Row<TData>`",
432 optional: false,
433 description: "The cell's row. Use `row.original` to access the record, and `row.index` for its position. `TData` is the [row type](#type-arguments).",
434 },
435 {
436 name: "column",
437 type: "`Column<TData, unknown>`",
438 optional: false,
439 description: "The column that the cell belongs to.",
440 },
441 {
442 name: "getValue",
443 type: "`() => unknown`",
444 optional: false,
445 description: "Returns the cell's value.",
446 },
447 {
448 name: "table",
449 type: "`Table<TData>`",
450 optional: false,
451 description: "The grid's table instance.",
452 },
453 ],
454 },
455 ],
456 },
457 {
458 name: "name",
459 type: "`string`",
460 optional: true,
461 description: "The column's name, shown in the column visibility menu.",
462 },
463 {
464 name: "field",
465 type: "`(context: FieldContext<TData>) => FieldPath<TFieldValues>` \\| `null`",
466 optional: true,
467 description: "Returns the path of the form field that the cell edits, where `TData` and `TFieldValues` are the [type arguments](#type-arguments). Return `null`, or omit the property, to make the cell read-only. Pass `type` with it.",
468 children: [
469 {
470 name: "context",
471 type: "`object`",
472 optional: false,
473 description: "The cell's context.",
474 children: [
475 {
476 name: "row",
477 type: "`Row<TData>`",
478 optional: false,
479 description: `The cell's row. Refer to [TanStack Table](https://tanstack.com/table/latest/docs/api/core/row) for the type's details.`,
480 },
481 {
482 name: "column",
483 type: "`ColumnDef<TData>`",
484 optional: false,
485 description: `The cell's column. Refer to [TanStack Table](https://tanstack.com/table/latest/docs/api/core/column-def) for the type's details.`,
486 },
487 ],
488 },
489 ],
490 },
491 {
492 name: "type",
493 type: "`\"text\"` \\| `\"multiline-text\"` \\| `\"number\"` \\| `\"boolean\"` \\| `\"togglable-number\"`",
494 optional: true,
495 description: "The type of value the cell holds. It's required when you pass `field`, and not allowed otherwise.",
496 },
497 {
498 name: "disableHiding",
499 type: "`boolean`",
500 optional: true,
501 defaultValue: "false",
502 description: "Whether the user can't hide the column.",
503 },
504 {
505 name: "size",
506 type: "`number`",
507 optional: true,
508 description: "The column's size in pixels.",
509 },
510 {
511 name: "minSize",
512 type: "`number`",
513 optional: true,
514 description: "The column's minimum size in pixels.",
515 },
516 {
517 name: "maxSize",
518 type: "`number`",
519 optional: true,
520 description: "The column's maximum size in pixels.",
521 },
522 ]}
523 />
524 ---
525
526 ## createDataGridPriceColumns
527
528 `createDataGridPriceColumns` returns a price column for each of the passed currencies and regions. Use it to let the user edit prices for many currencies and regions at once.
529
530 ```tsx
531 import {
532 createDataGridPriceColumns,
533 } from "@medusajs/dashboard/components"
534 import { useTranslation } from "react-i18next"
535
536 const { t } = useTranslation()
537
538 const priceColumns = createDataGridPriceColumns<
539 Variant,
540 VariantsFormValues
541 >({
542 currencies: ["usd", "eur"],
543 regions,
544 pricePreferences,
545 getFieldName: (context, value) =>
546 `variants.${context.row.index}.prices.${value}`,
547 t,
548 })
549 ```
550
551 It accepts an object with the following properties:
552
553 <TypeList
554 types={[
555 {
556 name: "getFieldName",
557 type: "`(context: FieldContext<TData>, value: string) => FieldPath<TFieldValues>` \\| `null`",
558 optional: false,
559 description: "Returns the path of the form field that a price cell edits, where `TData` and `TFieldValues` are the [type arguments](#type-arguments). `value` is the currency's code or the region's ID.",
560 children: [
561 {
562 name: "context",
563 type: "`object`",
564 optional: false,
565 description: "The cell's context.",
566 children: [
567 {
568 name: "row",
569 type: "`Row<TData>`",
570 optional: false,
571 description: `The cell's row. Refer to [TanStack Table](https://tanstack.com/table/latest/docs/api/core/row) for the type's details.`,
572 },
573 {
574 name: "column",
575 type: "`ColumnDef<TData>`",
576 optional: false,
577 description: `The cell's column. Refer to [TanStack Table](https://tanstack.com/table/latest/docs/api/core/column-def) for the type's details.`,
578 },
579 ],
580 },
581 {
582 name: "value",
583 type: "`string`",
584 optional: false,
585 description: "The currency's code or the region's ID that the column shows prices for.",
586 },
587 ],
588 },
589 {
590 name: "t",
591 type: "`TFunction`",
592 optional: false,
593 description: `The translation function returned by \`useTranslation\` of [react-i18next](https://react.i18next.com/), used for the columns' names.`,
594 },
595 {
596 name: "currencies",
597 type: "`string[]`",
598 optional: true,
599 description: "The currency codes to create a column for. Each column's ID is `currency_prices.{code}`.",
600 },
601 {
602 name: "regions",
603 type: "`AdminRegion[]`",
604 optional: true,
605 description: "The regions to create a column for. Each column's ID is `region_prices.{id}`.",
606 },
607 {
608 name: "pricePreferences",
609 type: "`AdminPricePreference[]`",
610 optional: true,
611 description: "The price preferences, used to show whether a column's prices include taxes.",
612 },
613 {
614 name: "isReadyOnly",
615 type: "`(context: FieldContext<TData>) => boolean`",
616 optional: true,
617 description: "Returns whether a cell is read-only, where `TData` is the [row type](#type-arguments). The function renders a read-only cell when it returns `true`.",
618 children: [
619 {
620 name: "context",
621 type: "`object`",
622 optional: false,
623 description: "The cell's context, with the same `row` and `column` properties that `getFieldName` receives.",
624 },
625 ],
626 },
627 {
628 name: "renderPriceCell",
629 type: "`(code: string, context: CellContext<TData, any>) => ReactNode`",
630 optional: true,
631 description: "Renders a custom price cell, where `TData` is the [row type](#type-arguments). If you don't pass it, the function renders a `DataGrid.CurrencyCell`.",
632 children: [
633 {
634 name: "code",
635 type: "`string`",
636 optional: false,
637 description: "The currency's code that the cell shows a price for.",
638 },
639 {
640 name: "context",
641 type: "`CellContext<TData, any>`",
642 optional: false,
643 description: `The cell's context. Refer to [TanStack Table](https://tanstack.com/table/latest/docs/api/core/column-def) for the type's details.`,
644 },
645 ],
646 },
647 ]}
648 />
649 ---
650
651 ## Related Components
652
653 - [Data Table](../../guides/data-table/page.mdx): a read-only table for a list of records.