File tree 2 files changed +27
-9
lines changed
2 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -116,19 +116,20 @@ class Pagination extends React.Component {
116
116
}
117
117
}
118
118
119
- static getDerivedStateFromProps ( props , state ) {
120
- let newState = { } ;
119
+ static getDerivedStateFromProps ( props , prevState ) {
120
+ const newState = { } ;
121
121
122
122
if ( 'current' in props ) {
123
- newState = {
124
- current : props . current ,
125
- currentInputValue : props . current ,
126
- } ;
123
+ newState . current = props . current ;
124
+
125
+ if ( props . current !== prevState . current ) {
126
+ newState . currentInputValue = newState . current ;
127
+ }
127
128
}
128
129
129
- if ( 'pageSize' in props ) {
130
- let current = state . current ;
131
- const newCurrent = calculatePage ( props . pageSize , state , props ) ;
130
+ if ( 'pageSize' in props && props . pageSize !== prevState . pageSize ) {
131
+ let current = prevState . current ;
132
+ const newCurrent = calculatePage ( props . pageSize , prevState , props ) ;
132
133
current = current > newCurrent ? newCurrent : current ;
133
134
134
135
if ( ! ( 'current' in props ) ) {
Original file line number Diff line number Diff line change @@ -197,6 +197,23 @@ describe('Controlled Pagination', () => {
197
197
ReactDOM . unmountComponentAtNode ( container ) ;
198
198
} ) ;
199
199
200
+ it ( 'not replace currentInputValue if current not change' , ( done ) => {
201
+ pagination . state . current = 1 ;
202
+ pagination . state . currentInputValue = 1 ;
203
+ pagination . forceUpdate ( ) ;
204
+ expect ( pagination . state . current ) . to . be ( 1 ) ;
205
+ expect ( pagination . state . currentInputValue ) . to . be ( 1 ) ;
206
+
207
+ setTimeout ( ( ) => {
208
+ pagination . state . currentInputValue = 1 ;
209
+ pagination . forceUpdate ( ) ;
210
+ expect ( pagination . state . current ) . to . be ( 2 ) ;
211
+ expect ( pagination . state . currentInputValue ) . to . be ( 1 ) ;
212
+
213
+ done ( ) ;
214
+ } , 10 ) ;
215
+ } ) ;
216
+
200
217
it ( 'current should equal defaultCurrent' , ( ) => {
201
218
expect ( pagination . state . current ) . to . be ( 2 ) ;
202
219
} ) ;
You can’t perform that action at this time.
0 commit comments