UNPKG

react-virtualized

Version:

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

114 lines (92 loc) 3.75 kB
import { Component, PropTypes } from 'react'; import shallowCompare from 'react-addons-shallow-compare'; import Grid from '../Grid'; /** * High-order component that auto-calculates column-widths for `Grid` cells. */ var ColumnSizer = function (_Component) { babelHelpers.inherits(ColumnSizer, _Component); function ColumnSizer(props, context) { babelHelpers.classCallCheck(this, ColumnSizer); var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(ColumnSizer).call(this, props, context)); _this._registerChild = _this._registerChild.bind(_this); return _this; } babelHelpers.createClass(ColumnSizer, [{ key: 'componentDidUpdate', value: function componentDidUpdate(prevProps, prevState) { var _props = this.props; var columnMaxWidth = _props.columnMaxWidth; var columnMinWidth = _props.columnMinWidth; var columnsCount = _props.columnsCount; var width = _props.width; if (columnMaxWidth !== prevProps.columnMaxWidth || columnMinWidth !== prevProps.columnMinWidth || columnsCount !== prevProps.columnsCount || width !== prevProps.width) { if (this._registeredChild) { this._registeredChild.recomputeGridSize(); } } } }, { key: 'render', value: function render() { var _props2 = this.props; var children = _props2.children; var columnMaxWidth = _props2.columnMaxWidth; var columnMinWidth = _props2.columnMinWidth; var columnsCount = _props2.columnsCount; var width = _props2.width; var safeColumnMinWidth = columnMinWidth || 1; var safeColumnMaxWidth = columnMaxWidth ? Math.min(columnMaxWidth, width) : width; var columnWidth = width / columnsCount; columnWidth = Math.max(safeColumnMinWidth, columnWidth); columnWidth = Math.min(safeColumnMaxWidth, columnWidth); columnWidth = Math.floor(columnWidth); var adjustedWidth = Math.min(width, columnWidth * columnsCount); return children({ adjustedWidth: adjustedWidth, getColumnWidth: function getColumnWidth() { return columnWidth; }, registerChild: this._registerChild }); } }, { key: 'shouldComponentUpdate', value: function shouldComponentUpdate(nextProps, nextState) { return shallowCompare(this, nextProps, nextState); } }, { key: '_registerChild', value: function _registerChild(child) { if (child !== null && !(child instanceof Grid)) { throw Error('Unexpected child type registered; only Grid children are supported.'); } this._registeredChild = child; if (this._registeredChild) { this._registeredChild.recomputeGridSize(); } } }]); return ColumnSizer; }(Component); ColumnSizer.propTypes = { /** * Function respondible for rendering a virtualized Grid. * This function should implement the following signature: * ({ adjustedWidth, getColumnWidth, registerChild }) => PropTypes.element * * The specified :getColumnWidth function should be passed to the Grid's :columnWidth property. * The :registerChild should be passed to the Grid's :ref property. * The :adjustedWidth property is optional; it reflects the lesser of the overall width or the width of all columns. */ children: PropTypes.func.isRequired, /** Optional maximum allowed column width */ columnMaxWidth: PropTypes.number, /** Optional minimum allowed column width */ columnMinWidth: PropTypes.number, /** Number of columns in Grid or FlexTable child */ columnsCount: PropTypes.number.isRequired, /** Width of Grid or FlexTable child */ width: PropTypes.number.isRequired }; export default ColumnSizer;