Skip to main content

Example: Client selection + actions

Selection respects active client filters via getFilteredSelectedRowModel().

"use client";

import type { ColumnDef, Table } from "@tanstack/react-table";
import {
DataTable,
DataTableColumnHeader,
} from "@/components/datatable";
import {
DataTableActionBar,
DataTableActionBarAction,
DataTableActionBarSelection,
} from "@/components/datatable/components/data-table-action-bar";

type Tag = { id: string; name: string; color: string };

const columns: ColumnDef<Tag>[] = [
{
id: "name",
accessorKey: "name",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Tag" />
),
enableColumnFilter: true,
meta: { variant: "text", label: "Tag" },
},
{
id: "color",
accessorKey: "color",
header: "Color",
cell: ({ row }) => (
<span
className="inline-block size-3 rounded-full"
style={{ background: row.original.color }}
/>
),
},
];

function TagsActionBar({ table }: { table: Table<Tag> }) {
const rows = table.getFilteredSelectedRowModel().rows.map((r) => r.original);

return (
<DataTableActionBar table={table}>
<DataTableActionBarSelection table={table} />
<DataTableActionBarAction
onClick={() => navigator.clipboard.writeText(rows.map((r) => r.name).join(", "))}
>
Copy names
</DataTableActionBarAction>
<DataTableActionBarAction
onClick={() => console.log("delete", rows.map((r) => r.id))}
>
Delete
</DataTableActionBarAction>
</DataTableActionBar>
);
}

export function TagsClientTable({ tags }: { tags: Tag[] }) {
return (
<DataTable<Tag>
columns={columns}
source={{ type: "client", data: tags }}
enableToolbar
enablePagination
enableRowSelection
getRowId={(r) => r.id}
initialState={{ columnPinning: { left: ["select"] } }}
actionBar={(table) => <TagsActionBar table={table} />}
/>
);
}