Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DataTable component with pagination #1

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@tanstack/react-query": "^5.44.0",
"@tanstack/react-query-devtools": "^5.44.0",
"@tanstack/react-router": "^1.35.3",
"@tanstack/react-table": "^8.17.3",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"lucide-react": "^0.394.0",
Expand Down
107 changes: 107 additions & 0 deletions ui/src/components/ui/data-table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import {
ColumnDef,
flexRender,
getCoreRowModel,
getPaginationRowModel,
useReactTable,
} from "@tanstack/react-table";
import { Button } from "@/components/ui/button";

import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";

interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[];
}

export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
});

return (
<div>
<div className="rounded-md border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className="h-24 text-center"
>
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className="flex items-center justify-end space-x-2 py-4">
<Button
variant="outline"
size="sm"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
Previous
</Button>
<Button
variant="outline"
size="sm"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
Next
</Button>
</div>
</div>
);
}
16 changes: 10 additions & 6 deletions ui/src/routes/tables.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import "react-data-grid/lib/styles.css";

import {
HardDrive,
DatabaseZap,
TableProperties,
Table as TableIcon,
} from "lucide-react";
import DataGrid from "react-data-grid";
import { CodeBlock } from "react-code-blocks";
import { useQuery } from "@tanstack/react-query";
import { createFileRoute } from "@tanstack/react-router";
import { ColumnDef } from "@tanstack/react-table";

import { fetchTable, fetchTables } from "@/api";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Tabs, TabsList, TabsContent, TabsTrigger } from "@/components/ui/tabs";
import { DataTable } from "@/components/ui/data-table";

export const Route = createFileRoute("/tables")({
component: Tables,
Expand Down Expand Up @@ -52,7 +51,13 @@ function Table({ name }: Props) {

if (!data) return;

const columns = data.columns.map((col) => ({ key: col, name: col }));
type Column = {
[key: string]: string;
};
const columns: ColumnDef<Column>[] = data.columns.map((col) => ({
accessorKey: col.toLowerCase(),
header: col,
}));
const rows = data.rows.map((row) =>
row.reduce((acc, curr, i) => {
acc[data.columns[i]] = curr;
Expand Down Expand Up @@ -120,8 +125,7 @@ function Table({ name }: Props) {
<Card className="font-mono text-sm">
<CodeBlock text={data.sql} language="sql" showLineNumbers={false} />
</Card>

<DataGrid columns={columns} rows={rows} className="rdg-light" />
<DataTable columns={columns} data={rows} />
</div>
);
}