Skip to content
Merged
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(
});
};

// 清理拖拽状态的统一函数
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
const clearDragState = () => {
mouseDownLock = false;
stopScroll();
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

可否移动到 onMouseUp 的位置,减少diff?

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

// 松开鼠标
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
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 没有被触发
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
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 没有被触发
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
expect(onScroll).not.toHaveBeenCalled();
});
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
});
Loading