Skip to content

Comments

fix: fix first rows scroll down when expand#1256

Closed
Vinh241 wants to merge 2 commits intoreact-component:masterfrom
Vinh241:fixbug/table-virtual-expand-first-row
Closed

fix: fix first rows scroll down when expand#1256
Vinh241 wants to merge 2 commits intoreact-component:masterfrom
Vinh241:fixbug/table-virtual-expand-first-row

Conversation

@Vinh241
Copy link

@Vinh241 Vinh241 commented Apr 12, 2025

fix ant-design/ant-design#53334

Summary by CodeRabbit

Summary by CodeRabbit

  • 新功能

    • 优化了虚拟表格在行展开和收缩时的滚动表现:第一行展开或收缩时自动回到顶部,而其他行保持之前的滚动位置,从而提升用户体验。
  • Tests

    • 添加了针对行展开/收缩行为的测试,确保滚动位置在操作后能稳定保持。

@vercel
Copy link

vercel bot commented Apr 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
table ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 13, 2025 10:21am

@coderabbitai
Copy link

coderabbitai bot commented Apr 12, 2025

Walkthrough

此次更改主要在 src/VirtualTable/BodyGrid.tsx 文件中添加了多个新的引用变量和副作用,用于跟踪滚动位置和第一行展开状态的变化,并在额外渲染函数中适配对应的滚动处理逻辑。同时,tests/Virtual.spec.tsx 文件中新增了测试用例,以验证在扩展非第一行时滚动位置的保持情况。

Changes

文件 修改说明
src/VirtualTable/BodyGrid.tsx 新增 scrollPositionRefprevExpandedKeysReffirstRowExpandChangeRefshouldPreserveScroll;添加了 useEffect 钩子检测 expandedKeysdata 的变化,用于更新状态及控制滚动行为;修改 extraRenderonScroll 逻辑以适配新的滚动处理。
tests/Virtual.spec.tsx 新增测试用例 "expanding and collapsing rows",使用模拟请求帧和间谍函数验证非第一行扩展时滚动位置保持正确,无不必要的滚动调用。

Sequence Diagram(s)

sequenceDiagram
    participant User as 用户操作
    participant Grid as 网格组件
    participant Effect as useEffect钩子
    participant Render as extraRender函数
    participant Scroll as handleScroll函数

    User->>Grid: 触发行展开/收缩
    Grid->>Effect: 监测expandedKeys和data变化
    Effect->>Grid: 检查第一行展开状态变化
    alt 第一行状态发生变化
        Effect-->>Grid: 更新 firstRowExpandChangeRef 为 true, 设置 shouldPreserveScroll 为 false
    else 非第一行或无变化
        Effect-->>Grid: 保持shouldPreserveScroll为true
    end
    Grid->>Render: 执行extraRender逻辑
    Render->>Grid: 根据状态重置或恢复滚动位置
    Grid->>Scroll: 触发onScroll事件,更新scrollPositionRef
Loading

Assessment against linked issues

