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 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
20 changes: 18 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,15 @@ export default function useScrollDrag(
mouseDownLock = true;
}
};

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

const onDragStart = () => {
clearDragState();
};

const onMouseMove = (e: MouseEvent) => {
if (mouseDownLock) {
const mouseY = getPageXY(e, false);
Expand All @@ -78,11 +89,16 @@ 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);

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);
stopScroll();
};
}
Expand Down
81 changes: 81 additions & 0 deletions tests/scroll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,4 +731,85 @@ describe('List.Scroll', () => {

jest.useRealTimers();
});

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

const onScroll = jest.fn();
// 这两个事件绑定在 document
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 with the rest of the codebase: '// These two events are bound to document'.

Suggested change
// 这两个事件绑定在 document
// These two events are bound to document

Copilot uses AI. Check for mistakes.

const onDragStart = jest.fn();
const onDragEnd = jest.fn();
document.addEventListener('dragstart', onDragStart);
document.addEventListener('dragend', onDragEnd);

const { container } = render(
<List
component="ul"
itemKey="id"
itemHeight={20}
height={100}
data={genData(200)}
onScroll={onScroll}
>
{({ id }) => <li className="fixed-item">{id}</li>}
</List>,
);
// 选择第一个可见的 fixed-item 的文本内容
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: '// Select the text content of the first visible fixed-item'.

Suggested change
// 选择第一个可见的 fixed-item 的文本内容
// Select the text content of the first visible fixed-item

Copilot uses AI. Check for mistakes.

const fixedItems = container.querySelectorAll('.fixed-item');
const targetItem = fixedItems[0]; // 使用第一个可见元素
if (targetItem) {
const range = document.createRange();
range.selectNodeContents(targetItem);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
// 选中 fixed-item 里的文本并拖拽文本到列表底部
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: '// Select text in fixed-item and drag it to the bottom of the list'.

Suggested change
// 选中 fixed-item 里的文本并拖拽文本到列表底部
// Select text in fixed-item and drag it to the bottom of the list

Copilot uses AI. Check for mistakes.

const listHolder = container.querySelector('.rc-virtual-list-holder');
if (targetItem && listHolder) {
// 创建选区,选中 fixed-item 的文本
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: '// Create selection range for fixed-item text'.

Suggested change
// 创建选区,选中 fixed-item 的文本
// Create selection range for fixed-item text

Copilot uses AI. Check for mistakes.

const range = document.createRange();
range.selectNodeContents(targetItem);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
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.

The text selection logic is duplicated at lines 759-765 and 769-774. Consider extracting this into a helper function to reduce code duplication.

Suggested change
selection.addRange(range);
selectTextContent(targetItem);
// 选中 fixed-item 里的文本并拖拽文本到列表底部
const listHolder = container.querySelector('.rc-virtual-list-holder');
if (targetItem && listHolder) {
// 创建选区,选中 fixed-item 的文本
selectTextContent(targetItem);

Copilot uses AI. Check for mistakes.


// 模拟拖拽文本
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: '// Simulate text dragging'.

Suggested change
// 模拟拖拽文本
// Simulate text dragging

Copilot uses AI. Check for mistakes.

fireEvent.dragStart(targetItem, { bubbles: true });

// 拖拽到列表底部
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: '// Drag to bottom of list'.

Suggested change
// 拖拽到列表底部
// Drag to bottom of list

Copilot uses AI. Check for mistakes.

const rect = listHolder.getBoundingClientRect();
fireEvent.dragOver(listHolder, {
clientY: rect.bottom + 10,
bubbles: true,
});

// 松开鼠标
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.

fireEvent.drop(listHolder, {
clientY: rect.bottom + 10,
bubbles: true,
});

fireEvent.dragEnd(targetItem, { bubbles: true });
}
// 检查 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();
expect(onDragStart).toHaveBeenCalled();
expect(onDragEnd).toHaveBeenCalled();

// 模拟鼠标移动到列表顶部
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: '// Simulate mouse movement to top of list'.

Suggested change
// 模拟鼠标移动到列表顶部
// Simulate mouse movement to top of list

Copilot uses AI. Check for mistakes.

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();

// 清理事件监听器
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: '// Clean up event listeners'.

Suggested change
// 清理事件监听器
// Clean up event listeners

Copilot uses AI. Check for mistakes.

document.removeEventListener('dragstart', onDragStart);
document.removeEventListener('dragend', onDragEnd);
});
});
Loading