@@ -4,6 +4,7 @@ import Pager from './Pager';
44import Options from './Options' ;
55import KEYCODE from './KeyCode' ;
66import LOCALE from './locale/zh_CN' ;
7+ import { polyfill } from 'react-lifecycles-compat' ;
78
89function noop ( ) {
910}
@@ -18,7 +19,15 @@ function defaultItemRender(page, type, element) {
1819 return element ;
1920}
2021
21- export default class Pagination extends React . Component {
22+ function calculatePage ( p , state , props ) {
23+ let pageSize = p ;
24+ if ( typeof pageSize === 'undefined' ) {
25+ pageSize = state . pageSize ;
26+ }
27+ return Math . floor ( ( props . total - 1 ) / pageSize ) + 1 ;
28+ }
29+
30+ class Pagination extends React . Component {
2231 static propTypes = {
2332 prefixCls : PropTypes . string ,
2433 current : PropTypes . number ,
@@ -93,28 +102,6 @@ export default class Pagination extends React.Component {
93102 } ;
94103 }
95104
96- componentWillReceiveProps ( nextProps ) {
97- if ( 'current' in nextProps ) {
98- this . setState ( {
99- current : nextProps . current ,
100- currentInputValue : nextProps . current ,
101- } ) ;
102- }
103-
104- if ( 'pageSize' in nextProps ) {
105- const newState = { } ;
106- let current = this . state . current ;
107- const newCurrent = this . calculatePage ( nextProps . pageSize ) ;
108- current = current > newCurrent ? newCurrent : current ;
109- if ( ! ( 'current' in nextProps ) ) {
110- newState . current = current ;
111- newState . currentInputValue = current ;
112- }
113- newState . pageSize = nextProps . pageSize ;
114- this . setState ( newState ) ;
115- }
116- }
117-
118105 componentDidUpdate ( prevProps , prevState ) {
119106 // When current page change, fix focused style of prev item
120107 // A hacky solution of https://github.com/ant-design/ant-design/issues/8948
@@ -129,12 +116,40 @@ export default class Pagination extends React.Component {
129116 }
130117 }
131118
132- getJumpPrevPage ( ) {
119+ static getDerivedStateFromProps ( props , state ) {
120+ let newState = { } ;
121+
122+ if ( 'current' in props ) {
123+ newState = {
124+ current : props . current ,
125+ currentInputValue : props . current ,
126+ } ;
127+ }
128+
129+ if ( 'pageSize' in props ) {
130+ let current = state . current ;
131+ const newCurrent = calculatePage ( props . pageSize , state , props ) ;
132+ current = current > newCurrent ? newCurrent : current ;
133+
134+ if ( ! ( 'current' in props ) ) {
135+ newState . current = current ;
136+ newState . currentInputValue = current ;
137+ }
138+ newState . pageSize = props . pageSize ;
139+ }
140+
141+ return newState ;
142+ }
143+
144+ getJumpPrevPage = ( ) => {
133145 return Math . max ( 1 , this . state . current - ( this . props . showLessItems ? 3 : 5 ) ) ;
134146 }
135147
136- getJumpNextPage ( ) {
137- return Math . min ( this . calculatePage ( ) , this . state . current + ( this . props . showLessItems ? 3 : 5 ) ) ;
148+ getJumpNextPage = ( ) => {
149+ return Math . min (
150+ calculatePage ( undefined , this . state , this . props ) ,
151+ this . state . current + ( this . props . showLessItems ? 3 : 5 )
152+ ) ;
138153 }
139154
140155 /**
@@ -156,14 +171,6 @@ export default class Pagination extends React.Component {
156171 this . paginationNode = node ;
157172 }
158173
159- calculatePage = ( p ) => {
160- let pageSize = p ;
161- if ( typeof pageSize === 'undefined' ) {
162- pageSize = this . state . pageSize ;
163- }
164- return Math . floor ( ( this . props . total - 1 ) / pageSize ) + 1 ;
165- }
166-
167174 isValid = ( page ) => {
168175 return isInteger ( page ) && page >= 1 && page !== this . state . current ;
169176 }
@@ -204,7 +211,7 @@ export default class Pagination extends React.Component {
204211
205212 changePageSize = ( size ) => {
206213 let current = this . state . current ;
207- const newCurrent = this . calculatePage ( size ) ;
214+ const newCurrent = calculatePage ( size , this . state , this . props ) ;
208215 current = current > newCurrent ? newCurrent : current ;
209216 // fix the issue:
210217 // Once 'total' is 0, 'current' in 'onShowSizeChange' is 0, which is not correct.
@@ -231,8 +238,9 @@ export default class Pagination extends React.Component {
231238 handleChange = ( p ) => {
232239 let page = p ;
233240 if ( this . isValid ( page ) ) {
234- if ( page > this . calculatePage ( ) ) {
235- page = this . calculatePage ( ) ;
241+ const currentPage = calculatePage ( undefined , this . state , this . props ) ;
242+ if ( page > currentPage ) {
243+ page = currentPage ;
236244 }
237245
238246 if ( ! ( 'current' in this . props ) ) {
@@ -263,14 +271,6 @@ export default class Pagination extends React.Component {
263271 }
264272 }
265273
266- getJumpPrevPage ( ) {
267- return Math . max ( 1 , this . state . current - ( this . props . showLessItems ? 3 : 5 ) ) ;
268- }
269-
270- getJumpNextPage ( ) {
271- return Math . min ( this . calculatePage ( ) , this . state . current + ( this . props . showLessItems ? 3 : 5 ) ) ;
272- }
273-
274274 jumpPrev = ( ) => {
275275 this . handleChange ( this . getJumpPrevPage ( ) ) ;
276276 }
@@ -284,7 +284,7 @@ export default class Pagination extends React.Component {
284284 }
285285
286286 hasNext = ( ) => {
287- return this . state . current < this . calculatePage ( ) ;
287+ return this . state . current < calculatePage ( undefined , this . state , this . props ) ;
288288 }
289289
290290 runIfEnter = ( event , callback , ...restParams ) => {
@@ -325,7 +325,7 @@ export default class Pagination extends React.Component {
325325 const locale = props . locale ;
326326
327327 const prefixCls = props . prefixCls ;
328- const allPages = this . calculatePage ( ) ;
328+ const allPages = calculatePage ( undefined , this . state , this . props ) ;
329329 const pagerList = [ ] ;
330330 let jumpPrev = null ;
331331 let jumpNext = null ;
@@ -647,3 +647,7 @@ export default class Pagination extends React.Component {
647647 ) ;
648648 }
649649}
650+
651+ polyfill ( Pagination ) ;
652+
653+ export default Pagination ;
0 commit comments