Skip to content

feat: 🆕 size changer will be shown when total >= 100 #258

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 1 commit 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
7 changes: 6 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# 2.1.0

- When `total` is greater then 100, will show size changer defaultly.
- Update default page size options from `10,20,30,40` to `10,25,50,100`.

# 2.0.0

- Remove prop-types and react-lifecycles-compat
- Remove `prop-types` and `react-lifecycles-compat`

# 1.20.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ React.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 |
| showSizeChanger | show pageSize changer | Bool | `false` when total less then 100, `true` when otherwise |
| 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
9 changes: 8 additions & 1 deletion examples/sizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ class App extends React.Component {
/>
<Pagination
selectComponentClass={Select}
showSizeChanger
pageSize={this.state.pageSize}
onShowSizeChange={this.onShowSizeChange}
defaultCurrent={3}
total={500}
/>
<Pagination
selectComponentClass={Select}
showSizeChanger={false}
pageSize={this.state.pageSize}
onShowSizeChange={this.onShowSizeChange}
defaultCurrent={3}
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', '20', '30', '40'],
pageSizeOptions: ['10', '25', '50', '100'],
};

state = {
Expand Down
12 changes: 9 additions & 3 deletions src/Pagination.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class Pagination extends React.Component {
hideOnSinglePage: false,
showPrevNextJumpers: true,
showQuickJumper: false,
showSizeChanger: false,
showLessItems: false,
showTitle: true,
onShowSizeChange: noop,
Expand Down Expand Up @@ -278,6 +277,14 @@ class Pagination extends React.Component {
hasNext = () =>
this.state.current < calculatePage(undefined, this.state, this.props);

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

runIfEnter = (event, callback, ...restParams) => {
if (event.key === 'Enter' || event.charCode === 13) {
callback(...restParams);
Expand Down Expand Up @@ -337,7 +344,6 @@ class Pagination extends React.Component {
showLessItems,
showTitle,
showTotal,
showSizeChanger,
simple,
itemRender,
showPrevNextJumpers,
Expand Down Expand Up @@ -669,7 +675,7 @@ class Pagination extends React.Component {
rootPrefixCls={prefixCls}
selectComponentClass={selectComponentClass}
selectPrefixCls={selectPrefixCls}
changeSize={showSizeChanger ? this.changePageSize : null}
changeSize={this.getShowSizeChanger() ? this.changePageSize : null}
current={current}
pageSize={pageSize}
pageSizeOptions={pageSizeOptions}
Expand Down
35 changes: 34 additions & 1 deletion 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,
20,
25,
);
});

Expand All @@ -336,4 +336,37 @@ describe('current value on onShowSizeChange when total is 0', () => {
const totalText = wrapper.find('.rc-pagination-total-text');
expect(totalText.text()).toBe('0 - 0 of 0 items');
});

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