UNPKG

react-virtualized

Version:

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

260 lines (205 loc) 9.26 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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 _Grid = require('../Grid'); var _Grid2 = _interopRequireDefault(_Grid); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); 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; } /** * It is inefficient to create and manage a large list of DOM elements within a scrolling container * if only a few of those elements are visible. The primary purpose of this component is to improve * performance by only rendering the DOM nodes that a user is able to see based on their current * scroll position. * * This component renders a virtualized list of elements with either fixed or dynamic heights. */ var List = function (_Component) { _inherits(List, _Component); function List(props, context) { _classCallCheck(this, List); var _this = _possibleConstructorReturn(this, (List.__proto__ || Object.getPrototypeOf(List)).call(this, props, context)); _this._cellRenderer = _this._cellRenderer.bind(_this); _this._onScroll = _this._onScroll.bind(_this); _this._onSectionRendered = _this._onSectionRendered.bind(_this); return _this; } _createClass(List, [{ key: 'forceUpdateGrid', value: function forceUpdateGrid() { this.Grid.forceUpdate(); } /** See Grid#measureAllCells */ }, { key: 'measureAllRows', value: function measureAllRows() { this.Grid.measureAllCells(); } /** See Grid#recomputeGridSize */ }, { key: 'recomputeRowHeights', value: function recomputeRowHeights() { var index = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0]; this.Grid.recomputeGridSize({ rowIndex: index }); this.forceUpdateGrid(); } }, { key: 'render', value: function render() { var _this2 = this; var _props = this.props; var className = _props.className; var noRowsRenderer = _props.noRowsRenderer; var scrollToIndex = _props.scrollToIndex; var width = _props.width; var classNames = (0, _classnames2.default)('ReactVirtualized__List', className); return _react2.default.createElement(_Grid2.default, _extends({}, this.props, { autoContainerWidth: true, cellRenderer: this._cellRenderer, className: classNames, columnWidth: width, columnCount: 1, noContentRenderer: noRowsRenderer, onScroll: this._onScroll, onSectionRendered: this._onSectionRendered, ref: function ref(_ref) { _this2.Grid = _ref; }, scrollToRow: scrollToIndex })); } }, { key: 'shouldComponentUpdate', value: function shouldComponentUpdate(nextProps, nextState) { return (0, _reactAddonsShallowCompare2.default)(this, nextProps, nextState); } }, { key: '_cellRenderer', value: function _cellRenderer(_ref2) { var columnIndex = _ref2.columnIndex; var isScrolling = _ref2.isScrolling; var key = _ref2.key; var rowIndex = _ref2.rowIndex; var style = _ref2.style; var rowRenderer = this.props.rowRenderer; // By default, List cells should be 100% width. // This prevents them from flowing under a scrollbar (if present). style.width = '100%'; return rowRenderer({ index: rowIndex, isScrolling: isScrolling, key: key, style: style }); } }, { key: '_onScroll', value: function _onScroll(_ref3) { var clientHeight = _ref3.clientHeight; var scrollHeight = _ref3.scrollHeight; var scrollTop = _ref3.scrollTop; var onScroll = this.props.onScroll; onScroll({ clientHeight: clientHeight, scrollHeight: scrollHeight, scrollTop: scrollTop }); } }, { key: '_onSectionRendered', value: function _onSectionRendered(_ref4) { var rowOverscanStartIndex = _ref4.rowOverscanStartIndex; var rowOverscanStopIndex = _ref4.rowOverscanStopIndex; var rowStartIndex = _ref4.rowStartIndex; var rowStopIndex = _ref4.rowStopIndex; var onRowsRendered = this.props.onRowsRendered; onRowsRendered({ overscanStartIndex: rowOverscanStartIndex, overscanStopIndex: rowOverscanStopIndex, startIndex: rowStartIndex, stopIndex: rowStopIndex }); } }]); return List; }(_react.Component); List.propTypes = { 'aria-label': _react.PropTypes.string, /** * Removes fixed height from the scrollingContainer so that the total height * of rows can stretch the window. Intended for use with WindowScroller */ autoHeight: _react.PropTypes.bool, /** Optional CSS class name */ className: _react.PropTypes.string, /** * Used to estimate the total height of a List before all of its rows have actually been measured. * The estimated total height is adjusted as rows are rendered. */ estimatedRowSize: _react.PropTypes.number.isRequired, /** Height constraint for list (determines how many actual rows are rendered) */ height: _react.PropTypes.number.isRequired, /** Optional renderer to be used in place of rows when rowCount is 0 */ noRowsRenderer: _react.PropTypes.func.isRequired, /** * Callback invoked with information about the slice of rows that were just rendered. * ({ startIndex, stopIndex }): void */ onRowsRendered: _react.PropTypes.func.isRequired, /** * Number of rows to render above/below the visible bounds of the list. * These rows can help for smoother scrolling on touch devices. */ overscanRowCount: _react.PropTypes.number.isRequired, /** * Callback invoked whenever the scroll offset changes within the inner scrollable region. * This callback can be used to sync scrolling between lists, tables, or grids. * ({ clientHeight, scrollHeight, scrollTop }): void */ onScroll: _react.PropTypes.func.isRequired, /** * Either a fixed row height (number) or a function that returns the height of a row given its index. * ({ index: number }): number */ rowHeight: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.func]).isRequired, /** Responsbile for rendering a row given an index; ({ index: number }): node */ rowRenderer: _react.PropTypes.func.isRequired, /** Number of rows in list. */ rowCount: _react.PropTypes.number.isRequired, /** See Grid#scrollToAlignment */ scrollToAlignment: _react.PropTypes.oneOf(['auto', 'end', 'start', 'center']).isRequired, /** Row index to ensure visible (by forcefully scrolling if necessary) */ scrollToIndex: _react.PropTypes.number, /** Vertical offset. */ scrollTop: _react.PropTypes.number, /** Optional inline style */ style: _react.PropTypes.object, /** Tab index for focus */ tabIndex: _react.PropTypes.number, /** Width of list */ width: _react.PropTypes.number.isRequired }; List.defaultProps = { estimatedRowSize: 30, noRowsRenderer: function noRowsRenderer() { return null; }, onRowsRendered: function onRowsRendered() { return null; }, onScroll: function onScroll() { return null; }, overscanRowCount: 10, scrollToAlignment: 'auto', style: {} }; exports.default = List;