Skip to content

Commit e6cd621

Browse files
author
vvo
committed
feat(pagination): add scrollTo option
Allows to scroll to a particular element/CSSSelector after a click. On by default (scroll to body). Can be deactivated by setting it to false. fixes #73
1 parent 6b42e34 commit e6cd621

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ search.addWidget(
369369
* @param {String} [options.labels.first] Label for the First link
370370
* @param {String} [options.labels.last] Label for the Last link
371371
* @param {Number} [maxPages=20] The max number of pages to browse
372+
* @param {String|DOMElement|boolean} [scrollTo='body'] Where to scroll after a click, set to `false` to disable
372373
* @param {boolean} [showFirstLast=true] Define if the First and Last links should be displayed
373374
* @param {boolean} [hideWhenNoResults=true] Hide the container when no results match
374375
* @return {Object}

components/Pagination/PaginationLink.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
var React = require('react');
22

33
class PaginationLink extends React.Component {
4+
handleClick(page, e) {
5+
e.preventDefault();
6+
this.props.setCurrentPage(page);
7+
}
8+
49
render() {
510
var {className, label, ariaLabel, handleClick} = this.props;
611

widgets/pagination.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ var defaultLabels = {
2929
* @param {String} [options.labels.next] Label for the Next link
3030
* @param {String} [options.labels.first] Label for the First link
3131
* @param {String} [options.labels.last] Label for the Last link
32-
* @param {Number} [maxPages=20] The max number of pages to browse
33-
* @param {Number} [padding=3] The number of pages to display on each side of the current page
34-
* @param {boolean} [showFirstLast=true] Define if the First and Last links should be displayed
35-
* @param {boolean} [hideWhenNoResults=true] Hide the container when no results match
32+
* @param {Number} [options.maxPages=20] The max number of pages to browse
33+
* @param {Number} [options.padding=3] The number of pages to display on each side of the current page
34+
* @param {String|DOMElement|boolean} [options.scrollTo='body'] Where to scroll after a click, set to `false` to disable
35+
* @param {boolean} [options.showFirstLast=true] Define if the First and Last links should be displayed
36+
* @param {boolean} [options.hideWhenNoResults=true] Hide the container when no results match
3637
* @return {Object}
3738
*/
3839
function pagination({
@@ -42,9 +43,15 @@ function pagination({
4243
maxPages = 20,
4344
padding = 3,
4445
showFirstLast = true,
45-
hideWhenNoResults = true
46+
hideWhenNoResults = true,
47+
scrollTo = 'body'
4648
}) {
49+
if (scrollTo === true) {
50+
scrollTo = 'body';
51+
}
52+
4753
var containerNode = utils.getContainerNode(container);
54+
var scrollToNode = scrollTo !== false ? utils.getContainerNode(scrollTo) : false;
4855

4956
if (!container) {
5057
throw new Error('Usage: pagination({container[, cssClasses.{root,item,page,previous,next,first,last,active,disabled}, labels.{previous,next,first,last}, maxPages, showFirstLast, hideWhenNoResults]})');
@@ -62,8 +69,6 @@ function pagination({
6269
var nbPages = results.nbPages;
6370
var nbHits = results.nbHits;
6471
var hasResults = nbHits > 0;
65-
var setCurrentPage = this.setCurrentPage.bind(this, helper);
66-
6772

6873
if (maxPages !== undefined) {
6974
nbPages = Math.min(maxPages, results.nbPages);
@@ -79,7 +84,7 @@ function pagination({
7984
nbHits={nbHits}
8085
nbPages={nbPages}
8186
padding={padding}
82-
setCurrentPage={setCurrentPage}
87+
setCurrentPage={setCurrentPage(helper, scrollToNode)}
8388
showFirstLast={showFirstLast}
8489
/>,
8590
containerNode
@@ -88,4 +93,13 @@ function pagination({
8893
};
8994
}
9095

96+
function setCurrentPage(helper, scrollToNode) {
97+
return askedPage => {
98+
helper.setCurrentPage(askedPage).search();
99+
if (scrollToNode !== false) {
100+
scrollToNode.scrollIntoView();
101+
}
102+
};
103+
}
104+
91105
module.exports = pagination;

0 commit comments

Comments
 (0)