Skip to content

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

Merged
zombieJ merged 12 commits intoreact-component:masterfrom
QdabuliuQ:master
Sep 10, 2025
Merged

fix: after selecting and dragging text, the table still scrolls even after releasing the mouse.#328
zombieJ merged 12 commits intoreact-component:masterfrom
QdabuliuQ:master

Conversation

@QdabuliuQ
Copy link
Contributor

@QdabuliuQ QdabuliuQ commented Jul 16, 2025

🤔 This is a ...

  • 🆕 New feature
  • 🐞 Bug fix
  • 📝 Site / documentation improvement
  • 📽️ Demo improvement
  • 💄 Component style improvement
  • 🤖 TypeScript definition improvement
  • 📦 Bundle size optimization
  • ⚡️ Performance optimization
  • ⭐️ Feature enhancement
  • 🌐 Internationalization
  • 🛠 Refactoring
  • 🎨 Code style optimization
  • ✅ Test Case
  • 🔀 Branch merge
  • ⏩ Workflow
  • ⌨️ Accessibility improvement
  • ❓ Other (about what?)

🔗 Related Issues

fix ant-design/ant-design#53924
fix ant-design/ant-design#54548

💡 Background and Solution

选择文本,鼠标按住文本拖拽到表格外边,松开鼠标,鼠标上下移动会触发表格滚动

467032935-9bec2f1c-866f-40d0-be9e-b6e19b6c0be8.mov

rc-virtual-list 的 useScrollDrag hook 监听 mouseup

📝 Change Log

Language Changelog
🇺🇸 English Fix virtual-list drag scrolling issue
🇨🇳 Chinese 修复 virtual-list 拖拽滚动异常

Summary by CodeRabbit

  • Bug Fixes

    • 修复在列表中拖拽并放下选中文本后可能继续触发滚动的问题:在文档级别处理鼠标松开与原生拖拽结束事件以正确终止拖拽滚动,提升交互稳定性。
  • Tests

    • 新增回归测试,验证拖拽并放下文本不会触发额外滚动或滚动回调。

The package version was changed from 3.19.2 to 3.19.1, possibly to correct a versioning error or revert a previous update.
@vercel
Copy link

vercel bot commented Jul 16, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
virtual-list Ready Ready Preview Comment Sep 10, 2025 6:37am

@coderabbitai
Copy link

coderabbitai bot commented Jul 16, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

useScrollDrag 钩子内新增并使用 clearDragState() 集中重置拖拽状态与停止滚动;将原有组件内联的 onMouseUp 替换为 document 层的 mouseupdragend 监听并在 cleanup 中移除。新增测试验证在选中文本并执行原生拖放时不会触发滚动回调。

Changes

Cohort / File(s) 变更摘要
Hook 实现更新
src/hooks/useScrollDrag.ts
新增内部辅助函数 clearDragState() 用于重置拖拽状态并停止滚动;移除组件内联的 onMouseUp,在 document 层注册 mouseupdragend(并绑定 clearDragState);更新 cleanup 以注销新增的 document 事件监听;onMouseDown/onMouseMove 行为保持不变。
测试新增
tests/scroll.test.js
新增用例 should not scroll after dropping selected list text:在对列表项选中文本并模拟原生 dragstart/dragover/drop/dragend 流程后,断言 onScroll 在 drop/dragend 期间不再被触发,验证文本拖放不会触发滚动。

Sequence Diagram(s)

sequenceDiagram
    participant User as 用户
    participant Document as Document
    participant Hook as useScrollDrag

    User->>Document: mousedown (可能选中文本)
    Document->>Hook: onMouseDown / onMouseMove (保持原有行为)
    Note right of Hook #DFF2E1: 可能设置 mouseDownLock\n并触发滚动
    User->>Document: dragstart (原生)
    Document->>Hook: dragstart (不改动 onMouseDown)
    Note right of Hook #FCE8D6: text selection + drag begins
    Document->>Hook: dragend -> clearDragState()
    Document->>Hook: mouseup -> clearDragState()
    Note right of Hook #FCE8D6: clearDragState 停止滚动并重置锁\ndocument 级别 cleanup 移除监听器
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Pre-merge checks (2 passed, 1 warning)

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed PR当前标题明确概括了修复的核心问题,即在选中并拖拽文本后松开鼠标仍然触发表格滚动,与实际修改内容高度一致,能够让审阅者快速理解本次变更的目的。

