Editing

DataGrid inline cell and row editing

Overview

Set editingMode in useDataGrid to cell or row to enable editing. Only one cell or row can be edited at a time. Cells of accessor columns are editable unless meta.disableEditing is set. Editing is not available in list view.

Cell editing

Set editingMode: 'cell' to edit one cell at a time. Double-click a cell to open its editor, or set editTrigger: 'click' to open it with a single click. Press Enter to save and Escape to cancel, the value is also saved when the editor loses focus. onEditSave is called with the updated row:

First Name
Last Name
Department
Salary
AliceJohnsonEngineering95000
BobSmithMarketing72000
CarolWilliamsSales68000
DavidBrownHR65000
EvaDavisFinance88000
FrankMillerEngineering102000
GraceWilsonMarketing76000
HenryTaylorSales71000
IreneAndersonEngineering97000
JamesThomasFinance91000
KarenJacksonHR63000
LeoWhiteEngineering110000
MiaHarrisSales74000
NathanMartinMarketing69000
OliviaGarciaFinance85000
PatrickMartinezEngineering99000
QuinnRobinsonHR67000
RachelClarkSales73000
import { useState } from 'react';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const [data, setData] = useState(employees);

  const store = useDataGrid({
    data,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary', meta: { align: 'end' } },
    ],
    getRowId: (row) => row.id,
    editingMode: 'cell',
    onEditSave: ({ newRow }) => {
      setData((prev) => prev.map((item) => (item.id === newRow.id ? newRow : item)));
    },
  });

  return <DataGrid store={store} />;
}

Row editing

Set editingMode: 'row' to edit all editable cells of a row at once. Editing starts with a double-click on a cell or from an actions column. Enter or a click outside the row saves it, Escape cancels editing:

First Name
Last Name
Department
Salary
AliceJohnsonEngineering95000
BobSmithMarketing72000
CarolWilliamsSales68000
DavidBrownHR65000
EvaDavisFinance88000
FrankMillerEngineering102000
GraceWilsonMarketing76000
HenryTaylorSales71000
IreneAndersonEngineering97000
JamesThomasFinance91000
KarenJacksonHR63000
LeoWhiteEngineering110000
MiaHarrisSales74000
NathanMartinMarketing69000
OliviaGarciaFinance85000
PatrickMartinezEngineering99000
QuinnRobinsonHR67000
RachelClarkSales73000
import { useState } from 'react';
import { ActionIcon, Group } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';
import { CheckIcon, CloseIcon, PencilIcon } from './icons';

function Demo() {
  const [data, setData] = useState(employees);

  const store = useDataGrid({
    data,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary', meta: { align: 'end' } },
      {
        id: 'actions',
        meta: { type: 'actions' },
        cell: ({ row }) => (
          <Group gap={4} justify="flex-end" wrap="nowrap">
            {row.getIsEditing() ? (
              <>
                <ActionIcon
                  variant="default"
                  size={store.getControlSize()}
                  aria-label="Save"
                  onClick={() => row.saveEditing()}
                >
                  <CheckIcon />
                </ActionIcon>
                <ActionIcon
                  variant="default"
                  size={store.getControlSize()}
                  aria-label="Cancel"
                  onClick={() => row.cancelEditing()}
                >
                  <CloseIcon />
                </ActionIcon>
              </>
            ) : (
              <ActionIcon
                variant="default"
                size={store.getControlSize()}
                aria-label="Edit"
                onClick={() => row.startEditing()}
              >
                <PencilIcon />
              </ActionIcon>
            )}
          </Group>
        ),
      },
    ],
    getRowId: (row) => row.id,
    editingMode: 'row',
    onEditSave: ({ newRow }) => {
      setData((prev) => prev.map((item) => (item.id === newRow.id ? newRow : item)));
    },
  });

  return <DataGrid store={store} />;
}

Actions column

Add a display column with meta.type: 'actions' to render row action buttons. In its cell, use row.getIsEditing() to switch between Edit and Save / Cancel buttons and call row.startEditing(), row.saveEditing() and row.cancelEditing() when they are clicked. Pass size={store.getControlSize()} to your buttons so they match the grid size:

