@@ -24,6 +24,15 @@ const BaseTableWrapper = styled.div`
24
24
max-width: 100%;
25
25
border-bottom: 1px solid ${ ( props ) => props . theme . colors . quartenary . main } ;
26
26
` ;
27
+ export const DEFAULT_ITEMS_PER_PAGE = 50 ;
28
+ export type Pagination = { itemsPerPage ?: number } & (
29
+ | { serverSide ?: undefined }
30
+ | {
31
+ serverSide : true ;
32
+ currentPage : number ;
33
+ totalItems : number ;
34
+ }
35
+ ) ;
27
36
28
37
interface InternalTableBaseProps {
29
38
hasCheckbox : boolean ;
@@ -170,7 +179,8 @@ export class TableBase<T extends {}> extends React.Component<
170
179
}
171
180
172
181
public componentDidUpdate ( prevProps : TableBaseProps < T > ) {
173
- const { sort, checkedItems, data, itemsPerPage, rowKey } = this . props ;
182
+ const { sort, checkedItems, data, pagination, rowKey } = this . props ;
183
+ const serverSide = pagination ?. serverSide ;
174
184
if ( sort && ! isEqual ( prevProps . sort , sort ) ) {
175
185
this . setState ( {
176
186
sort,
@@ -186,10 +196,12 @@ export class TableBase<T extends {}> extends React.Component<
186
196
this . setRowSelection ( checkedItems ) ;
187
197
}
188
198
189
- const totalItems = data ?. length ?? 0 ;
199
+ const totalItems = serverSide ? pagination ?. totalItems : data ?. length ?? 0 ;
200
+ const currentPage = serverSide ? pagination ?. currentPage : this . state . page ;
201
+ const itemsPerPage = this . props . itemsPerPage ?? pagination ?. itemsPerPage ;
190
202
if (
191
- this . state . page !== 0 &&
192
- totalItems <= this . state . page * ( itemsPerPage ?? 50 )
203
+ currentPage !== 0 &&
204
+ totalItems <= currentPage * ( itemsPerPage ?? DEFAULT_ITEMS_PER_PAGE )
193
205
) {
194
206
this . resetPager ( ) ;
195
207
}
@@ -462,19 +474,20 @@ export class TableBase<T extends {}> extends React.Component<
462
474
} ;
463
475
464
476
public toggleSort = ( e : React . MouseEvent < HTMLButtonElement > ) => {
465
- const { field } = e . currentTarget . dataset ;
477
+ const { field, refScheme } = e . currentTarget . dataset ;
466
478
const { sort } = this . state ;
467
479
if ( ! field ) {
468
480
return ;
469
481
}
470
482
471
483
let nextSort = {
472
484
field : field as keyof T ,
485
+ refScheme,
473
486
reverse : false ,
474
487
} ;
475
488
476
489
if ( sort . field === field ) {
477
- nextSort = { field : sort . field , reverse : ! sort . reverse } ;
490
+ nextSort = { field : sort . field , refScheme , reverse : ! sort . reverse } ;
478
491
}
479
492
480
493
this . setState ( { sort : nextSort } ) ;
@@ -522,11 +535,10 @@ export class TableBase<T extends {}> extends React.Component<
522
535
}
523
536
} ;
524
537
525
- public setPage = ( change : any ) => {
526
- if ( this . props . onPageChange ) {
527
- this . props . onPageChange ( change ) ;
528
- }
529
-
538
+ public setPage = ( change : number ) => {
539
+ const { pagination } = this . props ;
540
+ const itemsPerPage = this . props . itemsPerPage ?? pagination ?. itemsPerPage ;
541
+ this . props . onPageChange ?.( change , itemsPerPage ?? DEFAULT_ITEMS_PER_PAGE ) ;
530
542
this . setState ( { page : change } ) ;
531
543
} ;
532
544
@@ -547,7 +559,6 @@ export class TableBase<T extends {}> extends React.Component<
547
559
columns,
548
560
data,
549
561
usePager,
550
- itemsPerPage,
551
562
pagerPosition,
552
563
rowAnchorAttributes,
553
564
rowKey,
@@ -557,36 +568,40 @@ export class TableBase<T extends {}> extends React.Component<
557
568
getRowClass,
558
569
className,
559
570
fuzzyPager,
571
+ pagination,
560
572
} = this . props ;
561
573
562
574
const { page, sort } = this . state ;
575
+ const serverSide = pagination ?. serverSide ;
563
576
const items = data || [ ] ;
564
- const totalItems = items . length ;
565
- const _itemsPerPage = itemsPerPage || 50 ;
577
+ const totalItems = serverSide ? pagination ?. totalItems : items . length ;
578
+ const itemsPerPage =
579
+ this . props . itemsPerPage ??
580
+ pagination ?. itemsPerPage ??
581
+ DEFAULT_ITEMS_PER_PAGE ;
566
582
const _pagerPosition = pagerPosition || 'top' ;
583
+ let sortedData = items ;
567
584
568
- const lowerBound = usePager ? page * _itemsPerPage : 0 ;
569
- const upperBound = usePager
570
- ? Math . min ( ( page + 1 ) * _itemsPerPage , totalItems )
571
- : totalItems ;
572
-
573
- const sortedData = this . sortData ( items ) . slice ( lowerBound , upperBound ) ;
574
-
575
- const shouldShowPaper = ! ! usePager && totalItems > 0 ;
585
+ if ( ! serverSide ) {
586
+ const lowerBound = page * itemsPerPage ;
587
+ const upperBound = Math . min ( ( page + 1 ) * itemsPerPage , items . length ) ;
588
+ sortedData = this . sortData ( items ) . slice ( lowerBound , upperBound ) ;
589
+ }
576
590
591
+ const shouldShowPager = ! ! usePager && totalItems > 0 ;
577
592
const checkedRowIdentifiers = this . getCheckedRowIdentifiers ( ) ;
578
593
const highlightedRowIdentifiers = this . getHighlightedRowIdentifiers ( ) ;
579
594
const disabledRowIdentifiers = this . getDisabledRowIdentifiers ( ) ;
580
595
581
596
return (
582
597
< >
583
- { shouldShowPaper &&
598
+ { shouldShowPager &&
584
599
( _pagerPosition === 'top' || _pagerPosition === 'both' ) && (
585
600
< Pager
586
601
fuzzy = { fuzzyPager }
587
602
totalItems = { totalItems }
588
- itemsPerPage = { _itemsPerPage }
589
- page = { page }
603
+ itemsPerPage = { itemsPerPage }
604
+ page = { serverSide ? pagination ?. currentPage : page }
590
605
nextPage = { this . incrementPage }
591
606
prevPage = { this . decrementPage }
592
607
mb = { 2 }
@@ -620,6 +635,7 @@ export class TableBase<T extends {}> extends React.Component<
620
635
>
621
636
< Button
622
637
data-field = { item . field }
638
+ data-ref-scheme = { item . refScheme }
623
639
plain
624
640
primary = { sort . field === item . field }
625
641
onClick = { this . toggleSort }
@@ -692,12 +708,12 @@ export class TableBase<T extends {}> extends React.Component<
692
708
</ Base >
693
709
</ BaseTableWrapper >
694
710
695
- { shouldShowPaper &&
711
+ { shouldShowPager &&
696
712
( _pagerPosition === 'bottom' || _pagerPosition === 'both' ) && (
697
713
< Pager
698
714
fuzzy = { fuzzyPager }
699
715
totalItems = { totalItems }
700
- itemsPerPage = { _itemsPerPage }
716
+ itemsPerPage = { itemsPerPage }
701
717
page = { page }
702
718
nextPage = { this . incrementPage }
703
719
prevPage = { this . decrementPage }
@@ -712,6 +728,7 @@ export class TableBase<T extends {}> extends React.Component<
712
728
export interface TableSortOptions < T > {
713
729
reverse : boolean ;
714
730
field : keyof T | null ;
731
+ refScheme ?: string ;
715
732
}
716
733
717
734
export interface TableBaseProps < T > {
@@ -730,7 +747,7 @@ export interface TableBaseProps<T> {
730
747
/** A function that is called when a column is sorted */
731
748
onSort ?: ( sort : TableSortOptions < T > ) => void ;
732
749
/** A function that is called when the page is incremented, decremented and reset */
733
- onPageChange ?: ( page : number ) => void ;
750
+ onPageChange ?: ( page : number , itemsPerPage : number ) => void ;
734
751
/** sort options to be used both as a default sort, and on subsequent renders if the passed sort changes */
735
752
sort ?: TableSortOptions < T > ;
736
753
/** Attributes to pass to the anchor element used in a row */
@@ -752,7 +769,10 @@ export interface TableBaseProps<T> {
752
769
/** If true, the total number of items shown on the page will be indicated as being approximate. Useful for when you don't now the full size of your dataset. Only used if `usePager` is true. */
753
770
fuzzyPager ?: boolean ;
754
771
/** The number of items to be shown per page. Only used if `usePager` is true. Defaults to 50. */
772
+ /** @deprecated use pagination.itemsPerPage */
755
773
itemsPerPage ?: number ;
774
+ /** Information from a server side pagination */
775
+ pagination ?: Pagination ;
756
776
/** Sets whether the pager is displayed at the top of the table, the bottom of the table or in both positions. Only used if `usePager` is true. Defaults to `top`. */
757
777
pagerPosition ?: 'top' | 'bottom' | 'both' ;
758
778
className ?: string ;
0 commit comments