Poem

小兔轻跳守边界,文选静落不惊雷,
事件在文档处收尾,拖拽归位风亦悄,
清锁一声,滚动止,代码安睡梦中蹦 🐇✨

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 294ab97 and f958296.

📒 Files selected for processing (1)
  • src/hooks/useScrollDrag.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/hooks/useScrollDrag.ts
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@QdabuliuQ
Copy link
Contributor Author

@codecov
Copy link

codecov bot commented Jul 17, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.86%. Comparing base (5769845) to head (f958296).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #328   +/-   ##
=======================================
  Coverage   97.85%   97.86%           
=======================================
  Files          19       19           
  Lines         794      796    +2     
  Branches      193      191    -2     
=======================================
+ Hits          777      779    +2     
  Misses         17       17           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Introduces a test to verify that the list does not scroll when table text is dragged and dropped, ensuring onScroll is not triggered during drag-and-drop interactions.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0ce568c and bae7ddc.

📒 Files selected for processing (1)
  • tests/scroll.test.js (1 hunks)

@QdabuliuQ
Copy link
Contributor Author

有没有大佬review呀

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where text dragging in a table continues to trigger scrolling after the mouse is released. The issue occurred when users selected text, dragged it outside the table, and released the mouse - subsequent mouse movements would still cause unwanted table scrolling.

  • Adds native drag event listeners to properly reset drag state during native drag operations
  • Introduces a centralized clearDragState function to handle state cleanup consistently
  • Adds comprehensive test coverage for the text dragging scenario

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 13 comments.

File Description
src/hooks/useScrollDrag.ts Implements drag event handlers and centralizes state cleanup logic
tests/scroll.test.js Adds test case to verify scrolling doesn't occur after text drag operations

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
tests/scroll.test.js (3)

767-772: 重复调用 selectElementText,无实际意义

selectElementText 在进入 if 分支前已执行一次,分支内再次调用并不改变行为,建议去除以避免混淆。

-    if (targetItem && listHolder) {
-      selectElementText(targetItem);
+    if (targetItem && listHolder) {

735-735: 用例标题建议从“table”改为“list”

本仓库场景是虚拟列表,标题中的 “table” 容易引起误解,建议更准确表达。

-it('should not scroll after drop table text', () => {
+it('should not scroll after dropping selected list text', () => {

761-761: 选择器与渲染结构的耦合建议降低

为了选择目标元素,这里给 li 人为加上了 fixed-item 类。虽然能工作,但会让测试对类名有依赖。也可以直接选择第一个 li 元素,贴近真实渲染结构,降低耦合。

-        {({ id }) => <li className="fixed-item">{id}</li>}
+        {({ id }) => <li>{id}</li>}

同时把后续查询改为:

-    const fixedItems = container.querySelectorAll('.fixed-item');
+    const fixedItems = container.querySelectorAll('li');
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between dc9f00e and b77332d.

📒 Files selected for processing (2)
  • src/hooks/useScrollDrag.ts (3 hunks)
  • tests/scroll.test.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/hooks/useScrollDrag.ts

mouseDownLock = false;
stopScroll();
};

Copy link
Member

Choose a reason for hiding this comment

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

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

ele.ownerDocument.removeEventListener('mouseup', onMouseUp);
ele.ownerDocument.removeEventListener('mouseup', clearDragState);
ele.ownerDocument.removeEventListener('mousemove', onMouseMove);
ele.ownerDocument.removeEventListener('dragstart', clearDragState);
Copy link
Member

Choose a reason for hiding this comment

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

其实实现就是为了让 drag 的时候可以滚动才这么做的……

@zombieJ zombieJ merged commit 92085a0 into react-component:master Sep 10, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tree 在设置 height 虚拟滚动的时候会触发自动滚动 Unexpected scrolling visual error occurs after dragging text in the virtual table

3 participants