11import React from 'react'
22import { shape , number , string , object , bool , element , oneOf , oneOfType } from 'prop-types'
33import classNames from 'classnames'
4- import { getPageInfo } from 'paging-algorithm'
54import Link from '../Link'
65
76export default class Pagination extends React . Component {
@@ -13,8 +12,11 @@ export default class Pagination extends React.Component {
1312 className : string ,
1413 pageInfo : shape ( {
1514 itemCount : number . isRequired ,
15+ perPage : number . isRequired ,
16+ pageCount : number . isRequired ,
1617 currentPage : number . isRequired ,
17- perPage : number . isRequired
18+ hasNextPage : bool . isRequired ,
19+ hasPreviousPage : bool . isRequired
1820 } ) . isRequired ,
1921 labels : shape ( {
2022 prev : oneOfType ( [ string , element ] ) ,
@@ -41,8 +43,8 @@ export default class Pagination extends React.Component {
4143 range : 6 ,
4244 labels : {
4345 first : '« First' ,
44- prev : '⟨ Previous' ,
45- next : 'Next ⟩ ' ,
46+ prev : '← Previous' ,
47+ next : 'Next → ' ,
4648 last : 'Last »'
4749 } ,
4850 theme : {
@@ -59,39 +61,19 @@ export default class Pagination extends React.Component {
5961 renderDisabled : true
6062 }
6163
62- constructor ( props ) {
63- super ( props )
64-
65- this . state = getPageInfo ( {
66- limit : props . pageInfo . perPage ,
67- pageCount : props . range ,
68- total : props . pageInfo . itemCount ,
69- page : props . pageInfo . currentPage
70- } )
71- }
72-
7364 render ( ) {
7465 const {
75- totalPages,
76- hasPreviousPage,
77- hasNextPage,
78- previousPage,
79- nextPage,
80- firstPage,
81- lastPage,
82- currentPage
83- } = this . state
84-
85- if ( ! totalPages || ! / ^ \d + $ / . test ( totalPages ) ) {
86- if ( process . env . NODE_ENV !== `production` ) {
87- console . error (
88- 'Warning: Invalid pageInfo prop supplied to `Pagination`' +
89- ' component: ' + JSON . stringify ( this . props . pageInfo ) )
66+ ui,
67+ pageInfo : {
68+ perPage,
69+ pageCount,
70+ itemCount,
71+ currentPage,
72+ hasNextPage,
73+ hasPreviousPage
9074 }
91- return < div className = { this . props . className } > Pagination not available</ div >
92- }
75+ } = this . props
9376
94- const { ui } = this . props
9577 const labels = Object . assign ( { } , Pagination . defaultProps . labels , this . props . labels )
9678 const theme = Object . assign ( { } , Pagination . defaultProps . theme , this . props . theme )
9779 const pages = [ ]
@@ -108,14 +90,15 @@ export default class Pagination extends React.Component {
10890
10991 pages . push ( {
11092 key : 'prev' ,
111- number : previousPage ,
93+ number : currentPage - 1 ,
11294 type : 'prev' ,
11395 label : labels . prev ,
11496 disabled : ! hasPreviousPage
11597 } )
11698
11799 if ( ui !== 'mini' ) {
118- for ( let i = firstPage ; i <= lastPage ; i ++ ) {
100+ const [ fp , lp ] = this . calcRange ( )
101+ for ( let i = fp ; i <= lp ; i ++ ) {
119102 pages . push ( {
120103 key : i ,
121104 number : i ,
@@ -128,7 +111,7 @@ export default class Pagination extends React.Component {
128111
129112 pages . push ( {
130113 key : 'next' ,
131- number : nextPage ,
114+ number : currentPage + 1 ,
132115 type : 'next' ,
133116 label : labels . next ,
134117 disabled : ! hasNextPage
@@ -137,7 +120,7 @@ export default class Pagination extends React.Component {
137120 if ( ui === 'full' ) {
138121 pages . push ( {
139122 key : 'last' ,
140- number : totalPages ,
123+ number : pageCount ,
141124 type : 'last' ,
142125 label : labels . last ,
143126 disabled : ! hasNextPage
@@ -192,4 +175,32 @@ export default class Pagination extends React.Component {
192175 </ nav >
193176 )
194177 }
178+
179+ calcRange ( ) {
180+ const { range, pageInfo : { currentPage, pageCount } } = this . props
181+
182+ let fp = Math . max ( 1 , currentPage - Math . floor ( range / 2 ) ) ;
183+ let lp = Math . min ( pageCount , currentPage + Math . floor ( range / 2 ) ) ;
184+
185+ if ( lp - fp + 1 < range ) {
186+ if ( currentPage < pageCount / 2 ) {
187+ lp = Math . min (
188+ pageCount ,
189+ lp + ( range - ( lp - fp ) )
190+ ) ;
191+ } else {
192+ fp = Math . max ( 1 , fp - ( range - ( lp - fp ) ) ) ;
193+ }
194+ }
195+
196+ if ( lp - fp + 1 > range ) {
197+ if ( currentPage > pageCount / 2 ) {
198+ fp = fp + 1 ;
199+ } else {
200+ lp = lp - 1 ;
201+ }
202+ }
203+
204+ return [ fp , lp ]
205+ }
195206}
0 commit comments