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

Add an onPageActive callback #349

Merged
merged 5 commits into from Mar 22, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## >= 7.1.1

- Add an onPageActive callback (see: https://github.com/AdeleD/react-paginate/pull/349).

## >= 7.1.0

- Compatibility with React v17.0
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -74,6 +74,7 @@ Open your browser and go to [http://localhost:3000/](http://localhost:3000/)
| `breakClassName` | `String` | The classname on tag `li` of the ellipsis element. |
| `breakLinkClassName` | `String` | The classname on tag `a` of the ellipsis element. |
| `onPageChange` | `Function` | The method to call when a page is clicked. Exposes the current page object as an argument. |
| `onPageActive` | `Function` | The method to call when an active page is clicked. Exposes the active page object as an argument. |
| `initialPage` | `Number` | The initial page selected. |
| `forcePage` | `Number` | To override selected page with parent prop. |
| `disableInitialCallback` | `boolean` | Disable `onPageChange` callback with initial page. Default: `false` |
Expand Down
19 changes: 19 additions & 0 deletions __tests__/PaginationBoxView-test.js
Expand Up @@ -836,6 +836,25 @@ describe('Test custom props', () => {
});
});

describe('onPageActive', () => {
it('should use the onPageActive prop when defined', () => {
const myOnPageActiveMethod = jest.fn();
const myOnPageChangeMethod = jest.fn();
const pagination = ReactTestUtils.renderIntoDocument(
<PaginationBoxView
onPageActive={myOnPageActiveMethod}
onPageChange={myOnPageChangeMethod} />
);
const activeItem = ReactDOM.findDOMNode(pagination).querySelector(
'.selected a'
);
ReactTestUtils.Simulate.click(activeItem);

expect(myOnPageActiveMethod).toHaveBeenCalledWith({ selected: 0 });
expect(myOnPageChangeMethod).not.toHaveBeenCalled();
});
});

describe('initialPage', () => {
it('should use the initialPage prop when defined', () => {
const pagination = ReactTestUtils.renderIntoDocument(
Expand Down
2 changes: 1 addition & 1 deletion dist/react-paginate.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/react-paginate.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "react-paginate",
"version": "7.1.0",
"version": "7.1.1",
"description": "A ReactJS component that creates a pagination.",
"main": "./dist/react-paginate.js",
"repository": {
Expand Down
15 changes: 14 additions & 1 deletion react_components/PaginationBoxView.js
Expand Up @@ -19,6 +19,7 @@ export default class PaginationBoxView extends Component {
breakLabel: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
hrefBuilder: PropTypes.func,
onPageChange: PropTypes.func,
onPageActive: PropTypes.func,
initialPage: PropTypes.number,
forcePage: PropTypes.number,
disableInitialCallback: PropTypes.bool,
Expand Down Expand Up @@ -123,7 +124,10 @@ export default class PaginationBoxView extends Component {
handlePageSelected = (selected, evt) => {
evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);

if (this.state.selected === selected) return;
if (this.state.selected === selected) {
this.callActiveCallback(selected);
return;
}

this.setState({ selected: selected });

Expand Down Expand Up @@ -203,6 +207,15 @@ export default class PaginationBoxView extends Component {
}
};

callActiveCallback = (selectedItem) => {
if (
typeof this.props.onPageActive !== 'undefined' &&
typeof this.props.onPageActive === 'function'
) {
this.props.onPageActive({ selected: selectedItem });
}
};

getPageElement(index) {
const { selected } = this.state;
const {
Expand Down