Skip to content

Hooks

React hooks for integrating datagrid behavior into your components.

useGridBehavior

The main hook for initializing grid behavior. Call this once at the grid container level.

tsx
import { useGridBehavior } from "@ts-zen/react-datagrid";

function DataGrid({ table }) {
  const grid = useGridBehavior({
    rows: table.getRowModel().rows,
    columns: table.getVisibleLeafColumns(),
    config: {
      selection: { mode: "range" },
      editing: { commitOnBlur: true },
    },
    onCommit: ({ cell, value }) => {
      // Save the edited value
    },
  });

  return <div {...grid.props}>{/* Grid content */}</div>;
}

Options

OptionTypeDescription
rows{ id: string }[]Array of row objects with unique IDs
columns{ id: string }[]Array of column objects with unique IDs
configGridConfigConfiguration for navigation, selection, editing
onCommit(params) => voidCallback when a cell value is committed
onSelectionChange(params) => voidCallback when selection changes

Returns

PropertyTypeDescription
propsobjectProps to spread on the grid container
storeStoreApiZustand store for advanced usage
dispatch(action) => voidDispatch actions directly

useCellBehavior

Hook for individual cell behavior. Call this for each cell in the grid.

tsx
import { useCellBehavior } from "@ts-zen/react-datagrid";

function Cell({ grid, rowId, colId, children }) {
  const cell = useCellBehavior(grid, rowId, colId, {
    type: "editable", // or 'readonly', 'interactive'
  });

  return (
    <div
      {...cell.props}
      className={cn(
        cell.isFocused && "ring-2 ring-blue-500",
        cell.isSelected && "bg-blue-50",
      )}
    >
      {cell.isEditing ? (
        <input
          autofocus
          defaultValue={children}
          onBlur={(e) => cell.commit(e.target.value)}
        />
      ) : (
        children
      )}
    </div>
  );
}

Options

OptionTypeDescription
type'readonly' | 'editable' | 'interactive'Cell interaction type

Returns

PropertyTypeDescription
propsobjectProps to spread on the cell element
isFocusedbooleanWhether this cell has focus
isSelectedbooleanWhether this cell is in the selection
isEditingbooleanWhether this cell is in edit mode
commit(value) => voidCommit the current edit
cancelEdit() => voidCancel the current edit

useHeaderBehavior

Hook for header cell behavior. Enables keyboard navigation through headers.

tsx
import { useHeaderBehavior } from "@ts-zen/react-datagrid";

function Header({ grid, colId, children }) {
  const header = useHeaderBehavior(grid, colId);

  return (
    <div {...header.props} className={header.isFocused && "ring-2"}>
      {children}
      <button {...header.interactive("sort")}>Sort</button>
    </div>
  );
}

Returns

PropertyTypeDescription
propsobjectProps to spread on the header element
isFocusedbooleanWhether this header has focus
interactive(id: string) => propsGet props for interactive elements

useRowBehavior

Hook for row-level behavior. Useful for row selection indicators.

tsx
import { useRowBehavior } from "@ts-zen/react-datagrid";

function Row({ grid, rowId, children }) {
  const row = useRowBehavior(grid, rowId);

  return (
    <div {...row.props} className={row.hasSelection && "bg-blue-50"}>
      {children}
    </div>
  );
}

Returns

PropertyTypeDescription
propsobjectProps to spread on the row element
hasSelectionbooleanWhether any cell in this row is selected
hasFocusbooleanWhether any cell in this row has focus

See Also

Released under the MIT License.