Data Table
A generic data table: search, select filters, click-to-sort headers, pagination, row actions, an empty state and CSV export. Describe the columns once and it does the rest.
npx shadcn@latest add https://ui.develop-better-solutions.com/r/data-table.jsonDependencies:
lucide-react@radix-ui/react-selectUses:select
| Actions | |||
|---|---|---|---|
| Nadia Fassi | 12 | ||
| Karim Idrissi | Critical | 4 | |
| Leila Bennani | Observation | 21 | |
| Omar Haddad | 14 | ||
| Salma Tazi | Discharged | 9 |
1 to 5 of 10
1 / 2
Usage
data-table-example.tsx
"use client";
import { DataTable, type DataColumn } from "@dbs/ui";
import { DashScope } from "./_store";
interface Patient {
id: string;
name: string;
ward: string;
status: "Stable" | "Critical" | "Observation" | "Discharged";
age: number;
room: number;
}
const ROWS: Patient[] = [
{ id: "1", name: "Nadia Fassi", ward: "Cardiology", status: "Stable", age: 54, room: 12 },
{ id: "2", name: "Karim Idrissi", ward: "Neurology", status: "Critical", age: 67, room: 4 },
{ id: "3", name: "Leila Bennani", ward: "Pediatrics", status: "Observation", age: 8, room: 21 },
{ id: "4", name: "Omar Haddad", ward: "Cardiology", status: "Stable", age: 72, room: 14 },
{ id: "5", name: "Salma Tazi", ward: "Orthopedics", status: "Discharged", age: 41, room: 9 },
{ id: "6", name: "Hamza Alami", ward: "Neurology", status: "Observation", age: 59, room: 6 },
{ id: "7", name: "Imane Chraibi", ward: "Pediatrics", status: "Stable", age: 6, room: 22 },
{ id: "8", name: "Reda Mansouri", ward: "Orthopedics", status: "Critical", age: 63, room: 3 },
{ id: "9", name: "Zineb Lahlou", ward: "Cardiology", status: "Stable", age: 38, room: 15 },
{ id: "10", name: "Anas Benkirane", ward: "Neurology", status: "Stable", age: 45, room: 7 },
];
const TONE: Record<Patient["status"], string> = {
Stable: "bg-[color-mix(in_oklab,var(--dash-bar)_14%,transparent)]",
Critical: "bg-[color-mix(in_oklab,var(--dash-warn)_16%,transparent)]",
Observation: "bg-[color-mix(in_oklab,var(--dash-ok)_20%,transparent)]",
Discharged: "bg-[var(--dash-hover)]",
};
const COLUMNS: DataColumn<Patient>[] = [
{ key: "name", header: "Patient", sortValue: (r) => r.name, render: (r) => <span className="font-medium">{r.name}</span> },
{ key: "ward", header: "Ward", sortValue: (r) => r.ward, hideBelow: "sm" },
{ key: "status", header: "Status", sortValue: (r) => r.status, render: (r) => <span className={`inline-flex rounded-full px-2.5 py-1 text-[11px] font-medium ${TONE[r.status]}`}>{r.status}</span> },
{ key: "age", header: "Age", sortValue: (r) => r.age, align: "right", hideBelow: "md" },
{ key: "room", header: "Room", sortValue: (r) => r.room, align: "right" },
];
export default function DataTableExample() {
return (
<DashScope minHeight={0} className="!p-3 sm:!p-5">
<DataTable
rows={ROWS}
columns={COLUMNS}
rowKey={(r) => r.id}
caption="Patients"
pageSize={5}
search={{ placeholder: "Search patients", match: (r, q) => `${r.name} ${r.ward}`.toLowerCase().includes(q) }}
filters={[
{ key: "status", label: "Status", options: [{ value: "all", label: "All statuses" }, { value: "Stable", label: "Stable" }, { value: "Critical", label: "Critical" }, { value: "Observation", label: "Observation" }, { value: "Discharged", label: "Discharged" }], test: (r, v) => v === "all" || r.status === v },
]}
exportCsv={{ filename: "patients.csv" }}
onRowClick={() => undefined}
rowActions={() => <button type="button" className="h-9 rounded-full border border-[var(--dash-line)] px-3 text-[13px] hover:bg-[var(--dash-hover)]">Open</button>}
empty={{ title: "No patients match", hint: "Try a different search or filter." }}
/>
</DashScope>
);
}