Skip to content

fix: after selecting and dragging text, the table still scrolls even after releasing the mouse. #328

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
38 changes: 36 additions & 2 deletions src/hooks/useScrollDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export default function useScrollDrag(
});
};

// 清理拖拽状态的统一函数
Copy link
Preview

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment contains Chinese text. Consider using English for consistency: '// Unified function to clear drag state'.

Suggested change
// 清理拖拽状态的统一函数
// Unified function to clear drag state

Copilot uses AI. Check for mistakes.

const clearDragState = () => {
mouseDownLock = false;
stopScroll();
};

const onMouseDown = (e: MouseEvent) => {
// Skip if element set draggable
if ((e.target as HTMLElement).draggable || e.button !== 0) {
Expand All @@ -52,10 +58,28 @@ export default function useScrollDrag(
mouseDownLock = true;
}
};

const onMouseUp = () => {
mouseDownLock = false;
stopScroll();
clearDragState();
};

// 当开始原生拖拽时清理状态
const onDragStart = () => {
clearDragState();
};

// 当失去焦点时清理状态
const onBlur = () => {
clearDragState();
};

// 当页面不可见时清理状态
const onVisibilityChange = () => {
if (document.hidden) {
clearDragState();
}
};

const onMouseMove = (e: MouseEvent) => {
if (mouseDownLock) {
const mouseY = getPageXY(e, false);
Expand All @@ -78,11 +102,21 @@ export default function useScrollDrag(
ele.addEventListener('mousedown', onMouseDown);
ele.ownerDocument.addEventListener('mouseup', onMouseUp);
ele.ownerDocument.addEventListener('mousemove', onMouseMove);

// 添加额外的状态清理事件监听器
ele.ownerDocument.addEventListener('dragstart', onDragStart);
ele.ownerDocument.addEventListener('dragend', clearDragState);
window.addEventListener('blur', onBlur);
document.addEventListener('visibilitychange', onVisibilityChange);

return () => {
ele.removeEventListener('mousedown', onMouseDown);
ele.ownerDocument.removeEventListener('mouseup', onMouseUp);
ele.ownerDocument.removeEventListener('mousemove', onMouseMove);
ele.ownerDocument.removeEventListener('dragstart', onDragStart);
ele.ownerDocument.removeEventListener('dragend', clearDragState);
window.removeEventListener('blur', onBlur);
document.removeEventListener('visibilitychange', onVisibilityChange);
stopScroll();
};
}
Expand Down
66 changes: 66 additions & 0 deletions tests/scroll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,4 +731,70 @@ describe('List.Scroll', () => {

jest.useRealTimers();
});

it('should not scroll after drop table text', () => {

const onScroll = jest.fn();
const { container } = render(
<List
component="ul"
itemKey="id"
itemHeight={20}
height={100}
data={genData(200)}
onScroll={onScroll}
>
{({ id }) => <li draggable>{id}</li>}
</List>,
);
// 选中第99个 fixed-item 的文本内容
const fixedItems = container.querySelectorAll('.fixed-item');
const targetItem = fixedItems[99];
if (targetItem) {
const range = document.createRange();
range.selectNodeContents(targetItem);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
// 模拟将选中的文本拖拽到列表最底部
const listHolder = container.querySelector('.rc-virtual-list-holder');
if (targetItem && listHolder) {
// 创建拖拽事件
const dragStartEvent = new DragEvent('dragstart', { bubbles: true });
targetItem.dispatchEvent(dragStartEvent);

// 拖拽到最底部
const rect = listHolder.getBoundingClientRect();
const dragOverEvent = new DragEvent('dragover', {
bubbles: true,
clientY: rect.bottom + 10,
});
listHolder.dispatchEvent(dragOverEvent);

// 松开鼠标
Copy link
Preview

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment contains Chinese text. Consider using English for consistency: '// Release mouse'.

Suggested change
// 松开鼠标
// Release mouse

Copilot uses AI. Check for mistakes.

const dropEvent = new DragEvent('drop', {
bubbles: true,
clientY: rect.bottom + 10,
});
listHolder.dispatchEvent(dropEvent);

const dragEndEvent = new DragEvent('dragend', { bubbles: true });
targetItem.dispatchEvent(dragEndEvent);
}
// 检查 onScroll 没有被触发
Copy link
Preview

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment contains Chinese text. Consider using English for consistency: '// Check that onScroll was not triggered'.

Suggested change
// 检查 onScroll 没有被触发
// Check that onScroll was not triggered

Copilot uses AI. Check for mistakes.

expect(onScroll).not.toHaveBeenCalled();

// 模拟将鼠标移动到列表最顶部
if (listHolder) {
const rect = listHolder.getBoundingClientRect();
const mouseMoveEvent = new MouseEvent('mousemove', {
bubbles: true,
clientY: rect.top - 10,
});
listHolder.dispatchEvent(mouseMoveEvent);
}
// 检查 onScroll 没有被触发
Copy link
Preview

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment contains Chinese text. Consider using English for consistency: '// Check that onScroll was not triggered'.

Suggested change
// 检查 onScroll 没有被触发
// Check that onScroll was not triggered

Copilot uses AI. Check for mistakes.

expect(onScroll).not.toHaveBeenCalled();
});
});
Loading