Skip to content

Commit b68fb12

Browse files
committed
Add pagerCount props
1 parent 9cfc49d commit b68fb12

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

examples/default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class App extends React.Component {
1515
});
1616
}
1717
render() {
18-
return <Pagination onChange={this.onChange} current={this.state.current} total={25} />;
18+
return <Pagination onChange={this.onChange} current={this.state.current} total={400} />;
1919
}
2020
}
2121

src/Pagination.jsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Pagination extends React.Component {
5454
jumpPrevIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
5555
jumpNextIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
5656
hideBoundary: PropTypes.bool,
57+
pagerCount: PropTypes.number,
5758
};
5859

5960
static defaultProps = {
@@ -76,6 +77,7 @@ class Pagination extends React.Component {
7677
style: {},
7778
itemRender: defaultItemRender,
7879
hideBoundary: false,
80+
pagerCount: 5,
7981
};
8082

8183
constructor(props) {
@@ -145,13 +147,13 @@ class Pagination extends React.Component {
145147
}
146148

147149
getJumpPrevPage = () => {
148-
return Math.max(1, this.state.current - (this.props.showLessItems ? 3 : 5));
150+
return Math.max(1, this.state.current - (this.props.showLessItems ? 3 : this.props.pagerCount));
149151
}
150152

151153
getJumpNextPage = () => {
152154
return Math.min(
153155
calculatePage(undefined, this.state, this.props),
154-
this.state.current + (this.props.showLessItems ? 3 : 5)
156+
this.state.current + (this.props.showLessItems ? 3 : this.props.pagerCount)
155157
);
156158
}
157159

@@ -336,8 +338,10 @@ class Pagination extends React.Component {
336338
let lastPager = null;
337339
let gotoButton = null;
338340

341+
const { pagerCount, hideBoundary } = props;
339342
const goButton = (props.showQuickJumper && props.showQuickJumper.goButton);
340-
const pageBufferSize = props.showLessItems ? 1 : 2;
343+
const halfPagerCount = Math.max(1, Math.floor(pagerCount / 2))
344+
const pageBufferSize = props.showLessItems ? 1 : halfPagerCount;
341345
const { current, pageSize } = this.state;
342346

343347
const prevPage = current - 1 > 0 ? current - 1 : 0;
@@ -427,7 +431,7 @@ class Pagination extends React.Component {
427431
);
428432
}
429433

430-
if (allPages <= 5 + pageBufferSize * 2) {
434+
if (allPages <= pagerCount + pageBufferSize * 2) {
431435
const pagerProps = {
432436
locale,
433437
rootPrefixCls: prefixCls,
@@ -532,13 +536,14 @@ class Pagination extends React.Component {
532536

533537
let left = Math.max(1, current - pageBufferSize);
534538
let right = Math.min(current + pageBufferSize, allPages);
535-
539+
const pageBoundaryCount = hideBoundary ? 0 : 1;
540+
536541
if (current - 1 <= pageBufferSize) {
537-
right = 1 + pageBufferSize * 2;
542+
right = 1 + pageBufferSize * 2 + pageBoundaryCount;
538543
}
539544

540545
if (allPages - current <= pageBufferSize) {
541-
left = allPages - pageBufferSize * 2;
546+
left = allPages - pageBufferSize * 2 - pageBoundaryCount;
542547
}
543548

544549
for (let i = left; i <= right; i++) {
@@ -558,23 +563,23 @@ class Pagination extends React.Component {
558563
);
559564
}
560565

561-
if (current - 1 >= pageBufferSize * 2 && current !== 1 + 2) {
566+
if (current - pageBoundaryCount > pageBufferSize && current !== pageBoundaryCount + pageBufferSize + 1) {
562567
pagerList[0] = React.cloneElement(pagerList[0], {
563568
className: `${prefixCls}-item-after-jump-prev`,
564569
});
565570
pagerList.unshift(jumpPrev);
566571
}
567-
if (allPages - current >= pageBufferSize * 2 && current !== allPages - 2) {
572+
if (allPages - current > pageBufferSize && current !== allPages - pageBufferSize - 1) {
568573
pagerList[pagerList.length - 1] = React.cloneElement(pagerList[pagerList.length - 1], {
569574
className: `${prefixCls}-item-before-jump-next`,
570575
});
571576
pagerList.push(jumpNext);
572577
}
573578

574-
if (left !== 1 && !props.hideBoundary) {
579+
if (left !== 1 && !hideBoundary) {
575580
pagerList.unshift(firstPager);
576581
}
577-
if (right !== allPages && !props.hideBoundary) {
582+
if (right !== allPages && !hideBoundary) {
578583
pagerList.push(lastPager);
579584
}
580585
}

0 commit comments

Comments
 (0)