First Name
Last Name
Department
Salary
Actions
AliceJohnsonEngineering95000
BobSmithMarketing72000
CarolWilliamsSales68000
DavidBrownHR65000
EvaDavisFinance88000
FrankMillerEngineering102000
import { useState } from 'react';
import { EyeIcon, PencilSimpleIcon, TrashIcon } from '@phosphor-icons/react';
import { ActionIcon, Group, Tooltip } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const [data, setData] = useState(employees);

  const store = useDataGrid({
    data,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary', meta: { align: 'end' } },
      {
        id: 'actions',
        header: 'Actions',
        size: 110,
        meta: { type: 'actions' },
        cell: ({ row }) => (
          <Group gap={4} justify="flex-end" wrap="nowrap">
            <Tooltip label="View profile">
              <ActionIcon variant="default" size={store.getControlSize()} aria-label="View">
                <EyeIcon size={14} />
              </ActionIcon>
            </Tooltip>
            <Tooltip label="Edit">
              <ActionIcon variant="default" size={store.getControlSize()} aria-label="Edit">
                <PencilSimpleIcon size={14} />
              </ActionIcon>
            </Tooltip>
            <Tooltip label="Delete">
              <ActionIcon
                variant="default"
                size={store.getControlSize()}
                aria-label="Delete"
                onClick={() => setData((prev) => prev.filter((item) => item.id !== row.id))}
              >
                <TrashIcon size={14} />
              </ActionIcon>
            </Tooltip>
          </Group>
        ),
      },
    ],
    getRowId: (row) => row.id,
    pageSize: 6,
    autoResetPageIndex: false,
  });

  return (
    <DataGrid store={store}>
      <DataGrid.Footer />
    </DataGrid>
  );
}

Floating actions

Set withFloatingEditActions on DataGrid to display floating Save and Cancel buttons next to the row being edited. It works in row mode only. Use saveRow and cancelRow labels to change the button text:

First Name
Last Name
Department
Salary
AliceJohnsonEngineering95000
BobSmithMarketing72000
CarolWilliamsSales68000
DavidBrownHR65000
EvaDavisFinance88000
FrankMillerEngineering102000
GraceWilsonMarketing76000
HenryTaylorSales71000
IreneAndersonEngineering97000
JamesThomasFinance91000
KarenJacksonHR63000
LeoWhiteEngineering110000
MiaHarrisSales74000
NathanMartinMarketing69000
OliviaGarciaFinance85000
PatrickMartinezEngineering99000
QuinnRobinsonHR67000
RachelClarkSales73000
import { useState } from 'react';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const [data, setData] = useState(employees);

  const store = useDataGrid({
    data,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary', meta: { align: 'end' } },
    ],
    getRowId: (row) => row.id,
    editingMode: 'row',
    onEditSave: ({ newRow }) => {
      setData((prev) => prev.map((item) => (item.id === newRow.id ? newRow : item)));
    },
  });

  return <DataGrid store={store} withFloatingEditActions />;
}

Keyboard

KeyContextAction
Enter / F2Focused row or cellStarts editing
EnterInside an editorValidates and saves the changes
EscapeInside an editorCancels editing
Tab / Shift + TabInside an editor, row modeMoves focus to the next / previous editor of the row
BlurDefault editor, cell modeSaves the value when focus leaves the editor
Outside clickRow modeSaves the row

Custom editors receive onSave and onCancel and decide which keys call them. When editing ends, focus returns to the row.

Editing options

useDataGrid accepts the following editing options:

interface UseDataGridOptions<TData extends RowData> {
  /** Editing is disabled when omitted. */
  editingMode?: 'cell' | 'row';

  /** Gesture that starts editing. Defaults to 'doubleClick'. */
  editTrigger?: 'click' | 'doubleClick';

  /** Receives the row and columnId when editing starts. */
  onEditStart?: (payload: DataGridEditStartPayload<DataGridFeatures, TData>) => void;

