Skip to content

fix: table forceScroll setTimeout issue #1259

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 1 commit into
base: master
Choose a base branch
from

Conversation

z2015
Copy link

@z2015 z2015 commented Apr 19, 2025

f修复 forceScroll 方法中当 setTimeout 调用不是最后一次时机时,有可能会出现最后一次的 target.scrollLeft 赋值操作被后续setTimeout中的 target.scrollLeft 赋值给覆盖,由此一来造成表头和表体错位的问题。

Summary by CodeRabbit

  • Bug Fixes
    • 修复了快速多次调用滚动时可能导致滚动位置重复重置的问题,提高了滚动行为的稳定性。

Copy link

vercel bot commented Apr 19, 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 19, 2025 6:45am

Copy link

coderabbitai bot commented Apr 19, 2025

Walkthrough

本次更改主要针对 forceScroll 函数进行了优化。通过在 HTMLDivElement 上引入可选的 _scrollTimeout 属性,实现了在多次快速调用 forceScroll 时,能够清除并重置已有的延时操作,避免了重复或冲突的滚动位置重置。此次修改未涉及导出或公共实体的声明变更。

Changes

文件/路径 变更摘要
src/Table.tsx 优化 forceScroll 函数,增加 _scrollTimeout 属性用于管理延时滚动,避免多次快速调用时产生冗余的定时器。

Suggested reviewers

  • afc163

Poem

🐇
小兔跳跃桌上行,
forceScroll 轻轻改良新。
定时器不再堆叠,
滚动流畅如清风。
代码整洁心欢喜,
Table 页面更灵动!

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/Table.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.

📜 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 c02189e and df97789.

📒 Files selected for processing (1)
  • src/Table.tsx (2 hunks)
🔇 Additional comments (1)
src/Table.tsx (1)

420-443: 优化了 forceScroll 函数以防止表头和表体在滚动时错位

该修改很好地解决了当多次快速调用 forceScroll 时可能出现的滚动位置不同步问题。通过跟踪和清除现有的 setTimeout,确保只有最新的 timeout 会实际设置滚动位置,从而防止表头和表体错位。

-function forceScroll(scrollLeft: number, target: HTMLDivElement | ((left: number) => void)) {
+function forceScroll(scrollLeft: number, target: HTMLDivElement & {_scrollTimeout?: number | null} | ((left: number) => void)) {
  if (!target) {
    return;
  }
  if (typeof target === 'function') {
    target(scrollLeft);
  } else if (target.scrollLeft !== scrollLeft) {
    target.scrollLeft = scrollLeft;

+    if (target._scrollTimeout) {
+      window.clearTimeout(target._scrollTimeout);
+      target._scrollTimeout = null;
+    }

    // Delay to force scroll position if not sync
    // ref: https://github.com/ant-design/ant-design/issues/37179
    if (target.scrollLeft !== scrollLeft) {
-      setTimeout(() => {
+      target._scrollTimeout = setTimeout(() => {
        target.scrollLeft = scrollLeft;
+        target._scrollTimeout = null;
      }, 0);
    }
  }
}
✨ Finishing Touches
  • 📝 Generate Docstrings

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

Copy link

@Copilot 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 addresses a bug in the forceScroll method where rapid consecutive calls could lead to a race condition, causing the table header and body to become misaligned.

  • Updated the forceScroll function to include a union type for the target parameter that supports an optional _scrollTimeout property.
  • Added logic to clear existing scheduled timeouts before scheduling a new one, ensuring the scrollLeft assignment reflects the latest call.

@@ -426,11 +426,17 @@ function Table<RecordType extends DefaultRecordType>(
} else if (target.scrollLeft !== scrollLeft) {
target.scrollLeft = scrollLeft;

if (target._scrollTimeout) {
Copy link
Preview

Copilot AI Apr 21, 2025

Choose a reason for hiding this comment

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

When the target parameter is a function, accessing the _scrollTimeout property may cause issues since functions do not have this property. Consider adding a type guard to ensure that target is an HTMLDivElement instance before checking _scrollTimeout.

Suggested change
if (target._scrollTimeout) {
if (target instanceof HTMLDivElement && target._scrollTimeout) {

Copilot uses AI. Check for mistakes.

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.

1 participant