Skip to content

Commit 98902d1

Browse files
authored
fix: input value should follow current (#546)
1 parent d37c0a0 commit 98902d1

File tree

5 files changed

+68
-66
lines changed

5 files changed

+68
-66
lines changed

docs/examples/simple.tsx

+33-21
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import Pagination from 'rc-pagination';
33
import Select from 'rc-select';
44
import '../../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+
};

src/Pagination.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import useMergedState from 'rc-util/lib/hooks/useMergedState';
33
import KeyCode from 'rc-util/lib/KeyCode';
44
import pickAttrs from 'rc-util/lib/pickAttrs';
55
import warning from 'rc-util/lib/warning';
6-
import React from 'react';
6+
import React, { useEffect } from 'react';
77
import type { PaginationProps } from './interface';
88
import zhCN from './locale/zh_CN';
99
import 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

tests/__snapshots__/demo.test.tsx.snap

+3-43
Original file line numberDiff line numberDiff line change
@@ -1105,49 +1105,9 @@ exports[`Example showTotal 1`] = `
11051105
`;
11061106

11071107
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>
11511111
`;
11521112

11531113
exports[`Example sizer 1`] = `

tests/simple.test.tsx

+26
Original file line numberDiff line numberDiff 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
});

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
"rc-pagination": ["src/index.ts"]
1414
}
1515
},
16-
"include": [".dumi/**/*", ".dumirc.ts", "**/*.ts", "**/*.tsx"]
16+
"include": [".dumirc.ts", "**/*.ts", "**/*.tsx"]
1717
}

0 commit comments

Comments
 (0)