gridcn2.0.0
Basic examples

Event log example

Log the payload of every grid callback in a side panel while you click, edit, sort, filter, fill, scroll, and resize.

The grid reports each user action through a callback prop. In the demo, every callback writes its payload to the Event inspector panel, newest first. The panel keeps the last 20 entries.

Click, double-click, right-click, or edit a cell, drag-select, sort/filter, drag-fill, scroll, resize/pin/hide a column.

Name
Email
Age
Score
Silent Tiger
user0@example.com
63
4
Quick Lion
user1@example.com
67
89
Swift Wolf
user2@example.com
40
80
Eager Lion
user3@example.com
50
51
Eager Tiger
user4@example.com
45
83
Silent Wolf
user5@example.com
24
15
Swift Lion
user6@example.com
34
62
Eager Lion
user7@example.com
21
42
Eager Eagle
user8@example.com
25
35
Bold Tiger
user9@example.com
19
52
Bold Bear
user10@example.com
42
28
Swift Wolf
user11@example.com
47
9
Swift Bear
user12@example.com
41
45
Eager Wolf
user13@example.com
35
44
Swift Eagle
user14@example.com
65
51
Quick Lion
user15@example.com
22
28
Bold Wolf
user16@example.com
30
76
Swift Eagle
user17@example.com
19
26
Eager Eagle
user18@example.com
35
70
Eager Bear
user19@example.com
33
67
Silent Lion
user20@example.com
48
89
Silent Tiger
user21@example.com
62
40
Eager Tiger
user22@example.com
51
82
Silent Lion
user23@example.com
26
16
Bold Eagle
user24@example.com
26
38
Bold Lion
user25@example.com
49
3
Quick Tiger
user26@example.com
26
41
Bold Eagle
user27@example.com
50
10
Swift Tiger
user28@example.com
55
6
Silent Lion
user29@example.com
35
42

Event inspector

Interact with the grid to see events appear here.

pnpm dlx shadcn@latest add @gridcn/data-grid-events-demo

What the demo shows

  • A click, a double-click, and a right-click on a cell log onCellClick, onCellDoubleClick, and onCellContextMenu. A move of the active cell logs onActiveCellChange.
  • An edit logs onBeforeEditStart, then onEditEnd. A committed value logs onDataChange with the change.
  • A drag over cells logs onSelectionChange. The panel shows the first five rows of details.getValues().
  • A header click logs onSortChange, because the demo sets headerClickBehavior="sort". The filter menu logs onFilterChange.
  • A scroll logs onRenderedRowsChange. A column resize logs onColumnResizing on each drag frame and onColumnLayoutChange on release. A pin or a hide from the header menu logs onColumnLayoutChange.
  • A paste or fill that refuses a cell logs onValidationError. A single refused edit logs onEditEnd with result: "rejected".
  • A fill-handle drag logs one onDataChange.
  • Quick search logs no entry.
  • The Simulate presence highlight button calls setPresenceHighlights of useDataGridPresence. The panel logs the highlights that it sets.

Each callback appends one entry to a capped list:

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

type Row = { id: string; name: string };
type Entry = { event: string; payload: unknown };

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

const rows: Row[] = [{ id: "1", name: "Ada" }];

export function LoggedGrid() {
  const [log, setLog] = useState<Entry[]>([]);
  const record = useCallback((event: string, payload: unknown) => {
    setLog((prev) => [{ event, payload }, ...prev].slice(0, 20));
  }, []);

  return (
    <>
      <DataGrid
        defaultData={rows}
        columns={columns}
        headerClickBehavior="sort"
        onDataChange={(_next, change) => record("onDataChange", change)}
        onSortChange={(sorts) => record("onSortChange", sorts)}
      />
      <pre>{JSON.stringify(log, null, 2)}</pre>
    </>
  );
}

On this page