File tree 5 files changed +68
-66
lines changed
5 files changed +68
-66
lines changed Original file line number Diff line number Diff line change 1
- import React from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import Pagination from 'rc-pagination' ;
3
3
import Select from 'rc-select' ;
4
4
import '../../assets/index.less' ;
5
5
6
- export default ( ) => (
7
- < >
8
- < Pagination simple defaultCurrent = { 1 } total = { 50 } />
9
- < br />
10
- < Pagination
11
- simple
12
- defaultCurrent = { 1 }
13
- total = { 50 }
14
- showTotal = { ( total ) => `Total ${ total } items` }
15
- />
16
- < br />
17
- < Pagination
18
- simple
19
- defaultCurrent = { 1 }
20
- total = { 50 }
21
- showSizeChanger
22
- selectComponentClass = { Select }
23
- />
24
- </ >
25
- ) ;
6
+ export default ( ) => {
7
+ const [ pageIndex , setPageIndex ] = useState ( 1 ) ;
8
+
9
+ return (
10
+ < >
11
+ < button onClick = { ( ) => setPageIndex ( ( i ) => ++ i ) } > increase</ button >
12
+ < Pagination
13
+ simple
14
+ current = { pageIndex }
15
+ total = { 50 }
16
+ onChange = { setPageIndex }
17
+ />
18
+ < br />
19
+ < Pagination simple defaultCurrent = { 1 } total = { 50 } />
20
+ < br />
21
+ < Pagination
22
+ simple
23
+ defaultCurrent = { 1 }
24
+ total = { 50 }
25
+ showTotal = { ( total ) => `Total ${ total } items` }
26
+ />
27
+ < br />
28
+ < Pagination
29
+ simple
30
+ defaultCurrent = { 1 }
31
+ total = { 50 }
32
+ showSizeChanger
33
+ selectComponentClass = { Select }
34
+ />
35
+ </ >
36
+ ) ;
37
+ } ;
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import useMergedState from 'rc-util/lib/hooks/useMergedState';
3
3
import KeyCode from 'rc-util/lib/KeyCode' ;
4
4
import pickAttrs from 'rc-util/lib/pickAttrs' ;
5
5
import warning from 'rc-util/lib/warning' ;
6
- import React from 'react' ;
6
+ import React , { useEffect } from 'react' ;
7
7
import type { PaginationProps } from './interface' ;
8
8
import zhCN from './locale/zh_CN' ;
9
9
import Options from './Options' ;
@@ -89,6 +89,10 @@ const Pagination: React.FC<PaginationProps> = (props) => {
89
89
90
90
const [ internalInputVal , setInternalInputVal ] = React . useState ( current ) ;
91
91
92
+ useEffect ( ( ) => {
93
+ setInternalInputVal ( current ) ;
94
+ } , [ current ] ) ;
95
+
92
96
const hasOnChange = onChange !== noop ;
93
97
const hasCurrent = 'current' in props ;
94
98
Original file line number Diff line number Diff line change @@ -1105,49 +1105,9 @@ exports[`Example showTotal 1`] = `
1105
1105
` ;
1106
1106
1107
1107
exports [` Example simple 1` ] = `
1108
- <ul
1109
- class = " rc-pagination rc-pagination-simple"
1110
- >
1111
- <li
1112
- aria-disabled = " true"
1113
- class = " rc-pagination-prev rc-pagination-disabled"
1114
- title = " 上一页"
1115
- >
1116
- <button
1117
- aria-label = " prev page"
1118
- class = " rc-pagination-item-link"
1119
- disabled = " "
1120
- type = " button"
1121
- />
1122
- </li >
1123
- <li
1124
- class = " rc-pagination-simple-pager"
1125
- title = " 1/5"
1126
- >
1127
- <input
1128
- size = " 3"
1129
- type = " text"
1130
- value = " 1"
1131
- />
1132
- <span
1133
- class = " rc-pagination-slash"
1134
- >
1135
- /
1136
- </span >
1137
- 5
1138
- </li >
1139
- <li
1140
- aria-disabled = " false"
1141
- class = " rc-pagination-next"
1142
- title = " 下一页"
1143
- >
1144
- <button
1145
- aria-label = " next page"
1146
- class = " rc-pagination-item-link"
1147
- type = " button"
1148
- />
1149
- </li >
1150
- </ul >
1108
+ <button >
1109
+ increase
1110
+ </button >
1151
1111
` ;
1152
1112
1153
1113
exports [` Example sizer 1` ] = `
Original file line number Diff line number Diff line change @@ -223,4 +223,30 @@ describe('simple Pagination', () => {
223
223
container . querySelector ( '.rc-pagination-simple-pager' ) ,
224
224
) . toHaveAttribute ( 'title' , '2/3' ) ;
225
225
} ) ;
226
+
227
+ it ( 'page should displayed correctly when controlled' , ( ) => {
228
+ const Demo = ( ) => {
229
+ const [ current , setCurrent ] = React . useState ( 1 ) ;
230
+
231
+ return (
232
+ < div >
233
+ < button onClick = { ( ) => setCurrent ( ( i ) => ++ i ) } > increase</ button >
234
+ < Pagination
235
+ simple
236
+ current = { current }
237
+ total = { 25 }
238
+ onChange = { setCurrent }
239
+ />
240
+ </ div >
241
+ ) ;
242
+ } ;
243
+
244
+ const { container } = render ( < Demo /> ) ;
245
+ const input = container . querySelector ( '.rc-pagination-simple input' ) ;
246
+ const button = container . querySelector ( 'button' ) ;
247
+
248
+ expect ( input ) . toHaveProperty ( 'value' , '1' ) ;
249
+ fireEvent . click ( button ) ;
250
+ expect ( input ) . toHaveProperty ( 'value' , '2' ) ;
251
+ } ) ;
226
252
} ) ;
Original file line number Diff line number Diff line change 13
13
"rc-pagination" : [" src/index.ts" ]
14
14
}
15
15
},
16
- "include" : [" .dumi/**/* " , " . dumirc.ts" , " **/*.ts" , " **/*.tsx" ]
16
+ "include" : [" .dumirc.ts" , " **/*.ts" , " **/*.tsx" ]
17
17
}
You can’t perform that action at this time.
0 commit comments