UNPKG

react-virtualized

Version:

React components for efficiently rendering large, scrollable lists and tabular data

87 lines (75 loc) 2.82 kB
import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of'; import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _createClass from 'babel-runtime/helpers/createClass'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; 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) { _inherits(ScrollSync, _Component); function ScrollSync(props, context) { _classCallCheck(this, ScrollSync); var _this = _possibleConstructorReturn(this, (ScrollSync.__proto__ || _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; } _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;