Skip to content

Commit 67c74e1

Browse files
committed
Hide empty data table pagination
- Make pagination conditional on there being rows and pages - Tighten props so manual pagination requires a page count
1 parent 3501af6 commit 67c74e1

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/components/ui/data-table.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { cn } from "../../lib/utils";
1515
import { DataTablePagination } from "./data-table-pagination";
1616
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "./table";
1717

18-
interface DataTableProps<TData, TValue> {
18+
interface DataTableBaseProps<TData, TValue> {
1919
columns: ColumnDef<TData, TValue>[];
2020
data: TData[];
2121

@@ -24,8 +24,6 @@ interface DataTableProps<TData, TValue> {
2424
defaultPageSize?: number;
2525
pagination?: PaginationState;
2626
onPaginationChange?: OnChangeFn<PaginationState>;
27-
pageCount?: number;
28-
manualPagination?: boolean;
2927

3028
// Sorting
3129
sorting?: SortingState;
@@ -40,6 +38,18 @@ interface DataTableProps<TData, TValue> {
4038
emptyMessage?: React.ReactNode;
4139
}
4240

41+
type DataTableProps<TData, TValue> = DataTableBaseProps<TData, TValue> &
42+
(
43+
| {
44+
manualPagination: true;
45+
pageCount: number;
46+
}
47+
| {
48+
manualPagination?: false;
49+
pageCount?: number;
50+
}
51+
);
52+
4353
function DataTable<TData, TValue>({
4454
columns,
4555
data,
@@ -89,6 +99,8 @@ function DataTable<TData, TValue>({
8999
pageCount,
90100
});
91101

102+
const showPagination = table.getRowModel().rows.length > 0 && table.getPageCount() > 0;
103+
92104
return (
93105
<div className={cn("space-y-4", className)}>
94106
<Table>
@@ -125,7 +137,9 @@ function DataTable<TData, TValue>({
125137
)}
126138
</TableBody>
127139
</Table>
128-
<DataTablePagination table={table} pageSizeOptions={pageSizeOptions} />
140+
{showPagination ? (
141+
<DataTablePagination table={table} pageSizeOptions={pageSizeOptions} />
142+
) : null}
129143
</div>
130144
);
131145
}

0 commit comments

Comments
 (0)