UNPKG

choerodon-ui

Version:

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

840 lines (726 loc) 28.6 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _extends from "@babel/runtime/helpers/extends"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; 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, { cloneElement, Component, isValidElement } from 'react'; import PropTypes from 'prop-types'; import { observer } from 'mobx-react'; import { action, computed, get, remove, set } from 'mobx'; import classNames from 'classnames'; import defer from 'lodash/defer'; import ReactIntersectionObserver from 'react-intersection-observer'; import { Size } from '../../../es/_util/enum'; import { pxToRem } from '../../../es/_util/UnitConvertor'; import measureScrollbar from '../../../es/_util/measureScrollbar'; import { columnWidth } from './Column'; import TableCell from './TableCell'; import Record from '../data-set/Record'; import TableContext from './TableContext'; import ExpandIcon from './ExpandIcon'; import { ColumnLock, DragColumnAlign, HighLightRowType, SelectionMode } from './enum'; import { findCell, getColumnKey, getColumnLock, isDisabledRow, isSelectedRow, isStickySupport } from './utils'; import { CUSTOMIZED_KEY, DRAG_KEY, EXPAND_KEY, SELECTION_KEY } from './TableStore'; import autobind from '../_util/autobind'; import { RecordStatus } from '../data-set/enum'; import ResizeObservedRow from './ResizeObservedRow'; import Spin from '../spin'; var TableRow = /*#__PURE__*/ function (_Component) { _inherits(TableRow, _Component); var _super = _createSuper(TableRow); function TableRow() { var _this; _classCallCheck(this, TableRow); _this = _super.apply(this, arguments); _this.rowExternalProps = {}; _this.childrenRendered = false; _this.isCurrent = false; return _this; } _createClass(TableRow, [{ key: "saveRef", value: function saveRef(node) { if (node) { this.node = node; var _this$props = this.props, lock = _this$props.lock, record = _this$props.record; var _this$context$tableSt = this.context.tableStore, rowHeight = _this$context$tableSt.rowHeight, lockColumnsBodyRowsHeight = _this$context$tableSt.lockColumnsBodyRowsHeight; if (rowHeight === 'auto' && !lock) { set(lockColumnsBodyRowsHeight, this.rowKey = record.key, node.offsetHeight); } } } }, { key: "handleMouseEnter", value: function handleMouseEnter() { this.isHover = true; } }, { key: "handleMouseLeave", value: function handleMouseLeave() { this.isHover = false; } }, { key: "handleSelectionByClick", value: function () { var _handleSelectionByClick = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee(e) { return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return this.handleClick(e); case 2: _context.t0 = _context.sent; if (!(_context.t0 !== false)) { _context.next = 5; break; } this.handleSelection(); case 5: case "end": return _context.stop(); } } }, _callee, this); })); function handleSelectionByClick(_x) { return _handleSelectionByClick.apply(this, arguments); } return handleSelectionByClick; }() }, { key: "handleSelectionByMouseDown", value: function handleSelectionByMouseDown(e) { this.handleSelection(); var onMouseDown = this.rowExternalProps.onMouseDown; if (typeof onMouseDown === 'function') { onMouseDown(e); } } }, { key: "handleSelectionByDblClick", value: function handleSelectionByDblClick(e) { this.handleSelection(); var onDoubleClick = this.rowExternalProps.onDoubleClick; if (typeof onDoubleClick === 'function') { onDoubleClick(e); } } }, { key: "handleExpandChange", value: function handleExpandChange() { if (this.expandable) { this.isExpanded = !this.isExpanded; } } }, { key: "handleClickCapture", value: function handleClickCapture(e) { var _this$props2 = this.props, record = _this$props2.record, dataSet = _this$props2.record.dataSet; if (dataSet && !isDisabledRow(record) && e.target.dataset.selectionKey !== SELECTION_KEY) { dataSet.current = record; } var onClickCapture = this.rowExternalProps.onClickCapture; if (typeof onClickCapture === 'function') { onClickCapture(e); } } }, { key: "handleClick", value: function handleClick(e) { var onClick = this.rowExternalProps.onClick; var _this$context = this.context, highLightRow = _this$context.tableStore.highLightRow, tableStore = _this$context.tableStore; if (highLightRow !== true) { this.isClicked = true; tableStore.setRowHighLight(true); } if (typeof onClick === 'function') { return onClick(e); } } }, { key: "handleResize", value: function handleResize(key, height) { this.setRowHeight(key, height); } }, { key: "setRowHeight", value: function setRowHeight(key, height) { var tableStore = this.context.tableStore; set(tableStore.lockColumnsBodyRowsHeight, key, height); } }, { key: "getCell", value: function getCell(column, index, props) { var _this$props3 = this.props, record = _this$props3.record, lock = _this$props3.lock, provided = _this$props3.provided, snapshot = _this$props3.snapshot, rowIndex = _this$props3.index; var _this$context$tableSt2 = this.context.tableStore, leafColumns = _this$context$tableSt2.leafColumns, rightLeafColumns = _this$context$tableSt2.rightLeafColumns, node = _this$context$tableSt2.node, virtualCell = _this$context$tableSt2.props.virtualCell; var columnIndex = lock === ColumnLock.right ? index + leafColumns.length - rightLeafColumns.length : index; var isDragging = snapshot ? snapshot.isDragging : false; var cell = React.createElement(TableCell, _extends({ inView: true, column: column, record: record, isDragging: isDragging, lock: lock, provided: props.key === DRAG_KEY ? provided : undefined }, props), this.hasExpandIcon(columnIndex) && this.renderExpandIcon()); return virtualCell ? React.createElement(ReactIntersectionObserver, { key: props.key, root: node.tableBodyWrap || node.element, rootMargin: "100px", initialInView: rowIndex <= 10 }, function (_ref) { var ref = _ref.ref, inView = _ref.inView; return cloneElement(cell, { inView: inView, intersectionRef: ref }); }) : cell; } }, { key: "focusRow", value: function focusRow() { var row = this.node; if (row) { var _this$context$tableSt3 = this.context.tableStore, node = _this$context$tableSt3.node, overflowY = _this$context$tableSt3.overflowY, editing = _this$context$tableSt3.editing; var _this$props4 = this.props, lock = _this$props4.lock, record = _this$props4.record; if (!lock && !editing) { var element = node.element; if (element && element.contains(document.activeElement) && (isStickySupport() ? !row.contains(document.activeElement) : Array.from(element.querySelectorAll("tr[data-index=\"".concat(record.id, "\"]"))).every(function (tr) { return !tr.contains(document.activeElement); }))) { row.focus(); } // table 包含目前被focus的element // 找到当前组件对应record生成的组件对象 然后遍历 每个 tr里面不是focus的目标那么这个函数触发row.focus } if (!isStickySupport() && overflowY) { var offsetParent = row.offsetParent; if (offsetParent) { var tableBodyWrap = offsetParent.parentNode; if (tableBodyWrap) { var offsetTop = row.offsetTop, offsetHeight = row.offsetHeight; var scrollTop = tableBodyWrap.scrollTop, height = tableBodyWrap.offsetHeight; var bottomSide = offsetTop + offsetHeight - height + measureScrollbar(); var st = scrollTop; if (st < bottomSide) { st = bottomSide; } if (st > offsetTop) { st = offsetTop + 1; } if (st !== scrollTop) { tableBodyWrap.scrollTop = st; node.handleBodyScrollTop({ target: tableBodyWrap, currentTarget: tableBodyWrap }); } } } } } } }, { key: "componentDidMount", value: function componentDidMount() { var _this$props5 = this.props, lock = _this$props5.lock, record = _this$props5.record; var tableStore = this.context.tableStore; if (record.status === RecordStatus.add && tableStore.autoFocus) { var editor = _toConsumableArray(tableStore.editors.values())[0]; if (editor && (isStickySupport() || getColumnLock(editor.props.column.lock) === getColumnLock(lock))) { var cell = findCell(tableStore, getColumnKey(editor.props.column), lock); if (cell) { defer(function () { return cell.focus(); }); } } } this.syncLoadData(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { var record = this.props.record; if (record.isCurrent && !this.isCurrent) { this.focusRow(); } this.isCurrent = record.isCurrent; this.syncLoadData(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var record = this.props.record; var tableStore = this.context.tableStore; /** * Fixed the when row resize has scrollbar the expanded row would be collapsed */ if (!tableStore.isRowExpanded(record)) { tableStore.setRowExpanded(record, false, true); } if (!isStickySupport()) { remove(tableStore.lockColumnsBodyRowsHeight, this.rowKey); } } }, { key: "handleSelection", value: function handleSelection() { var record = this.props.record; var dataSet = record.dataSet; if (dataSet) { dataSet[record.isSelected ? 'unSelect' : 'select'](record); } } }, { key: "hasExpandIcon", value: function hasExpandIcon(columnIndex) { var tableStore = this.context.tableStore; var _tableStore$props = tableStore.props, expandRowByClick = _tableStore$props.expandRowByClick, expandedRowRenderer = _tableStore$props.expandedRowRenderer, expandIconColumnIndex = tableStore.expandIconColumnIndex, isTree = tableStore.isTree; return !expandRowByClick && (expandedRowRenderer || isTree) && columnIndex === expandIconColumnIndex; } }, { key: "syncLoadData", value: function syncLoadData() { if (this.isLoading) return; var record = this.props.record; var tableStore = this.context.tableStore; var isExpanded = this.isExpanded, isLoaded = this.isLoaded, expandable = this.expandable; var canTreeLoadData = tableStore.canTreeLoadData; if (canTreeLoadData && isExpanded && expandable) { if (!record.children && !isLoaded) { tableStore.onTreeNodeLoad({ record: record }); } } } }, { key: "renderExpandIcon", value: function renderExpandIcon() { var record = this.props.record; var _this$context2 = this.context, tableStore = _this$context2.tableStore, _this$context2$tableS = _this$context2.tableStore, prefixCls = _this$context2$tableS.prefixCls, expandIcon = _this$context2$tableS.expandIcon; var expanded = this.isExpanded, expandable = this.expandable, handleExpandChange = this.handleExpandChange; if (typeof expandIcon === 'function') { return expandIcon({ prefixCls: prefixCls, expanded: expanded, expandable: expandable, needIndentSpaced: !expandable, record: record, onExpand: handleExpandChange }); } if (tableStore.isRowPending(record)) { return React.createElement(Spin, { size: Size.small }); } return React.createElement(ExpandIcon, { prefixCls: prefixCls, expandable: expandable, onChange: handleExpandChange, expanded: expanded }); } }, { key: "renderExpandRow", value: function renderExpandRow() { var isExpanded = this.isExpanded, _this$props6 = this.props, children = _this$props6.children, columns = _this$props6.columns, record = _this$props6.record, index = _this$props6.index; var tableStore = this.context.tableStore; var _tableStore$props2 = tableStore.props, expandedRowRenderer = _tableStore$props2.expandedRowRenderer, onRow = _tableStore$props2.onRow, prefixCls = tableStore.prefixCls, expandIconAsCell = tableStore.expandIconAsCell, overflowX = tableStore.overflowX; var expandRows = []; if (isExpanded || this.childrenRendered) { this.childrenRendered = true; if (expandedRowRenderer) { var rowExternalProps = typeof onRow === 'function' ? onRow({ dataSet: record.dataSet, record: record, expandedRow: true, index: index }) : {}; var classString = classNames("".concat(prefixCls, "-expanded-row"), rowExternalProps.className); var rowProps = { key: "".concat(record.key, "-expanded-row"), className: classString, style: _objectSpread({}, rowExternalProps.style) }; if (overflowX || !record.isCurrent) { rowProps.onMouseEnter = this.handleMouseEnter; rowProps.onMouseLeave = this.handleMouseLeave; } if (!isExpanded) { rowProps.style.display = 'none'; } expandRows.push(React.createElement("tr", _extends({}, rowExternalProps, rowProps), expandIconAsCell && React.createElement("td", { key: EXPAND_KEY }), React.createElement("td", { key: "".concat(EXPAND_KEY, "-rest"), className: "".concat(prefixCls, "-cell"), colSpan: columns.length - (expandIconAsCell ? 1 : 0) }, React.createElement("div", { className: "".concat(prefixCls, "-cell-inner") }, expandedRowRenderer({ dataSet: record.dataSet, record: record }))))); } if (isValidElement(children)) { expandRows.push(cloneElement(children, { isExpanded: isExpanded, key: "".concat(record.key, "-expanded-rows") })); } } return expandRows; } }, { key: "getColumns", value: function getColumns() { var _this2 = this; var _this$props7 = this.props, columns = _this$props7.columns, lock = _this$props7.lock; var tableStore = this.context.tableStore; var prefixCls = tableStore.prefixCls, customizable = tableStore.customizable, rowDraggable = tableStore.rowDraggable, dragColumnAlign = tableStore.dragColumnAlign; var leftWidth = 0; var rightWidth = isStickySupport() && tableStore.overflowX ? tableStore.rightLeafColumnsWidth : 0; var columnLength = columns.length; return columns.map(function (column, index, cols) { var key = getColumnKey(column); if (key !== CUSTOMIZED_KEY) { var colSpan = customizable && lock !== ColumnLock.left && (!rowDraggable || dragColumnAlign !== DragColumnAlign.right) && index === columnLength - 2 ? 2 : 1; var props = { key: key }; if (colSpan > 1) { props.colSpan = colSpan; } if (isStickySupport() && tableStore.overflowX) { var columnLock = getColumnLock(column.lock); if (columnLock === ColumnLock.left) { props.style = { left: pxToRem(leftWidth) }; var next = cols[index + 1]; if (!next || getColumnLock(next.lock) !== ColumnLock.left) { props.className = "".concat(prefixCls, "-cell-fix-left-last"); } leftWidth += columnWidth(column); } else if (columnLock === ColumnLock.right) { rightWidth -= columnWidth(column); var prev = cols[index - 1]; if (!prev || prev.lock !== ColumnLock.right) { props.className = "".concat(prefixCls, "-cell-fix-right-first"); } if (colSpan > 1) { for (var i = 1; i < colSpan; i++) { var _next = cols[index + i]; if (_next) { rightWidth -= columnWidth(_next); } } } props.style = { right: pxToRem(rightWidth) }; } } return _this2.getCell(column, index, props); } return undefined; }); } }, { key: "render", value: function render() { var _classNames, _this3 = this; var _this$props8 = this.props, record = _this$props8.record, lock = _this$props8.lock, hidden = _this$props8.hidden, index = _this$props8.index, provided = _this$props8.provided, className = _this$props8.className; var tableStore = this.context.tableStore; var prefixCls = tableStore.prefixCls, rowHeight = tableStore.rowHeight, overflowX = tableStore.overflowX, highLightRow = tableStore.highLightRow, selectedHighLightRow = tableStore.selectedHighLightRow, mouseBatchChooseIdList = tableStore.mouseBatchChooseIdList, mouseBatchChooseState = tableStore.mouseBatchChooseState, dragColumnAlign = tableStore.dragColumnAlign, rowDraggable = tableStore.rowDraggable, totalLeafColumnsWidth = tableStore.totalLeafColumnsWidth, width = tableStore.width, _tableStore$props3 = tableStore.props, onRow = _tableStore$props3.onRow, rowRenderer = _tableStore$props3.rowRenderer, selectionMode = _tableStore$props3.selectionMode; var key = record.key, id = record.id; var rowExternalProps = _objectSpread({}, typeof rowRenderer === 'function' ? rowRenderer(record, index) : {}, {}, typeof onRow === 'function' ? onRow({ dataSet: record.dataSet, record: record, expandedRow: false, index: index }) : {}); this.rowExternalProps = rowExternalProps; var disabled = isDisabledRow(record); var rowPrefixCls = "".concat(prefixCls, "-row"); var classString = classNames(rowPrefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(rowPrefixCls, "-current"), highLightRow && record.isCurrent), _defineProperty(_classNames, "".concat(rowPrefixCls, "-hover"), highLightRow && this.isHover), _defineProperty(_classNames, "".concat(rowPrefixCls, "-clicked"), highLightRow === HighLightRowType.click && this.isClicked), _defineProperty(_classNames, "".concat(rowPrefixCls, "-highlight"), this.isHighLightRow), _defineProperty(_classNames, "".concat(rowPrefixCls, "-selected"), selectedHighLightRow && isSelectedRow(record)), _defineProperty(_classNames, "".concat(rowPrefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(rowPrefixCls, "-mouse-batch-choose"), mouseBatchChooseState && (mouseBatchChooseIdList || []).includes(id)), _defineProperty(_classNames, "".concat(rowPrefixCls, "-expanded"), this.isExpanded), _classNames), className, // 增加可以自定义类名满足拖拽功能 rowExternalProps.className); var rowProps = { ref: function ref(_ref2) { _this3.saveRef(_ref2); if (provided) { provided.innerRef(_ref2); } }, className: classString, style: _objectSpread({}, rowExternalProps.style), onClick: this.handleClick, onClickCapture: this.handleClickCapture, tabIndex: -1, disabled: disabled, 'data-index': id }; if (!isStickySupport() && overflowX) { rowProps.onMouseEnter = this.handleMouseEnter; rowProps.onMouseLeave = this.handleMouseLeave; } if (hidden) { rowProps.style.display = 'none'; } if (!isStickySupport() && lock && rowHeight === 'auto') { rowProps.style.height = pxToRem(get(tableStore.lockColumnsBodyRowsHeight, key)); } if (selectionMode === SelectionMode.click) { rowProps.onClick = this.handleSelectionByClick; } else if (selectionMode === SelectionMode.dblclick) { rowProps.onDoubleClick = this.handleSelectionByDblClick; } else if (selectionMode === SelectionMode.mousedown) { rowProps.onMouseDown = this.handleSelectionByMouseDown; } if (rowDraggable && provided) { _extends(rowProps, provided.draggableProps); rowProps.style = _objectSpread({}, provided.draggableProps.style, {}, rowExternalProps.style, { width: Math.max(totalLeafColumnsWidth, width) }); if (!dragColumnAlign) { rowProps.style.cursor = 'move'; _extends(rowProps, provided.dragHandleProps); } } var tr = React.createElement("tr", _extends({ key: key }, rowExternalProps, rowProps), this.getColumns()); return [!isStickySupport() && !lock && rowHeight === 'auto' ? React.createElement(ResizeObservedRow, { onResize: this.handleResize, rowIndex: key }, tr) : tr].concat(_toConsumableArray(this.renderExpandRow())); } }, { key: "expandable", get: function get() { var tableStore = this.context.tableStore; var record = this.props.record; var isLeaf = this.rowExternalProps.isLeaf; var expandedRowRenderer = tableStore.props.expandedRowRenderer, isTree = tableStore.isTree, canTreeLoadData = tableStore.canTreeLoadData; if (isLeaf === true) { return false; } return !!expandedRowRenderer || isTree && (!!record.children || canTreeLoadData && !this.isLoaded); } }, { key: "isLoading", get: function get() { var tableStore = this.context.tableStore; var record = this.props.record; return tableStore.isRowPending(record); } }, { key: "isLoaded", get: function get() { var tableStore = this.context.tableStore; var record = this.props.record; return tableStore.isRowLoaded(record); } }, { key: "isExpanded", get: function get() { var tableStore = this.context.tableStore; var record = this.props.record; return tableStore.isRowExpanded(record); }, set: function set(expanded) { var tableStore = this.context.tableStore; var record = this.props.record; tableStore.setRowExpanded(record, expanded); } }, { key: "isHover", get: function get() { var tableStore = this.context.tableStore; var record = this.props.record; return tableStore.isRowHover(record); }, set: function set(hover) { var tableStore = this.context.tableStore; if (tableStore.highLightRow) { var record = this.props.record; tableStore.setRowHover(record, hover); } } }, { key: "isClicked", get: function get() { var tableStore = this.context.tableStore; var record = this.props.record; return tableStore.isRowClick(record); }, set: function set(click) { var tableStore = this.context.tableStore; if (tableStore.highLightRow) { var record = this.props.record; tableStore.setRowClicked(record, click); } } }, { key: "isHighLightRow", get: function get() { var _this$context3 = this.context, highLightRow = _this$context3.tableStore.highLightRow, tableStore = _this$context3.tableStore; if (highLightRow === false) { return false; } if (highLightRow === HighLightRowType.click) { return highLightRow && tableStore.isRowHighLight && this.isClicked; } return highLightRow && tableStore.isRowHighLight; } }]); return TableRow; }(Component); TableRow.displayName = 'TableRow'; TableRow.propTypes = { lock: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf([ColumnLock.right, ColumnLock.left])]), columns: PropTypes.array.isRequired, record: PropTypes.instanceOf(Record).isRequired }; TableRow.contextType = TableContext; __decorate([computed], TableRow.prototype, "expandable", null); __decorate([computed], TableRow.prototype, "isLoading", null); __decorate([computed], TableRow.prototype, "isLoaded", null); __decorate([computed], TableRow.prototype, "isExpanded", null); __decorate([computed], TableRow.prototype, "isHover", null); __decorate([computed], TableRow.prototype, "isClicked", null); __decorate([computed], TableRow.prototype, "isHighLightRow", null); __decorate([autobind, action], TableRow.prototype, "saveRef", null); __decorate([autobind], TableRow.prototype, "handleMouseEnter", null); __decorate([autobind], TableRow.prototype, "handleMouseLeave", null); __decorate([autobind], TableRow.prototype, "handleSelectionByClick", null); __decorate([autobind], TableRow.prototype, "handleSelectionByMouseDown", null); __decorate([autobind], TableRow.prototype, "handleSelectionByDblClick", null); __decorate([autobind], TableRow.prototype, "handleExpandChange", null); __decorate([autobind], TableRow.prototype, "handleClickCapture", null); __decorate([autobind], TableRow.prototype, "handleClick", null); __decorate([autobind], TableRow.prototype, "handleResize", null); __decorate([action], TableRow.prototype, "setRowHeight", null); __decorate([autobind], TableRow.prototype, "getCell", null); __decorate([action], TableRow.prototype, "componentWillUnmount", null); TableRow = __decorate([observer], TableRow); export default TableRow; //# sourceMappingURL=TableRow.js.map