react-virtualized
Version:
React components for efficiently rendering large, scrollable lists and tabular data
163 lines (141 loc) • 5.37 kB
JavaScript
import Grid from '../Grid';
import React, { Component, PropTypes } from 'react';
import cn from 'classnames';
import shallowCompare from 'react-addons-shallow-compare';
/**
* 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 VirtualScroll = function (_Component) {
babelHelpers.inherits(VirtualScroll, _Component);
function VirtualScroll() {
babelHelpers.classCallCheck(this, VirtualScroll);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(VirtualScroll).apply(this, arguments));
}
babelHelpers.createClass(VirtualScroll, [{
key: 'recomputeRowHeights',
/**
* See Grid#recomputeGridSize
*/
value: function recomputeRowHeights() {
this.refs.Grid.recomputeGridSize();
}
}, {
key: 'render',
value: function render() {
var _props = this.props;
var className = _props.className;
var height = _props.height;
var noRowsRenderer = _props.noRowsRenderer;
var onRowsRendered = _props.onRowsRendered;
var _onScroll = _props.onScroll;
var rowHeight = _props.rowHeight;
var rowRenderer = _props.rowRenderer;
var overscanRowsCount = _props.overscanRowsCount;
var rowsCount = _props.rowsCount;
var scrollToIndex = _props.scrollToIndex;
var scrollTop = _props.scrollTop;
var width = _props.width;
var classNames = cn('VirtualScroll', className);
return React.createElement(Grid, {
ref: 'Grid',
className: classNames,
columnWidth: width,
columnsCount: 1,
height: height,
noContentRenderer: noRowsRenderer,
onScroll: function onScroll(_ref) {
var clientHeight = _ref.clientHeight;
var scrollHeight = _ref.scrollHeight;
var scrollTop = _ref.scrollTop;
return _onScroll({ clientHeight: clientHeight, scrollHeight: scrollHeight, scrollTop: scrollTop });
},
onSectionRendered: function onSectionRendered(_ref2) {
var rowOverscanStartIndex = _ref2.rowOverscanStartIndex;
var rowOverscanStopIndex = _ref2.rowOverscanStopIndex;
var rowStartIndex = _ref2.rowStartIndex;
var rowStopIndex = _ref2.rowStopIndex;
return onRowsRendered({
overscanStartIndex: rowOverscanStartIndex,
overscanStopIndex: rowOverscanStopIndex,
startIndex: rowStartIndex,
stopIndex: rowStopIndex
});
},
overscanRowsCount: overscanRowsCount,
renderCell: function renderCell(_ref3) {
var columnIndex = _ref3.columnIndex;
var rowIndex = _ref3.rowIndex;
return rowRenderer(rowIndex);
},
rowHeight: rowHeight,
rowsCount: rowsCount,
scrollToRow: scrollToIndex,
scrollTop: scrollTop,
width: width
});
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
}]);
return VirtualScroll;
}(Component);
VirtualScroll.propTypes = {
/** Optional CSS class name */
className: PropTypes.string,
/** Height constraint for list (determines how many actual rows are rendered) */
height: PropTypes.number.isRequired,
/** Optional renderer to be used in place of rows when rowsCount is 0 */
noRowsRenderer: PropTypes.func.isRequired,
/**
* Callback invoked with information about the slice of rows that were just rendered.
* ({ startIndex, stopIndex }): void
*/
onRowsRendered: 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.
*/
overscanRowsCount: 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: 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: PropTypes.oneOfType([PropTypes.number, PropTypes.func]).isRequired,
/** Responsbile for rendering a row given an index */
rowRenderer: PropTypes.func.isRequired,
/** Number of rows in list. */
rowsCount: PropTypes.number.isRequired,
/** Row index to ensure visible (by forcefully scrolling if necessary) */
scrollToIndex: PropTypes.number,
/** Vertical offset. */
scrollTop: PropTypes.number,
/** Width of list */
width: PropTypes.number.isRequired
};
VirtualScroll.defaultProps = {
noRowsRenderer: function noRowsRenderer() {
return null;
},
onRowsRendered: function onRowsRendered() {
return null;
},
onScroll: function onScroll() {
return null;
},
overscanRowsCount: 10
};
export default VirtualScroll;