Objective Addressed Explanation
不应在展开第一行时自动滚动 (#53334)
验证扩展非第一行时滚动位置保持 (#53334)

Poem

我是一只快乐的小兔子,🐰
代码跃动似草原的风起云涌,
第一行的展开轻轻翻飞,
滚动位置稳如老树盘根,
测试齐奏赞美之歌,
一同享受这份纯净的变更韵律!
祝你编码愉快,无忧无虑!

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/VirtualTable/BodyGrid.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Vinh241 Vinh241 changed the title fix first rows scroll down when expand fix first rows scroll down when expand, Apr 12, 2025
@Vinh241 Vinh241 changed the title fix first rows scroll down when expand, fix first rows scroll down when expand, Bugs https://github.com/ant-design/ant-design/issues/53334 Apr 12, 2025
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: 0

🧹 Nitpick comments (1)
src/VirtualTable/BodyGrid.tsx (1)

69-94: 监控展开状态变化的副作用实现

这个 useEffect 的实现很好地监控了展开状态的变化,特别是区分了第一行和其他行的展开情况。通过比较前后的展开状态,能够判断是否需要保持滚动位置。

建议:当数据量非常大时,每次都创建新的 Set 可能会有性能影响,可以考虑只在必要时更新 prevExpandedKeysRef。

-    prevExpandedKeysRef.current = new Set(expandedKeys);
+    // 只在键集合有变化时才创建新的 Set
+    if (!setsAreEqual(prevExpandedKeysRef.current, expandedKeys)) {
+      prevExpandedKeysRef.current = new Set(expandedKeys);
+    }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c02189e and f791e68.

📒 Files selected for processing (2)
  • src/VirtualTable/BodyGrid.tsx (4 hunks)
  • tests/Virtual.spec.tsx (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/VirtualTable/BodyGrid.tsx (3)
src/interface.ts (1)
  • Key (21-21)
src/hooks/useFlattenRecords.ts (1)
  • useFlattenRecords (57-88)
tests/Table.spec.jsx (1)
  • data (10-13)
🔇 Additional comments (5)
tests/Virtual.spec.tsx (1)

563-690: 测试用例全面覆盖了展开行时的滚动问题

测试很好地验证了在展开非第一行时,滚动位置能够被正确保留,而不会意外滚动到表格顶部。测试方法通过监听 scrollTo 方法调用来确认没有不必要的滚动发生。

建议:

  1. 测试中仅检查了展开行的情况,建议也添加折叠行的测试用例,确保两种情况都能正常工作
  2. 可以考虑在测试中加入滚动到中间位置的步骤,以更好地模拟真实用户场景
src/VirtualTable/BodyGrid.tsx (4)

54-64: 新增的引用变量用于跟踪滚动位置和展开状态

这些引用变量很好地解决了展开行时滚动位置的管理问题。每个变量都有明确的用途:

  • scrollPositionRef:跟踪当前滚动位置
  • prevExpandedKeysRef:存储之前的展开键以便比较
  • firstRowExpandChangeRef:标记是否是第一行的展开状态变化
  • shouldPreserveScroll:决定是否应保留滚动位置

代码结构清晰,变量命名准确反映了其用途。


151-178: 滚动位置恢复逻辑

这段代码很好地解决了两种情况下的滚动位置处理:

  1. 第一行展开/折叠时,滚动到顶部
  2. 非第一行变化时,保持滚动位置

使用 requestAnimationFrame 确保滚动操作在渲染完成后执行,这是一个很好的实践。

建议:可以考虑添加一些调试日志(在开发环境),以便于排查潜在的滚动问题。


272-282: 滚动事件处理函数

handleScroll 函数实现了两个功能:

  1. 记录当前滚动位置
  2. 调用原始的滚动回调

逻辑简洁明了,依赖项设置正确。这样的封装方式不会影响原有功能,同时增加了新的行为。


321-321: 更新 VirtualList 的 onScroll 属性

使用新的 handleScroll 函数替换原有的 onTablePropScroll,确保滚动位置可以被正确记录和恢复。这种方式不会破坏原有功能,同时增加了新的滚动位置管理能力。

@codecov
Copy link

codecov bot commented Apr 13, 2025

Codecov Report

Attention: Patch coverage is 98.50746% with 2 lines in your changes missing coverage. Please review.

Project coverage is 98.04%. Comparing base (c02189e) to head (ce76329).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
tests/Virtual.spec.tsx 97.84% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master    #1256    +/-   ##
========================================
  Coverage   98.03%   98.04%            
========================================
  Files          76       76            
  Lines        7425     7558   +133     
  Branches     1146     1164    +18     
========================================
+ Hits         7279     7410   +131     
- Misses        140      142     +2     
  Partials        6        6            

☔ 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.

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: 0

🧹 Nitpick comments (2)
src/VirtualTable/BodyGrid.tsx (2)

54-65: 在多处使用 Ref 时注意命名与职责划分。
这些 Ref 变量能够清晰追踪滚动位置和首行展开状态,整体实现思路合理。但建议确认命名是否足够直观,并评估后续是否会有更多行的展开逻辑需求,以避免后期改动较大。


259-270: 可考虑同时记录水平滚动位置。
目前只存储并更新 scrollTop,若后续需要在首行展开时保持水平位置,可能需要 scrollLeft 同步。可根据需求决定是否要补充对横向滚动的跟踪。

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f791e68 and ce76329.

📒 Files selected for processing (1)
  • src/VirtualTable/BodyGrid.tsx (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/VirtualTable/BodyGrid.tsx (2)
src/interface.ts (1)
  • Key (21-21)
src/hooks/useFlattenRecords.ts (1)
  • useFlattenRecords (57-88)
🔇 Additional comments (3)
src/VirtualTable/BodyGrid.tsx (3)

69-94: 确保首行逻辑在数据变化时不会产生意外行为。
当前仅通过比较首行记录的展开键来判断,若数据顺序或首行记录发生变动(例如数据被重新排序),可能会导致误判。请验证是否需在其他场景下也对首行展开进行特殊处理,或保证数据的首行恒定不变。


151-166: 检查注释内容与 scrollTo({ top: 0 }) 行为是否一致。
注释中写到“不要进行自动滚动”,但实际代码仍将滚动位置设置为顶部,这可能与预期不符。请确认是故意将视图重置到顶部,还是应保持原有滚动位置。


308-308: 改为使用自定义事件时,请注意回调执行顺序。
此处替换为 onScroll={handleScroll} 后,滚动回调会在 handleScroll 中先更新 scrollPositionRef,再调用原先的 onTablePropScroll。如需在外部组件监听更改后的滚动位置,应保证调用次序满足需求。

@yoyo837 yoyo837 changed the title fix first rows scroll down when expand, Bugs https://github.com/ant-design/ant-design/issues/53334 fix: fix first rows scroll down when expand Apr 28, 2025
if (expanded) {
expandedRowKeys = [...expandedRowKeys, record.name];
} else {
expandedRowKeys = expandedRowKeys.filter(key => key !== record.name);
Copy link
Member

Choose a reason for hiding this comment

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

这行没跑到

requestAnimationFrame(() => {
// Keep the scroll position at its current position rather than jumping
if (listRef.current) {
listRef.current.scrollTo({ top: 0 });
Copy link
Member

Choose a reason for hiding this comment

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

This seems be the rc-virtual-list length extends bug. Not a better place to fix in rc-table

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.

Virtual table scrolls down when expanding top row's details

3 participants