# Editing
Package: @mantine/data-grid
Import: import { Editing } from '@mantine/data-grid';
Description: 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:

```tsx
// Demo.tsx
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} />;
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## 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](#actions-column). `Enter` or a click
outside the row saves it, `Escape` cancels editing:

```tsx
// Demo.tsx
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} />;
}

// icons.tsx
export function PencilIcon() {
  return (
    <svg
      width={14}
      height={14}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth={2}
      strokeLinecap="round"
      strokeLinejoin="round"
    >
      <path d="M4 20h4l10.5 -10.5a2.828 2.828 0 1 0 -4 -4l-10.5 10.5v4" />
      <path d="M13.5 6.5l4 4" />
    </svg>
  );
}

export function CheckIcon() {
  return (
    <svg
      width={14}
      height={14}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth={2}
      strokeLinecap="round"
      strokeLinejoin="round"
    >
      <path d="M5 12l5 5l10 -10" />
    </svg>
  );
}

export function CloseIcon() {
  return (
    <svg
      width={14}
      height={14}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth={2}
      strokeLinecap="round"
      strokeLinejoin="round"
    >
      <path d="M18 6l-12 12" />
      <path d="M6 6l12 12" />
    </svg>
  );
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## 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:

```tsx
// Demo.tsx
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>
  );
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## 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:

```tsx
// Demo.tsx
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 />;
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## Keyboard

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:

```ts
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:

```tsx
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:

```tsx
// Demo.tsx
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} />;
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## 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:

```tsx
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`:

```tsx
// Demo.tsx
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} />;
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## Disabling editing per column

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

```tsx
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:

```tsx
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:

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