Skip to content
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

feat: infiniteloading default scroll #3010

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

Fangzhizheng
Copy link

@Fangzhizheng Fangzhizheng commented Feb 23, 2025

🤔 这个变动的性质是?

  • [ 1] 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

提供设置默认滚动距离的能力

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • [1 ] 文档已补充或无须补充
  • [ 1] 代码演示已提供或无须提供
  • [ 1] TypeScript 定义已补充或无须补充
  • [ 1] fork仓库代码是否为最新避免文件冲突
  • [ 1] Files changed 没有 package.json lock 等无关文件

Summary by CodeRabbit

  • 新功能
    • InfiniteLoading 组件新增了一个可选属性 defaultScrollTop,允许用户设置加载时的默认滚动位置,从而改善滚动体验。
  • 文档
    • 更新了组件文档,详细描述了 defaultScrollTop 属性的类型和用途。

Copy link

coderabbitai bot commented Feb 23, 2025

Walkthrough

本次变更为 InfiniteLoading 组件新增了一个可选属性 defaultScrollTop(类型为 number),用于设定组件的默认滚动位置。文档部分也进行了相应的更新以说明该属性的用途。在组件实现中,通过新增的 useEffect 钩子,在 defaultScrollTop 变化时调整滚动位置,从而整合到组件生命周期内。

Changes

File(s) Change Summary
src/packages/infiniteloading/doc.md
src/packages/infiniteloading/infiniteloading.tsx
InfiniteLoading 添加了新的可选属性 defaultScrollTop (number)。文档中描述了该属性,组件代码中在接口中声明并通过 useEffect 钩子来更新滚动位置。

Sequence Diagram(s)

sequenceDiagram
    participant IL as InfiniteLoading 组件
    participant Hook as useEffect 钩子
    participant DOM as 滚动元素 (scrollEl)
    
    IL->>Hook: 接收 defaultScrollTop 属性
    Hook->>DOM: 计算子元素高度
    Hook->>DOM: 在超时后更新 scrollTop 为 defaultScrollTop
Loading

Possibly related PRs

Suggested reviewers

  • oasis-cloud

Poem

我是一只快乐的小兔子,
跳跃在代码的花园里。
新增的属性如晨曦般明亮,
滚动动作为页面点缀新意。
在这数字世界里欢欣鼓舞,
代码与梦想齐飞,共舞不停!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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. (Beta)
  • @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.

@github-actions github-actions bot added the 2.x Target branch 2.x label Feb 23, 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: 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 994800f and 090a9fe.

📒 Files selected for processing (2)
  • src/packages/infiniteloading/doc.md (1 hunks)
  • src/packages/infiniteloading/infiniteloading.tsx (3 hunks)
🧰 Additional context used
🪛 ESLint
src/packages/infiniteloading/infiniteloading.tsx

[error] 84-84: Replace ·(getRefreshTop().firstElementChild·as·HTMLElement).offsetHeight·||·0; with ⏎········(getRefreshTop().firstElementChild·as·HTMLElement).offsetHeight·||·0

(prettier/prettier)


[error] 85-85: Delete ;

(prettier/prettier)


[error] 86-86: Insert ·

(prettier/prettier)


[error] 90-90: Delete ;

(prettier/prettier)

🔇 Additional comments (3)
src/packages/infiniteloading/infiniteloading.tsx (2)

26-26: 新属性定义正确且清晰!

defaultScrollTop 属性的类型定义恰当,使用可选参数符合最佳实践。


64-64: 属性解构实现正确!

defaultScrollTop 属性的解构位置恰当,保持了代码的一致性。

src/packages/infiniteloading/doc.md (1)

71-71: 文档更新准确完整!

新属性 defaultScrollTop 的文档说明清晰,与实现保持一致。

