UNPKG

choerodon-ui

Version:

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

300 lines (256 loc) 9.07 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; 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 = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } import { __decorate } from "tslib"; import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { observer } from 'mobx-react'; import { computed, get } from 'mobx'; import classNames from 'classnames'; import isNil from 'lodash/isNil'; import measureScrollbar from '../../../es/_util/measureScrollbar'; import { pxToRem } from '../../../es/_util/UnitConvertor'; import TableContext from './TableContext'; import { minColumnWidth } from './Column'; import { ColumnLock, DragColumnAlign } from './enum'; import TableEditor from './TableEditor'; import TableCol from './TableCol'; import { getColumnKey, isStickySupport } from './utils'; import autobind from '../_util/autobind'; var TableWrapper = /*#__PURE__*/ function (_Component) { _inherits(TableWrapper, _Component); var _super = _createSuper(TableWrapper); function TableWrapper() { _classCallCheck(this, TableWrapper); return _super.apply(this, arguments); } _createClass(TableWrapper, [{ key: "getCol", value: function getCol(column, width) { if (!column.hidden) { return React.createElement(TableCol, { key: getColumnKey(column), width: width, minWidth: minColumnWidth(column) }); } } }, { key: "getColGroup", value: function getColGroup() { var _this = this; var _this$props = this.props, lock = _this$props.lock, hasHeader = _this$props.hasHeader, hasFooter = _this$props.hasFooter; var _this$context$tableSt = this.context.tableStore, overflowY = _this$context$tableSt.overflowY, overflowX = _this$context$tableSt.overflowX, customizable = _this$context$tableSt.customizable, rowDraggable = _this$context$tableSt.rowDraggable, dragColumnAlign = _this$context$tableSt.dragColumnAlign; var hasEmptyWidth = false; var fixedColumnLength = 1; if (customizable) { fixedColumnLength += 1; } if (rowDraggable && dragColumnAlign === DragColumnAlign.right) { fixedColumnLength += 1; } var cols = this.leafColumns.map(function (column, index, array) { var width = get(column, 'width'); if (!overflowX) { if (!hasEmptyWidth && index === array.length - fixedColumnLength) { width = undefined; } else if (isNil(width)) { hasEmptyWidth = true; } } return _this.getCol(column, width); }); if (overflowY && lock !== ColumnLock.left && (hasHeader || hasFooter)) { cols.push(React.createElement("col", { key: "fixed-column", style: { width: pxToRem(measureScrollbar()) } })); } return React.createElement("colgroup", null, cols); } }, { key: "getEditors", value: function getEditors() { return this.leafEditorColumns.map(function (column) { return React.createElement(TableEditor, { key: getColumnKey(column), column: column }); }); } }, { key: "saveRef", value: function saveRef(node) { this.tableWrapper = node; } }, { key: "render", value: function render() { var _this$props2 = this.props, children = _this$props2.children, lock = _this$props2.lock, hasBody = _this$props2.hasBody; var _this$context$tableSt2 = this.context.tableStore, overflowY = _this$context$tableSt2.overflowY, height = _this$context$tableSt2.height, prefixCls = _this$context$tableSt2.prefixCls; var editors = hasBody && this.getEditors(); var className = classNames(_defineProperty({}, "".concat(prefixCls, "-last-row-bordered"), hasBody && !overflowY && height !== undefined)); var table = React.createElement("table", { key: "table", ref: lock ? undefined : this.saveRef, className: className, style: { width: this.tableWidth } }, this.getColGroup(), children); return [table, editors]; } }, { key: "leafColumnsWidth", get: function get() { var tableStore = this.context.tableStore; var lock = this.props.lock; switch (lock) { case ColumnLock.left: case true: return tableStore.leftLeafColumnsWidth; case ColumnLock.right: return tableStore.rightLeafColumnsWidth; default: if (tableStore.overflowX) { return tableStore.totalLeafColumnsWidth; } } return undefined; } }, { key: "leafEditorColumns", get: function get() { var tableStore = this.context.tableStore; var lock = this.props.lock; switch (lock) { case ColumnLock.left: case true: return tableStore.leftLeafColumns.filter(function (_ref) { var editor = _ref.editor, name = _ref.name, hidden = _ref.hidden; return editor && name && !hidden; }); case ColumnLock.right: return tableStore.rightLeafColumns.filter(function (_ref2) { var editor = _ref2.editor, name = _ref2.name, hidden = _ref2.hidden; return editor && name && !hidden; }); default: return tableStore.leafColumns.filter(function (_ref3) { var editor = _ref3.editor, name = _ref3.name, hidden = _ref3.hidden, columnLock = _ref3.lock; return editor && name && !hidden && (isStickySupport() || !columnLock || !tableStore.overflowX); }); } } }, { key: "leafColumns", get: function get() { var tableStore = this.context.tableStore; var lock = this.props.lock; switch (lock) { case ColumnLock.left: case true: return tableStore.leftLeafColumns.filter(function (_ref4) { var hidden = _ref4.hidden; return !hidden; }); case ColumnLock.right: return tableStore.rightLeafColumns.filter(function (_ref5) { var hidden = _ref5.hidden; return !hidden; }); default: return tableStore.leafColumns.filter(function (_ref6) { var hidden = _ref6.hidden; return !hidden; }); } } }, { key: "tableWidth", get: function get() { var _this$props3 = this.props, lock = _this$props3.lock, hasBody = _this$props3.hasBody; var _this$context$tableSt3 = this.context.tableStore, overflowY = _this$context$tableSt3.overflowY, overflowX = _this$context$tableSt3.overflowX; if (overflowX) { var tableWidth = this.leafColumnsWidth; if (tableWidth !== undefined && overflowY && lock !== ColumnLock.left && !hasBody) { tableWidth += measureScrollbar(); } return pxToRem(tableWidth); } return '100%'; } }]); return TableWrapper; }(Component); TableWrapper.contextType = TableContext; TableWrapper.propTypes = { lock: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf([ColumnLock.right, ColumnLock.left])]), hasBody: PropTypes.bool, hasHeader: PropTypes.bool, hasFooter: PropTypes.bool }; __decorate([computed], TableWrapper.prototype, "leafColumnsWidth", null); __decorate([computed], TableWrapper.prototype, "leafEditorColumns", null); __decorate([computed], TableWrapper.prototype, "leafColumns", null); __decorate([autobind], TableWrapper.prototype, "saveRef", null); __decorate([computed], TableWrapper.prototype, "tableWidth", null); TableWrapper = __decorate([observer], TableWrapper); export default TableWrapper; //# sourceMappingURL=TableWrapper.js.map