@douyinfe/semi-ui
Version:
A modern, comprehensive, flexible design system and UI library. Connect DesignOps & DevOps. Quickly build beautiful React apps. Maintained by Douyin-fe team.
233 lines (232 loc) • 9.38 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _findIndex2 = _interopRequireDefault(require("lodash/findIndex"));
var _omit2 = _interopRequireDefault(require("lodash/omit"));
var _set2 = _interopRequireDefault(require("lodash/set"));
var _map2 = _interopRequireDefault(require("lodash/map"));
var _noop2 = _interopRequireDefault(require("lodash/noop"));
var _get2 = _interopRequireDefault(require("lodash/get"));
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _constants = require("@douyinfe/semi-foundation/lib/cjs/table/constants");
var _utils = require("@douyinfe/semi-foundation/lib/cjs/table/utils");
var _baseComponent = _interopRequireDefault(require("../_base/baseComponent"));
var _tableContext = _interopRequireDefault(require("./table-context"));
var _tooltip = _interopRequireDefault(require("../tooltip"));
var _localeConsumer = _interopRequireDefault(require("../locale/localeConsumer"));
var _utils2 = require("./utils");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
var __rest = void 0 && (void 0).__rest || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
class TableHeaderRow extends _baseComponent.default {
get adapter() {
return Object.assign({}, super.adapter);
}
constructor(props) {
super(props);
this.cacheRef = node => {
this.headerNode = node;
if (node && this.context.setHeadWidths) {
const {
prefixCls,
row,
index
} = this.props;
const cellSelector = `.${prefixCls}-row-head`;
const heads = node && node.querySelectorAll && node.querySelectorAll(cellSelector);
this.context.setHeadWidths((0, _map2.default)(heads, (head, headIndex) => {
let configWidth = (0, _get2.default)(row, [headIndex, 'column', 'width']);
const key = (0, _get2.default)(row, [headIndex, 'column', 'key']);
if (typeof configWidth !== 'number') {
configWidth = head && head.getBoundingClientRect().width || 0;
}
return {
width: configWidth,
key
};
}), index);
}
};
this.headerNode = null;
}
componentDidUpdate(prevProps) {
if (prevProps.columns !== this.props.columns && this.headerNode) {
this.cacheRef(this.headerNode);
}
}
render() {
const {
components,
row,
prefixCls,
onHeaderRow,
index,
style,
columns
} = this.props;
const {
getCellWidths,
direction
} = this.context;
const isRTL = direction === 'rtl';
const slicedColumns = (0, _utils.sliceColumnsByLevel)(columns, index);
const headWidths = getCellWidths(slicedColumns);
const HeaderRow = (0, _get2.default)(components, 'header.row', 'tr');
const HeaderCell = (0, _get2.default)(components, 'header.cell', 'th');
const rowProps = onHeaderRow(columns, index) || {};
(0, _set2.default)(rowProps, 'className', (0, _classnames.default)((0, _get2.default)(rowProps, 'className'), `${prefixCls}-row`));
const cells = (0, _map2.default)(row, (cell, cellIndex) => {
const {
column
} = cell,
cellProps = __rest(cell, ["column"]);
const customProps = typeof column.onHeaderCell === 'function' ? column.onHeaderCell(column, cellIndex, index) : {};
let cellStyle = Object.assign({}, customProps.style);
if (column.align) {
const textAlign = (0, _utils.getRTLAlign)(column.align, direction);
cellStyle = Object.assign(Object.assign({}, cellStyle), {
textAlign
});
customProps.className = (0, _classnames.default)(customProps.className, column.className, {
[`${prefixCls}-align-${textAlign}`]: Boolean(textAlign)
});
}
let fixedLeft, fixedRight, fixedLeftLast, fixedRightFirst;
if (isRTL) {
fixedLeft = (0, _utils.isFixedRight)(column);
fixedRight = (0, _utils.isFixedLeft)(column);
fixedLeftLast = (0, _utils.isFirstFixedRight)(slicedColumns, column);
fixedRightFirst = (0, _utils.isLastLeftFixed)(slicedColumns, column);
} else {
fixedLeft = (0, _utils.isFixedLeft)(column);
fixedRight = (0, _utils.isFixedRight)(column);
fixedLeftLast = (0, _utils.isLastLeftFixed)(slicedColumns, column);
fixedRightFirst = (0, _utils.isFirstFixedRight)(slicedColumns, column);
}
customProps.className = (0, _classnames.default)(`${prefixCls}-row-head`, column.className, customProps.className,
// `${prefixCls}-fixed-columns`,
{
[`${prefixCls}-cell-fixed-left`]: fixedLeft,
[`${prefixCls}-cell-fixed-left-last`]: fixedLeftLast,
[`${prefixCls}-cell-fixed-right`]: fixedRight,
[`${prefixCls}-cell-fixed-right-first`]: fixedRightFirst,
[`${prefixCls}-row-head-ellipsis`]: column.ellipsis,
[`${prefixCls}-row-head-clickSort`]: column.clickToSort
});
if (headWidths.length && slicedColumns.length) {
const indexOfSlicedColumns = (0, _findIndex2.default)(slicedColumns, item => item && item.key != null && item.key === column.key);
if (indexOfSlicedColumns > -1) {
if ((0, _utils.isFixedLeft)(column)) {
const xPositionKey = isRTL ? 'right' : 'left';
cellStyle = Object.assign(Object.assign({}, cellStyle), {
position: 'sticky',
[xPositionKey]: (0, _utils.arrayAdd)(headWidths, 0, indexOfSlicedColumns)
});
} else if ((0, _utils.isFixedRight)(column)) {
const xPositionKey = isRTL ? 'left' : 'right';
cellStyle = Object.assign(Object.assign({}, cellStyle), {
position: 'sticky',
[xPositionKey]: (0, _utils.arrayAdd)(headWidths, indexOfSlicedColumns + 1)
});
}
}
}
Object.assign(cellProps, {
resize: column.resize
});
const props = (0, _omit2.default)(Object.assign(Object.assign({}, cellProps), customProps), ['colStart', 'colEnd', 'hasSubColumns', 'parents', 'level']);
const {
rowSpan,
colSpan
} = props;
if (rowSpan === 0 || colSpan === 0) {
return null;
}
if (typeof column.clickToSort === 'function') {
if (props.onClick) {
const onClick = props.onClick;
props.onClick = e => {
onClick(e);
column.clickToSort(e);
};
} else {
props.onClick = column.clickToSort;
}
}
if (typeof column.mouseDown === 'function') {
if (props.onMouseDown) {
const onMouseDown = props.onMouseDown;
props.onMouseDown = e => {
onMouseDown(e);
column.mouseDown(e);
};
} else {
props.onMouseDown = column.mouseDown;
}
}
const headerCellNode = /*#__PURE__*/_react.default.createElement(HeaderCell, Object.assign({
role: "columnheader",
"aria-colindex": cellIndex + 1
}, props, {
style: cellStyle,
key: column.key || column.dataIndex || cellIndex
}));
if (typeof column.clickToSort === 'function' && column.showSortTip === true) {
let content = (0, _utils2.getNextSortOrder)(column.sortOrder);
return /*#__PURE__*/_react.default.createElement(_localeConsumer.default, {
componentName: "Table",
key: column.key || column.dataIndex || cellIndex
}, (locale, localeCode) => (/*#__PURE__*/_react.default.createElement(_tooltip.default, {
content: locale[content]
}, headerCellNode)));
}
return headerCellNode;
});
return (
/*#__PURE__*/
// @ts-ignore no need to do complex ts type checking and qualification
_react.default.createElement(HeaderRow, Object.assign({
role: "row",
"aria-rowindex": index + 1
}, rowProps, {
style: style,
ref: this.cacheRef
}), cells)
);
}
}
exports.default = TableHeaderRow;
TableHeaderRow.contextType = _tableContext.default;
TableHeaderRow.propTypes = {
components: _propTypes.default.object,
row: _propTypes.default.array,
prefixCls: _propTypes.default.string,
onHeaderRow: _propTypes.default.func,
index: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
style: _propTypes.default.object,
columns: _propTypes.default.array,
fixed: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.string]),
selectedRowKeysSet: _propTypes.default.instanceOf(Set).isRequired
};
TableHeaderRow.defaultProps = {
onHeaderRow: _noop2.default,
prefixCls: _constants.cssClasses.PREFIX,
columns: [],
components: {
header: {
wrapper: 'thead',
row: 'tr',
cell: 'th'
}
}
};
;