Skip to content
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

Fix forcePage to control page properly #496

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 3 additions & 3 deletions __tests__/PaginationBoxView-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ describe('Test custom props', () => {
expect(
ReactDOM.findDOMNode(pagination).querySelector('.selected a')
.textContent
).toBe('4');
).toBe('3');
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenLastCalledWith(
'(react-paginate): Both initialPage (3) and forcePage (2) props are provided, which is discouraged.' +
Expand Down Expand Up @@ -1355,7 +1355,7 @@ describe('Test custom props', () => {
expect(
ReactDOM.findDOMNode(pagination).querySelector('.selected a')
.textContent
).toBe('2');
).toBe('3');
});

it('(observation) is not totally controlled when forcePage is provided, even when it is 0', async () => {
Expand All @@ -1378,7 +1378,7 @@ describe('Test custom props', () => {
expect(
ReactDOM.findDOMNode(pagination).querySelector('.selected a')
.textContent
).toBe('2');
).toBe('1');
});
});

Expand Down
56 changes: 23 additions & 33 deletions react_components/PaginationBoxView.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ export default class PaginationBoxView extends Component {
let initialSelected;
if (props.initialPage) {
initialSelected = props.initialPage;
} else if (props.forcePage) {
initialSelected = props.forcePage;
} else {
initialSelected = 0;
}
Expand All @@ -103,6 +101,12 @@ export default class PaginationBoxView extends Component {
};
}

selectedPage() {
return typeof this.props.forcePage === 'number'
? this.props.forcePage
: this.state.selected;
}

componentDidMount() {
const {
initialPage,
Expand Down Expand Up @@ -146,21 +150,6 @@ export default class PaginationBoxView extends Component {
}

componentDidUpdate(prevProps) {
if (
this.props.forcePage !== undefined &&
this.props.forcePage !== prevProps.forcePage
) {
if (this.props.forcePage > this.props.pageCount - 1) {
console.warn(
`(react-paginate): The forcePage prop provided is greater than the maximum page index from pageCount prop (${
this.props.forcePage
} > ${this.props.pageCount - 1}).`
);
}

this.setState({ selected: this.props.forcePage });
}

if (
Number.isInteger(prevProps.pageCount) &&
!Number.isInteger(this.props.pageCount)
Expand All @@ -172,15 +161,15 @@ export default class PaginationBoxView extends Component {
}

handlePreviousPage = (event) => {
const { selected } = this.state;
const selected = this.selectedPage();

this.handleClick(event, null, selected > 0 ? selected - 1 : undefined, {
isPrevious: true,
});
};

handleNextPage = (event) => {
const { selected } = this.state;
const selected = this.selectedPage();
const { pageCount } = this.props;

this.handleClick(
Expand All @@ -192,7 +181,7 @@ export default class PaginationBoxView extends Component {
};

handlePageSelected = (selected, event) => {
if (this.state.selected === selected) {
if (this.selectedPage() === selected) {
this.callActiveCallback(selected);
this.handleClick(event, null, undefined, { isActive: true });
return;
Expand All @@ -202,12 +191,13 @@ export default class PaginationBoxView extends Component {
};

handlePageChange = (selected) => {
if (this.state.selected === selected) {
if (this.selectedPage() === selected) {
return;
}
this.setState({ selected });

// Call the callback with the new selected item:
if (this.props.forcePage === undefined) {
this.setState({ selected });
}
this.callCallback(selected);
};

Expand All @@ -219,15 +209,15 @@ export default class PaginationBoxView extends Component {
};

getForwardJump() {
const { selected } = this.state;
const selected = this.selectedPage();
const { pageCount, pageRangeDisplayed } = this.props;

const forwardJump = selected + pageRangeDisplayed;
return forwardJump >= pageCount ? pageCount - 1 : forwardJump;
}

getBackwardJump() {
const { selected } = this.state;
const selected = this.selectedPage();
const { pageRangeDisplayed } = this.props;

const backwardJump = selected - pageRangeDisplayed;
Expand All @@ -246,7 +236,7 @@ export default class PaginationBoxView extends Component {
} = {}
) => {
event.preventDefault ? event.preventDefault() : (event.returnValue = false);
const { selected } = this.state;
const selected = this.selectedPage();
const { onClick } = this.props;

let newPage = nextSelectedPage;
Expand Down Expand Up @@ -279,7 +269,7 @@ export default class PaginationBoxView extends Component {
};

handleBreakClick = (index, event) => {
const { selected } = this.state;
const selected = this.selectedPage();

this.handleClick(
event,
Expand All @@ -293,12 +283,12 @@ export default class PaginationBoxView extends Component {
const { hrefBuilder, pageCount, hrefAllControls } = this.props;
if (!hrefBuilder) return;
if (hrefAllControls || (pageIndex >= 0 && pageIndex < pageCount)) {
return hrefBuilder(pageIndex + 1, pageCount, this.state.selected);
return hrefBuilder(pageIndex + 1, pageCount, this.selectedPage());
}
}

ariaLabelBuilder(pageIndex) {
const selected = pageIndex === this.state.selected;
const selected = pageIndex === this.selectedPage();
if (
this.props.ariaLabelBuilder &&
pageIndex >= 0 &&
Expand Down Expand Up @@ -333,7 +323,7 @@ export default class PaginationBoxView extends Component {
};

getElementPageRel = (index) => {
const { selected } = this.state;
const selected = this.selectedPage();
const { nextPageRel, prevPageRel, selectedPageRel } = this.props;

if (selected - 1 === index) {
Expand All @@ -347,7 +337,7 @@ export default class PaginationBoxView extends Component {
};

getPageElement(index) {
const { selected } = this.state;
const selected = this.selectedPage();
const {
pageClassName,
pageLinkClassName,
Expand Down Expand Up @@ -389,7 +379,7 @@ export default class PaginationBoxView extends Component {
breakAriaLabels,
} = this.props;

const { selected } = this.state;
const selected = this.selectedPage();

if (pageCount <= pageRangeDisplayed) {
for (let index = 0; index < pageCount; index++) {
Expand Down Expand Up @@ -549,7 +539,7 @@ export default class PaginationBoxView extends Component {
nextRel,
} = this.props;

const { selected } = this.state;
const selected = this.selectedPage();

const isPreviousDisabled = selected === 0;
const isNextDisabled = selected === pageCount - 1;
Expand Down