tdesign-mobile-vue
Version:
tdesign-mobile-vue
309 lines (305 loc) • 14.4 kB
JavaScript
/**
* tdesign v1.9.3
* (c) 2025 TDesign Group
* @license MIT
*/
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, ref, h, computed, createVNode, mergeProps } from 'vue';
import { isFunction, get, isString } from 'lodash-es';
import baseTableProps from './base-table-props.js';
import config from '../config.js';
import useClassName from './hooks/useClassName.js';
import useStyle, { formatCSSUnit } from './hooks/useStyle.js';
import useFixed, { getColumnFixedStyles, getRowFixedStyles } from './hooks/useFixed.js';
import { Loading } from '../loading/index.js';
import { useConfig } from '../config-provider/useConfig.js';
import { useTNodeJSX } from '../hooks/tnode.js';
import { formatRowClassNames, formatRowAttributes, formatClassNames } from './utils/index.js';
import '../hooks/useClass.js';
import '@babel/runtime/helpers/slicedToArray';
import '../_common/js/log/log.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/setStyle.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../loading/icon/spinner.js';
import '../loading/props.js';
import '../shared/dom.js';
import '../loading/plugin.js';
import '../hooks/render-tnode.js';
import '../_common/js/utils/general.js';
import '../config-provider/context.js';
import '../_common/js/global-config/mobile/default-config.js';
import '../_common/js/global-config/mobile/locale/zh_CN.js';
import '../_chunks/dep-f785f3d3.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-0d44b254.js';
import 'dayjs';
import '../shared/component.js';
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var prefix = config.prefix;
var BaseTable = defineComponent({
name: "".concat(prefix, "-base-table"),
props: baseTableProps,
emits: ["cell-click", "row-click", "scroll"],
setup: function setup(props, context) {
var tableRef = ref();
var theadRef = ref();
var tableElmRef = ref();
var renderTNodeJSX = useTNodeJSX();
var _useClassName = useClassName(),
classPrefix = _useClassName.classPrefix,
tableLayoutClasses = _useClassName.tableLayoutClasses,
tableHeaderClasses = _useClassName.tableHeaderClasses,
tableBaseClass = _useClassName.tableBaseClass,
tdAlignClasses = _useClassName.tdAlignClasses,
tdEllipsisClass = _useClassName.tdEllipsisClass,
tableRowFixedClasses = _useClassName.tableRowFixedClasses,
tableColFixedClasses = _useClassName.tableColFixedClasses;
var _useConfig = useConfig("table"),
globalConfig = _useConfig.globalConfig,
t = _useConfig.t;
var defaultLoadingContent = h(Loading, _objectSpread({}, props.loadingProps));
var _useStyle = useStyle(props),
tableClasses = _useStyle.tableClasses,
tableContentStyles = _useStyle.tableContentStyles,
tableElementStyles = _useStyle.tableElementStyles;
var _useFixed = useFixed(props),
rowAndColFixedPosition = _useFixed.rowAndColFixedPosition,
tableContentRef = _useFixed.tableContentRef,
isFixedColumn = _useFixed.isFixedColumn,
isFixedHeader = _useFixed.isFixedHeader,
showColumnShadow = _useFixed.showColumnShadow,
refreshTable = _useFixed.refreshTable,
updateColumnFixedShadow = _useFixed.updateColumnFixedShadow;
var defaultColWidth = props.tableLayout === "fixed" ? "80px" : void 0;
var theadClasses = computed(function () {
return [tableHeaderClasses.header, _defineProperty(_defineProperty({}, tableHeaderClasses.fixed, Boolean(props.maxHeight || props.height)), tableBaseClass.bordered, props.bordered)];
});
var tbodyClasses = computed(function () {
return [tableBaseClass.body];
});
var ellipsisClasses = computed(function () {
return ["".concat(classPrefix, "-table__ellipsis"), "".concat(classPrefix, "-text-ellipsis")];
});
var handleRowClick = function handleRowClick(row, rowIndex, e) {
var _props$onRowClick;
(_props$onRowClick = props.onRowClick) === null || _props$onRowClick === void 0 || _props$onRowClick.call(props, {
row: row,
index: rowIndex,
e: e
});
};
var handleCellClick = function handleCellClick(row, col, rowIndex, colIndex, e) {
var _props$onCellClick;
if (col.stopPropagation) {
e.stopPropagation();
}
(_props$onCellClick = props.onCellClick) === null || _props$onCellClick === void 0 || _props$onCellClick.call(props, {
row: row,
col: col,
rowIndex: rowIndex,
colIndex: colIndex,
e: e
});
};
var dynamicBaseTableClasses = computed(function () {
return [_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, tableBaseClass.headerFixed, isFixedHeader.value), tableBaseClass.columnFixed, isFixedColumn.value), tableColFixedClasses.leftShadow, showColumnShadow.left), tableColFixedClasses.rightShadow, showColumnShadow.right), tableClasses.value];
});
var tableElmClasses = computed(function () {
return [[tableLayoutClasses[props.tableLayout || "fixed"]]];
});
var renderCell = function renderCell(params, cellEmptyContent) {
var _context$slots;
var col = params.col,
row = params.row,
rowIndex = params.rowIndex;
if (col.colKey === "serial-number") {
return rowIndex + 1;
}
if (isFunction(col.cell)) {
return col.cell(h, params);
}
if (context.slots[col.colKey]) {
return context.slots[col.colKey](params);
}
if (isString(col.cell) && (_context$slots = context.slots) !== null && _context$slots !== void 0 && _context$slots[col.cell]) {
return context.slots[col.cell](params);
}
var r = get(row, col.colKey);
if (![void 0, "", null].includes(r)) return r;
if (cellEmptyContent) {
return isFunction(cellEmptyContent) ? cellEmptyContent(h, params) : cellEmptyContent;
}
if (context.slots.cellEmptyContent) return context.slots.cellEmptyContent(params);
if (context.slots["cell-empty-content"]) return context.slots["cell-empty-content"](params);
return r;
};
var loadingClasses = computed(function () {
return ["".concat(classPrefix, "-table__loading--full")];
});
var onInnerVirtualScroll = function onInnerVirtualScroll(e) {
var _props$onScroll;
var target = e.target || e.srcElement;
updateColumnFixedShadow(target);
(_props$onScroll = props.onScroll) === null || _props$onScroll === void 0 || _props$onScroll.call(props, {
params: e
});
};
var tdClassName = function tdClassName(td_item, extra) {
var className = "";
if (td_item.ellipsis) {
className = tdEllipsisClass;
}
if (td_item.align && td_item.align !== "left") {
className = "".concat(className, " ").concat(tdAlignClasses["".concat(td_item.align)]);
}
return [className].concat(_toConsumableArray(extra));
};
var colStyle = function colStyle(col_item) {
return {
width: "".concat(formatCSSUnit(col_item.width || defaultColWidth)),
minWidth: "".concat(!formatCSSUnit(col_item.width || defaultColWidth) && !col_item.minWidth && props.tableLayout === "fixed" ? "80px" : formatCSSUnit(col_item.minWidth))
};
};
var thClassName = function thClassName(item_th, extra) {
var className = "";
if (item_th.colKey) {
className = "".concat(classPrefix, "-table__th-").concat(item_th.colKey);
}
if (item_th.ellipsisTitle || item_th.ellipsis) {
className = "".concat(className, " ").concat(tdEllipsisClass);
}
if (item_th.align && item_th.align !== "left") {
className = "".concat(className, " ").concat(tdAlignClasses["".concat(item_th.align)]);
}
return [className, extra];
};
var renderTitle = function renderTitle(item_th, index) {
if (isFunction(item_th === null || item_th === void 0 ? void 0 : item_th.title)) {
return item_th === null || item_th === void 0 ? void 0 : item_th.title(h, {
col: item_th,
colIndex: index
});
}
return item_th === null || item_th === void 0 ? void 0 : item_th.title;
};
var renderTableBody = function renderTableBody() {
var _props$data, _props$data2;
var renderContentEmpty = renderTNodeJSX("empty") || t(globalConfig.value.empty);
if (!((_props$data = props.data) !== null && _props$data !== void 0 && _props$data.length) && renderContentEmpty) {
var _props$columns;
return createVNode("tr", {
"class": tableBaseClass.emptyRow
}, [createVNode("td", {
"colspan": (_props$columns = props.columns) === null || _props$columns === void 0 ? void 0 : _props$columns.length
}, [createVNode("div", {
"class": tableBaseClass.empty
}, [renderContentEmpty])])]);
}
if ((_props$data2 = props.data) !== null && _props$data2 !== void 0 && _props$data2.length) {
var _props$data3;
return (_props$data3 = props.data) === null || _props$data3 === void 0 ? void 0 : _props$data3.map(function (tr_item, tr_index) {
var _props$data4, _props$columns2;
var _getRowFixedStyles = getRowFixedStyles(get(tr_item, props.rowKey || "id"), tr_index, ((_props$data4 = props.data) === null || _props$data4 === void 0 ? void 0 : _props$data4.length) || 0, props.fixedRows, rowAndColFixedPosition.value, tableRowFixedClasses),
style = _getRowFixedStyles.style,
classes = _getRowFixedStyles.classes;
var customClasses = formatRowClassNames(props.rowClassName, {
row: tr_item,
rowKey: props.rowKey,
rowIndex: tr_index,
type: "body"
}, props.rowKey || "id");
var trAttributes = formatRowAttributes(props.rowAttributes, {
row: tr_item,
rowIndex: tr_index,
type: "body"
}) || {};
return createVNode("tr", mergeProps(trAttributes, {
"key": tr_index,
"style": style,
"class": [classes, customClasses],
"onClick": function onClick($event) {
handleRowClick(tr_item, tr_index, $event);
}
}), [(_props$columns2 = props.columns) === null || _props$columns2 === void 0 ? void 0 : _props$columns2.map(function (td_item, td_index) {
var tdStyles = getColumnFixedStyles(td_item, td_index, rowAndColFixedPosition.value, tableColFixedClasses);
var customClasses2 = formatClassNames(td_item.className, {
col: td_item,
colIndex: td_index,
row: tr_item,
rowIndex: tr_index,
type: "td"
});
return createVNode("td", {
"key": td_index,
"style": tdStyles.style,
"class": tdClassName(td_item, [tdStyles.classes, customClasses2]),
"onClick": function onClick($event) {
handleCellClick(tr_item, td_item, tr_index, td_index, $event);
}
}, [createVNode("div", {
"class": td_item.ellipsis && ellipsisClasses.value
}, [renderCell({
row: tr_item,
col: td_item,
rowIndex: tr_index,
colIndex: td_index
}, props.cellEmptyContent)])]);
})]);
});
}
};
context.expose({
refreshTable: refreshTable
});
return function () {
var _props$columns3, _props$columns4;
var renderLoading = renderTNodeJSX("loading", {
defaultNode: defaultLoadingContent
});
return createVNode("div", {
"ref": tableRef,
"class": dynamicBaseTableClasses.value,
"style": "position: relative"
}, [createVNode("div", {
"ref": tableContentRef,
"class": tableBaseClass.content,
"style": tableContentStyles.value,
"onScroll": onInnerVirtualScroll
}, [createVNode("table", {
"ref": tableElmRef,
"class": tableElmClasses.value,
"style": tableElementStyles.value
}, [createVNode("colgroup", null, [(_props$columns3 = props.columns) === null || _props$columns3 === void 0 ? void 0 : _props$columns3.map(function (col_item) {
return createVNode("col", {
"key": col_item.colKey,
"style": colStyle(col_item)
}, null);
})]), props.showHeader && createVNode("thead", {
"ref": theadRef,
"class": theadClasses.value
}, [createVNode("tr", null, [(_props$columns4 = props.columns) === null || _props$columns4 === void 0 ? void 0 : _props$columns4.map(function (item_th, index_th) {
var thStyles = getColumnFixedStyles(item_th, index_th, rowAndColFixedPosition.value, tableColFixedClasses);
return createVNode("th", {
"key": index_th,
"class": thClassName(item_th, thStyles.classes),
"style": thStyles.style,
"data-colKey": item_th.colKey
}, [createVNode("div", {
"class": (item_th.ellipsisTitle || item_th.ellipsis) && ellipsisClasses.value
}, [renderTitle(item_th, index_th)])]);
})])]), createVNode("tbody", {
"class": tbodyClasses.value
}, [renderTableBody()])]), renderLoading && createVNode("div", {
"class": loadingClasses.value
}, [renderLoading])])]);
};
}
});
export { BaseTable as default };
//# sourceMappingURL=base-table.js.map