  /** Receives row, newRow, oldRow and changedColumnIds on save. */
  onEditSave?: (payload: DataGridEditSavePayload<DataGridFeatures, TData>) => void | Promise<void>;

  /** Called when onEditSave throws or rejects. */
  onEditSaveError?: (
    error: unknown,
    payload: DataGridEditSaveErrorPayload<DataGridFeatures, TData>
  ) => void;

  /** Receives the row when editing is cancelled. */
  onEditCancel?: (payload: DataGridEditCancelPayload<DataGridFeatures, TData>) => void;

  /** Return 'dirty' or 'invalid' to tint a cell. */
  getCellStatus?: (
    payload: DataGridCellStatusPayload<DataGridFeatures, TData>
  ) => DataGridCellStatus;

  /** Controlled editing state, null when nothing is being edited. */
  editing?: DataGridEditingState | null;
  onEditingChange?: OnChangeFn<DataGridEditingState | null>;
}

newRow is the row with changed values applied. Dot paths are supported: accessorKey: 'address.city' updates newRow.address.city. For columns defined with accessorFn, the new value is written to newRow[column.id].

Async saves

Return a promise from onEditSave to keep the editor open while the save is pending. If the promise is rejected, onEditSaveError is called and the editor stays open:

const store = useDataGrid({
  data,
  columns,
  editingMode: 'row',
  onEditSave: async ({ newRow, changedColumnIds }) => {
    await api.updateEmployee(newRow.id, pick(newRow, changedColumnIds));
    setData((prev) => prev.map((item) => (item.id === newRow.id ? newRow : item)));
  },
  onEditSaveError: (error) => {
    notifications.show({ color: 'red', message: String(error) });
  },
});

In custom editors, use cell.row.getIsSaving() to disable the input while the save is pending.

Validation

Add meta.validate to a column to validate the value before it is saved. The function receives the value and the row and returns an error message, or null if the value is valid. The error is displayed in a tooltip and the value cannot be saved until it is changed:

First Name
Last Name
Email
Salary
AliceJohnsonalice.johnson@example.com95000
BobSmithbob.smith@example.com72000
CarolWilliamscarol.williams@example.com68000
DavidBrowndavid.brown@example.com65000
EvaDaviseva.davis@example.com88000
FrankMillerfrank.miller@example.com102000
GraceWilsongrace.wilson@example.com76000
HenryTaylorhenry.taylor@example.com71000
IreneAndersonirene.anderson@example.com97000
JamesThomasjames.thomas@example.com91000
KarenJacksonkaren.jackson@example.com63000
LeoWhiteleo.white@example.com110000
MiaHarrismia.harris@example.com74000
NathanMartinnathan.martin@example.com69000
OliviaGarciaolivia.garcia@example.com85000
PatrickMartinezpatrick.martinez@example.com99000
QuinnRobinsonquinn.robinson@example.com67000
RachelClarkrachel.clark@example.com73000
import { useState } from 'react';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const [data, setData] = useState(employees);
  const [dirtyCells, setDirtyCells] = useState<Set<string>>(new Set());

  const store = useDataGrid({
    data,
    columns: [
      {
        accessorKey: 'firstName',
        header: 'First Name',
        meta: {
          validate: (value) => (String(value).trim() ? null : 'First name is required'),
        },
      },
      { accessorKey: 'lastName', header: 'Last Name' },
      {
        accessorKey: 'email',
        header: 'Email',
        size: 220,
        meta: {
          validate: (value) =>
            /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(value)) ? null : 'Enter a valid email address',
        },
      },
      {
        accessorKey: 'salary',
        header: 'Salary',
        meta: {
          align: 'end',
          validate: (value) =>
            Number(value) >= 30000 ? null : 'Salary must be at least $30,000',
        },
      },
    ],
    getRowId: (row) => row.id,
    editingMode: 'cell',
    onEditSave: ({ row, newRow, changedColumnIds }) => {
      setData((prev) => prev.map((item) => (item.id === newRow.id ? newRow : item)));
      setDirtyCells((prev) => {
        const next = new Set(prev);
        changedColumnIds.forEach((columnId) => next.add(`${row.id}:${columnId}`));
        return next;
      });
    },
    getCellStatus: ({ row, columnId }) =>
      dirtyCells.has(`${row.id}:${columnId}`) ? 'dirty' : undefined,
  });

  return <DataGrid store={store} />;
}

