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.
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, andonCellContextMenu. A move of the active cell logsonActiveCellChange. - An edit logs
onBeforeEditStart, thenonEditEnd. A committed value logsonDataChangewith the change. - A drag over cells logs
onSelectionChange. The panel shows the first five rows ofdetails.getValues(). - A header click logs
onSortChange, because the demo setsheaderClickBehavior="sort". The filter menu logsonFilterChange. - A scroll logs
onRenderedRowsChange. A column resize logsonColumnResizingon each drag frame andonColumnLayoutChangeon release. A pin or a hide from the header menu logsonColumnLayoutChange. - A paste or fill that refuses a cell logs
onValidationError. A single refused edit logsonEditEndwithresult: "rejected". - A fill-handle drag logs one
onDataChange. - Quick search logs no entry.
- The Simulate presence highlight button calls
setPresenceHighlightsofuseDataGridPresence. 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>
</>
);
}Related
- Event shapes: the call order and the error rules of every callback
- Data change events: the shape of
onDataChange - Selection and cell events: the click, selection, and active cell callbacks