wetrade-design
Version:
一款多语言支持Vue3的UI框架
155 lines • 6.79 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { createVNode as _createVNode } from "vue";
import ColGroup from '../ColGroup';
import { computed, defineComponent, nextTick, onBeforeUnmount, onMounted, ref, toRef, watch, watchEffect } from 'vue';
import { useInjectTable } from '../context/TableContext';
import classNames from '../../_util/classNames';
import addEventListenerWrap from '../../vc-util/Dom/addEventListener';
function useColumnWidth(colWidthsRef, columCountRef) {
return computed(function () {
var cloneColumns = [];
var colWidths = colWidthsRef.value;
var columCount = columCountRef.value;
for (var i = 0; i < columCount; i += 1) {
var val = colWidths[i];
if (val !== undefined) {
cloneColumns[i] = val;
} else {
return null;
}
}
return cloneColumns;
});
}
export default defineComponent({
name: 'FixedHolder',
inheritAttrs: false,
props: ['columns', 'flattenColumns', 'stickyOffsets', 'customHeaderRow', 'noData', 'maxContentScroll', 'colWidths', 'columCount', 'direction', 'fixHeader', 'stickyTopOffset', 'stickyBottomOffset', 'stickyClassName', 'isScrollBar'],
emits: ['scroll'],
setup: function setup(props, _ref) {
var attrs = _ref.attrs,
slots = _ref.slots,
emit = _ref.emit;
var tableContext = useInjectTable();
var combinationScrollBarSize = computed(function () {
return tableContext.isSticky && !props.fixHeader ? 0 : tableContext.scrollbarSize;
});
var scrollRef = ref();
var onWheel = function onWheel(e) {
if (props.noData) return;
var currentTarget = e.currentTarget,
deltaX = e.deltaX;
if (deltaX) {
emit('scroll', {
currentTarget: currentTarget,
scrollLeft: currentTarget.scrollLeft + deltaX
});
e.preventDefault();
}
};
var wheelEvent = ref();
onMounted(function () {
nextTick(function () {
wheelEvent.value = addEventListenerWrap(scrollRef.value, 'wheel', onWheel);
});
});
onBeforeUnmount(function () {
var _wheelEvent$value;
(_wheelEvent$value = wheelEvent.value) === null || _wheelEvent$value === void 0 ? void 0 : _wheelEvent$value.remove();
});
// Check if all flattenColumns has width
var allFlattenColumnsWithWidth = computed(function () {
return props.flattenColumns.every(function (column) {
return column.width && column.width !== 0 && column.width !== '0px';
});
});
var columnsWithScrollbar = ref([]);
var flattenColumnsWithScrollbar = ref([]);
watchEffect(function () {
// Add scrollbar column
// const lastColumn = props.flattenColumns[props.flattenColumns.length - 1];
// const ScrollBarColumn: ColumnType<unknown> & { scrollbar: true } = {
// fixed: lastColumn ? lastColumn.fixed : null,
// scrollbar: true,
// customHeaderCell: () => ({
// class: `${tableContext.prefixCls}-cell-scrollbar`,
// }),
// };
// columnsWithScrollbar.value = combinationScrollBarSize.value
// ? [...props.columns, ScrollBarColumn]
// : props.columns;
// flattenColumnsWithScrollbar.value = combinationScrollBarSize.value
// ? [...props.flattenColumns, ScrollBarColumn]
// : props.flattenColumns;
columnsWithScrollbar.value = props.columns;
flattenColumnsWithScrollbar.value = props.flattenColumns;
});
// Calculate the sticky offsets
var headerStickyOffsets = computed(function () {
var stickyOffsets = props.stickyOffsets,
direction = props.direction,
isScrollBar = props.isScrollBar;
var right = stickyOffsets.right,
left = stickyOffsets.left;
return _objectSpread(_objectSpread({}, stickyOffsets), {}, {
left: direction === 'rtl' ? [].concat(_toConsumableArray(left.map(function (width) {
return width + (isScrollBar ? combinationScrollBarSize.value : 0);
})), [0]) : left,
right: direction === 'rtl' ? right : [].concat(_toConsumableArray(right.map(function (width) {
return width + (isScrollBar ? combinationScrollBarSize.value : 0);
})), [0]),
isSticky: tableContext.isSticky
});
});
var mergedColumnWidth = useColumnWidth(toRef(props, 'colWidths'), toRef(props, 'columCount'));
watch(function () {
return mergedColumnWidth.value;
}, function () {
var _mergedColumnWidth$va;
if (!scrollRef.value) return;
var mergedColumnTotalWidth = (_mergedColumnWidth$va = mergedColumnWidth.value) === null || _mergedColumnWidth$va === void 0 ? void 0 : _mergedColumnWidth$va.reduce(function (prev, curr) {
return prev + curr;
}, 0);
// 解决拖拽后出现多余的占位单元格导致表头偏移问题
if (mergedColumnTotalWidth <= scrollRef.value.offsetWidth) {
scrollRef.value.scrollLeft = 0;
}
});
return function () {
var _slots$default;
var noData = props.noData,
columCount = props.columCount,
stickyTopOffset = props.stickyTopOffset,
stickyBottomOffset = props.stickyBottomOffset,
stickyClassName = props.stickyClassName,
maxContentScroll = props.maxContentScroll,
isScrollBar = props.isScrollBar;
var isSticky = tableContext.isSticky;
return _createVNode("div", {
"style": _objectSpread({
overflow: 'hidden'
}, isSticky ? {
top: "".concat(stickyTopOffset, "px"),
bottom: "".concat(stickyBottomOffset, "px")
} : {}),
"ref": scrollRef,
"class": classNames(attrs.class, _defineProperty({}, stickyClassName, !!stickyClassName))
}, [_createVNode("table", {
"style": {
tableLayout: 'fixed',
visibility: noData || mergedColumnWidth.value ? null : 'hidden'
}
}, [(!noData || !maxContentScroll || allFlattenColumnsWithWidth.value) && _createVNode(ColGroup, {
"colWidths": mergedColumnWidth.value ? [].concat(_toConsumableArray(mergedColumnWidth.value), [isScrollBar ? combinationScrollBarSize.value : 0]) : [],
"columCount": columCount + 1,
"columns": flattenColumnsWithScrollbar.value
}, null), (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots, _objectSpread(_objectSpread({}, props), {}, {
stickyOffsets: headerStickyOffsets.value,
columns: columnsWithScrollbar.value,
flattenColumns: flattenColumnsWithScrollbar.value
}))])]);
};
}
});