Skip to content

Commit b708827

Browse files
committed
Updates
1 parent ede47ce commit b708827

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed

examples/pagerCount.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,30 @@ function onShowSizeChange(current, pageSize) {
2121

2222
ReactDOM.render(
2323
<div>
24+
<p> pageCount = 10, hide prev and next jumpers </p>
2425
<Pagination total={500} itemRender={itemRender} pagerCount={10} showPrevNextJumpers={false} />
2526

27+
<p> Has `showSizeChanger` and `showQuickJumper` </p>
2628
<Pagination
2729
selectComponentClass={Select}
2830
showSizeChanger
2931
onShowSizeChange={onShowSizeChange}
3032
defaultCurrent={3}
3133
total={500}
3234
pagerCount={7}
35+
showQuickJumper
3336
/>
3437

35-
<Pagination total={500} pagerCount={8} />
38+
<p> pagerCount less than 3 </p>
39+
<Pagination total={100} pagerCount={0} />
40+
41+
<p> Has `showLessItems` and `pagerCount` </p>
42+
<Pagination total={500} pagerCount={8} showLessItems />
43+
44+
<p> The pagerCount is odd </p>
45+
<Pagination total={200} pagerCount={7} />
46+
47+
<p> The pagerCount is even </p>
48+
<Pagination total={200} pagerCount={8} />
3649
</div>
3750
, document.getElementById('__react-content'));

src/Pager.jsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,26 @@ const Pager = (props) => {
2525
props.onKeyPress(e, props.onClick, props.page);
2626
};
2727

28-
const itemRender = props.itemRender(props.page, 'page', <a>{props.page}</a>);
29-
28+
let pageType = 'page';
29+
if (props.last) {
30+
pageType = 'jump-last';
31+
}
32+
if (props.first) {
33+
pageType = 'jump-first';
34+
}
35+
36+
const itemNode = props.itemRender(props.page, pageType, <a>{props.page}</a>);
37+
3038
return (
31-
itemRender === null ? null :
39+
itemNode === null ? null :
3240
<li
3341
title={props.showTitle ? props.page : null}
3442
className={cls}
3543
onClick={handleClick}
3644
onKeyPress={handleKeyPress}
3745
tabIndex="0"
3846
>
39-
{itemRender}
47+
{itemNode}
4048
</li>
4149
);
4250
};

src/Pagination.jsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,15 @@ class Pagination extends React.Component {
8383

8484
const hasOnChange = props.onChange !== noop;
8585
const hasCurrent = ('current' in props);
86+
const hasShowLessItems = ('showLessItems' in props);
8687
if (hasCurrent && !hasOnChange) {
8788
console.warn('Warning: You provided a `current` prop to a Pagination component without an `onChange` handler. This will render a read-only component.'); // eslint-disable-line
8889
}
90+
if (hasShowLessItems && props.showLessItems) {
91+
console.warn(
92+
'Warning: `showLessItems` is deprecated since 1.18.0. Please use pagerCount instead.'
93+
) // eslint-disable-line
94+
}
8995

9096
let current = props.defaultCurrent;
9197
if ('current' in props) {
@@ -145,13 +151,17 @@ class Pagination extends React.Component {
145151
}
146152

147153
getJumpPrevPage = () => {
148-
return Math.max(1, this.state.current - (this.props.showLessItems ? 3 : 5));
154+
const { showLessItems } = this.props;
155+
const hasPagerCount = this.hasPagerCount()
156+
return Math.max(1, this.state.current - (showLessItems && !hasPagerCount ? 3 : 5));
149157
}
150158

151159
getJumpNextPage = () => {
160+
const { showLessItems } = this.props;
161+
const hasPagerCount = this.hasPagerCount()
152162
return Math.min(
153163
calculatePage(undefined, this.state, this.props),
154-
this.state.current + (this.props.showLessItems ? 3 : 5)
164+
this.state.current + (showLessItems && !hasPagerCount ? 3 : 5)
155165
);
156166
}
157167

@@ -318,6 +328,8 @@ class Pagination extends React.Component {
318328
}
319329
}
320330

331+
hasPagerCount = () => !(this.props.pagerCount === 5)
332+
321333
render() {
322334
// When hideOnSinglePage is true and there is only 1 page, hide the pager
323335
if (this.props.hideOnSinglePage === true && this.props.total <= this.state.pageSize) {
@@ -336,11 +348,15 @@ class Pagination extends React.Component {
336348
let lastPager = null;
337349
let gotoButton = null;
338350

339-
const { pagerCount } = props;
340-
const boundaryRemainder = pagerCount % 2 === 0 ? 1 : 0;
351+
const { pagerCount, showLessItems } = props;
352+
// `pagerCount` priority is greater than `showLessItems`.
353+
const hasPagerCount = this.hasPagerCount();
354+
const boundary = pagerCount === 0 ? 0 : 1;
355+
const boundaryRemainder = hasPagerCount ? (pagerCount % 2 !== 0 ? 0 : boundary) : 0;
356+
const halfPagerCount = Math.max(0, Math.floor((pagerCount - 1) / 2));
357+
const pageBufferSize = hasPagerCount ? halfPagerCount : (showLessItems ? 1 : halfPagerCount);
358+
341359
const goButton = (props.showQuickJumper && props.showQuickJumper.goButton);
342-
const halfPagerCount = Math.max(1, Math.floor((pagerCount - 1) / 2));
343-
const pageBufferSize = props.showLessItems ? 1 : halfPagerCount;
344360
const { current, pageSize } = this.state;
345361

346362
const prevPage = current - 1 > 0 ? current - 1 : 0;
@@ -461,8 +477,8 @@ class Pagination extends React.Component {
461477
);
462478
}
463479
} else {
464-
const prevItemTitle = props.showLessItems ? locale.prev_3 : locale.prev_5;
465-
const nextItemTitle = props.showLessItems ? locale.next_3 : locale.next_5;
480+
const prevItemTitle = showLessItems && !hasPagerCount ? locale.prev_3 : locale.prev_5;
481+
const nextItemTitle = showLessItems && !hasPagerCount ? locale.next_3 : locale.next_5;
466482
if (props.showPrevNextJumpers) {
467483
let jumpPrevClassString = `${prefixCls}-jump-prev`;
468484
if (props.jumpPrevIcon) {
@@ -505,19 +521,8 @@ class Pagination extends React.Component {
505521
</li>
506522
);
507523
}
508-
const firstPagerRender = props.itemRender(
509-
1,
510-
'jump-first',
511-
this.getItemIcon(props.jumpNextIcon)
512-
);
513-
const lastPagerRender = props.itemRender(
514-
allPages,
515-
'jump-last',
516-
this.getItemIcon(props.jumpNextIcon)
517-
);
518524

519525
lastPager = (
520-
firstPagerRender === null ? null :
521526
<Pager
522527
locale={props.locale}
523528
last
@@ -532,9 +537,9 @@ class Pagination extends React.Component {
532537
/>
533538
);
534539
firstPager = (
535-
lastPagerRender === null ? null :
536540
<Pager
537541
locale={props.locale}
542+
first
538543
rootPrefixCls={prefixCls}
539544
onClick={this.handleChange}
540545
onKeyPress={this.runIfEnter}

0 commit comments

Comments
 (0)