Cell status

Use getCellStatus to highlight cells that are not being edited. Return dirty for unsaved changes, invalid for invalid values or undefined for the default appearance. The following example marks empty email cells as invalid:

const store = useDataGrid({
  data,
  columns,
  getCellStatus: ({ columnId, value }) =>
    columnId === 'email' && !String(value ?? '').trim() ? 'invalid' : undefined,
});

Custom edit components

Set meta.editComponent on a column to replace the default text input. The component receives the following props:

  • value and onChange – the current value and a function to change it
  • onSave and onCancel – functions to save the value and to cancel editing
  • error – validation error
  • cell – the cell being edited

To save the value as soon as an option is picked, call onSave in onChange:

First Name
Last Name
Department
Salary
AliceJohnsonEngineering95000
BobSmithMarketing72000
CarolWilliamsSales68000
DavidBrownHR65000
EvaDavisFinance88000
FrankMillerEngineering102000
GraceWilsonMarketing76000
HenryTaylorSales71000
IreneAndersonEngineering97000
JamesThomasFinance91000
KarenJacksonHR63000
LeoWhiteEngineering110000
MiaHarrisSales74000
NathanMartinMarketing69000
OliviaGarciaFinance85000
PatrickMartinezEngineering99000
QuinnRobinsonHR67000
RachelClarkSales73000
import { useState } from 'react';
import { Select } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

const DEPARTMENTS = ['Engineering', 'Marketing', 'Sales', 'HR', 'Finance'];

function Demo() {
  const [data, setData] = useState(employees);

  const store = useDataGrid({
    data,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      {
        accessorKey: 'department',
        header: 'Department',
        meta: {
          editComponent: ({ value, onChange, onSave, onCancel, error }) => (
            <Select
              data={DEPARTMENTS}
              value={value as string}
              error={error}
              onChange={(next) => {
                onChange(next);
                onSave();
              }}
              onKeyDown={(event) => event.key === 'Escape' && onCancel()}
              variant="unstyled"
              autoFocus
            />
          ),
        },
      },
      { accessorKey: 'salary', header: 'Salary', meta: { align: 'end' } },
    ],
    getRowId: (row) => row.id,
    editingMode: 'cell',
    onEditSave: ({ newRow }) => {
      setData((prev) => prev.map((item) => (item.id === newRow.id ? newRow : item)));
    },
  });

  return <DataGrid store={store} />;
}

Disabling editing per column

Set meta.disableEditing: true on a column definition to make the column read-only:

const columns = [
  { accessorKey: 'id', header: 'ID', meta: { disableEditing: true } },
  { accessorKey: 'firstName', header: 'First Name' },
  { accessorKey: 'department', header: 'Department' },
];

Utility columns, display columns and the actions column are never editable.

Imperative API and controlled state

The editing state is available as store.getEditing() and can be controlled with editing and onEditingChange. The table, rows and cells have the following methods:

store.table.startEditing(rowId, columnId?);
store.table.saveEditing();
store.table.cancelEditing();
store.table.setEditing(updater);

row.getCanEdit();
row.getIsEditing();
row.getIsSaving();
row.startEditing(columnId?);
row.saveEditing();
row.cancelEditing();

cell.getCanEdit();
cell.getIsEditing();
cell.getEditingValue();
cell.setEditingValue(value);
cell.getEditingError();

When editing of another row or cell is started, the current one is saved first. If it cannot be saved, the new one is not opened.

Modal editing

For complex edit forms, use onRowDoubleClick to open a modal instead of inline editing:

<DataGrid
  data={data}
  columns={columns}
  onRowDoubleClick={(row) => {
    setEditingRow(row.original);
    openModal();
  }}
/>