1- import { faAnglesLeft , faAnglesRight , faChevronLeft , faChevronRight } from "@fortawesome/free-solid-svg-icons" ;
2- import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ;
31import {
42 type ColumnDef ,
53 type ColumnFiltersState ,
64 flexRender ,
75 getCoreRowModel ,
86 getFilteredRowModel ,
9- getPaginationRowModel ,
107 getSortedRowModel ,
118 useReactTable ,
129} from "@tanstack/react-table" ;
13- import { type ChangeEvent , useCallback , useState } from "react" ;
14- import { useTranslation } from "react-i18next" ;
15- import store2 from "store2" ;
16- import Button from "../Button.js" ;
10+ import { useState } from "react" ;
1711import TextFilter from "./TextFilter.js" ;
1812
1913interface Props < T > {
2014 id : string ;
2115 columns : ColumnDef < T , unknown > [ ] ;
2216 data : T [ ] ;
23- pageSizeStoreKey ?: string ;
2417 visibleColumns ?: Record < string , boolean > ;
2518}
2619
27- // XXX: workaround typing
28- const local = store2 as unknown as typeof store2 . default ;
29-
30- const PAGE_SIZES = [ 10 , 30 , 50 , 100 ] ;
31-
3220export default function Table < T > ( props : Props < T > ) {
33- const { id, columns, data, pageSizeStoreKey, visibleColumns } = props ;
34- const { t } = useTranslation ( "common" ) ;
21+ const { id, columns, data, visibleColumns } = props ;
3522 const [ columnVisibility ] = useState < Record < string , boolean > > ( visibleColumns ?? { } ) ;
3623 const [ columnFilters , setColumnFilters ] = useState < ColumnFiltersState > ( [ ] ) ;
3724 const table = useReactTable ( {
@@ -41,34 +28,24 @@ export default function Table<T>(props: Props<T>) {
4128 state : {
4229 columnFilters,
4330 columnVisibility,
44- } ,
45- initialState : {
4631 pagination : {
4732 pageIndex : 0 , // custom initial page index
48- pageSize : pageSizeStoreKey ? local . get ( pageSizeStoreKey , 10 ) : 10 , // custom default page size
33+ pageSize : 500 , // custom default page size
4934 } ,
5035 } ,
5136 onColumnFiltersChange : setColumnFilters ,
5237 getCoreRowModel : getCoreRowModel ( ) ,
5338 getFilteredRowModel : getFilteredRowModel ( ) , // client side filtering
5439 getSortedRowModel : getSortedRowModel ( ) ,
55- getPaginationRowModel : getPaginationRowModel ( ) ,
40+ // getPaginationRowModel: getPaginationRowModel(),
41+ manualPagination : true ,
5642 // debugTable: false,
5743 // debugHeaders: false,
5844 // debugColumns: false,
5945 // debugCells: false,
6046 // debugRows: false,
6147 // debugAll: false,
6248 } ) ;
63- const onPageSizeChange = useCallback (
64- ( e : ChangeEvent < HTMLSelectElement > ) => {
65- const newSize = Number ( e . target . value ) ;
66-
67- local . set ( pageSizeStoreKey , newSize ) ;
68- table . setPageSize ( newSize ) ;
69- } ,
70- [ pageSizeStoreKey , table . setPageSize ] ,
71- ) ;
7249
7350 return (
7451 < div className = "overflow-x-auto" >
@@ -117,40 +94,6 @@ export default function Table<T>(props: Props<T>) {
11794 ) ) }
11895 </ tbody >
11996 </ table >
120- < div className = "divider" />
121- < div className = "navbar bg-base-100" >
122- < div className = "navbar-start" />
123- < div className = "navbar-center join" >
124- < Button className = "btn btn-square join-item" onClick = { ( ) => table . setPageIndex ( 0 ) } disabled = { ! table . getCanPreviousPage ( ) } >
125- < FontAwesomeIcon icon = { faAnglesLeft } />
126- </ Button >
127- < Button className = "btn btn-square join-item" onClick = { ( ) => table . previousPage ( ) } disabled = { ! table . getCanPreviousPage ( ) } >
128- < FontAwesomeIcon icon = { faChevronLeft } />
129- </ Button >
130- < Button className = "btn join-item pointer-events-none" >
131- { t ( "page" ) } { table . getState ( ) . pagination . pageIndex + 1 } of { table . getPageCount ( ) }
132- </ Button >
133- < Button className = "btn btn-square join-item" onClick = { ( ) => table . nextPage ( ) } disabled = { ! table . getCanNextPage ( ) } >
134- < FontAwesomeIcon icon = { faChevronRight } />
135- </ Button >
136- < Button
137- className = "btn btn-square join-item"
138- onClick = { ( ) => table . setPageIndex ( table . getPageCount ( ) - 1 ) }
139- disabled = { ! table . getCanNextPage ( ) }
140- >
141- < FontAwesomeIcon icon = { faAnglesRight } />
142- </ Button >
143- </ div >
144- < div className = "navbar-end" >
145- < select value = { table . getState ( ) . pagination . pageSize } onChange = { onPageSizeChange } className = "select w-32 mx-1" >
146- { PAGE_SIZES . map ( ( pageSize ) => (
147- < option key = { pageSize } value = { pageSize } >
148- { t ( "show" ) } { pageSize }
149- </ option >
150- ) ) }
151- </ select >
152- </ div >
153- </ div >
15497 </ div >
15598 ) ;
15699}
0 commit comments