Skip to content

Improve pagination #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2.2.0

- Add prop `totalBoundaryShowSizeChanger`.
- Fix items count not being consistent. [#18201](https://github.com/ant-design/ant-design/issues/18201)
- Update default page size options from `10,25,30,40` to `10,20,50,100`.

# 2.1.0

- When `total` is greater then 100, will show size changer defaultly.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ ReactDOM.render(<Pagination />, container);
| defaultPageSize | default items per page | Number | 10 |
| pageSize | items per page | Number | 10 |
| onChange | page change callback | Function(current, pageSize) | - |
| showSizeChanger | show pageSize changer | Bool | `false` when total less then 100, `true` when otherwise |
| showSizeChanger | show pageSize changer | Bool | `false` when total less then `totalBoundaryShowSizeChanger`, `true` when otherwise |
| totalBoundaryShowSizeChanger | when total larger than it, `showSizeChanger` will be true | number | 50 |
| pageSizeOptions | specify the sizeChanger selections | Array<String> | ['10', '20', '30', '40'] |
| onShowSizeChange | pageSize change callback | Function(current, size) | - |
| hideOnSinglePage | hide on single page | Bool | false |
Expand Down
14 changes: 13 additions & 1 deletion examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import '../assets/index.less';
import React from 'react';
import Pagination from '..';

const App = () => <Pagination defaultCurrent={2} total={25} />;
const App = () => (
<>
<Pagination total={25} />
<Pagination total={50} />
<Pagination total={60} />
<Pagination total={70} />
<Pagination total={80} />
<Pagination total={90} />
<Pagination total={100} />
<Pagination total={120} />
<Pagination total={500} />
</>
);

export default App;
15 changes: 11 additions & 4 deletions examples/sizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'rc-select/assets/index.less';

class App extends React.Component {
state = {
pageSize: 20,
pageSize: 10,
};

onShowSizeChange = (current, pageSize) => {
Expand All @@ -24,22 +24,29 @@ class App extends React.Component {
pageSize={this.state.pageSize}
onShowSizeChange={this.onShowSizeChange}
defaultCurrent={3}
total={500}
total={40}
/>
<Pagination
selectComponentClass={Select}
pageSize={this.state.pageSize}
onShowSizeChange={this.onShowSizeChange}
defaultCurrent={3}
total={500}
total={50}
/>
<Pagination
selectComponentClass={Select}
pageSize={this.state.pageSize}
onShowSizeChange={this.onShowSizeChange}
defaultCurrent={3}
total={60}
/>
<Pagination
selectComponentClass={Select}
showSizeChanger={false}
pageSize={this.state.pageSize}
onShowSizeChange={this.onShowSizeChange}
defaultCurrent={3}
total={500}
total={60}
/>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/Options.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import KEYCODE from './KeyCode';

class Options extends React.Component {
static defaultProps = {
pageSizeOptions: ['10', '25', '50', '100'],
pageSizeOptions: ['10', '20', '50', '100'],
};

state = {
Expand Down
7 changes: 4 additions & 3 deletions src/Pagination.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Pagination extends React.Component {
locale: LOCALE,
style: {},
itemRender: defaultItemRender,
totalBoundaryShowSizeChanger: 50,
};

constructor(props) {
Expand Down Expand Up @@ -278,11 +279,11 @@ class Pagination extends React.Component {
this.state.current < calculatePage(undefined, this.state, this.props);

getShowSizeChanger() {
const { showSizeChanger, total } = this.props;
const { showSizeChanger, total, totalBoundaryShowSizeChanger } = this.props;
if (typeof showSizeChanger !== 'undefined') {
return showSizeChanger;
}
return total >= 100;
return total > totalBoundaryShowSizeChanger;
}

runIfEnter = (event, callback, ...restParams) => {
Expand Down Expand Up @@ -470,7 +471,7 @@ class Pagination extends React.Component {
);
}

if (allPages <= 5 + pageBufferSize * 2) {
if (allPages <= 3 + pageBufferSize * 2) {
const pagerProps = {
locale,
rootPrefixCls: prefixCls,
Expand Down
45 changes: 41 additions & 4 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ describe('current value on onShowSizeChange when total is 0', () => {
input.simulate('keyDown', { key: 'Enter', keyCode: 13, which: 13 });
expect(onShowSizeChange).toHaveBeenLastCalledWith(
wrapper.state().current,
25,
20,
);
});

Expand All @@ -341,30 +341,67 @@ describe('current value on onShowSizeChange when total is 0', () => {
const wrapper1 = mount(
<Pagination
selectComponentClass={Select}
total={99}
total={50}
/>,
);
expect(wrapper1.exists('.rc-pagination-options-size-changer')).toBe(false);
const wrapper2 = mount(
<Pagination
selectComponentClass={Select}
total={100}
total={51}
/>,
);
expect(wrapper2.exists('.rc-pagination-options-size-changer')).toBe(true);
const wrapper3 = mount(
<Pagination
selectComponentClass={Select}
showSizeChanger={false}
total={51}
/>,
);
expect(wrapper3.exists('.rc-pagination-options-size-changer')).toBe(false);
const wrapper4 = mount(
<Pagination
selectComponentClass={Select}
showSizeChanger
total={50}
/>,
);
expect(wrapper4.exists('.rc-pagination-options-size-changer')).toBe(true);
});

it('totalBoundaryShowSizeChanger works', () => {
const wrapper1 = mount(
<Pagination
selectComponentClass={Select}
total={100}
totalBoundaryShowSizeChanger={100}
/>,
);
expect(wrapper1.exists('.rc-pagination-options-size-changer')).toBe(false);
const wrapper2 = mount(
<Pagination
selectComponentClass={Select}
total={101}
totalBoundaryShowSizeChanger={100}
/>,
);
expect(wrapper2.exists('.rc-pagination-options-size-changer')).toBe(true);
const wrapper3 = mount(
<Pagination
selectComponentClass={Select}
showSizeChanger={false}
total={101}
totalBoundaryShowSizeChanger={100}
/>,
);
expect(wrapper3.exists('.rc-pagination-options-size-changer')).toBe(false);
const wrapper4 = mount(
<Pagination
selectComponentClass={Select}
showSizeChanger
total={99}
total={100}
totalBoundaryShowSizeChanger={100}
/>,
);
expect(wrapper4.exists('.rc-pagination-options-size-changer')).toBe(true);
Expand Down