Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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(
});
};

// 清理拖拽状态的统一函数
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,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
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
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 的文本内容
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
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 里的文本并拖拽文本到列表底部
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
const listHolder = container.querySelector('.rc-virtual-list-holder');
if (targetItem && listHolder) {
// 创建选区,选中 fixed-item 的文本
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
const range = document.createRange();
range.selectNodeContents(targetItem);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated

// 模拟拖拽文本
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
fireEvent.dragStart(targetItem, { bubbles: true });

// 拖拽到列表底部
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
const rect = listHolder.getBoundingClientRect();
fireEvent.dragOver(listHolder, {
clientY: rect.bottom + 10,
bubbles: true,
});

// 松开鼠标
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
fireEvent.drop(listHolder, {
clientY: rect.bottom + 10,
bubbles: true,
});

fireEvent.dragEnd(targetItem, { bubbles: true });
}
// 检查 onScroll 没有被触发
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
expect(onScroll).not.toHaveBeenCalled();
expect(onDragStart).toHaveBeenCalled();
expect(onDragEnd).toHaveBeenCalled();

// 模拟鼠标移动到列表顶部
Comment thread
QdabuliuQ marked this conversation as resolved.
Outdated
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
document.removeEventListener('dragstart', onDragStart);
document.removeEventListener('dragend', onDragEnd);
});
});
Loading