Skip to content
Stylhero 0.8.0
Components

Core

Layout and type

Inputs

Overlays

Navigation

Data display

Charts

Transitions and utilities

Page pieces (landing pages, portfolios, docs)

Icons

Components / Data display

DataGrid

Table with sorting, search, selection, pagination, column hiding and CSV export (MUI X's DataGrid). Add virtualize for thousands of rows.

import { DataGrid } from "@stylhero/ui";

Example

Code
import { Badge, DataGrid, Stack, type GridColumn } from "@stylhero/ui";

type Part = { id: string; name: string; kind: string; price: number };

const parts: Part[] = [
  { id: "1", name: "Ryzen 5 7600", kind: "CPU", price: 199 },
  { id: "2", name: "RTX 4060", kind: "GPU", price: 299 },
  { id: "3", name: "32 GB DDR5", kind: "Memory", price: 94 },
  { id: "4", name: "1 TB NVMe", kind: "Storage", price: 69 },
  { id: "5", name: "650 W Gold", kind: "Power", price: 85 },
];

const columns: GridColumn<Part>[] = [
  { field: "name", header: "Part" },
  { field: "kind", header: "Type", render: (r) => <Badge>{r.kind}</Badge> },
  { field: "price", header: "Price", numeric: true, format: (v) => `$${v}` },
];

type Order = { id: string; order: string; items: number; total: number };

const orders: Order[] = Array.from({ length: 5000 }, (_, i) => ({ id: String(i + 1), order: `#${10001 + i}`, items: 1 + (i % 7), total: 20 + ((i * 37) % 900) }));

function DataGridExample() {
  return (
    <Stack gap={6}>
      <DataGrid label="Parts" rows={parts} columns={columns} search checkboxSelection pageSize={0} exportFileName="parts" />
      <DataGrid<Order>
        label="Orders (5,000 rows, virtualized)"
        rows={orders}
        virtualize
        maxHeight={320}
        search="Search orders"
        columns={[
          { field: "order", header: "Order" },
          { field: "items", header: "Items", numeric: true },
          { field: "total", header: "Total", numeric: true, format: (v) => `$${v}` },
        ]}
      />
    </Stack>
  );
}

Props

PropTypeDefaultDescription
columns required GridColumn<T>[] -
rows required T[] -
actions ReactNode - Extra buttons in the toolbar (right side).
checkboxSelection boolean - Checkbox column; read the result with onSelectionChange.
columnsButton boolean - Shows a "Columns" button to hide and show columns.
defaultSort SortState null
density "compact" | "standard" | "comfortable" "standard"
emptyText ReactNode "No rows"
exportFileName string - Shows an "Export" button that downloads the filtered rows as CSV with this file name.
getRowId (row: T) => string (r) => String((r as { id: unknown }).id) Defaults to row.id.
label string - Accessible name.
loading boolean -
maxHeight number | string - Scroll the body inside this height with a sticky header.
onRowClick (row: T) => void -
onSelectionChange (ids: string[]) => void -
pageSize number 10 Rows per page. 0 shows everything.
pageSizeOptions number[] [10, 25, 50]
rowHeight number - With virtualize: row height in pixels. Measured from the first row when not set.
selection string[] -
virtualize boolean - Only render the rows scrolled into view, for thousands of rows. Shows every row (no pages) in a body that scrolls inside maxHeight (480px if not set). Rows should all be the same height.

Also takes every <div> attribute (id, style, data-*, aria-*, event handlers…), passed to that element.

Types

type GridColumn<T> = {
  /** Key in the row object. Also the column id. */
  field: string;
  header: ReactNode;
  /** Custom cell, e.g. a Badge. Search and sort still use the raw value. */
  render?: (row: T) => ReactNode;
  /** Turns the raw value into text (e.g. money). Used for the cell, search and CSV. */
  format?: (value: unknown, row: T) => string;
  /** Value used for sorting and search, when it isn't row[field]. */
  getValue?: (row: T) => unknown;
  /** Right-aligns and uses tabular numbers. */
  numeric?: boolean;
  sortable?: boolean;
  /** Start hidden; people can show it from "Columns". */
  hidden?: boolean;
  width?: number | string;
};

type SortState = { field: string; direction: "asc" | "desc" } | null;

Keyboard

KeyDoes
TabMoves through the toolbar, sortable headers, checkboxes and pagination
Enter / SpaceSorts by the focused header

Accessibility

  • A real <table> with aria-sort on sorted columns and aria-rowcount for the filtered total. Give it a label.
  • With checkboxSelection, the selected count is announced through a status message. loading sets aria-busy.
  • Every row has aria-rowindex, so with pages or virtualize screen readers still say which row of the total it is.

Generated from src/components/DataGrid.tsx. Something wrong or missing? Open an issue.