diff --git a/packages/components/table/__tests__/table.test.tsx b/packages/components/table/__tests__/table.test.tsx
index c8d77c4600..d0db105cc9 100644
--- a/packages/components/table/__tests__/table.test.tsx
+++ b/packages/components/table/__tests__/table.test.tsx
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
-import { fireEvent, mockDelay, render, vi, waitFor } from '@test/utils';
+import { createEvent, fireEvent, mockDelay, mockElementSizes, render, vi, waitFor } from '@test/utils';
import { Table } from '..';
@@ -58,6 +58,35 @@ function paginationTable(props: PaginationMountProps, options?: { remote?: boole
return
;
}
+const RESIZE_GROUP_COLUMNS = [
+ { colKey: 'category', title: '普通列', width: 100 },
+ {
+ colKey: 'metrics',
+ title: '分组表头',
+ children: [
+ { colKey: 'valueA', title: '子列 A', width: 100 },
+ { colKey: 'valueB', title: '子列 B', width: 100 },
+ ],
+ },
+];
+const RESIZE_GROUP_DATA = [{ id: 1, category: '示例 A', valueA: 10, valueB: 20 }];
+
+function fireColumnResizeMouseEvent(
+ target: Element | Document,
+ type: 'mouseMove' | 'mouseDown' | 'mouseUp',
+ x: number,
+ buttons = 0,
+) {
+ const event = createEvent[type](target, {
+ button: 0,
+ buttons,
+ clientX: x,
+ clientY: 20,
+ });
+ Object.defineProperties(event, { pageX: { value: x }, x: { value: x } });
+ fireEvent(target, event);
+}
+
function mockScrollHeight(element: HTMLElement, scrollHeight: number, clientHeight: number) {
Object.defineProperty(element, 'scrollHeight', {
value: scrollHeight,
@@ -1075,6 +1104,8 @@ describe('Table', () => {
describe('Virtual scroll', () => {
const descriptors: Array<[string, PropertyDescriptor]> = [];
const { scrollTo } = HTMLElement.prototype;
+ const hadOwnBoundingRect = Object.prototype.hasOwnProperty.call(HTMLElement.prototype, 'getBoundingClientRect');
+ const originalGetBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;
function mockSize(prop: string, value: number) {
const origin = Object.getOwnPropertyDescriptor(HTMLElement.prototype, prop);
@@ -1111,6 +1142,11 @@ describe('Table', () => {
afterAll(() => {
descriptors.forEach(([prop, descriptor]) => Object.defineProperty(HTMLElement.prototype, prop, descriptor));
HTMLElement.prototype.scrollTo = scrollTo;
+ if (hadOwnBoundingRect) {
+ HTMLElement.prototype.getBoundingClientRect = originalGetBoundingClientRect;
+ } else {
+ delete HTMLElement.prototype.getBoundingClientRect;
+ }
});
it('filter and virtual header rendering keep a single popup instance', async () => {
@@ -1237,6 +1273,204 @@ describe('Table', () => {
});
});
+ describe('resizable', () => {
+ let restoreSizes: () => void;
+
+ beforeEach(() => {
+ vi.useFakeTimers();
+ restoreSizes = mockElementSizes([
+ { selector: '.t-table', width: 300, height: 200 },
+ { selector: '.t-table__content', width: 300, height: 200 },
+ { selector: 'th[data-colkey="category"]', width: 100, height: 80 },
+ {
+ selector: 'th[data-colkey="metrics"]',
+ width: 200,
+ height: 40,
+ left: 100,
+ },
+ {
+ selector: 'th[data-colkey="valueA"]',
+ width: 100,
+ height: 40,
+ left: 100,
+ top: 40,
+ },
+ {
+ selector: 'th[data-colkey="valueB"]',
+ width: 100,
+ height: 40,
+ left: 200,
+ top: 40,
+ },
+ ]);
+ });
+
+ afterEach(() => {
+ vi.clearAllTimers();
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ restoreSizes();
+ });
+
+ it.each([true, false])(
+ 'should not start resize drag on adjacent group header (mousemove: %s)',
+ (shouldMoveOverGroup) => {
+ const onColumnResizeChange = vi.fn();
+ const { container } = render(
+ ,
+ );
+ const leaf = container.querySelector('th[data-colkey="category"]');
+ const group = container.querySelector('th[data-colkey="metrics"]');
+ const originalSelectStart = document.onselectstart;
+ const originalDragStart = document.ondragstart;
+
+ fireColumnResizeMouseEvent(leaf, 'mouseMove', 99);
+ expect(leaf).toHaveStyle({ cursor: 'col-resize' });
+ if (shouldMoveOverGroup) {
+ fireColumnResizeMouseEvent(group, 'mouseMove', 101);
+ }
+
+ const addListener = vi.spyOn(document, 'addEventListener');
+ try {
+ fireColumnResizeMouseEvent(group, 'mouseDown', 101, 1);
+ expect(addListener.mock.calls.filter(([type]) => type === 'mouseup' || type === 'mousemove')).toHaveLength(
+ 0,
+ );
+ fireColumnResizeMouseEvent(document, 'mouseUp', 119);
+ expect(onColumnResizeChange).not.toHaveBeenCalled();
+ expect(document.onselectstart).toBe(originalSelectStart);
+ expect(document.ondragstart).toBe(originalDragStart);
+ } finally {
+ addListener.mock.calls.forEach(([type, listener, options]) => {
+ if (type === 'mouseup' || type === 'mousemove') document.removeEventListener(type, listener, options);
+ });
+ addListener.mockRestore();
+ document.onselectstart = originalSelectStart;
+ document.ondragstart = originalDragStart;
+ }
+ },
+ );
+
+ it('should preserve custom cursor on group header', () => {
+ const { container } = render(
+ ,
+ );
+ const leaf = container.querySelector('th[data-colkey="category"]');
+ const group = container.querySelector('th[data-colkey="metrics"]');
+ expect(group).toHaveStyle({ cursor: 'pointer' });
+ fireColumnResizeMouseEvent(leaf, 'mouseMove', 99);
+ fireColumnResizeMouseEvent(group, 'mouseMove', 101);
+ expect(group).toHaveStyle({ cursor: 'pointer' });
+ });
+
+ it.each([
+ {
+ name: 'from right edge',
+ colKey: 'category',
+ start: 99,
+ end: 119,
+ moveOverGroup: false,
+ widths: { category: 120, metrics: 200, valueA: 80, valueB: 100 },
+ },
+ {
+ name: 'from left edge',
+ colKey: 'valueB',
+ start: 201,
+ end: 221,
+ moveOverGroup: false,
+ widths: { category: 100, metrics: 200, valueA: 120, valueB: 80 },
+ },
+ {
+ name: 'after drag started across group header',
+ colKey: 'category',
+ start: 99,
+ end: 119,
+ moveOverGroup: true,
+ widths: { category: 120, metrics: 200, valueA: 80, valueB: 100 },
+ },
+ ])('should resize column width $name', ({ colKey, start, end, moveOverGroup, widths }) => {
+ const onColumnResizeChange = vi.fn();
+ const { container } = render(
+ ,
+ );
+ const leaf = container.querySelector(`th[data-colkey="${colKey}"]`);
+ const group = container.querySelector('th[data-colkey="metrics"]');
+
+ fireColumnResizeMouseEvent(leaf, 'mouseMove', start);
+ fireColumnResizeMouseEvent(leaf, 'mouseDown', start, 1);
+ fireColumnResizeMouseEvent(moveOverGroup ? group : document, 'mouseMove', end, 1);
+ fireColumnResizeMouseEvent(document, 'mouseUp', end);
+
+ expect(onColumnResizeChange).toHaveBeenCalledTimes(1);
+ expect(onColumnResizeChange).toHaveBeenCalledWith({
+ columnsWidth: widths,
+ });
+ });
+
+ it('should resize from left edge when right fixed column shadow is active', () => {
+ vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(300);
+ vi.spyOn(HTMLElement.prototype, 'scrollWidth', 'get').mockReturnValue(400);
+ const onColumnResizeChange = vi.fn();
+ const { container } = render(
+ ,
+ );
+ const fixed = container.querySelector('th[data-colkey="valueB"]');
+
+ fireColumnResizeMouseEvent(fixed, 'mouseMove', 201);
+ fireColumnResizeMouseEvent(fixed, 'mouseDown', 201, 1);
+ fireColumnResizeMouseEvent(document, 'mouseMove', 181, 1);
+ fireColumnResizeMouseEvent(document, 'mouseUp', 181);
+
+ expect(onColumnResizeChange).toHaveBeenCalledTimes(1);
+ expect(onColumnResizeChange).toHaveBeenCalledWith({
+ columnsWidth: {
+ category: 100,
+ metrics: 200,
+ valueA: 100,
+ valueB: 120,
+ },
+ });
+ });
+ });
+
describe('virtual header horizontal scrolling', () => {
const DATA = Array.from({ length: 150 }, (_, id) => ({
id,
diff --git a/packages/components/table/hooks/useColumnResize.tsx b/packages/components/table/hooks/useColumnResize.tsx
index 194c34cc89..679bdc2ebb 100644
--- a/packages/components/table/hooks/useColumnResize.tsx
+++ b/packages/components/table/hooks/useColumnResize.tsx
@@ -127,7 +127,11 @@ export default function useColumnResize(params: {
if (!target) return;
// 判断是否为叶子阶段,仅叶子结点允许拖拽
const colKey = target.getAttribute('data-colkey');
- if (!leafColumns.find((t) => t.colKey === colKey)) return;
+ if (!leafColumns.find((t) => t.colKey === colKey)) {
+ resizeLineParams.draggingCol = null;
+ resizeLineParams.effectCol = null;
+ return;
+ }
const targetBoundRect = target.getBoundingClientRect();
const thRightCursor = targetBoundRect.right - e.pageX <= distance;
const thLeftCursor = e.pageX - targetBoundRect.left <= distance;
@@ -230,7 +234,12 @@ export default function useColumnResize(params: {
col: BaseTableCol,
index: number,
) => {
- if (e.button === CONTEXTMENU || !resizeLineParams.draggingCol) return;
+ if (
+ e.button === CONTEXTMENU ||
+ !resizeLineParams.draggingCol ||
+ !leafColumns.some((item) => item.colKey === col.colKey)
+ )
+ return;
const target = resizeLineParams.draggingCol;
const targetBoundRect = target.getBoundingClientRect();
const tableBoundRect = tableContentRef.current?.getBoundingClientRect();
diff --git a/packages/tdesign-react/.changelog/pr-4471.md b/packages/tdesign-react/.changelog/pr-4471.md
new file mode 100644
index 0000000000..ea289395d6
--- /dev/null
+++ b/packages/tdesign-react/.changelog/pr-4471.md
@@ -0,0 +1,6 @@
+---
+pr_number: 4471
+contributor: cdevq
+---
+
+- fix(Table): 修复开启 `resizable` 且存在分组表头时,在叶子列与分组表头交界处点击引发异常的问题 @cdevq ([#4471](https://github.com/Tencent/tdesign-react/pull/4471))