Comment on lines +82 to +90
useEffect(() => {
if (defaultScrollTop) {
const childHeight = (getRefreshTop().firstElementChild as HTMLElement).offsetHeight || 0;
refreshMaxH.current = Math.floor(childHeight * 1 + 10);
setTimeout(() =>{
scrollEl.current.scrollTop = defaultScrollTop
}, 10)
}
}, [defaultScrollTop]);
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

建议优化 useEffect 实现

当前实现存在以下需要改进的地方:

  1. 缺少清理函数以处理组件卸载情况
  2. setTimeout 使用了魔法数字 (10ms)
  3. 没有处理可能的空值情况
  4. 可能与 DOM 更新产生竞态条件

建议按照以下方式重构:

 useEffect(() => {
   if (defaultScrollTop) {
+    const element = getRefreshTop();
+    if (!element?.firstElementChild) return;
+
     const childHeight = (getRefreshTop().firstElementChild as HTMLElement).offsetHeight || 0;
     refreshMaxH.current = Math.floor(childHeight * 1 + 10);
-    setTimeout(() =>{
-      scrollEl.current.scrollTop = defaultScrollTop
-    }, 10)
+    const timeoutId = setTimeout(() => {
+      if (scrollEl.current) {
+        scrollEl.current.scrollTop = defaultScrollTop;
+      }
+    }, 100); // 使用更合理的延迟时间
+
+    return () => clearTimeout(timeoutId);
   }
 }, [defaultScrollTop]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
if (defaultScrollTop) {
const childHeight = (getRefreshTop().firstElementChild as HTMLElement).offsetHeight || 0;
refreshMaxH.current = Math.floor(childHeight * 1 + 10);
setTimeout(() =>{
scrollEl.current.scrollTop = defaultScrollTop
}, 10)
}
}, [defaultScrollTop]);
useEffect(() => {
if (defaultScrollTop) {
+ const element = getRefreshTop();
+ if (!element?.firstElementChild) return;
+
const childHeight = (getRefreshTop().firstElementChild as HTMLElement).offsetHeight || 0;
refreshMaxH.current = Math.floor(childHeight * 1 + 10);
- setTimeout(() =>{
- scrollEl.current.scrollTop = defaultScrollTop
- }, 10)
+ const timeoutId = setTimeout(() => {
+ if (scrollEl.current) {
+ scrollEl.current.scrollTop = defaultScrollTop;
+ }
+ }, 100); // 使用更合理的延迟时间
+
+ return () => clearTimeout(timeoutId);
}
}, [defaultScrollTop]);
🧰 Tools
🪛 ESLint

[error] 84-84: Replace ·(getRefreshTop().firstElementChild·as·HTMLElement).offsetHeight·||·0; with ⏎········(getRefreshTop().firstElementChild·as·HTMLElement).offsetHeight·||·0

(prettier/prettier)


[error] 85-85: Delete ;

(prettier/prettier)


[error] 86-86: Insert ·

(prettier/prettier)


[error] 90-90: Delete ;

(prettier/prettier)

@github-actions github-actions bot added the action:review This PR needs more reviews (less than 2 approvals) label Feb 24, 2025
@oasis-cloud oasis-cloud changed the title Feat/infiniteloading default scroll feat: infiniteloading default scroll Feb 26, 2025
Copy link

codecov bot commented Feb 26, 2025

Codecov Report

Attention: Patch coverage is 40.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 85.54%. Comparing base (994800f) to head (090a9fe).
Report is 2 commits behind head on next.

Files with missing lines Patch % Lines
src/packages/infiniteloading/infiniteloading.tsx 40.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             next    #3010      +/-   ##
==========================================
- Coverage   85.56%   85.54%   -0.02%     
==========================================
  Files         267      267              
  Lines       18003    18013      +10     
  Branches     2726     2729       +3     
==========================================
+ Hits        15405    15410       +5     
- Misses       2595     2600       +5     
  Partials        3        3              

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

@oasis-cloud
Copy link
Collaborator

ci 问题可以先修复一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x Target branch 2.x action:review This PR needs more reviews (less than 2 approvals) size/S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants