gridcn2.0.0
Add-on optionsClipboard and undo

Undo and redo options

Look up the options and the result of useDataGridHistory and useDataGridState, the hooks of the data-grid-history add-on.

The data-grid-history add-on has two hooks. useDataGridHistory adds a history to a grid whose data you own. useDataGridState owns the data array and the history together. Both options objects are optional. Undo and redo explains the behavior.

This sample sets all four options of useDataGridHistory:

import { useState } from "react";
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
import {
  useDataGridHistory,
} from "@/components/data-grid-history/data-grid-history";

type Row = { id: string; name: string };

const columns = defineColumns<Row>()([
  { id: "name", header: "Name", accessorKey: "name" },
]);

export function HistoryGrid({ rows }: { rows: Row[] }) {
  const [data, setData] = useState<readonly Row[]>(rows);
  const history = useDataGridHistory<Row>({
    capacity: 50,
    datasetKey: "2026",
    recordSources: ["edit", "paste", "fill"],
    onSkip: ({ direction, rowIds }) => {
      console.info(`Cannot ${direction}: ${rowIds.length} rows are missing`);
    },
  });
  return (
    <DataGrid
      data={data}
      columns={columns}
      onDataChange={setData}
      plugins={[history.plugin]}
    />
  );
}

The stack keeps the last 50 entries. Only edits, pastes, and fills go on it. If an undo or a redo finds a needed row missing, onSkip logs it.

UseDataGridHistoryOptions

Prop

Type

To record streams too, pass recordSources: (defaults) => [...defaults, "stream"].

  • capacity defaults to 100. It counts entries, not operations. A change of capacity between renders keeps the entries and trims the oldest.
  • recordSources defaults to edit, paste, fill, delete, cut, row-operation, import, replace, and app. The default leaves out stream. Pass a list to replace the default, or a function that receives the default list and returns the list to use.
  • The history never records history, rollback, merge, or id-swap, whatever the list says.
  • A change of datasetKey between renders clears both stacks. The first render clears nothing.

UseDataGridHistoryResult

Prop

Type

UseDataGridStateOptions

useDataGridState(defaultRows, options) takes the starting rows as its first argument.

Prop

Type

getRowId defaults to the id field of a row. Every option of useDataGridHistory works the same way here, including recordSources and onSkip. The plugins option takes the other plugins of the grid, such as the fill plugin: useDataGridState(rows, { plugins: [fill.plugin] }). The returned plugins holds them, then the history plugin.

UseDataGridStateResult

Spread the result onto the grid: <DataGrid {...grid} columns={columns} />. The history member is not a grid prop. It holds the controls for your own buttons.

Prop

Type

On this page