react-virtualized
Version:
React components for efficiently rendering large, scrollable lists and tabular data
126 lines (111 loc) • 3.93 kB
JavaScript
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 React, { Component, PropTypes } from 'react';
import shallowCompare from 'react-addons-shallow-compare';
/**
* This HOC decorates a virtualized component and responds to arrow-key events by scrolling one row or column at a time.
*/
var ArrowKeyStepper = function (_Component) {
_inherits(ArrowKeyStepper, _Component);
function ArrowKeyStepper(props, context) {
_classCallCheck(this, ArrowKeyStepper);
var _this = _possibleConstructorReturn(this, (ArrowKeyStepper.__proto__ || _Object$getPrototypeOf(ArrowKeyStepper)).call(this, props, context));
_this.state = {
scrollToColumn: 0,
scrollToRow: 0
};
_this._columnStartIndex = 0;
_this._columnStopIndex = 0;
_this._rowStartIndex = 0;
_this._rowStopIndex = 0;
_this._onKeyDown = _this._onKeyDown.bind(_this);
_this._onSectionRendered = _this._onSectionRendered.bind(_this);
return _this;
}
_createClass(ArrowKeyStepper, [{
key: 'render',
value: function render() {
var _props = this.props;
var className = _props.className;
var children = _props.children;
var _state = this.state;
var scrollToColumn = _state.scrollToColumn;
var scrollToRow = _state.scrollToRow;
return React.createElement(
'div',
{
className: className,
onKeyDown: this._onKeyDown
},
children({
onSectionRendered: this._onSectionRendered,
scrollToColumn: scrollToColumn,
scrollToRow: scrollToRow
})
);
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
}, {
key: '_onKeyDown',
value: function _onKeyDown(event) {
var _props2 = this.props;
var columnCount = _props2.columnCount;
var rowCount = _props2.rowCount;
// The above cases all prevent default event event behavior.
// This is to keep the grid from scrolling after the snap-to update.
switch (event.key) {
case 'ArrowDown':
event.preventDefault();
this.setState({
scrollToRow: Math.min(this._rowStopIndex + 1, rowCount - 1)
});
break;
case 'ArrowLeft':
event.preventDefault();
this.setState({
scrollToColumn: Math.max(this._columnStartIndex - 1, 0)
});
break;
case 'ArrowRight':
event.preventDefault();
this.setState({
scrollToColumn: Math.min(this._columnStopIndex + 1, columnCount - 1)
});
break;
case 'ArrowUp':
event.preventDefault();
this.setState({
scrollToRow: Math.max(this._rowStartIndex - 1, 0)
});
break;
}
}
}, {
key: '_onSectionRendered',
value: function _onSectionRendered(_ref) {
var columnStartIndex = _ref.columnStartIndex;
var columnStopIndex = _ref.columnStopIndex;
var rowStartIndex = _ref.rowStartIndex;
var rowStopIndex = _ref.rowStopIndex;
this._columnStartIndex = columnStartIndex;
this._columnStopIndex = columnStopIndex;
this._rowStartIndex = rowStartIndex;
this._rowStopIndex = rowStopIndex;
}
}]);
return ArrowKeyStepper;
}(Component);
ArrowKeyStepper.propTypes = {
children: PropTypes.func.isRequired,
className: PropTypes.string,
columnCount: PropTypes.number.isRequired,
rowCount: PropTypes.number.isRequired
};
export default ArrowKeyStepper;