UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

395 lines (338 loc) 13.2 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _reactDom = _interopRequireDefault(require("react-dom")); var _propTypes = _interopRequireDefault(require("prop-types")); var _miniStore = require("mini-store"); var _TableCell = _interopRequireDefault(require("./TableCell")); var _warning = _interopRequireDefault(require("../../_util/warning")); function _createSuper(Derived) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } return function () { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; } var TableRow = /*#__PURE__*/ function (_Component) { (0, _inherits2["default"])(TableRow, _Component); var _super = _createSuper(TableRow); function TableRow(props) { var _this; (0, _classCallCheck2["default"])(this, TableRow); _this = _super.call(this, props); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_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); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_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); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_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); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_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); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_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); } }); _this.shouldRender = props.visible; return _this; } (0, _createClass2["default"])(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: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps) { return !!(this.props.visible || nextProps.visible); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { if (this.shouldRender && !this.rowRef) { this.saveRowRef(); } } }, { key: "setExpanedRowHeight", value: function setExpanedRowHeight() { var _this$props6 = this.props, store = _this$props6.store, rowKey = _this$props6.rowKey; var _store$getState = store.getState(), expandedRowsHeight = _store$getState.expandedRowsHeight; var height = this.rowRef.getBoundingClientRect().height; expandedRowsHeight = (0, _objectSpread3["default"])({}, expandedRowsHeight, (0, _defineProperty2["default"])({}, rowKey, height)); store.setState({ expandedRowsHeight: expandedRowsHeight }); } }, { key: "setRowHeight", value: function setRowHeight() { var _this$props7 = this.props, store = _this$props7.store, index = _this$props7.index; var fixedColumnsBodyRowsHeight = store.getState().fixedColumnsBodyRowsHeight.slice(); var height = this.rowRef.getBoundingClientRect().height; fixedColumnsBodyRowsHeight[index] = height; store.setState({ fixedColumnsBodyRowsHeight: fixedColumnsBodyRowsHeight }); } }, { key: "getStyle", value: function getStyle() { var _this$props8 = this.props, height = _this$props8.height, visible = _this$props8.visible; if (height && height !== this.style.height) { this.style = (0, _objectSpread3["default"])({}, this.style, { height: height }); } if (!visible && !this.style.display) { this.style = (0, _objectSpread3["default"])({}, this.style, { display: 'none' }); } return this.style; } }, { key: "saveRowRef", value: function saveRowRef() { this.rowRef = _reactDom["default"].findDOMNode(this); var _this$props9 = this.props, isAnyColumnsFixed = _this$props9.isAnyColumnsFixed, fixed = _this$props9.fixed, expandedRow = _this$props9.expandedRow, ancestorKeys = _this$props9.ancestorKeys; if (!isAnyColumnsFixed) { return; } if (!fixed && expandedRow) { this.setExpanedRowHeight(); } if (!fixed && ancestorKeys.length >= 0) { this.setRowHeight(); } } }, { key: "render", value: function render() { if (!this.shouldRender) { return null; } var _this$props10 = this.props, prefixCls = _this$props10.prefixCls, columns = _this$props10.columns, record = _this$props10.record, 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, 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); for (var i = 0; i < columns.length; i++) { var column = columns[i]; (0, _warning["default"])(column.onCellClick === undefined, 'column[onCellClick] is deprecated, please use column[onCell] instead.'); cells.push(_react["default"].createElement(_TableCell["default"], { prefixCls: prefixCls, record: record, indentSize: indentSize, indent: indent, index: index, column: column, key: column.key || column.dataIndex, expandIcon: hasExpandIcon(i) && renderExpandIcon(), component: BodyCell })); } 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 = (0, _objectSpread3["default"])({}, style, {}, customStyle); return _react["default"].createElement(BodyRow, (0, _extends2["default"])({ onClick: this.onRowClick, onDoubleClick: this.onRowDoubleClick, onMouseEnter: this.onMouseEnter, onMouseLeave: this.onMouseLeave, onContextMenu: this.onContextMenu, className: rowClassName }, rowProps, { style: style }), cells); } }]); return TableRow; }(_react.Component); (0, _defineProperty2["default"])(TableRow, "propTypes", { onRow: _propTypes["default"].func, onRowClick: _propTypes["default"].func, onRowDoubleClick: _propTypes["default"].func, onRowContextMenu: _propTypes["default"].func, onRowMouseEnter: _propTypes["default"].func, onRowMouseLeave: _propTypes["default"].func, record: _propTypes["default"].object, prefixCls: _propTypes["default"].string, onHover: _propTypes["default"].func, columns: _propTypes["default"].array, height: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]), index: _propTypes["default"].number, rowKey: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]).isRequired, className: _propTypes["default"].string, indent: _propTypes["default"].number, indentSize: _propTypes["default"].number, hasExpandIcon: _propTypes["default"].func.isRequired, hovered: _propTypes["default"].bool.isRequired, visible: _propTypes["default"].bool.isRequired, store: _propTypes["default"].object.isRequired, fixed: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].bool]), renderExpandIcon: _propTypes["default"].func, renderExpandIconCell: _propTypes["default"].func, components: _propTypes["default"].any, expandedRow: _propTypes["default"].bool, isAnyColumnsFixed: _propTypes["default"].bool, ancestorKeys: _propTypes["default"].array.isRequired }); (0, _defineProperty2["default"])(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, index = props.index, rowKey = props.rowKey; if (!fixed) { return null; } if (expandedRowsHeight[rowKey]) { return expandedRowsHeight[rowKey]; } if (fixedColumnsBodyRowsHeight[index]) { return fixedColumnsBodyRowsHeight[index]; } return null; } var _default = (0, _miniStore.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); exports["default"] = _default; //# sourceMappingURL=TableRow.js.map