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)
(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 holdingCtrl/Shift, and the log shows the modifier keys. - Moving with arrow keys or
Tabalso callsonActiveCellChange. 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 withF2/Enterstill 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
rowandrowKey—rowIndexis the view position after filtering / sorting (the same ashandle.selectCell) and changes when reordered. The position in the originalrowsissourceRowIndex. - Tell columns apart with
columnKey(orcolumn) — use it to change behavior by the clicked column.colIndexis 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 tohandle.selectCelland the like from a column key, usehandle.getColIndex(columnKey). value— the cell's raw value (for columns withgetValue, its return value; before formatting withvalueFormatter).row/valueare snapshots at the time of the notification — editing the same cell doesn't callonActiveCellChange, so therowyou received keeps the values from before the edit. For a panel that keeps showing values after editing, remember therowKeyand look the row up again in the currentrows(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,
onActiveCellChangereceivesnull. - 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, callevent.stopPropagation()in that button. - Called even when read-only — reported regardless of
readOnly/ the column'sreadOnly/canEditCell. - Order — clicking another cell calls
onActiveCellChange→onCellClick. A double-click callsonCellClick× 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(eventis thePointerEventof the finger lifting).
All props are in props.