Skip to content

fix(component->table->cell):fix the table component rendering when hover #7451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions components/vc-table/Cell/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import classNames from '../../_util/classNames';
import { filterEmpty, flattenChildren, isValidElement } from '../../_util/props-util';
import type { CSSProperties, VNodeArrayChildren } from 'vue';
import { Text, computed, defineComponent, isVNode } from 'vue';

import { Text, computed, defineComponent, isVNode, ref, watch } from 'vue';
import eagerComputed from '../../_util/eagerComputed';
import type {
DataIndex,
ColumnType,
Expand All @@ -22,7 +22,6 @@ import { useInjectHover } from '../context/HoverContext';
import { useInjectSticky } from '../context/StickyContext';
import { warning } from '../../vc-util/warning';
import type { MouseEventHandler } from '../../_util/EventInterface';
import eagerComputed from '../../_util/eagerComputed';
import { customRenderSlot } from '../../_util/vnode';

/** Check if cell is in hover range */
Expand Down Expand Up @@ -157,6 +156,29 @@ export default defineComponent<CellProps>({
return vnode;
}
};

// AddEventListener Hover
const hoverRef = ref(null);
let componentPropsCommonClassName;
let additionalProps;
let cellPrefixCls;
let cellProps: CellType;
let cellClassName;
watch([rowSpan, startRow, endRow], () => {
hoverRef.value?.setAttribute(
'class',
classNames(
cellPrefixCls,
{
...componentPropsCommonClassName,
[`${cellPrefixCls}-row-hover`]: !cellProps && hovering.value,
},
additionalProps.class,
cellClassName,
),
);
});

return () => {
const {
prefixCls,
Expand All @@ -173,18 +195,16 @@ export default defineComponent<CellProps>({
firstFixRight,
lastFixRight,
appendNode = slots.appendNode?.(),
additionalProps = {},
ellipsis,
align,
rowType,
isSticky,
column = {},
cellType,
} = props;
const cellPrefixCls = `${prefixCls}-cell`;

cellPrefixCls = `${prefixCls}-cell`;
additionalProps = props.additionalProps ? props.additionalProps : {};
// ==================== Child Node ====================
let cellProps: CellType;
let childNode;
const children = slots.default?.();
if (validateValue(children) || cellType === 'header') {
Expand Down Expand Up @@ -273,12 +293,11 @@ export default defineComponent<CellProps>({
colSpan: cellColSpan,
rowSpan: cellRowSpan,
style: cellStyle,
class: cellClassName,
...restCellProps
} = cellProps || {};
cellClassName = cellProps ? cellProps.class : '';
const mergedColSpan = (cellColSpan !== undefined ? cellColSpan : colSpan.value) ?? 1;
const mergedRowSpan = (cellRowSpan !== undefined ? cellRowSpan : rowSpan.value) ?? 1;

if (mergedColSpan === 0 || mergedRowSpan === 0) {
return null;
}
Expand Down Expand Up @@ -315,6 +334,19 @@ export default defineComponent<CellProps>({
}
}

componentPropsCommonClassName = {
[`${cellPrefixCls}-fix-left`]: isFixLeft && supportSticky.value,
[`${cellPrefixCls}-fix-left-first`]: firstFixLeft && supportSticky.value,
[`${cellPrefixCls}-fix-left-last`]: lastFixLeft && supportSticky.value,
[`${cellPrefixCls}-fix-right`]: isFixRight && supportSticky.value,
[`${cellPrefixCls}-fix-right-first`]: firstFixRight && supportSticky.value,
[`${cellPrefixCls}-fix-right-last`]: lastFixRight && supportSticky.value,
[`${cellPrefixCls}-ellipsis`]: ellipsis,
[`${cellPrefixCls}-with-append`]: appendNode,
[`${cellPrefixCls}-fix-sticky`]:
(isFixLeft || isFixRight) && isSticky && supportSticky.value,
};

const componentProps = {
title,
...restCellProps,
Expand All @@ -323,19 +355,7 @@ export default defineComponent<CellProps>({
rowSpan: mergedRowSpan !== 1 ? mergedRowSpan : null,
class: classNames(
cellPrefixCls,
{
[`${cellPrefixCls}-fix-left`]: isFixLeft && supportSticky.value,
[`${cellPrefixCls}-fix-left-first`]: firstFixLeft && supportSticky.value,
[`${cellPrefixCls}-fix-left-last`]: lastFixLeft && supportSticky.value,
[`${cellPrefixCls}-fix-right`]: isFixRight && supportSticky.value,
[`${cellPrefixCls}-fix-right-first`]: firstFixRight && supportSticky.value,
[`${cellPrefixCls}-fix-right-last`]: lastFixRight && supportSticky.value,
[`${cellPrefixCls}-ellipsis`]: ellipsis,
[`${cellPrefixCls}-with-append`]: appendNode,
[`${cellPrefixCls}-fix-sticky`]:
(isFixLeft || isFixRight) && isSticky && supportSticky.value,
[`${cellPrefixCls}-row-hover`]: !cellProps && hovering.value,
},
componentPropsCommonClassName,
additionalProps.class,
cellClassName,
),
Expand All @@ -347,7 +367,7 @@ export default defineComponent<CellProps>({
};

return (
<Component {...componentProps}>
<Component {...componentProps} ref={hoverRef}>
{appendNode}
{childNode}
{slots.dragHandle?.()}
Expand Down