Row markers example
Replace the content of the row marker column with your own icons and select-all button while the marker keeps selecting rows.
The marker column sits at the start of the grid, outside your columns. This grid replaces its content with renderMarker and renderMarkerHeader. A press or drag on a marker cell still selects rows. To pick a built-in mode, see Row markers.
The marker has three states: row selected (check), only cells of the row selected (minus), or neither (circle). Pressing or dragging a marker cell selects the row; a plain cell click selects that cell and clears the row selection (Ctrl+click adds a range without clearing it).
pnpm dlx shadcn@latest add @gridcn/data-grid-custom-markers-demo
What the demo shows
rowMarkers="checkbox"sets the width of the marker column. The custom nodes replace the checkboxes.- A marker shows a check for a checked row and a minus for a row with selected cells. Any other row shows a circle.
- The header button checks all rows. Its icon follows
allSelected: a list with a minus for some rows, a list with checks for all rows, a plain list for none. - The header button carries its own accessible name, because the built-in
labels.markers.selectAllname belongs to the built-in checkbox.
Custom markers
renderMarker receives rowId, viewRowIndex, isChecked, and isCellSelected. isChecked is true for a checked row. A cell selection never checks a row.
A cell click sets isCellSelected. With checkedRowsLifetime="until-click" (the default for markers without a checkbox), a cell click also clears the checked rows.
renderMarkerHeader receives allSelected, ariaLabel, and toggleAll. It runs inside the provider, so it can call useDataGridActions. A marker renders again when its row changes, so both renderers can be inline functions. The sample defines them at module scope:
import {
Check,
Circle,
List,
ListChecks,
ListMinus,
Minus,
} from "lucide-react";
import {
DataGrid,
defineColumns,
useDataGridActions,
type MarkerCellRenderer,
type MarkerHeaderRenderCtx,
type MarkerHeaderRenderer,
} from "@/components/data-grid/data-grid";
import { Button } from "@/components/ui/button";
type Row = { id: string; name: string };
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
const rows: Row[] = [{ id: "1", name: "Ada" }];
const renderMarker: MarkerCellRenderer = ({ isChecked, isCellSelected }) =>
isChecked ? (
<Check className="size-3.5" aria-hidden="true" />
) : isCellSelected ? (
<Minus className="size-3.5" aria-hidden="true" />
) : (
<Circle className="size-3.5" aria-hidden="true" />
);
type AllSelected = MarkerHeaderRenderCtx["allSelected"];
function SelectAllButton({ allSelected }: { allSelected: AllSelected }) {
const { setAllRowsChecked } = useDataGridActions();
const checked = allSelected === "checked";
return (
<Button
variant="ghost"
size="icon"
aria-label={checked ? "Unselect all rows" : "Select all rows"}
aria-pressed={checked}
onClick={() => setAllRowsChecked(!checked)}
>
{allSelected === "indeterminate" ? (
<ListMinus className="size-3.5" aria-hidden="true" />
) : checked ? (
<ListChecks className="size-3.5" aria-hidden="true" />
) : (
<List className="size-3.5" aria-hidden="true" />
)}
</Button>
);
}
const renderMarkerHeader: MarkerHeaderRenderer = ({ allSelected }) => (
<SelectAllButton allSelected={allSelected} />
);
function MarkerGrid() {
return (
<DataGrid
defaultData={rows}
columns={columns}
getRowId={(row) => row.id}
rowMarkers="checkbox"
renderMarker={renderMarker}
renderMarkerHeader={renderMarkerHeader}
/>
);
}Related
- Row markers: the modes, widths, and selection gestures
- Pointer selection: the marker press and drag
- Labels: the names of the built-in marker controls