1- /* eslint-disable react/jsx-no-undef */
2- import React , { useState } from "react" ;
31import { ArrowLeftIcon , ArrowRightIcon , ChevronLeftIcon , ChevronRightIcon } from "@chakra-ui/icons" ;
4- import {
5- Flex ,
6- IconButton ,
7- NumberDecrementStepper ,
8- NumberIncrementStepper ,
9- NumberInput ,
10- NumberInputField ,
11- NumberInputStepper ,
12- Text ,
13- Tooltip ,
14- } from "@chakra-ui/react" ;
2+ import { Flex , IconButton , Text , Tooltip } from "@chakra-ui/react" ;
153
16- export default function Pagination ( ) {
17- const [ pageCount ] = useState ( 0 ) ;
18- const [ canPreviousPage ] = useState ( false ) ;
19- const [ canNextPage ] = useState ( false ) ;
20- const [ pageIndex ] = useState ( 0 ) ;
21- const [ pageOptions ] = useState ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ) ;
4+ type PageValues = {
5+ page : number ;
6+ limit : number ;
7+ total ?: number ;
8+ } ;
229
23- const previousPage = ( ) => { } ;
24- const nextPage = ( ) => { } ;
10+ export default function Pagination ( props : {
11+ values : PageValues ;
12+ onChange : ( values : PageValues ) => void ;
13+ } ) {
14+ const { values, onChange } = props ;
15+ const { page = 1 , total, limit = 10 } = values ;
16+ const maxPage = total ? Math . ceil ( total / limit ) : undefined ;
2517
2618 return (
27- < Flex justifyContent = "space-between " m = { 4 } alignItems = "center" >
19+ < Flex justifyContent = "end " m = { 4 } alignItems = "center" >
2820 < Flex >
2921 < Tooltip label = "First Page" >
3022 < IconButton
31- onClick = { ( ) => gotoPage ( 0 ) }
32- isDisabled = { ! canPreviousPage }
23+ onClick = { ( ) => {
24+ props . onChange ( {
25+ ...values ,
26+ page : 1 ,
27+ } ) ;
28+ } }
29+ isDisabled = { page === 1 }
3330 icon = { < ArrowLeftIcon h = { 3 } w = { 3 } /> }
3431 mr = { 4 }
3532 aria-label = { "" }
3633 />
3734 </ Tooltip >
3835 < Tooltip label = "Previous Page" >
3936 < IconButton
40- onClick = { previousPage }
41- isDisabled = { ! canPreviousPage }
37+ onClick = { ( ) =>
38+ onChange ( {
39+ ...values ,
40+ page : page - 1 ,
41+ } )
42+ }
43+ isDisabled = { page === 1 }
4244 icon = { < ChevronLeftIcon h = { 6 } w = { 6 } /> }
4345 aria-label = { "" }
4446 />
4547 </ Tooltip >
4648 </ Flex >
4749
4850 < Flex alignItems = "center" >
49- < Text flexShrink = "0" mr = { 8 } >
50- < Text fontWeight = "bold" as = "span" >
51- { pageIndex + 1 }
52- </ Text >
53- /
54- < Text fontWeight = "bold" as = "span" >
55- { pageOptions . length }
56- </ Text >
57- </ Text >
58- < Text flexShrink = "0" > Go:</ Text > { " " }
59- < NumberInput
60- ml = { 2 }
61- mr = { 8 }
62- w = { 28 }
63- min = { 1 }
64- max = { pageOptions . length }
65- onChange = { ( value ) => {
66- const page = value ? parseInt ( value , 10 ) - 1 : 0 ;
67- gotoPage ( page ) ;
68- } }
69- defaultValue = { pageIndex + 1 }
70- >
71- < NumberInputField />
72- < NumberInputStepper >
73- < NumberIncrementStepper />
74- < NumberDecrementStepper />
75- </ NumberInputStepper >
76- </ NumberInput >
77- { /* <Select
78- w={32}
79- value={pageSize}
80- onChange={(e) => {
81- setPageSize(Number(e.target.value));
82- }}
51+ < Text
52+ fontWeight = "bold"
53+ as = "span"
54+ width = { "40px" }
55+ display = "inline-block"
56+ textAlign = { "center" }
8357 >
84- {[10, 20, 30, 40, 50].map((pageSize) => (
85- <option key={pageSize} value={pageSize} >
86- Show {pageSize}
87- </option >
88- )) }
89- </Select> */ }
58+ { page }
59+ </ Text >
60+ /
61+ < Text fontWeight = "bold" as = "p" width = { "40px" } display = "inline-block" textAlign = { "center" } >
62+ { isNaN ( maxPage ) ? "" : maxPage }
63+ </ Text >
9064 </ Flex >
9165
9266 < Flex >
9367 < Tooltip label = "Next Page" >
9468 < IconButton
95- onClick = { nextPage }
96- isDisabled = { ! canNextPage }
69+ isDisabled = { maxPage === page }
9770 icon = { < ChevronRightIcon h = { 6 } w = { 6 } /> }
9871 aria-label = { "" }
72+ onClick = { ( ) => {
73+ props . onChange ( {
74+ ...values ,
75+ page : page + 1 ,
76+ } ) ;
77+ } }
9978 />
10079 </ Tooltip >
10180 < Tooltip label = "Last Page" >
10281 < IconButton
103- onClick = { ( ) => gotoPage ( pageCount - 1 ) }
104- isDisabled = { ! canNextPage }
82+ onClick = { ( ) => {
83+ props . onChange ( {
84+ ...values ,
85+ page : maxPage ,
86+ } ) ;
87+ } }
88+ isDisabled = { maxPage === page }
10589 icon = { < ArrowRightIcon h = { 3 } w = { 3 } /> }
10690 ml = { 4 }
10791 aria-label = { "" }
@@ -111,6 +95,3 @@ export default function Pagination() {
11195 </ Flex >
11296 ) ;
11397}
114- function gotoPage ( page : number ) {
115- throw new Error ( "Function not implemented." ) ;
116- }
0 commit comments