choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
370 lines (321 loc) • 11.5 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { connect } from 'mini-store';
import ResizeObserver from 'resize-observer-polyfill';
import TableCell from './TableCell';
import warning from '../../_util/warning';
import TableRowContext, { InnerRowCtx } from './TableRowContext';
var TableRow = /*#__PURE__*/function (_Component) {
_inherits(TableRow, _Component);
var _super = _createSuper(TableRow);
function TableRow(props) {
var _this;
_classCallCheck(this, TableRow);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "onRowClick", function (event) {
var _this$props = _this.props,
record = _this$props.record,
index = _this$props.index,
onRowClick = _this$props.onRowClick;
if (onRowClick) {
onRowClick(record, index, event);
}
});
_defineProperty(_assertThisInitialized(_this), "onRowDoubleClick", function (event) {
var _this$props2 = _this.props,
record = _this$props2.record,
index = _this$props2.index,
onRowDoubleClick = _this$props2.onRowDoubleClick;
if (onRowDoubleClick) {
onRowDoubleClick(record, index, event);
}
});
_defineProperty(_assertThisInitialized(_this), "onContextMenu", function (event) {
var _this$props3 = _this.props,
record = _this$props3.record,
index = _this$props3.index,
onRowContextMenu = _this$props3.onRowContextMenu;
if (onRowContextMenu) {
onRowContextMenu(record, index, event);
}
});
_defineProperty(_assertThisInitialized(_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);
if (onRowMouseEnter) {
onRowMouseEnter(record, index, event);
}
});
_defineProperty(_assertThisInitialized(_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);
if (onRowMouseLeave) {
onRowMouseLeave(record, index, event);
}
});
_defineProperty(_assertThisInitialized(_this), "syncRowHeight", function () {
var _this$props6 = _this.props,
isAnyColumnsFixed = _this$props6.isAnyColumnsFixed,
fixed = _this$props6.fixed,
expandedRow = _this$props6.expandedRow,
ancestorKeys = _this$props6.ancestorKeys;
if (!isAnyColumnsFixed) {
return;
}
if (!fixed && expandedRow) {
_this.setExpanedRowHeight();
}
if (!fixed && ancestorKeys.length >= 0) {
_this.setRowHeight();
}
});
_this.shouldRender = props.visible;
_this.resizeObserver = null;
_this.state = {};
return _this;
}
_createClass(TableRow, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.state.shouldRender) {
this.saveRowRef();
if (this.rowRef) {
this.resizeObserver = new ResizeObserver(this.syncRowHeight);
var dom = ReactDOM.findDOMNode(this);
this.resizeObserver.observe(dom);
}
}
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps) {
return !!(this.props.visible || nextProps.visible);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
if (this.state.shouldRender && !this.rowRef) {
this.saveRowRef();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
delete this.resizeObserver;
}
}
}, {
key: "setExpanedRowHeight",
value: function setExpanedRowHeight() {
var _this$props7 = this.props,
store = _this$props7.store,
rowKey = _this$props7.rowKey;
var _store$getState = store.getState(),
expandedRowsHeight = _store$getState.expandedRowsHeight;
var height = this.rowRef.getBoundingClientRect().height;
var oldHeight = expandedRowsHeight && expandedRowsHeight[rowKey];
if (height && oldHeight !== height) {
expandedRowsHeight = _objectSpread(_objectSpread({}, expandedRowsHeight), {}, _defineProperty({}, rowKey, height));
store.setState({
expandedRowsHeight: expandedRowsHeight
});
}
}
}, {
key: "setRowHeight",
value: function setRowHeight() {
var _this$props8 = this.props,
store = _this$props8.store,
rowKey = _this$props8.rowKey;
var _store$getState2 = store.getState(),
fixedColumnsBodyRowsHeight = _store$getState2.fixedColumnsBodyRowsHeight;
var height = this.rowRef.getBoundingClientRect().height;
var oldHeight = fixedColumnsBodyRowsHeight && fixedColumnsBodyRowsHeight[rowKey];
if (height && oldHeight !== height) {
store.setState({
fixedColumnsBodyRowsHeight: _objectSpread(_objectSpread({}, fixedColumnsBodyRowsHeight), {}, _defineProperty({}, rowKey, height))
});
}
}
}, {
key: "getStyle",
value: function getStyle() {
var _this$props9 = this.props,
height = _this$props9.height,
visible = _this$props9.visible;
if (height && height !== this.style.height) {
this.style = _objectSpread(_objectSpread({}, this.style), {}, {
height: height
});
}
if (!visible && !this.style.display) {
this.style = _objectSpread(_objectSpread({}, this.style), {}, {
display: 'none'
});
}
return this.style;
}
}, {
key: "saveRowRef",
value: function saveRowRef() {
this.rowRef = ReactDOM.findDOMNode(this);
this.syncRowHeight();
}
}, {
key: "render",
value: function render() {
if (!this.state.shouldRender) {
return null;
}
var _this$props10 = this.props,
prefixCls = _this$props10.prefixCls,
columns = _this$props10.columns,
record = _this$props10.record,
rowKey = _this$props10.rowKey,
index = _this$props10.index,
onRow = _this$props10.onRow,
indent = _this$props10.indent,
indentSize = _this$props10.indentSize,
hovered = _this$props10.hovered,
height = _this$props10.height,
visible = _this$props10.visible,
fixed = _this$props10.fixed,
components = _this$props10.components,
hasExpandIcon = _this$props10.hasExpandIcon,
renderExpandIcon = _this$props10.renderExpandIcon,
renderExpandIconCell = _this$props10.renderExpandIconCell;
var BodyRow = components.body.row;
var BodyCell = components.body.cell;
var className = this.props.className;
if (hovered) {
className += " ".concat(prefixCls, "-hover");
}
var cells = [];
renderExpandIconCell(cells);
var _loop = function _loop(i) {
var column = columns[i];
warning(column.onCellClick === undefined, 'column[onCellClick] is deprecated, please use column[onCell] instead.');
cells.push( /*#__PURE__*/React.createElement(TableRowContext.Consumer, {
key: column.key || column.dataIndex
}, function (form) {
return /*#__PURE__*/React.createElement(TableCell, {
prefixCls: prefixCls,
record: record,
indentSize: indentSize,
indent: indent,
form: form,
index: index,
column: column,
key: column.key || column.dataIndex,
expandIcon: hasExpandIcon(i) && renderExpandIcon(),
component: BodyCell
});
}));
};
for (var i = 0; i < columns.length; i++) {
_loop(i);
}
var rowClassName = "".concat(prefixCls, " ").concat(className, " ").concat(prefixCls, "-level-").concat(indent).trim();
var rowProps = onRow(record, index);
var customStyle = rowProps ? rowProps.style : {};
var style = {
height: height
};
if (!visible) {
style.display = 'none';
}
style = _objectSpread(_objectSpread({}, style), customStyle);
return /*#__PURE__*/React.createElement(InnerRowCtx.Provider, {
value: {
syncRowHeight: this.syncRowHeight
}
}, /*#__PURE__*/React.createElement(BodyRow, _extends({
onClick: this.onRowClick,
onDoubleClick: this.onRowDoubleClick,
onMouseEnter: this.onMouseEnter,
onMouseLeave: this.onMouseLeave,
onContextMenu: this.onContextMenu,
className: rowClassName,
fixed: fixed,
record: record
}, rowProps, {
style: style,
"data-row-key": rowKey
}), cells));
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps, prevState) {
if (prevState.visible || !prevState.visible && nextProps.visible) {
return {
shouldRender: true,
visible: nextProps.visible
};
}
return {
visible: nextProps.visible
};
}
}]);
return TableRow;
}(Component);
_defineProperty(TableRow, "defaultProps", {
onRow: function onRow() {},
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,
rowKey = props.rowKey;
if (!fixed) {
return null;
}
if (expandedRowsHeight[rowKey]) {
return expandedRowsHeight[rowKey];
}
if (fixedColumnsBodyRowsHeight[rowKey]) {
return fixedColumnsBodyRowsHeight[rowKey];
}
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);
//# sourceMappingURL=TableRow.js.map