Buckets:
| 'use client'; | |
| import * as React from 'react'; | |
| import type { ColumnDef, ColumnFiltersState, SortingState, VisibilityState } from '@tanstack/react-table'; | |
| import { | |
| flexRender, | |
| getCoreRowModel, | |
| getFilteredRowModel, | |
| getPaginationRowModel, | |
| getSortedRowModel, | |
| useReactTable, | |
| } from '@tanstack/react-table'; | |
| import { ChevronDown } from 'lucide-react'; | |
| import { Button } from '@/components/ui/button'; | |
| import { | |
| DropdownMenu, | |
| DropdownMenuCheckboxItem, | |
| DropdownMenuContent, | |
| DropdownMenuTrigger, | |
| } from '@/components/ui/dropdown-menu'; | |
| import { Input } from '@/components/ui/input'; | |
| import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'; | |
| interface DataTableProps<TData, TValue> { | |
| columns: ColumnDef<TData, TValue>[]; | |
| data: TData[]; | |
| searchColumn?: string; | |
| searchPlaceholder?: string; | |
| defaultColumnVisibility?: VisibilityState; | |
| pageSize?: number; | |
| defaultSorting?: SortingState; | |
| } | |
| export function DataTable<TData, TValue>({ | |
| columns, | |
| data, | |
| searchColumn, | |
| searchPlaceholder = 'Filter...', | |
| defaultColumnVisibility = {}, | |
| pageSize = 25, | |
| defaultSorting = [], | |
| }: DataTableProps<TData, TValue>) { | |
| const [sorting, setSorting] = React.useState<SortingState>(defaultSorting); | |
| const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>([]); | |
| const [columnVisibility, setColumnVisibility] = React.useState<VisibilityState>(defaultColumnVisibility); | |
| const [rowSelection, setRowSelection] = React.useState({}); | |
| const table = useReactTable({ | |
| data, | |
| columns, | |
| onSortingChange: setSorting, | |
| onColumnFiltersChange: setColumnFilters, | |
| getCoreRowModel: getCoreRowModel(), | |
| getPaginationRowModel: getPaginationRowModel(), | |
| getSortedRowModel: getSortedRowModel(), | |
| getFilteredRowModel: getFilteredRowModel(), | |
| onColumnVisibilityChange: setColumnVisibility, | |
| onRowSelectionChange: setRowSelection, | |
| initialState: { | |
| pagination: { | |
| pageSize: pageSize, | |
| }, | |
| }, | |
| state: { | |
| sorting, | |
| columnFilters, | |
| columnVisibility, | |
| rowSelection, | |
| }, | |
| }); | |
| return ( | |
| <div className="w-full"> | |
| <div className="flex items-center py-4"> | |
| {searchColumn && ( | |
| <Input | |
| placeholder={searchPlaceholder} | |
| value={(table.getColumn(searchColumn)?.getFilterValue() as string) ?? ''} | |
| onChange={(event) => table.getColumn(searchColumn)?.setFilterValue(event.target.value)} | |
| className="max-w-sm" | |
| /> | |
| )} | |
| <DropdownMenu> | |
| <DropdownMenuTrigger asChild> | |
| <Button variant="outline" className="ml-auto"> | |
| Columns <ChevronDown className="ml-2 h-4 w-4" /> | |
| </Button> | |
| </DropdownMenuTrigger> | |
| <DropdownMenuContent align="end"> | |
| {table | |
| .getAllColumns() | |
| .filter((column) => column.getCanHide()) | |
| .map((column) => { | |
| return ( | |
| <DropdownMenuCheckboxItem | |
| key={column.id} | |
| className="capitalize" | |
| checked={column.getIsVisible()} | |
| onCheckedChange={(value) => column.toggleVisibility(!!value)} | |
| > | |
| {column.id} | |
| </DropdownMenuCheckboxItem> | |
| ); | |
| })} | |
| </DropdownMenuContent> | |
| </DropdownMenu> | |
| </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"> | |
| <div className="text-muted-foreground flex-1 text-sm"> | |
| {table.getFilteredRowModel().rows.length} row(s) total. | |
| </div> | |
| <div className="space-x-2"> | |
| <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> | |
| </div> | |
| ); | |
| } | |
Xet Storage Details
- Size:
- 4.78 kB
- Xet hash:
- 04b2aa3c101befbbd4b942fb9c6292820588d047e501f318fd15cc36e3393de5
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.