Undo and redo
Record cell edits, pastes and fills, undo them with Ctrl+Z, and keep your save handler in sync with undo and redo.
With undoRedo on, the grid records every change that fires cellValueChanged: inline edits, setValue() calls, and paste and fill from Grid Pro. Undo is part of the MIT grid in @birchui/core.
import { createGrid } from '@birchui/core';
const grid = createGrid<Order>(el, {
columns,
rows: orders,
getRowId: (order) => order.id,
undoRedo: { limit: 50 },
});undoRedo: true keeps the last 100 steps. { limit } sets another positive whole number; an invalid limit logs one warning and falls back to 100. Setting undoRedo to false with update() clears the history.
Keys and methods#
| Keys (Windows, Linux) | macOS | Action |
|---|---|---|
| Ctrl+Z | ⌘+Z | Undo |
| Ctrl+Y or Ctrl+Shift+Z | ⌘+Shift+Z | Redo |
The keys work while the grid has focus and no cell is being edited; inside an editor they belong to the text box. After undo or redo, focus moves to the restored cell.
undoButton.addEventListener('click', () => grid.undo());
redoButton.addEventListener('click', () => grid.redo());
grid.on('cellValueChanged', () => {
undoButton.disabled = !grid.canUndo();
redoButton.disabled = !grid.canRedo();
});Steps#
Writes made in the same task form one step. Pasting a 10 by 4 range, dragging the fill handle, or a listener that writes three cells in one go are each undone at once.
Saving changes#
Undo and redo write through setValue(), so cellValueChanged fires with source: 'undo' or source: 'redo'. A save handler written for edits keeps working:
grid.on('cellValueChanged', ({ row, colId, newValue, source }) => {
void api.patchOrder(row.id, { [colId]: newValue }, { reason: source });
});Writes made by undo and redo, and writes your listeners make in response to them, are not recorded again.
Limits#
- Rows are matched by
getRowId. Give the grid one; changes to rows that have since been removed are skipped. setRows()keeps the history. CallclearHistory()when you load a different dataset, or after a successful save if users should not undo past it.- Only cell values are recorded. Sorting, filtering, column moves and row transactions are not undoable; use saved state for view changes.