Skip to content

Commit bc83767

Browse files
committed
Add jump-first and jump-last renders
1 parent e958ba1 commit bc83767

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

examples/pagerCount.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
placeholder

examples/pagerCount.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'rc-pagination/assets/index.less';
2+
import Pagination from 'rc-pagination';
3+
import React from 'react';
4+
import ReactDOM from 'react-dom';
5+
6+
const total = 500;
7+
8+
const itemRender = (current, type, element) => {
9+
const hideItems = ['jump-last', 'jump-first'];
10+
11+
if (hideItems.includes(type)) {
12+
return null;
13+
}
14+
15+
return element;
16+
};
17+
18+
ReactDOM.render(
19+
<div>
20+
<Pagination total={total} itemRender={itemRender} pagerCount={10} showPrevNextJumpers={false} />
21+
</div>
22+
, document.getElementById('__react-content'));

src/Pager.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ 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+
2830
return (
31+
itemRender === null ? null :
2932
<li
3033
title={props.showTitle ? props.page : null}
3134
className={cls}
3235
onClick={handleClick}
3336
onKeyPress={handleKeyPress}
3437
tabIndex="0"
3538
>
36-
{props.itemRender(props.page, 'page', <a>{props.page}</a>)}
39+
{itemRender}
3740
</li>
3841
);
3942
};

src/Pagination.jsx

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class Pagination extends React.Component {
5353
nextIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
5454
jumpPrevIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
5555
jumpNextIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
56-
hideBoundary: PropTypes.bool,
5756
pagerCount: PropTypes.number,
5857
};
5958

@@ -76,7 +75,6 @@ class Pagination extends React.Component {
7675
locale: LOCALE,
7776
style: {},
7877
itemRender: defaultItemRender,
79-
hideBoundary: false,
8078
pagerCount: 5,
8179
};
8280

@@ -338,8 +336,8 @@ class Pagination extends React.Component {
338336
let lastPager = null;
339337
let gotoButton = null;
340338

341-
const { pagerCount, hideBoundary } = props;
342-
const pageBoundaryCount = hideBoundary ? 0 : 1;
339+
const { pagerCount } = props;
340+
const boundaryRemainder = pagerCount % 2 === 0 ? 1 : 0;
343341
const goButton = (props.showQuickJumper && props.showQuickJumper.goButton);
344342
const halfPagerCount = Math.max(1, Math.floor(pagerCount / 2));
345343
const pageBufferSize = props.showLessItems ? 1 : halfPagerCount;
@@ -507,7 +505,19 @@ class Pagination extends React.Component {
507505
</li>
508506
);
509507
}
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+
)
518+
510519
lastPager = (
520+
firstPagerRender === null ? null :
511521
<Pager
512522
locale={props.locale}
513523
last
@@ -522,6 +532,7 @@ class Pagination extends React.Component {
522532
/>
523533
);
524534
firstPager = (
535+
lastPagerRender === null ? null :
525536
<Pager
526537
locale={props.locale}
527538
rootPrefixCls={prefixCls}
@@ -536,14 +547,14 @@ class Pagination extends React.Component {
536547
);
537548

538549
let left = Math.max(1, current - pageBufferSize);
539-
let right = Math.min(current + pageBufferSize, allPages);
550+
let right = Math.min(current + pageBufferSize - boundaryRemainder, allPages);
540551

541552
if (current - 1 <= pageBufferSize) {
542-
right = 1 + pageBufferSize * 2 + pageBoundaryCount;
553+
right = 1 + pageBufferSize * 2 - boundaryRemainder;
543554
}
544555

545556
if (allPages - current <= pageBufferSize) {
546-
left = allPages - pageBufferSize * 2 - pageBoundaryCount;
557+
left = allPages - pageBufferSize * 2;
547558
}
548559

549560
for (let i = left; i <= right; i++) {
@@ -564,8 +575,8 @@ class Pagination extends React.Component {
564575
}
565576

566577
if (
567-
current - pageBoundaryCount > pageBufferSize &&
568-
current !== pageBoundaryCount + pageBufferSize + 1
578+
current - boundaryRemainder > pageBufferSize &&
579+
current !== boundaryRemainder + pageBufferSize + 1
569580
) {
570581
pagerList[0] = React.cloneElement(pagerList[0], {
571582
className: `${prefixCls}-item-after-jump-prev`,
@@ -579,10 +590,10 @@ class Pagination extends React.Component {
579590
pagerList.push(jumpNext);
580591
}
581592

582-
if (left !== 1 && !hideBoundary) {
593+
if (left !== 1) {
583594
pagerList.unshift(firstPager);
584595
}
585-
if (right !== allPages && !hideBoundary) {
596+
if (right !== allPages) {
586597
pagerList.push(lastPager);
587598
}
588599
}

0 commit comments

Comments
 (0)