choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
286 lines (248 loc) • 10.3 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
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 * as React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import noop from 'lodash/noop';
import ColumnResizeHandler from './ColumnResizeHandler';
import { defaultClassPrefix, getUnhandledProps, isNullOrUndefined, prefix } from './utils';
import Cell from './Cell';
import TableContext from './TableContext';
var propTypes = {
index: PropTypes.number,
sortColumn: PropTypes.string,
dataIndex: PropTypes.string,
sortType: PropTypes.oneOf(['desc', 'asc']),
sortable: PropTypes.bool,
resizable: PropTypes.bool,
minWidth: PropTypes.number,
onColumnResizeStart: PropTypes.func,
onColumnResizeEnd: PropTypes.func,
onResize: PropTypes.func,
onColumnResizeMove: PropTypes.func,
onSortColumn: PropTypes.func,
flexGrow: PropTypes.number,
fixed: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['left', 'right'])]),
children: PropTypes.node,
onMouseEnterHandler: PropTypes.func,
onMouseLeaveHandler: PropTypes.func
};
var HeaderCell =
/*#__PURE__*/
function (_React$PureComponent) {
_inherits(HeaderCell, _React$PureComponent);
var _super = _createSuper(HeaderCell);
function HeaderCell(props) {
var _this;
_classCallCheck(this, HeaderCell);
_this = _super.call(this, props);
_this.handleColumnResizeStart = function () {
var _this$props = _this.props,
_this$props$left = _this$props.left,
left = _this$props$left === void 0 ? 0 : _this$props$left,
fixed = _this$props.fixed,
onColumnResizeStart = _this$props.onColumnResizeStart,
_this$props$resizeLef = _this$props.resizeLeft,
resizeLeft = _this$props$resizeLef === void 0 ? 0 : _this$props$resizeLef;
onColumnResizeStart === null || onColumnResizeStart === void 0 ? void 0 : onColumnResizeStart(_this.state.columnWidth, left + resizeLeft, !!fixed);
};
_this.handleColumnResizeMove = function (width) {
var left = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var fixed = arguments.length > 2 ? arguments[2] : undefined;
var _this$props2 = _this.props,
_this$props2$onColumn = _this$props2.onColumnResizeMove,
onColumnResizeMove = _this$props2$onColumn === void 0 ? noop : _this$props2$onColumn,
_this$props2$resizeLe = _this$props2.resizeLeft,
resizeLeft = _this$props2$resizeLe === void 0 ? 0 : _this$props2$resizeLe;
onColumnResizeMove(width, left + resizeLeft, fixed);
};
_this.handleColumnResizeEnd = function (columnWidth, cursorDelta) {
var _this$props3 = _this.props,
dataKey = _this$props3.dataKey,
index = _this$props3.index,
onColumnResizeEnd = _this$props3.onColumnResizeEnd,
onResize = _this$props3.onResize;
_this.setState({
columnWidth: columnWidth
});
onColumnResizeEnd === null || onColumnResizeEnd === void 0 ? void 0 : onColumnResizeEnd(columnWidth, cursorDelta, dataKey, index);
onResize === null || onResize === void 0 ? void 0 : onResize(columnWidth, dataKey);
};
_this.handleClick = function () {
if (_this.props.sortable) {
var _this$props$onSortCol, _this$props4;
(_this$props$onSortCol = (_this$props4 = _this.props).onSortColumn) === null || _this$props$onSortCol === void 0 ? void 0 : _this$props$onSortCol.call(_this$props4, _this.props.dataKey);
}
};
_this.handleShowMouseArea = function (left) {
var _this$props5 = _this.props,
_this$props5$onMouseE = _this$props5.onMouseEnterHandler,
onMouseEnterHandler = _this$props5$onMouseE === void 0 ? noop : _this$props5$onMouseE,
fixed = _this$props5.fixed;
onMouseEnterHandler(left, fixed);
}; // @ts-ignore
_this.addPrefix = function (name) {
return prefix(_this.props.classPrefix)(name);
};
_this.state = {
width: props.width,
flexGrow: props.flexGrow,
columnWidth: isNullOrUndefined(props.flexGrow) ? props.width : 0
};
return _this;
}
_createClass(HeaderCell, [{
key: "renderResizeSpanner",
value: function renderResizeSpanner() {
var _this$props6 = this.props,
resizable = _this$props6.resizable,
_this$props6$left = _this$props6.left,
left = _this$props6$left === void 0 ? 0 : _this$props6$left,
onMouseLeaveHandler = _this$props6.onMouseLeaveHandler,
fixed = _this$props6.fixed,
headerHeight = _this$props6.headerHeight,
minWidth = _this$props6.minWidth,
groupCount = _this$props6.groupCount,
children = _this$props6.children,
style = _this$props6.style;
var columnWidth = this.state.columnWidth;
if (!resizable) {
return null;
}
var defaultColumnWidth = columnWidth; // 处理组合列第一列拖拽柄定位问题
if (groupCount && groupCount > 1) {
defaultColumnWidth = children === null || children === void 0 ? void 0 : children.props.children[0].props.width;
}
return React.createElement(ColumnResizeHandler, {
defaultColumnWidth: defaultColumnWidth,
key: columnWidth,
columnLeft: left,
columnFixed: fixed,
height: headerHeight ? headerHeight - 1 : undefined,
minWidth: minWidth,
style: {
top: style ? style.top : 0
},
onColumnResizeMove: this.handleColumnResizeMove,
onColumnResizeStart: this.handleColumnResizeStart,
onColumnResizeEnd: this.handleColumnResizeEnd,
onMouseEnterHandler: this.handleShowMouseArea,
onMouseLeaveHandler: onMouseLeaveHandler
});
}
}, {
key: "renderSortColumn",
value: function renderSortColumn() {
var _this$props7 = this.props,
sortable = _this$props7.sortable,
sortColumn = _this$props7.sortColumn,
_this$props7$sortType = _this$props7.sortType,
sortType = _this$props7$sortType === void 0 ? '' : _this$props7$sortType,
dataKey = _this$props7.dataKey,
groupHeader = _this$props7.groupHeader;
if (sortable && !groupHeader) {
var iconClasses = classNames(this.addPrefix('icon-sort icon'), _defineProperty({}, this.addPrefix("icon-sort-".concat(sortType)), sortColumn === dataKey));
return React.createElement("span", {
className: this.addPrefix('sort-wrapper')
}, React.createElement("i", {
className: iconClasses
}));
}
return null;
}
}, {
key: "render",
value: function render() {
var _this$props8 = this.props,
className = _this$props8.className,
width = _this$props8.width,
dataKey = _this$props8.dataKey,
headerHeight = _this$props8.headerHeight,
children = _this$props8.children,
left = _this$props8.left,
sortable = _this$props8.sortable,
classPrefix = _this$props8.classPrefix,
sortColumn = _this$props8.sortColumn,
sortType = _this$props8.sortType,
groupHeader = _this$props8.groupHeader,
rest = _objectWithoutProperties(_this$props8, ["className", "width", "dataKey", "headerHeight", "children", "left", "sortable", "classPrefix", "sortColumn", "sortType", "groupHeader"]);
var classes = classNames(classPrefix, className, _defineProperty({}, this.addPrefix('sortable'), sortable));
var unhandledProps = getUnhandledProps(propTypes, rest);
var ariaSort;
if (sortColumn === dataKey) {
ariaSort = 'other';
if (sortType === 'asc') {
ariaSort = 'ascending';
} else if (sortType === 'desc') {
ariaSort = 'descending';
}
}
return React.createElement("div", {
className: classes
}, React.createElement(Cell, _extends({
"aria-sort": ariaSort
}, unhandledProps, {
width: width,
dataKey: dataKey,
left: left,
headerHeight: headerHeight,
isHeaderCell: true,
onClick: !groupHeader ? this.handleClick : null
}), children, this.renderSortColumn()), this.renderResizeSpanner());
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.width !== prevState.width || nextProps.flexGrow !== prevState.flexGrow) {
return {
width: nextProps.width,
flexGrow: nextProps.flexGrow,
columnWidth: isNullOrUndefined(nextProps.flexGrow) ? nextProps.width : 0
};
}
return null;
}
}, {
key: "contextType",
get: function get() {
return TableContext;
}
}]);
return HeaderCell;
}(React.PureComponent);
HeaderCell.propTypes = propTypes;
HeaderCell.defaultProps = {
classPrefix: defaultClassPrefix('performance-table-cell-header')
};
export default HeaderCell;
//# sourceMappingURL=HeaderCell.js.map