SpreadsheetGrid
Guides

Cell events

Receive clicks, double-clicks and active cell changes with onCellClick / onCellDoubleClick / onActiveCellChange

You can receive cell clicks, double-clicks and active cell changes, together with the row data. Behaviors common on business screens, such as "copy the clicked row into the form above" or "confirm with a double-click" in a read-only list, can be built entirely outside the grid. All three are optional; without them, the grid behaves the same.

Demo (device list)

#
1
2
3
4
5
6
Rows 6 of 6Columns 4 of 4
Cell —Range —0 cells / 0 rows0 columns

(Click or double-click a cell, or move with the arrow keys)

  • Click a row, and its values are copied into the form above (onCellClick). Click while holding Ctrl / Shift, and the log shows the modifier keys.
  • Moving with arrow keys or Tab also calls onActiveCellChange. Clicking the same cell again doesn't.
  • "Device code" is read-only, but double-clicks are still reported. Check the box to stop editing from starting with params.preventDefault() (editing with F2 / Enter still works).

Usage

<SpreadsheetGrid<Device>
  rows={devices}
  columns={columns}
  rowKeyGetter={(row) => row.code}
  readOnly
  // copy the clicked row into the entry form
  onCellClick={({ row }) => form.setValues(row)}
  // confirm with the double-clicked row (a picker modal and so on)
  onCellDoubleClick={({ row }) => onConfirm(row)}
  // to follow moves by keys too
  onActiveCellChange={(cell) => setCurrent(cell?.row ?? null)}
/>

What you receive is GridCellRef<T> = { row, rowKey, rowIndex, sourceRowIndex, column, columnKey, colIndex, value }. Clicks / double-clicks also have event (the browser's standard MouseEvent).

  • Handle rows with row and rowKey — rowIndex is the view position after filtering / sorting (the same as handle.selectCell) and changes when reordered. The position in the original rows is sourceRowIndex.
  • Tell columns apart with columnKey (or column) — use it to change behavior by the clicked column. colIndex is the order of columns on screen (left-pinned → center → right-pinned) and also counts the row drag handle column, detail toggle column and auto group column (columns the grid adds). To get the position to pass to handle.selectCell and the like from a column key, use handle.getColIndex(columnKey).
  • value — the cell's raw value (for columns with getValue, its return value; before formatting with valueFormatter).
  • row / value are snapshots at the time of the notification — editing the same cell doesn't call onActiveCellChange, so the row you received keeps the values from before the edit. For a panel that keeps showing values after editing, remember the rowKey and look the row up again in the current rows (rows.find((row) => row.code === currentKey)).

Stopping the default action on double-click

The default action of a double-click is "start editing if the cell is editable". onCellDoubleClick is called before that, and calling params.preventDefault() keeps editing from starting.

onCellDoubleClick={(params) => {
  if (params.columnKey === 'subject') {
    params.preventDefault();          // don't edit the subject cell; open a picker modal instead
    openSubjectPicker(params.rowKey);
  }
}}

params.event.preventDefault() (on the browser event) doesn't stop it. It doesn't affect starting an edit with F2 / Enter / character keys.

Key points

  • Data cells only — clicks / double-clicks aren't reported for column headers, the row number "#", group rows, label rows, detail row bands, unloaded SSRM rows, or synthetic columns (detail toggle, row drag handle, auto group column). When the active cell moves onto these, onActiveCellChange receives null.
  • Clicks follow the browser's click — left button only. It isn't called when the pressed cell and the released cell differ (a range selection drag). Clicks on your own buttons placed in a cell also arrive as clicks on that cell; to exclude them, call event.stopPropagation() in that button.
  • Called even when read-only — reported regardless of readOnly / the column's readOnly / canEditCell.
  • Order — clicking another cell calls onActiveCellChange → onCellClick. A double-click calls onCellClick × 2 → onCellDoubleClick (the browser's behavior).
  • "The same cell" for the active cell — no notification when the row key, column key and position are all the same. Even at the same coordinates, it's reported when sorting and the like change the row at that position. A change in the row data alone (an edit being applied and so on) isn't reported. It isn't called on the first mount either.
  • Touch — a double-tap is also reported as onCellDoubleClick (event is the PointerEvent of the finger lifting).

All props are in props.

On this page