meck-rc-table
Version:
table ui component for react
280 lines (235 loc) • 8.11 kB
JavaScript
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 from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { connect } from 'mini-store';
import TableCell from './TableCell';
var TableRow = function (_React$Component) {
_inherits(TableRow, _React$Component);
function TableRow(props) {
_classCallCheck(this, TableRow);
var _this = _possibleConstructorReturn(this, (TableRow.__proto__ || Object.getPrototypeOf(TableRow)).call(this, props));
_this.onRowClick = function (event) {
var _this$props = _this.props,
record = _this$props.record,
index = _this$props.index,
onRowClick = _this$props.onRowClick;
onRowClick(record, index, event);
};
_this.onRowDoubleClick = function (event) {
var _this$props2 = _this.props,
record = _this$props2.record,
index = _this$props2.index,
onRowDoubleClick = _this$props2.onRowDoubleClick;
onRowDoubleClick(record, index, event);
};
_this.onContextMenu = function (event) {
var _this$props3 = _this.props,
record = _this$props3.record,
index = _this$props3.index,
onRowContextMenu = _this$props3.onRowContextMenu;
onRowContextMenu(record, index, event);
};
_this.onMouseEnter = function (event) {
var _this$props4 = _this.props,
record = _this$props4.record,
index = _this$props4.index,
onRowMouseEnter = _this$props4.onRowMouseEnter,
onHover = _this$props4.onHover,
rowKey = _this$props4.rowKey;
onHover(true, rowKey);
onRowMouseEnter(record, index, event);
};
_this.onMouseLeave = function (event) {
var _this$props5 = _this.props,
record = _this$props5.record,
index = _this$props5.index,
onRowMouseLeave = _this$props5.onRowMouseLeave,
onHover = _this$props5.onHover,
rowKey = _this$props5.rowKey;
onHover(false, rowKey);
onRowMouseLeave(record, index, event);
};
_this.shouldRender = props.visible;
return _this;
}
_createClass(TableRow, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (this.shouldRender) {
this.saveRowRef();
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (this.props.visible || !this.props.visible && nextProps.visible) {
this.shouldRender = true;
}
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate() {
if (this.shouldRender && !this.rowRef) {
this.saveRowRef();
}
}
}, {
key: 'setHeight',
value: function setHeight() {
var _props = this.props,
store = _props.store,
rowKey = _props.rowKey;
var _store$getState = store.getState(),
expandedRowsHeight = _store$getState.expandedRowsHeight;
var height = this.rowRef.getBoundingClientRect().height;
expandedRowsHeight[rowKey] = height;
store.setState({ expandedRowsHeight: expandedRowsHeight });
}
}, {
key: 'saveRowRef',
value: function saveRowRef() {
this.rowRef = ReactDOM.findDOMNode(this);
if (!this.props.fixed) {
this.setHeight();
}
}
}, {
key: 'render',
value: function render() {
if (!this.shouldRender) {
return null;
}
var _props2 = this.props,
prefixCls = _props2.prefixCls,
columns = _props2.columns,
record = _props2.record,
index = _props2.index,
indent = _props2.indent,
indentSize = _props2.indentSize,
visible = _props2.visible,
height = _props2.height,
hovered = _props2.hovered,
components = _props2.components,
hasExpandIcon = _props2.hasExpandIcon,
renderExpandIcon = _props2.renderExpandIcon,
renderExpandIconCell = _props2.renderExpandIconCell;
var BodyRow = 'tr';
var BodyCell = 'td';
if (components && components.body) {
BodyRow = components.body.row || BodyRow;
BodyCell = components.body.cell || BodyCell;
}
var className = this.props.className;
if (hovered) {
className += ' ' + prefixCls + '-hover';
}
var cells = [];
renderExpandIconCell(cells);
for (var i = 0; i < columns.length; i++) {
cells.push(React.createElement(TableCell, {
prefixCls: prefixCls,
record: record,
indentSize: indentSize,
indent: indent,
index: index,
column: columns[i],
key: columns[i].key || columns[i].dataIndex,
expandIcon: hasExpandIcon(i) && renderExpandIcon(),
component: BodyCell
}));
}
var style = { height: height };
if (!visible) {
style.display = 'none';
}
var rowClassName = (prefixCls + ' ' + className + ' ' + prefixCls + '-level-' + indent).trim();
return React.createElement(
BodyRow,
{
onClick: this.onRowClick,
onDoubleClick: this.onRowDoubleClick,
onMouseEnter: this.onMouseEnter,
onMouseLeave: this.onMouseLeave,
onContextMenu: this.onContextMenu,
className: rowClassName,
style: style
},
cells
);
}
}]);
return TableRow;
}(React.Component);
TableRow.propTypes = {
onRowClick: PropTypes.func,
onRowDoubleClick: PropTypes.func,
onRowContextMenu: PropTypes.func,
onRowMouseEnter: PropTypes.func,
onRowMouseLeave: PropTypes.func,
record: PropTypes.object,
prefixCls: PropTypes.string,
onHover: PropTypes.func,
columns: PropTypes.array,
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
index: PropTypes.number,
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
className: PropTypes.string,
indent: PropTypes.number,
indentSize: PropTypes.number,
hasExpandIcon: PropTypes.func.isRequired,
hovered: PropTypes.bool.isRequired,
visible: PropTypes.bool.isRequired,
store: PropTypes.object.isRequired,
fixed: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
renderExpandIcon: PropTypes.func,
renderExpandIconCell: PropTypes.func,
components: PropTypes.any
};
TableRow.defaultProps = {
onRowClick: function onRowClick() {},
onRowDoubleClick: function onRowDoubleClick() {},
onRowContextMenu: function onRowContextMenu() {},
onRowMouseEnter: function onRowMouseEnter() {},
onRowMouseLeave: function onRowMouseLeave() {},
expandIconColumnIndex: 0,
expandRowByClick: false,
onHover: function onHover() {},
hasExpandIcon: function hasExpandIcon() {},
renderExpandIcon: function renderExpandIcon() {},
renderExpandIconCell: function renderExpandIconCell() {}
};
function getRowHeight(state, props) {
var expandedRowsHeight = state.expandedRowsHeight,
fixedColumnsBodyRowsHeight = state.fixedColumnsBodyRowsHeight;
var fixed = props.fixed,
index = props.index,
rowKey = props.rowKey;
if (!fixed) {
return null;
}
if (expandedRowsHeight[rowKey]) {
return expandedRowsHeight[rowKey];
}
if (fixedColumnsBodyRowsHeight[index]) {
return fixedColumnsBodyRowsHeight[index];
}
return null;
}
export default connect(function (state, props) {
var currentHoverKey = state.currentHoverKey,
expandedRowKeys = state.expandedRowKeys;
var rowKey = props.rowKey,
ancestorKeys = props.ancestorKeys;
var visible = ancestorKeys.length === 0 || ancestorKeys.every(function (k) {
return ~expandedRowKeys.indexOf(k);
});
return {
visible: visible,
hovered: currentHoverKey === rowKey,
height: getRowHeight(state, props)
};
})(TableRow);