File tree Expand file tree Collapse file tree 5 files changed +68
-66
lines changed Expand file tree Collapse file tree 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' ;
22import Pagination from 'rc-pagination' ;
33import Select from 'rc-select' ;
44import '../../assets/index.less' ;
55
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';
33import KeyCode from 'rc-util/lib/KeyCode' ;
44import pickAttrs from 'rc-util/lib/pickAttrs' ;
55import warning from 'rc-util/lib/warning' ;
6- import React from 'react' ;
6+ import React , { useEffect } from 'react' ;
77import type { PaginationProps } from './interface' ;
88import zhCN from './locale/zh_CN' ;
99import Options from './Options' ;
@@ -89,6 +89,10 @@ const Pagination: React.FC<PaginationProps> = (props) => {
8989
9090 const [ internalInputVal , setInternalInputVal ] = React . useState ( current ) ;
9191
92+ useEffect ( ( ) => {
93+ setInternalInputVal ( current ) ;
94+ } , [ current ] ) ;
95+
9296 const hasOnChange = onChange !== noop ;
9397 const hasCurrent = 'current' in props ;
9498
Original file line number Diff line number Diff line change @@ -1105,49 +1105,9 @@ exports[`Example showTotal 1`] = `
11051105` ;
11061106
11071107exports [` 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 >
11511111` ;
11521112
11531113exports [` Example sizer 1` ] = `
Original file line number Diff line number Diff line change @@ -223,4 +223,30 @@ describe('simple Pagination', () => {
223223 container . querySelector ( '.rc-pagination-simple-pager' ) ,
224224 ) . toHaveAttribute ( 'title' , '2/3' ) ;
225225 } ) ;
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+ } ) ;
226252} ) ;
Original file line number Diff line number Diff line change 1313 "rc-pagination" : [" src/index.ts" ]
1414 }
1515 },
16- "include" : [" .dumi/**/* " , " . dumirc.ts" , " **/*.ts" , " **/*.tsx" ]
16+ "include" : [" .dumirc.ts" , " **/*.ts" , " **/*.tsx" ]
1717}
You can’t perform that action at this time.
0 commit comments