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

N-FIN-19: add page size drop down #24

Merged
merged 1 commit into from
Mar 11, 2024
Merged
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
102 changes: 65 additions & 37 deletions src/components/ui/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { ColumnDef, flexRender, getCoreRowModel, getPaginationRowModel, useReact

import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { Button } from '@/components/ui/button';
import React from 'react';
import React, { useEffect, useState } from 'react';
import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight } from 'lucide-react';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';

interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
Expand All @@ -20,13 +21,21 @@ export function DataTable<TData, TValue>({
pagination,
className,
}: DataTableProps<TData, TValue>) {

const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: pagination ? getPaginationRowModel() : undefined,
});

const [pageSize, setPageSize] = useState(50);
useEffect(() => {
if (pagination) {
table.setPageSize(pageSize);
}
}, [table, pagination, pageSize]);

return (
<div className={className}>
<div className="rounded-md border">
Expand Down Expand Up @@ -75,43 +84,62 @@ export function DataTable<TData, TValue>({
</div>
{
pagination && (
<div className="flex items-center justify-end space-x-2 py-4">
<Button
variant="outline"
size="icon"
onClick={() => table.firstPage()}
disabled={!table.getCanPreviousPage()}
>
<span className="sr-only">First page</span>
<ChevronsLeft/>
</Button>
<Button
variant="outline"
size="icon"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
<div className="flex items-center justify-between py-4">
<Select
onValueChange={(value) => {
setPageSize(parseInt(value));
}}
value={pageSize.toString()}
>
<span className="sr-only">Previous page</span>
<ChevronLeft/>
</Button>
<Button
variant="outline"
size="icon"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
<span className="sr-only">Next page</span>
<ChevronRight/>
</Button>
<Button
variant="outline"
size="icon"
onClick={() => table.lastPage()}
disabled={!table.getCanNextPage()}
>
<span className="sr-only">Last page</span>
<ChevronsRight/>
</Button>
<SelectTrigger className="w-[150px]">
<SelectValue placeholder="Select a scope"/>
</SelectTrigger>
<SelectContent>
<SelectItem value={'25'} key={'25'}>25</SelectItem>
<SelectItem value={'50'} key={'50'}>50</SelectItem>
<SelectItem value={'75'} key={'75'}>75</SelectItem>
<SelectItem value={'100'} key={'100'}>100</SelectItem>
</SelectContent>
</Select>

<div className="flex flex-row items-center space-x-2">
<Button
variant="outline"
size="icon"
onClick={() => table.firstPage()}
disabled={!table.getCanPreviousPage()}
>
<span className="sr-only">First page</span>
<ChevronsLeft/>
</Button>
<Button
variant="outline"
size="icon"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
<span className="sr-only">Previous page</span>
<ChevronLeft/>
</Button>
<Button
variant="outline"
size="icon"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
<span className="sr-only">Next page</span>
<ChevronRight/>
</Button>
<Button
variant="outline"
size="icon"
onClick={() => table.lastPage()}
disabled={!table.getCanNextPage()}
>
<span className="sr-only">Last page</span>
<ChevronsRight/>
</Button>
</div>
</div>
)
}
Expand Down