1- import { last } from 'lodash'
1+ import { capitalize , last } from 'lodash'
22import { useCallback , useId } from 'react'
33import { range } from '@curvefi/prices-api/objects.util'
44import IconButton from '@mui/material/IconButton'
@@ -50,6 +50,20 @@ const getPageOptions = (pageIndex: number, pageCount: number): [number[], number
5050 range ( pageCount - 2 , 2 ) . filter ( ( p ) => p > pageIndex + 1 ) ,
5151]
5252
53+ /**
54+ * A button component for navigating to the previous or next page in pagination.
55+ */
56+ const NeighborButton = < T extends TableItem > ( { table, type } : { table : Table < T > ; type : 'previous' | 'next' } ) => (
57+ < IconButton
58+ size = "extraSmall"
59+ { ...( table [ `getCan${ capitalize ( type ) } Page` ] ( )
60+ ? { 'data-testid' : `btn-page-${ type . substring ( 0 , 4 ) } ` , onClick : table [ `${ type } Page` ] }
61+ : { disabled : true } ) }
62+ >
63+ < ChevronDownIcon sx = { { transform : `rotate(${ { previous : '-90' , next : '90' } [ type ] } deg)` } } />
64+ </ IconButton >
65+ )
66+
5367/**
5468 * Table pagination component for navigating through pages of a data table.
5569 * Renders previous/next buttons and page number buttons with ellipses for skipped pages.
@@ -59,34 +73,20 @@ export const TablePagination = <T extends TableItem>({ table }: { table: Table<T
5973 const [ firstPages , aroundPages , lastPages ] = getPageOptions ( pageIndex , table . getPageCount ( ) )
6074 return (
6175 < Stack justifyContent = "center" direction = "row" data-testid = "table-pagination" >
62- < IconButton
63- size = "extraSmall"
64- { ...( table . getCanPreviousPage ( )
65- ? { 'data-testid' : 'btn-page-prev' , onClick : table . previousPage }
66- : { disabled : true } ) }
67- >
68- < ChevronDownIcon sx = { { transform : `rotate(90deg)` } } />
69- </ IconButton >
76+ < NeighborButton table = { table } type = "previous" />
7077 < ToggleButtonGroup value = { pageIndex } size = "extraSmall" exclusive data-testid = "page-buttons" >
7178 { firstPages . map ( ( o ) => (
7279 < PageButton key = { o } page = { o } table = { table } />
7380 ) ) }
74- { firstPages . length > 0 && last ( firstPages ) != aroundPages [ 0 ] - 1 && < Spacer /> }
81+ { firstPages . length > 0 && last ( firstPages ) !== aroundPages [ 0 ] - 1 && < Spacer /> }
7582 { aroundPages . map ( ( o ) => (
7683 < PageButton key = { o } page = { o } table = { table } />
7784 ) ) }
78- { lastPages . length > 0 && lastPages [ 0 ] - 1 != last ( aroundPages ) && < Spacer /> }
85+ { lastPages . length > 0 && lastPages [ 0 ] - 1 !== last ( aroundPages ) && < Spacer /> }
7986 { lastPages . map ( ( o ) => (
8087 < PageButton key = { o } page = { o } table = { table } />
8188 ) ) }
82- < IconButton
83- size = "extraSmall"
84- { ...( table . getCanNextPage ( )
85- ? { 'data-testid' : 'btn-page-next' , onClick : table . nextPage }
86- : { disabled : true } ) }
87- >
88- < ChevronDownIcon sx = { { transform : `rotate(-90deg)` } } />
89- </ IconButton >
89+ < NeighborButton table = { table } type = "next" />
9090 </ ToggleButtonGroup >
9191 </ Stack >
9292 )
0 commit comments