Documentation menu
Getting Started
Components
Primitives
- Agent Thinking
- Alert
- Alert Dialog
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Calendar
- Card
- Checkbox
- Command
- Command Palette
- Context Menu
- Data Table
- Dialog
- Dropdown Menu
- Empty
- Field
- Glow Cloud
- Input
- Label
- Medication Schedule
- Period Tabs
- Popover
- Rainbow Button
- Scroll Area
- Select
- Selectable Card
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Tag
- Textarea
- Timeline
- Tooltip
- Use Count Up
Charts and metrics
Patterns
- Confirm Button
- Date Picker
- Embedded
- Empty State
- Entity Drawer
- Error Boundary
- Field Row
- File Dropzone
- Form Dialog
- Kv
- List Editor
- List Page
- Live Time
- Mini Table
- Money
- Note Field
- Note Text
- Page Card
- Page Header
- Page Tabs
- Page Toolbar
- Person Cell
- Picture Field
- Print Card
- Range Switcher
- Row Actions
- Search Input
- Search Select
- Selected Box
- Signature Pad
- Status Badge
- Sub Nav Layout
- Suggest Input
- Tab Icons
- Table Footer
- Theme Accent Picker
- Time Picker
- View All Link
Dashboard cards
Advanced
Device frames
Website widgets
Website screens
Website panels
Primitives
Data Table
A full table on TanStack Table: sorting, selection, pinned columns, column visibility, pagination and skeletons.
import { Tag } from "@/registry/vcare/ui/tag"
import { DataTable, DataTableColumnHeader, createSelectionColumn, type DataTableColumnDef } from "@/registry/vcare/ui/data-table"
type Patient = { id: string; name: string; mrn: string; age: number; department: string; status: "Waiting" | "With doctor" | "Done"; billed: number }
const patients: Patient[] = [
{ id: "1", name: "Meera Raghavan", mrn: "00482", age: 34, department: "General medicine", status: "With doctor", billed: 1250 },
{ id: "2", name: "Arjun Nair", mrn: "00483", age: 52, department: "Orthopaedics", status: "Waiting", billed: 480 },
{ id: "3", name: "Sana Iqbal", mrn: "00484", age: 27, department: "Gynaecology", status: "Done", billed: 3900 },
{ id: "4", name: "Joseph Mathew", mrn: "00485", age: 61, department: "Cardiology", status: "Waiting", billed: 2200 },
{ id: "5", name: "Fathima Rahim", mrn: "00486", age: 8, department: "Paediatrics", status: "Done", billed: 650 },
{ id: "6", name: "Vivek Menon", mrn: "00487", age: 45, department: "General medicine", status: "With doctor", billed: 900 },
{ id: "7", name: "Anjali Das", mrn: "00488", age: 39, department: "Physiotherapy", status: "Waiting", billed: 700 },
]
const tone = { Waiting: "amber", "With doctor": "blue", Done: "green" } as const
const columns: DataTableColumnDef<Patient>[] = [
createSelectionColumn<Patient>(),
{ accessorKey: "name", header: ({ column }) => <DataTableColumnHeader column={column} title="Patient" />, cell: ({ row }) => <span className="font-medium">{row.original.name}</span> },
{ accessorKey: "mrn", header: "MRN" },
{ accessorKey: "age", header: ({ column }) => <DataTableColumnHeader column={column} title="Age" align="right" />, cell: ({ row }) => <div className="text-end tabular-nums">{row.original.age}</div> },
{ accessorKey: "department", header: "Department" },
{ accessorKey: "status", header: "Status", cell: ({ row }) => <Tag color={tone[row.original.status]}>{row.original.status}</Tag> },
]
export default function DataTableDemo() {
return <DataTable className="w-full" columns={columns} data={patients} pageSize={5} searchKey="name" searchPlaceholder="Search patients" enableRowSelection getRowId={(r) => r.id} />
}Installation
npx shadcn@latest add @vcare/data-tableOr install directly from the URL: https://ui.vcarehms.com/r/data-table.json
- Install the dependencies:
npm i @tanstack/react-table lucide-react - Add the registry items it uses: button, checkbox, dropdown-menu, input, skeleton, table, utils.
- Copy this file into your project and change the
@/registry/vcare/…imports to your own paths.
components/ui/data-table.tsx
'use client';
import * as React from 'react';
import {
columnFilteringFeature,
columnOrderingFeature,
columnPinningFeature,
columnSizingFeature,
columnVisibilityFeature,
createColumnHelper,
createFilteredRowModel,
createPaginatedRowModel,
createSortedRowModel,
filterFn_includesString,
flexRender,
rowPaginationFeature,
rowSelectionFeature,
rowSortingFeature,
sortFn_alphanumeric,
sortFn_basic,
sortFn_datetime,
sortFn_text,
tableFeatures,
useTable,
type Column,
type ColumnDef,
type ColumnFiltersState,
type ColumnOrderState,
type ColumnPinningState,
type ColumnVisibilityState,
type ReactTable,
type Row,
type RowData,
type RowSelectionState,
type SortingState,
} from '@tanstack/react-table';
import { ArrowDown, ArrowUp, ArrowUpDown, ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight, GripVertical, Settings2, X } from 'lucide-react';
import { cn } from '@/registry/vcare/lib/utils';
import { Button } from '@/registry/vcare/ui/button';
import { Checkbox } from '@/registry/vcare/ui/checkbox';
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/registry/vcare/ui/dropdown-menu';
import { Input } from '@/registry/vcare/ui/input';
import { Skeleton } from '@/registry/vcare/ui/skeleton';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/registry/vcare/ui/table';
/**
* TanStack Table v9 is feature-gated: only what is registered here ships in
* the bundle. Sorting, filtering, pagination, visibility, ordering, pinning,
* and row selection cover the usual dashboard table.
*/
const dataTableFeatures = tableFeatures({
columnFilteringFeature,
columnOrderingFeature,
columnPinningFeature,
columnSizingFeature,
columnVisibilityFeature,
rowPaginationFeature,
rowSelectionFeature,
rowSortingFeature,
filteredRowModel: createFilteredRowModel(),
paginatedRowModel: createPaginatedRowModel(),
sortedRowModel: createSortedRowModel(),
filterFns: { includesString: filterFn_includesString },
sortFns: {
alphanumeric: sortFn_alphanumeric,
basic: sortFn_basic,
datetime: sortFn_datetime,
text: sortFn_text,
},
});
type DataTableFeatures = typeof dataTableFeatures;
type DataTableColumnDef<TData extends RowData> = ColumnDef<
DataTableFeatures,
TData,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any
>;
/** The table instance returned by `useDataTable`. */
type DataTableInstance<TData extends RowData> = ReactTable<DataTableFeatures, TData>;
type DataTableColumn<TData extends RowData> = Column<DataTableFeatures, TData, unknown>;
type DataTableRow<TData extends RowData> = Row<DataTableFeatures, TData>;
/** Typed column helper bound to the data table's feature set. */
function createDataTableColumnHelper<TData extends RowData>() {
return createColumnHelper<DataTableFeatures, TData>();
}
/** Row density presets. Only the cell padding changes. */
type DataTableDensity = 'compact' | 'default' | 'relaxed';
const densityClasses: Record<DataTableDensity, string> = {
// Heights come from --head-h / --row-h (see themes.css); "compact" and "default" are identical on purpose.
compact: '',
default: '',
relaxed: '[&_[data-slot=table-cell]]:h-[calc(var(--row-h)+8px)]',
};
/* -------------------------------------------------------------------------- */
/* hook */
/* -------------------------------------------------------------------------- */
export interface UseDataTableOptions<TData extends RowData> {
columns: ReadonlyArray<DataTableColumnDef<TData>>;
data: TData[];
/** Rows per page. Pass 0 to keep every row on a single page. */
pageSize?: number;
initialSorting?: SortingState;
initialColumnVisibility?: ColumnVisibilityState;
/**
* Column ids held against the leading and trailing edge. Read once, on
* mount; use `table.setColumnPinning` to change it afterwards. Pinned
* columns should declare a `size` so the sticky offsets line up.
*/
pinnedColumns?: Partial<ColumnPinningState>;
/** Turn on checkbox selection, or decide per row. */
enableRowSelection?: boolean | ((row: DataTableRow<TData>) => boolean);
/** Stable row ids. Selection survives sorting and paging with this set. */
getRowId?: (row: TData, index: number) => string;
onRowSelectionChange?: (selection: RowSelectionState) => void;
}
/**
* Wires the table state the components below read. Call it when you want to
* lay the pieces out yourself; `DataTable` calls it for you.
*/
function useDataTable<TData extends RowData>({
columns,
data,
pageSize = 10,
initialSorting,
initialColumnVisibility,
pinnedColumns,
enableRowSelection,
getRowId,
onRowSelectionChange,
}: UseDataTableOptions<TData>): DataTableInstance<TData> {
const [sorting, setSorting] = React.useState<SortingState>(initialSorting ?? []);
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>([]);
const [columnVisibility, setColumnVisibility] = React.useState<ColumnVisibilityState>(initialColumnVisibility ?? {});
const [columnOrder, setColumnOrder] = React.useState<ColumnOrderState>([]);
const [columnPinning, setColumnPinning] = React.useState<ColumnPinningState>(() => ({
start: pinnedColumns?.start ?? [],
end: pinnedColumns?.end ?? [],
}));
const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({});
// Report the selection from an effect, not from inside the state updater:
// the table can call the updater while it renders.
const notify = React.useRef(onRowSelectionChange);
const mounted = React.useRef(false);
React.useEffect(() => {
notify.current = onRowSelectionChange;
});
React.useEffect(() => {
if (!mounted.current) {
mounted.current = true;
return;
}
notify.current?.(rowSelection);
}, [rowSelection]);
return useTable({
features: dataTableFeatures,
data,
columns: columns as DataTableColumnDef<TData>[],
initialState: {
pagination: {
pageIndex: 0,
pageSize: pageSize > 0 ? pageSize : Number.MAX_SAFE_INTEGER,
},
},
getRowId,
enableRowSelection,
// Shift-click extends the selection from the last row you touched.
isRowRangeSelectionEvent: (event) => Boolean((event as { shiftKey?: boolean }).shiftKey),
onSortingChange: setSorting,
onColumnFiltersChange: setColumnFilters,
onColumnVisibilityChange: setColumnVisibility,
onColumnOrderChange: setColumnOrder,
onColumnPinningChange: setColumnPinning,
onRowSelectionChange: setRowSelection,
state: {
sorting,
columnFilters,
columnVisibility,
columnOrder,
columnPinning,
rowSelection,
},
});
}
/* -------------------------------------------------------------------------- */
/* preset */
/* -------------------------------------------------------------------------- */
export interface DataTableProps<TData extends RowData> extends Omit<React.ComponentProps<'div'>, 'onSelect'>, UseDataTableOptions<TData> {
/** Column id to filter with the search input. Omit to hide the input. */
searchKey?: string;
searchPlaceholder?: string;
/** Extra toolbar content, rendered between the search input and view options. */
toolbar?: React.ReactNode;
showViewOptions?: boolean;
showPagination?: boolean;
pageSizeOptions?: number[];
emptyMessage?: React.ReactNode;
/** Swap the rows for a skeleton, e.g. on first load. */
loading?: boolean;
/** Keep the rows but dim them, e.g. while a filter change is in flight. */
pending?: boolean;
skeletonRows?: number;
stickyHeader?: boolean;
/** Caps the scroll area, e.g. 420 or "60vh". Pair it with stickyHeader. */
maxHeight?: number | string;
/** Let columns be dragged into a new order. Pass ids to limit which ones. */
reorderable?: boolean | string[];
density?: DataTableDensity;
onRowClick?: (row: DataTableRow<TData>) => void;
rowClassName?: (row: DataTableRow<TData>) => string | undefined;
/** Anything else to put on a row, e.g. a data attribute or a handler. */
rowProps?: (row: DataTableRow<TData>) => React.ComponentProps<'tr'>;
/** Wrap the row element, e.g. in a context menu trigger. */
renderRow?: (row: DataTableRow<TData>, element: React.ReactElement) => React.ReactNode;
/** Actions shown in the bar that appears while rows are selected. */
selectionActions?: React.ReactNode;
}
/**
* The batteries-included table: toolbar, rows, and pagination. Every part is
* exported on its own, so reach for `useDataTable` and compose them by hand
* when this shape is not the one you want.
*/
function DataTable<TData extends RowData>({
columns,
data,
pageSize = 10,
initialSorting,
initialColumnVisibility,
pinnedColumns,
enableRowSelection,
getRowId,
onRowSelectionChange,
searchKey,
searchPlaceholder = 'Filter...',
toolbar,
showViewOptions = true,
showPagination = true,
pageSizeOptions,
emptyMessage = 'No results.',
loading = false,
pending = false,
skeletonRows,
stickyHeader = false,
maxHeight,
reorderable = false,
density = 'default',
onRowClick,
rowClassName,
rowProps,
renderRow,
selectionActions,
className,
...props
}: DataTableProps<TData>) {
const table = useDataTable({
columns,
data,
pageSize,
initialSorting,
initialColumnVisibility,
pinnedColumns,
enableRowSelection,
getRowId,
onRowSelectionChange,
});
const hasToolbar = Boolean(searchKey || toolbar || showViewOptions);
const selectedCount = table.getSelectedRowModel().rows.length;
return (
<div data-slot="data-table" className={cn('flex flex-col gap-4', className)} {...props}>
{hasToolbar ? (
<DataTableToolbar>
{searchKey ? <DataTableSearch table={table} column={searchKey} placeholder={searchPlaceholder} disabled={loading} /> : null}
{toolbar}
{showViewOptions ? <DataTableViewOptions table={table} reorderable={reorderable} className="ml-auto" disabled={loading} /> : null}
</DataTableToolbar>
) : null}
{selectedCount > 0 ? <DataTableSelectionBar table={table}>{selectionActions}</DataTableSelectionBar> : null}
<DataTableContent
table={table}
loading={loading}
pending={pending}
skeletonRows={skeletonRows ?? Math.min(pageSize || 10, 10)}
stickyHeader={stickyHeader}
maxHeight={maxHeight}
reorderable={reorderable}
density={density}
emptyMessage={emptyMessage}
onRowClick={onRowClick}
rowClassName={rowClassName}
rowProps={rowProps}
renderRow={renderRow}
/>
{showPagination ? <DataTablePagination table={table} pageSizeOptions={pageSizeOptions} /> : null}
</div>
);
}
/* -------------------------------------------------------------------------- */
/* toolbar */
/* -------------------------------------------------------------------------- */
/** The row above the table. Anything can go in it. */
function DataTableToolbar({ className, ...props }: React.ComponentProps<'div'>) {
return <div data-slot="data-table-toolbar" className={cn('flex flex-wrap items-center gap-2', className)} {...props} />;
}
interface DataTableSearchProps<TData extends RowData> extends Omit<React.ComponentProps<typeof Input>, 'value' | 'onChange'> {
table: DataTableInstance<TData>;
/** Column id to filter. */
column: string;
}
/** An input bound to one column's filter. */
function DataTableSearch<TData extends RowData>({ table, column, className, ...props }: DataTableSearchProps<TData>) {
const target = table.getColumn(column);
if (!target) return null;
return (
<Input
data-slot="data-table-search"
value={(target.getFilterValue() as string) ?? ''}
onChange={(event) => target.setFilterValue(event.target.value)}
className={cn('h-8 max-w-sm', className)}
{...props}
/>
);
}
interface DataTableViewOptionsProps<TData extends RowData> extends React.ComponentProps<typeof Button> {
table: DataTableInstance<TData>;
/** Adds "move left / move right" items for the reorderable columns. */
reorderable?: boolean | string[];
}
/** Column visibility, and column order when reordering is on. */
function DataTableViewOptions<TData extends RowData>({ table, reorderable = false, className, ...props }: DataTableViewOptionsProps<TData>) {
const columns = table.getAllColumns().filter((column) => column.getCanHide());
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="sm" className={cn('h-8', className)} {...props}>
<Settings2 />
View
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-44">
<DropdownMenuLabel>Toggle columns</DropdownMenuLabel>
<DropdownMenuSeparator />
{columns.map((column) => (
<DropdownMenuCheckboxItem
key={column.id}
className="capitalize"
checked={column.getIsVisible()}
onCheckedChange={(value) => column.toggleVisibility(!!value)}
>
{column.id}
</DropdownMenuCheckboxItem>
))}
{reorderable ? (
<>
<DropdownMenuSeparator />
<DropdownMenuLabel>Column order</DropdownMenuLabel>
<DropdownMenuItem onSelect={() => table.resetColumnOrder()}>Reset to default</DropdownMenuItem>
</>
) : null}
</DropdownMenuContent>
</DropdownMenu>
);
}
interface DataTableColumnHeaderProps<TData extends RowData, TValue> extends React.ComponentProps<'div'> {
column: Column<DataTableFeatures, TData, TValue>;
title: string;
align?: 'left' | 'right';
}
function DataTableColumnHeader<TData extends RowData, TValue>({
column,
title,
align = 'left',
className,
...props
}: DataTableColumnHeaderProps<TData, TValue>) {
if (!column.getCanSort()) {
return (
<div className={cn(align === 'right' && 'text-right', className)} {...props}>
{title}
</div>
);
}
const sorted = column.getIsSorted();
const Icon = sorted === 'asc' ? ArrowUp : sorted === 'desc' ? ArrowDown : ArrowUpDown;
return (
<div className={cn('flex', align === 'right' && 'justify-end', className)} {...props}>
<Button
variant="ghost"
size="sm"
className="data-[sorted=true]:text-foreground -ml-2 h-8 gap-1.5 px-2"
data-sorted={sorted !== false}
onClick={() => column.toggleSorting(sorted === 'asc')}
>
{title}
<Icon className="text-muted-foreground size-3.5" />
</Button>
</div>
);
}
/* -------------------------------------------------------------------------- */
/* selection */
/* -------------------------------------------------------------------------- */
/**
* A checkbox column. Put it first in your column list and give the table a
* `getRowId` so the selection survives sorting and paging.
*/
function createSelectionColumn<TData extends RowData>(overrides?: Partial<DataTableColumnDef<TData>>): DataTableColumnDef<TData> {
return {
id: 'select',
size: 36,
enableSorting: false,
enableHiding: false,
header: ({ table }) => (
<Checkbox
checked={table.getIsAllPageRowsSelected() ? true : table.getIsSomePageRowsSelected() ? 'indeterminate' : false}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
aria-label="Select all rows on this page"
/>
),
cell: ({ row }) => (
<Checkbox
checked={row.getIsSelected()}
disabled={!row.getCanSelect()}
// The handler wants the checkbox's next value and the shift key, which
// is what turns a click into a range selection.
onClick={(event) =>
row.getToggleSelectedHandler()({
target: { checked: !row.getIsSelected() },
shiftKey: event.shiftKey,
})
}
aria-label="Select row"
/>
),
...overrides,
} as DataTableColumnDef<TData>;
}
interface DataTableSelectionBarProps<TData extends RowData> extends React.ComponentProps<'div'> {
table: DataTableInstance<TData>;
}
/** The bar that appears once rows are selected. Children are the actions. */
function DataTableSelectionBar<TData extends RowData>({ table, children, className, ...props }: DataTableSelectionBarProps<TData>) {
const count = table.getSelectedRowModel().rows.length;
return (
<div
data-slot="data-table-selection-bar"
className={cn('bg-muted/50 flex flex-wrap items-center gap-2 rounded-md border px-3 py-2', className)}
{...props}
>
<span className="text-sm tabular-nums">
{count} {count === 1 ? 'row' : 'rows'} selected
</span>
<div className="ml-auto flex items-center gap-2">
{children}
<Button variant="ghost" size="sm" className="h-8" onClick={() => table.resetRowSelection()}>
<X />
Clear
</Button>
</div>
</div>
);
}
/* -------------------------------------------------------------------------- */
/* content */
/* -------------------------------------------------------------------------- */
/** Moves `columnId` to the slot `targetId` currently occupies. */
function moveColumn<TData extends RowData>(table: DataTableInstance<TData>, columnId: string, targetId: string) {
const order = table.getAllLeafColumns().map((column) => column.id);
const from = order.indexOf(columnId);
const to = order.indexOf(targetId);
if (from < 0 || to < 0 || from === to) return;
const next = order.slice();
next.splice(from, 1);
next.splice(to, 0, columnId);
table.setColumnOrder(next);
}
/** Nudges `columnId` one slot left (-1) or right (1). */
function shiftColumn<TData extends RowData>(table: DataTableInstance<TData>, columnId: string, delta: number) {
const order = table.getAllLeafColumns().map((column) => column.id);
const from = order.indexOf(columnId);
const to = from + delta;
if (from < 0 || to < 0 || to >= order.length) return;
const next = order.slice();
next.splice(from, 1);
next.splice(to, 0, columnId);
table.setColumnOrder(next);
}
/**
* Sticky offsets and hairlines for a pinned cell. The feature computes the
* regions; the renderer owns the CSS.
*/
function getPinnedStyle<TData extends RowData>(
column: DataTableColumn<TData>,
{ stickyHeader = false }: { stickyHeader?: boolean } = {},
): React.CSSProperties | undefined {
const pinned = column.getIsPinned();
const shadows: string[] = [];
if (stickyHeader) shadows.push('inset 0 -1px 0 0 var(--border)');
if (pinned === 'start' && column.getIsLastColumn('start')) {
shadows.push('inset -1px 0 0 0 var(--border)');
}
if (pinned === 'end' && column.getIsFirstColumn('end')) {
shadows.push('inset 1px 0 0 0 var(--border)');
}
if (!pinned && !shadows.length) return undefined;
// Auto table layout treats width as a suggestion, so pin all three: the
// sticky offsets are computed from `size` and have to match what renders.
const size = pinned ? column.getSize() : undefined;
return {
insetInlineStart: pinned === 'start' ? `${column.getStart('start')}px` : undefined,
insetInlineEnd: pinned === 'end' ? `${column.getAfter('end')}px` : undefined,
width: size,
minWidth: size,
maxWidth: size,
boxShadow: shadows.length ? shadows.join(', ') : undefined,
};
}
/** Grip on a reorderable header. Drag it, or focus it and press an arrow. */
function DataTableDragHandle<TData extends RowData>({ table, column }: { table: DataTableInstance<TData>; column: DataTableColumn<TData> }) {
return (
<button
type="button"
aria-label={`Reorder ${column.id} column`}
className="text-muted-foreground/50 hover:text-foreground focus-visible:ring-ring/50 -ml-1 rounded-sm outline-none focus-visible:ring-[3px]"
onKeyDown={(event) => {
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
event.preventDefault();
shiftColumn(table, column.id, event.key === 'ArrowLeft' ? -1 : 1);
}}
>
<GripVertical className="size-3.5" />
</button>
);
}
const skeletonWidths = [72, 44, 58, 36, 64, 48, 52];
export interface DataTableContentProps<TData extends RowData> extends React.ComponentProps<'div'> {
table: DataTableInstance<TData>;
loading?: boolean;
pending?: boolean;
skeletonRows?: number;
stickyHeader?: boolean;
maxHeight?: number | string;
reorderable?: boolean | string[];
density?: DataTableDensity;
emptyMessage?: React.ReactNode;
onRowClick?: (row: DataTableRow<TData>) => void;
rowClassName?: (row: DataTableRow<TData>) => string | undefined;
/**
* Anything else to put on a row: a handler, a data attribute, a title.
* Merged after the built-in props, so it wins.
*/
rowProps?: (row: DataTableRow<TData>) => React.ComponentProps<'tr'>;
/**
* Wrap the row element, e.g. in a context menu trigger or a link. Render
* the element you are handed somewhere inside what you return.
*/
renderRow?: (row: DataTableRow<TData>, element: React.ReactElement) => React.ReactNode;
}
/** The bordered table itself: header, rows, and their loading states. */
function DataTableContent<TData extends RowData>({
table,
loading = false,
pending = false,
skeletonRows = 8,
stickyHeader = false,
maxHeight,
reorderable = false,
density = 'default',
emptyMessage = 'No results.',
onRowClick,
rowClassName,
rowProps,
renderRow,
className,
style,
...props
}: DataTableContentProps<TData>) {
const [dragging, setDragging] = React.useState<string | null>(null);
const [dropTarget, setDropTarget] = React.useState<string | null>(null);
const { start, end } = table.state.columnPinning;
const hasPinned = start.length > 0 || end.length > 0;
const visibleColumns = table.getVisibleLeafColumns();
const rows = table.getRowModel().rows;
const canReorder = (column: DataTableColumn<TData>) => {
if (!reorderable || column.getIsPinned()) return false;
return Array.isArray(reorderable) ? reorderable.includes(column.id) : column.getCanHide();
};
/** Native drag and drop, so no drag library is pulled in for this. */
const dragProps = (columnId: string): React.ComponentProps<'th'> => ({
draggable: true,
onDragStart: (event) => {
setDragging(columnId);
event.dataTransfer.effectAllowed = 'move';
event.dataTransfer.setData('text/plain', columnId);
},
onDragOver: (event) => {
if (!dragging || dragging === columnId) return;
event.preventDefault();
event.dataTransfer.dropEffect = 'move';
setDropTarget(columnId);
},
onDragLeave: () => setDropTarget((current) => (current === columnId ? null : current)),
onDrop: (event) => {
event.preventDefault();
if (dragging) moveColumn(table, dragging, columnId);
setDragging(null);
setDropTarget(null);
},
onDragEnd: () => {
setDragging(null);
setDropTarget(null);
},
});
// Pinned cells are painted opaque so rows scroll under them, which would
// otherwise hide the row's own hover and selected colours. Move both onto
// the cells so pinned and unpinned columns stay in step. They have to stay
// opaque too: a translucent hover on a pinned cell lets the columns passing
// underneath show through, so the row hover is `bg-muted/50` pre-mixed over
// the background rather than blended with whatever is behind the cell.
const rowClasses = cn('group/row', hasPinned && 'hover:bg-transparent data-[state=selected]:bg-transparent');
const cellClasses = cn(
hasPinned && ['group-hover/row:bg-[color-mix(in_oklab,var(--muted)_50%,var(--background))]', 'group-data-[state=selected]/row:bg-muted'],
);
return (
<div
data-slot="data-table-content"
aria-busy={loading || pending || undefined}
className={cn(
'relative overflow-hidden rounded-md border',
'[&>[data-slot=table-container]]:[scrollbar-width:thin] [&>[data-slot=table-container]]:[scrollbar-color:color-mix(in_oklab,var(--foreground)_20%,transparent)_transparent]',
maxHeight !== undefined && '[&>[data-slot=table-container]]:max-h-[var(--data-table-max-height)]',
densityClasses[density],
className,
)}
style={
maxHeight !== undefined
? ({
...style,
'--data-table-max-height': typeof maxHeight === 'number' ? `${maxHeight}px` : maxHeight,
} as React.CSSProperties)
: style
}
{...props}
>
{pending ? <div aria-hidden className="bg-primary/60 absolute inset-x-0 top-0 z-40 h-0.5 animate-pulse" /> : null}
<Table>
<TableHeader className={cn(stickyHeader && '[&_tr]:border-b-0')}>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id} className={cn(rowClasses, 'hover:bg-transparent')}>
{headerGroup.headers.map((header) => {
const column = header.column as DataTableColumn<TData>;
const pinned = column.getIsPinned();
const draggable = canReorder(column);
return (
<TableHead
key={header.id}
colSpan={header.colSpan}
data-pinned={pinned || undefined}
data-dragging={dragging === column.id || undefined}
data-drop-target={dropTarget === column.id || undefined}
{...(draggable ? dragProps(column.id) : null)}
className={cn(
(stickyHeader || pinned) && 'bg-background',
stickyHeader && 'sticky top-0 z-20',
pinned && 'sticky z-20 overflow-hidden',
pinned && stickyHeader && 'z-30',
draggable && 'cursor-grab data-[dragging]:opacity-40',
'data-[drop-target]:bg-muted',
)}
style={getPinnedStyle(column, { stickyHeader })}
>
{header.isPlaceholder ? null : draggable ? (
<div className="flex items-center gap-1">
<DataTableDragHandle table={table} column={column} />
{flexRender(column.columnDef.header, header.getContext())}
</div>
) : (
flexRender(column.columnDef.header, header.getContext())
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody className={cn(pending && 'pointer-events-none opacity-50', 'transition-opacity')}>
{loading ? (
<DataTableSkeletonRows columns={visibleColumns.length} rows={skeletonRows} />
) : rows.length ? (
rows.map((row) => {
const extra = rowProps?.(row);
const element = (
<TableRow
data-state={row.getIsSelected() ? 'selected' : undefined}
onClick={onRowClick ? () => onRowClick(row) : undefined}
{...extra}
className={cn(rowClasses, onRowClick && 'cursor-pointer', rowClassName?.(row), extra?.className)}
>
{row.getVisibleCells().map((cell) => {
const column = cell.column as DataTableColumn<TData>;
const pinned = column.getIsPinned();
return (
<TableCell
key={cell.id}
data-pinned={pinned || undefined}
className={cn(cellClasses, pinned && 'bg-background sticky z-20 overflow-hidden')}
style={getPinnedStyle(column)}
>
{flexRender(column.columnDef.cell, cell.getContext())}
</TableCell>
);
})}
</TableRow>
);
return <React.Fragment key={row.id}>{renderRow ? renderRow(row, element) : element}</React.Fragment>;
})
) : (
<TableRow className="hover:bg-transparent">
<TableCell colSpan={visibleColumns.length} className="text-muted-foreground h-auto py-0 text-center leading-normal whitespace-normal">
<div className="flex flex-col items-center gap-2 py-10">
<span className="font-medium">{emptyMessage}</span>
</div>
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
);
}
/* -------------------------------------------------------------------------- */
/* skeletons */
/* -------------------------------------------------------------------------- */
function DataTableSkeletonRows({ columns, rows }: { columns: number; rows: number }) {
return (
<>
{Array.from({ length: rows }).map((_, rowIndex) => (
<TableRow key={rowIndex} className="hover:bg-transparent">
{Array.from({ length: columns }).map((__, columnIndex) => (
<TableCell key={columnIndex}>
<Skeleton
className="h-4"
style={{
width: `${skeletonWidths[(rowIndex + columnIndex) % skeletonWidths.length]}%`,
}}
/>
</TableCell>
))}
</TableRow>
))}
</>
);
}
export interface DataTableSkeletonProps extends React.ComponentProps<'div'> {
columns: number;
rows?: number;
showToolbar?: boolean;
showPagination?: boolean;
}
/**
* A standalone placeholder for when the columns are not known yet, e.g. a
* Suspense fallback. Once you have a table instance, `loading` on
* `DataTableContent` renders the same rows under the real header.
*/
function DataTableSkeleton({ columns, rows = 8, showToolbar = true, showPagination = true, className, ...props }: DataTableSkeletonProps) {
return (
<div data-slot="data-table-skeleton" aria-busy className={cn('flex flex-col gap-4', className)} {...props}>
{showToolbar ? (
<div className="flex items-center gap-2">
<Skeleton className="h-8 w-full max-w-sm" />
<Skeleton className="ml-auto h-8 w-20" />
</div>
) : null}
<div className="overflow-hidden rounded-md border">
<Table>
<TableHeader>
<TableRow className="hover:bg-transparent">
{Array.from({ length: columns }).map((_, index) => (
<TableHead key={index}>
<Skeleton className="h-3.5 w-16" />
</TableHead>
))}
</TableRow>
</TableHeader>
<TableBody>
<DataTableSkeletonRows columns={columns} rows={rows} />
</TableBody>
</Table>
</div>
{showPagination ? (
<div className="flex items-center justify-between gap-4">
<Skeleton className="h-5 w-28" />
<Skeleton className="h-8 w-48" />
</div>
) : null}
</div>
);
}
/* -------------------------------------------------------------------------- */
/* pagination */
/* -------------------------------------------------------------------------- */
interface DataTablePaginationProps<TData extends RowData> extends React.ComponentProps<'div'> {
table: DataTableInstance<TData>;
/** Adds a rows-per-page menu. */
pageSizeOptions?: number[];
}
function DataTablePagination<TData extends RowData>({ table, pageSizeOptions, className, ...props }: DataTablePaginationProps<TData>) {
const { pageIndex, pageSize } = table.state.pagination;
const total = table.getFilteredRowModel().rows.length;
const from = total === 0 ? 0 : pageIndex * pageSize + 1;
const to = Math.min((pageIndex + 1) * pageSize, total);
return (
<div data-slot="data-table-pagination" className={cn('flex flex-wrap items-center justify-between gap-4', className)} {...props}>
<div className="flex items-center gap-3">
<p className="text-muted-foreground text-sm tabular-nums">
{from}–{to} of {total}
</p>
{pageSizeOptions?.length ? (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="sm" className="h-8 tabular-nums">
{pageSize} per page
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="w-36">
{pageSizeOptions.map((option) => (
<DropdownMenuCheckboxItem
key={option}
checked={option === pageSize}
onCheckedChange={() => table.setPageSize(option)}
className="tabular-nums"
>
{option} per page
</DropdownMenuCheckboxItem>
))}
</DropdownMenuContent>
</DropdownMenu>
) : null}
</div>
<div className="flex items-center gap-1">
<Button
variant="outline"
size="icon"
className="size-8"
onClick={() => table.firstPage()}
disabled={!table.getCanPreviousPage()}
aria-label="First page"
>
<ChevronsLeft />
</Button>
<Button
variant="outline"
size="icon"
className="size-8"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
aria-label="Previous page"
>
<ChevronLeft />
</Button>
<span className="text-muted-foreground px-2 text-sm tabular-nums">
Page {pageIndex + 1} of {Math.max(table.getPageCount(), 1)}
</span>
<Button
variant="outline"
size="icon"
className="size-8"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
aria-label="Next page"
>
<ChevronRight />
</Button>
<Button
variant="outline"
size="icon"
className="size-8"
onClick={() => table.lastPage()}
disabled={!table.getCanNextPage()}
aria-label="Last page"
>
<ChevronsRight />
</Button>
</div>
</div>
);
}
export {
DataTable,
DataTableColumnHeader,
DataTableContent,
DataTablePagination,
DataTableSearch,
DataTableSelectionBar,
DataTableSkeleton,
DataTableToolbar,
DataTableViewOptions,
createDataTableColumnHelper,
createSelectionColumn,
dataTableFeatures,
moveColumn,
shiftColumn,
useDataTable,
type DataTableColumnDef,
type DataTableDensity,
type DataTableFeatures,
type DataTableInstance,
type DataTableRow,
};Usage
import { DataTable, DataTableColumnHeader, DataTableContent, DataTablePagination, DataTableSearch, DataTableSelectionBar } from "@/components/ui/data-table"Exports
DataTable, DataTableColumnHeader, DataTableContent, DataTablePagination, DataTableSearch, DataTableSelectionBar, DataTableSkeleton, DataTableToolbar, DataTableViewOptions, useDataTable
Dependencies
@tanstack/react-table@^9.3.1(npm)lucide-react@^1.80.0(npm)@vcare/button(registry)@vcare/checkbox(registry)@vcare/dropdown-menu(registry)@vcare/input(registry)@vcare/skeleton(registry)@vcare/table(registry)@vcare/utils(registry)
Raw item: /r/data-table.json