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

Moved custom scroll parent to a prop called getScrollParent #179

Merged
merged 6 commits into from
Oct 10, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,20 @@ import InfiniteScroll from 'react-infinite-scroller';
You can define a custom `parentNode` element to base the scroll calulations on.

```
class InfiniteScrollOverride extends InfiniteScroll {

/**
* We are overriding the getParentElement function to use a custom element as the scrollable element
*
* @param {any} el the scroller domNode
* @returns {any} the parentNode to base the scroll calulations on
*
* @memberOf InfiniteScrollOverride
*/
getParentElement(el) {
if (this.props.scrollParent) {
return this.props.scrollParent;
}
return super.getParentElement(el);
}

render() {
return super.render();
}
}
<div style="height:700px;overflow:auto;" ref={(ref) => this.scrollParentRef = ref}>
danbovey marked this conversation as resolved.
Show resolved Hide resolved
<div>
<InfiniteScroll
pageStart={0}
loadMore={loadFunc}
hasMore={true || false}
loader={<div className="loader" key={0}>Loading ...</div>}
useWindow={false}
getScrollParent={() => this.scrollParentRef}
>
{items}
</InfiniteScroll>
</div>
</div>
```

## Props
Expand All @@ -95,6 +88,7 @@ class InfiniteScrollOverride extends InfiniteScroll {
| `loadMore`       | `Function`   |           | A callback when more items are requested by the user. Receives a single parameter specifying the page to load e.g. `function handleLoadMore(page) { /* load more items here */ }` }|
| `loader` | `Component` | | A React component to render while more items are loading. The parent component must have a unique key prop. |
| `pageStart` | `Number` | `0` | The number of the first page to load, With the default of `0`, the first page is `1`.|
| `getScrollParent` | `Function`| | Override method to return a different scroll listener if not the immediate parent. |
| `threshold` | `Number` | `250` | The distance in pixels before the end of the items that will trigger a call to `loadMore`.|
| `useCapture` | `Boolean` | `false` | Proxy to the `useCapture` option of the added event listeners.|
| `useWindow` | `Boolean` | `true` | Add scroll listeners to the window, or else, the component's `parentNode`.|
18 changes: 13 additions & 5 deletions dist/InfiniteScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ var InfiniteScroll = (function(_Component) {
{
key: 'getParentElement',
value: function getParentElement(el) {
var scrollParent =
this.props.getScrollParent && this.props.getScrollParent();
if (scrollParent != null) {
return scrollParent;
}
return el && el.parentNode;
},
},
Expand All @@ -178,16 +183,15 @@ var InfiniteScroll = (function(_Component) {
{
key: 'attachScrollListener',
value: function attachScrollListener() {
if (
!this.props.hasMore ||
!this.getParentElement(this.scrollComponent)
) {
var parentElement = this.getParentElement(this.scrollComponent);

if (!this.props.hasMore || !parentElement) {
return;
}

var scrollEl = window;
if (this.props.useWindow === false) {
scrollEl = this.getParentElement(this.scrollComponent);
scrollEl = parentElement;
}

scrollEl.addEventListener(
Expand Down Expand Up @@ -305,6 +309,7 @@ var InfiniteScroll = (function(_Component) {
threshold = renderProps.threshold,
useCapture = renderProps.useCapture,
useWindow = renderProps.useWindow,
getScrollParent = renderProps.getScrollParent,
props = _objectWithoutProperties(renderProps, [
'children',
'element',
Expand All @@ -318,6 +323,7 @@ var InfiniteScroll = (function(_Component) {
'threshold',
'useCapture',
'useWindow',
'getScrollParent',
]);

props.ref = function(node) {
Expand Down Expand Up @@ -357,6 +363,7 @@ InfiniteScroll.propTypes = {
loadMore: _propTypes2.default.func.isRequired,
pageStart: _propTypes2.default.number,
ref: _propTypes2.default.func,
getScrollParent: _propTypes2.default.func,
threshold: _propTypes2.default.number,
useCapture: _propTypes2.default.bool,
useWindow: _propTypes2.default.bool,
Expand All @@ -372,6 +379,7 @@ InfiniteScroll.defaultProps = {
isReverse: false,
useCapture: false,
loader: null,
getScrollParent: null,
};
exports.default = InfiniteScroll;
module.exports = exports['default'];
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-infinite-scroller",
"version": "1.2.1",
"version": "1.2.2",
"description": "Infinite scroll component for React in ES6",
"main": "index.js",
"jsnext:main": "src/InfiniteScroll.js",
Expand Down Expand Up @@ -72,4 +72,4 @@
"sinon": "^2.1.0",
"webpack": "^2.5.1"
}
}
}
14 changes: 12 additions & 2 deletions src/InfiniteScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class InfiniteScroll extends Component {
loadMore: PropTypes.func.isRequired,
pageStart: PropTypes.number,
ref: PropTypes.func,
getScrollParent: PropTypes.func,
threshold: PropTypes.number,
useCapture: PropTypes.bool,
useWindow: PropTypes.bool,
Expand All @@ -28,6 +29,7 @@ export default class InfiniteScroll extends Component {
isReverse: false,
useCapture: false,
loader: null,
getScrollParent: null,
};

constructor(props) {
Expand Down Expand Up @@ -87,6 +89,11 @@ export default class InfiniteScroll extends Component {
}

getParentElement(el) {
const scrollParent =
this.props.getScrollParent && this.props.getScrollParent();
if (scrollParent != null) {
return scrollParent;
}
return el && el.parentNode;
}

Expand All @@ -95,13 +102,15 @@ export default class InfiniteScroll extends Component {
}

attachScrollListener() {
if (!this.props.hasMore || !this.getParentElement(this.scrollComponent)) {
const parentElement = this.getParentElement(this.scrollComponent);

if (!this.props.hasMore || !parentElement) {
return;
}

let scrollEl = window;
if (this.props.useWindow === false) {
scrollEl = this.getParentElement(this.scrollComponent);
scrollEl = parentElement;
}

scrollEl.addEventListener(
Expand Down Expand Up @@ -203,6 +212,7 @@ export default class InfiniteScroll extends Component {
threshold,
useCapture,
useWindow,
getScrollParent,
...props
} = renderProps;

Expand Down