react-virtualized
Version:
React components for efficiently rendering large, scrollable lists and tabular data
166 lines (132 loc) • 6.17 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactAddonsShallowCompare = require('react-addons-shallow-compare');
var _reactAddonsShallowCompare2 = _interopRequireDefault(_reactAddonsShallowCompare);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* Decorator component that automatically adjusts the width and height of a single child.
* Child component should not be declared as a child but should rather be specified by a `ChildComponent` property.
* All other properties will be passed through to the child component.
*/
var AutoSizer = function (_Component) {
_inherits(AutoSizer, _Component);
function AutoSizer(props) {
_classCallCheck(this, AutoSizer);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(AutoSizer).call(this, props));
_this.state = {
height: 0,
width: 0
};
_this._onResize = _this._onResize.bind(_this);
_this._onScroll = _this._onScroll.bind(_this);
_this._setRef = _this._setRef.bind(_this);
return _this;
}
_createClass(AutoSizer, [{
key: 'componentDidMount',
value: function componentDidMount() {
// Defer requiring resize handler in order to support server-side rendering.
// See issue #41
this._detectElementResize = require('../vendor/detectElementResize');
this._detectElementResize.addResizeListener(this._parentNode, this._onResize);
this._onResize();
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this._detectElementResize.removeResizeListener(this._parentNode, this._onResize);
}
}, {
key: 'render',
value: function render() {
var _props = this.props;
var children = _props.children;
var disableHeight = _props.disableHeight;
var disableWidth = _props.disableWidth;
var _state = this.state;
var height = _state.height;
var width = _state.width;
// Outer div should not force width/height since that may prevent containers from shrinking.
// Inner component should overflow and use calculated width/height.
// See issue #68 for more information.
var outerStyle = { overflow: 'visible' };
if (!disableHeight) {
outerStyle.height = 0;
}
if (!disableWidth) {
outerStyle.width = 0;
}
return _react2.default.createElement(
'div',
{
ref: this._setRef,
onScroll: this._onScroll,
style: outerStyle
},
children({ height: height, width: width })
);
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState) {
return (0, _reactAddonsShallowCompare2.default)(this, nextProps, nextState);
}
}, {
key: '_onResize',
value: function _onResize() {
var onResize = this.props.onResize;
var _parentNode$getBoundi = this._parentNode.getBoundingClientRect();
var height = _parentNode$getBoundi.height;
var width = _parentNode$getBoundi.width;
var style = getComputedStyle(this._parentNode);
var paddingLeft = parseInt(style.paddingLeft, 10);
var paddingRight = parseInt(style.paddingRight, 10);
var paddingTop = parseInt(style.paddingTop, 10);
var paddingBottom = parseInt(style.paddingBottom, 10);
this.setState({
height: height - paddingTop - paddingBottom,
width: width - paddingLeft - paddingRight
});
onResize({ height: height, width: width });
}
}, {
key: '_onScroll',
value: function _onScroll(event) {
// Prevent detectElementResize library from being triggered by this scroll event.
event.stopPropagation();
}
}, {
key: '_setRef',
value: function _setRef(autoSizer) {
// In case the component has been unmounted
this._parentNode = autoSizer && autoSizer.parentNode;
}
}]);
return AutoSizer;
}(_react.Component);
AutoSizer.propTypes = {
/**
* Function respondible for rendering children.
* This function should implement the following signature:
* ({ height, width }) => PropTypes.element
*/
children: _react.PropTypes.func.isRequired,
/** Disable dynamic :height property */
disableHeight: _react.PropTypes.bool,
/** Disable dynamic :width property */
disableWidth: _react.PropTypes.bool,
/** Callback to be invoked on-resize: ({ height, width }) */
onResize: _react.PropTypes.func.isRequired
};
AutoSizer.defaultProps = {
onResize: function onResize() {}
};
exports.default = AutoSizer;
;