react-virtualized
Version:
React components for efficiently rendering large, scrollable lists and tabular data
81 lines (70 loc) • 2.48 kB
JavaScript
import { Component, PropTypes } from 'react';
import shallowCompare from 'react-addons-shallow-compare';
/**
* HOC that simplifies the process of synchronizing scrolling between two or more virtualized components.
*/
var ScrollSync = function (_Component) {
babelHelpers.inherits(ScrollSync, _Component);
function ScrollSync(props, context) {
babelHelpers.classCallCheck(this, ScrollSync);
var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(ScrollSync).call(this, props, context));
_this.state = {
clientHeight: 0,
clientWidth: 0,
scrollHeight: 0,
scrollLeft: 0,
scrollTop: 0,
scrollWidth: 0
};
_this._onScroll = _this._onScroll.bind(_this);
return _this;
}
babelHelpers.createClass(ScrollSync, [{
key: 'render',
value: function render() {
var children = this.props.children;
var _state = this.state;
var clientHeight = _state.clientHeight;
var clientWidth = _state.clientWidth;
var scrollHeight = _state.scrollHeight;
var scrollLeft = _state.scrollLeft;
var scrollTop = _state.scrollTop;
var scrollWidth = _state.scrollWidth;
return children({
clientHeight: clientHeight,
clientWidth: clientWidth,
onScroll: this._onScroll,
scrollHeight: scrollHeight,
scrollLeft: scrollLeft,
scrollTop: scrollTop,
scrollWidth: scrollWidth
});
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
}, {
key: '_onScroll',
value: function _onScroll(_ref) {
var clientHeight = _ref.clientHeight;
var clientWidth = _ref.clientWidth;
var scrollHeight = _ref.scrollHeight;
var scrollLeft = _ref.scrollLeft;
var scrollTop = _ref.scrollTop;
var scrollWidth = _ref.scrollWidth;
this.setState({ clientHeight: clientHeight, clientWidth: clientWidth, scrollHeight: scrollHeight, scrollLeft: scrollLeft, scrollTop: scrollTop, scrollWidth: scrollWidth });
}
}]);
return ScrollSync;
}(Component);
ScrollSync.propTypes = {
/**
* Function respondible for rendering 2 or more virtualized components.
* This function should implement the following signature:
* ({ onScroll, scrollLeft, scrollTop }) => PropTypes.element
*/
children: PropTypes.func.isRequired
};
export default ScrollSync;