1
1
import propTypes from "prop-types" ;
2
2
3
- const Pagination = ( { currentPage, numberOfPages, onClick } ) => {
3
+ const Pagination = ( { currentPage, numberOfPages, onClick, limit } ) => {
4
+ const currentSet = Math . ceil ( currentPage / limit ) ;
5
+ const lastSet = Math . ceil ( numberOfPages / limit ) ;
6
+ const startPage = limit * ( currentSet - 1 ) + 1 ;
7
+ const numberOfPageForSet = currentSet === lastSet ? numberOfPages % limit : limit
4
8
return (
5
9
< nav aria-label = "Page navigation example" >
6
10
< ul className = "pagination justify-content-center" >
7
- < li className = "page-item disabled" >
8
- < a className = "page-link" href = "#" >
11
+ { currentSet !== 1 && < li className = "page-item" >
12
+ < div
13
+ className = "page-link cursor-pointer" href = "#"
14
+ onClick = { ( ) => onClick ( startPage - limit ) }
15
+ >
9
16
Previous
10
- </ a >
11
- </ li >
12
- { Array ( numberOfPages ) . fill ( 1 ) . map ( ( value , index ) => value + index )
17
+ </ div >
18
+ </ li > }
19
+ { Array ( numberOfPageForSet ) . fill ( startPage )
20
+ . map ( ( value , index ) => value + index )
13
21
. map ( ( pageNumber ) => {
14
22
return < li
15
23
key = { pageNumber }
@@ -25,11 +33,14 @@ const Pagination = ({ currentPage, numberOfPages, onClick }) => {
25
33
</ div >
26
34
</ li >
27
35
} ) }
28
- < li className = "page-item" >
29
- < a className = "page-link" href = "#" >
36
+ { currentSet !== lastSet && < li className = "page-item" >
37
+ < div
38
+ className = "page-link cursor-pointer" href = "#"
39
+ onClick = { ( ) => onClick ( startPage + limit ) }
40
+ >
30
41
Next
31
- </ a >
32
- </ li >
42
+ </ div >
43
+ </ li > }
33
44
</ ul >
34
45
</ nav >
35
46
)
@@ -38,11 +49,13 @@ const Pagination = ({ currentPage, numberOfPages, onClick }) => {
38
49
Pagination . propTypes = {
39
50
currentPage : propTypes . number ,
40
51
numberOfPages : propTypes . number . isRequired ,
41
- onClick : propTypes . func . isRequired
52
+ onClick : propTypes . func . isRequired ,
53
+ limit : propTypes . number
42
54
}
43
55
44
56
Pagination . defaultProps = {
45
- currentPage : 1
57
+ currentPage : 1 ,
58
+ limit : 5
46
59
}
47
60
48
61
export default Pagination ;
0 commit comments