@@ -18,6 +18,14 @@ function defaultItemRender(page, type, element) {
1818 return element ;
1919}
2020
21+ function calculatePage ( p , state , props ) {
22+ let pageSize = p ;
23+ if ( typeof pageSize === 'undefined' ) {
24+ pageSize = state . pageSize ;
25+ }
26+ return Math . floor ( ( props . total - 1 ) / pageSize ) + 1 ;
27+ }
28+
2129export default class Pagination extends React . Component {
2230 static propTypes = {
2331 prefixCls : PropTypes . string ,
@@ -93,28 +101,6 @@ export default class Pagination extends React.Component {
93101 } ;
94102 }
95103
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-
118104 componentDidUpdate ( prevProps , prevState ) {
119105 // When current page change, fix focused style of prev item
120106 // A hacky solution of https://github.com/ant-design/ant-design/issues/8948
@@ -129,12 +115,40 @@ export default class Pagination extends React.Component {
129115 }
130116 }
131117
118+ static getDerivedStateFromProps ( props , state ) {
119+ let newState = { } ;
120+
121+ if ( 'current' in props ) {
122+ newState = {
123+ current : props . current ,
124+ currentInputValue : props . current ,
125+ } ;
126+ }
127+
128+ if ( 'pageSize' in props ) {
129+ let current = state . current ;
130+ const newCurrent = calculatePage ( props . pageSize , state , props ) ;
131+ current = current > newCurrent ? newCurrent : current ;
132+
133+ if ( ! ( 'current' in props ) ) {
134+ newState . current = current ;
135+ newState . currentInputValue = current ;
136+ }
137+ newState . pageSize = props . pageSize ;
138+ }
139+
140+ return newState ;
141+ }
142+
132143 getJumpPrevPage = ( ) => {
133144 return Math . max ( 1 , this . state . current - ( this . props . showLessItems ? 3 : 5 ) ) ;
134145 }
135146
136147 getJumpNextPage = ( ) => {
137- return Math . min ( this . calculatePage ( ) , this . state . current + ( this . props . showLessItems ? 3 : 5 ) ) ;
148+ return Math . min (
149+ calculatePage ( undefined , this . state , this . props ) ,
150+ this . state . current + ( this . props . showLessItems ? 3 : 5 )
151+ ) ;
138152 }
139153
140154 /**
@@ -156,14 +170,6 @@ export default class Pagination extends React.Component {
156170 this . paginationNode = node ;
157171 }
158172
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-
167173 isValid = ( page ) => {
168174 return isInteger ( page ) && page >= 1 && page !== this . state . current ;
169175 }
@@ -204,7 +210,7 @@ export default class Pagination extends React.Component {
204210
205211 changePageSize = ( size ) => {
206212 let current = this . state . current ;
207- const newCurrent = this . calculatePage ( size ) ;
213+ const newCurrent = calculatePage ( size , this . state , this . props ) ;
208214 current = current > newCurrent ? newCurrent : current ;
209215 // fix the issue:
210216 // Once 'total' is 0, 'current' in 'onShowSizeChange' is 0, which is not correct.
@@ -231,8 +237,9 @@ export default class Pagination extends React.Component {
231237 handleChange = ( p ) => {
232238 let page = p ;
233239 if ( this . isValid ( page ) ) {
234- if ( page > this . calculatePage ( ) ) {
235- page = this . calculatePage ( ) ;
240+ const currentPage = calculatePage ( undefined , this . state , this . props ) ;
241+ if ( page > currentPage ) {
242+ page = currentPage ;
236243 }
237244
238245 if ( ! ( 'current' in this . props ) ) {
@@ -276,7 +283,7 @@ export default class Pagination extends React.Component {
276283 }
277284
278285 hasNext = ( ) => {
279- return this . state . current < this . calculatePage ( ) ;
286+ return this . state . current < calculatePage ( undefined , this . state , this . props ) ;
280287 }
281288
282289 runIfEnter = ( event , callback , ...restParams ) => {
@@ -317,7 +324,7 @@ export default class Pagination extends React.Component {
317324 const locale = props . locale ;
318325
319326 const prefixCls = props . prefixCls ;
320- const allPages = this . calculatePage ( ) ;
327+ const allPages = calculatePage ( undefined , this . state , this . props ) ;
321328 const pagerList = [ ] ;
322329 let jumpPrev = null ;
323330 let jumpNext = null ;
0 commit comments