DataGrid - Admin Components
The DataGrid component shows an editable spreadsheet-like grid whose values are bound to a react-hook-form form. The Medusa Admin uses it to bulk-edit variant prices and inventory quantities.
The grid supports keyboard navigation, range selection, copying and pasting across cells, and undo and redo.
Example#
Create the grid's columns with createDataGridHelper, then pass your form to the grid's state prop:
1import {2 DataGrid,3 createDataGridHelper,4} from "@medusajs/dashboard/components"5import { useForm } from "react-hook-form"6 7const columnHelper = createDataGridHelper<8 Variant,9 VariantsFormValues10>()11 12const VariantsPage = ({ variants }) => {13 const form = useForm<VariantsFormValues>({14 defaultValues: { variants },15 })16 17 const columns = [18 columnHelper.column({19 id: "title",20 name: "Title",21 header: "Title",22 cell: (context) => (23 <DataGrid.ReadonlyCell context={context}>24 {context.row.original.title}25 </DataGrid.ReadonlyCell>26 ),27 }),28 columnHelper.column({29 id: "sku",30 name: "SKU",31 header: "SKU",32 type: "text",33 field: (context) =>34 `variants.${context.row.index}.sku`,35 cell: (context) => (36 <DataGrid.TextCell context={context} />37 ),38 }),39 ]40 41 return (42 <DataGrid43 columns={columns}44 data={variants}45 state={form}46 />47 )48}49 50export default VariantsPage
The field function returns the path of the form field that the cell edits. If it returns null, the cell isn't editable.
Type Arguments#
DataGrid accepts two type arguments:
TData: the type of a single row in the grid.TFieldValues: the shape of the values in the react-hook-form form that the grid edits. It defaults toFieldValues.
You rarely set them explicitly. TypeScript infers TData from the columns and data props, and TFieldValues from the state prop:
Passing that form as state sets TFieldValues to PricesForm, and typing the columns sets TData:
createDataGridHelper takes the same two type arguments, so set them there and the grid infers the rest:
To set the type arguments explicitly, pass them when you use the component:
Props#
DataGrid accepts the DataGridProps type, which extends DataGridRootProps:
Cell Components#
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:
Component | Description |
|---|---|
| Edits the cell's value in a single-line text input. |
| Edits the cell's value in a multi-line text input. |
| Edits long text. The user can expand the cell into a modal. |
| Edits the cell's value in a number input. It also accepts the |
| Edits a price. It also accepts a |
| Edits the cell's value with a checkbox. It also accepts a |
| Shows its |
createDataGridHelper#
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.
For example, the following column shows a variant's title in an editable text cell:
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.
The column method accepts an object with the following properties:
createDataGridPriceColumns#
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.
1import {2 createDataGridPriceColumns,3} from "@medusajs/dashboard/components"4import { useTranslation } from "react-i18next"5 6const { t } = useTranslation()7 8const priceColumns = createDataGridPriceColumns<9 Variant,10 VariantsFormValues11>({12 currencies: ["usd", "eur"],13 regions,14 pricePreferences,15 getFieldName: (context, value) =>16 `variants.${context.row.index}.prices.${value}`,17 t,18})
It accepts an object with the following properties:
Related Components#
- Data Table: a read-only table for a list of records.