Skip to content

Commit

Permalink
Merge pull request #349 from AdeleD/add-an-on-page-active-callback
Browse files Browse the repository at this point in the history
Add an onPageActive callback
  • Loading branch information
AdeleD committed Mar 22, 2021
2 parents e5aa093 + f93b89c commit 721e643
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 4 deletions.
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

0 comments on commit 721e643

Please sign in to comment.