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

fix: DOMRect value issues #481

Merged
merged 5 commits into from
Aug 30, 2024
Merged

Conversation

consistent-k
Copy link
Contributor

@consistent-k consistent-k commented Aug 29, 2024

兼容 getBoundingClientRect 取值逻辑

Summary by CodeRabbit

  • 新特性
    • 改进了目标和弹出元素的边界矩形坐标检索方式,增强了对坐标的可靠性和一致性。
  • 测试
    • 新增了针对 Trigger 组件的单元测试,验证其在不同事件下的对齐和可见性行为。
    • 更新了现有测试,增强了矩形对象的定义,提供更详细的位置信息。

Copy link

coderabbitai bot commented Aug 29, 2024

Walkthrough

此次更改增强了useAlign函数中目标和弹出元素的边界矩形坐标检索方式。通过为targetRectpopupRect对象的xy属性引入后备值,确保在getBoundingClientRect()方法未提供预期属性时,能够使用替代值,从而提高了坐标检索的可靠性。此外,新增了针对Trigger组件的单元测试,验证其对各种事件的对齐和可见性行为。

Changes

文件路径 更改摘要
src/hooks/useAlign.ts 修改targetRectpopupRect对象的xy属性,添加后备值以提高坐标检索的可靠性。
tests/align.test.tsx getBoundingClientRect方法返回的对象中新增lefttop属性,增强位置信息。
tests/arrow.test.jsx Trigger.Arrow组件的测试中新增lefttop属性,提供更全面的定位数据。
tests/flip-visibleFirst.test.tsx containerRecttargetRectpopupRect对象中新增lefttop属性,增强矩形定义。
tests/flip.test.tsx spanRectpopupRect对象中新增lefttop属性,改进对齐计算的精确度。
tests/flipShift.test.tsx 更新spanRect对象,新增lefttop属性,增强测试中的定位精度。
tests/rect.test.tsx 新增对Trigger组件的全面单元测试,涵盖对齐、可见性和动态定位等功能。

Poem

在草地上跳跃的小兔子,
发现了新变化的乐趣。
坐标更稳,弹出更快,
让我们一起欢呼吧!
小小的调整,快乐无比,
兔子们舞动,心中欢喜! 🐇✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 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

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c629b9c and c2424eb.

Files selected for processing (1)
  • src/hooks/useAlign.ts (1 hunks)
Additional comments not posted (3)
src/hooks/useAlign.ts (3)

216-217: 改进了代码的健壮性

targetRectxy 属性现在使用了回退值。这确保了如果 rect.xrect.y 未定义,将使用 rect.leftrect.top。这提高了代码的健壮性。

代码更改已批准。


223-224: 改进了代码的健壮性

popupRectxy 属性现在使用了回退值。这确保了如果 popupRect.xpopupRect.y 未定义,将使用 popupRect.leftpopupRect.top。这提高了代码的健壮性。

代码更改已批准。


216-217: 整体更改已批准

这些更改提高了坐标检索的可靠性,这对于在 UI 中正确对齐元素至关重要。

代码更改已批准。

Also applies to: 223-224

@yoyo837
Copy link
Member

yoyo837 commented Aug 29, 2024

加个用例?

@consistent-k
Copy link
Contributor Author

consistent-k commented Aug 29, 2024

加个用例?

复制了一个之前align的 改了里面的 getBoundingClientRect 方法, 看一下这样行吗

@yoyo837
Copy link
Member

yoyo837 commented Aug 29, 2024

就只针对这个场景写个用例即可

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

Commits

Files that changed from the base of the PR and between c2424eb and f2e92ee.

Files selected for processing (1)
  • tests/rect.test.tsx (1 hunks)
Additional context used
Biome
tests/rect.test.tsx

[error] 9-14: Do not export from a test file.

(lint/suspicious/noExportsInTest)

Additional comments not posted (10)
tests/rect.test.tsx (10)

16-46: 设置代码看起来不错。

这些设置确保了测试中元素的尺寸和位置值的一致性。


48-62: 每个测试的设置和清理代码看起来不错。

这些设置和清理代码确保了测试的隔离性。


64-110: 测试用例 not show 看起来不错。

这个测试用例检查了可见性和事件监听器的行为,覆盖了必要的场景。


112-137: 测试用例 resize align 看起来不错。

这个测试用例检查了在调整大小时的对齐行为,覆盖了必要的场景。


139-159: 测试用例 placement is higher than popupAlign 看起来不错。

这个测试用例检查了 placement 优先于 popupAlign 的情况,覆盖了必要的场景。


161-196: 测试用例 invisible should not align 看起来不错。

这个测试用例检查了当目标不可见时不进行对齐的情况,覆盖了必要的场景。


198-225: 测试用例 align should merge into placement 看起来不错。

这个测试用例检查了对齐合并到 placement 的情况,覆盖了必要的场景。


227-248: 测试用例 targetOffset support ptg 看起来不错。

这个测试用例检查了百分比 targetOffset 的支持情况,覆盖了必要的场景。


250-271: 测试用例 support dynamicInset 看起来不错。

这个测试用例检查了 dynamicInset 的支持情况,覆盖了必要的场景。


273-296: 测试用例 round when decimal precision 看起来不错。

这个测试用例检查了处理小数精度时的舍入行为,覆盖了必要的场景。

tests/rect.test.tsx Outdated Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f2e92ee and 7984a14.

Files selected for processing (1)
  • tests/rect.test.tsx (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/rect.test.tsx

@afc163
Copy link
Member

afc163 commented Aug 30, 2024

ci 挂了

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7984a14 and ad3d5e6.

Files selected for processing (6)
  • src/hooks/useAlign.ts (2 hunks)
  • tests/align.test.tsx (1 hunks)
  • tests/arrow.test.jsx (1 hunks)
  • tests/flip-visibleFirst.test.tsx (2 hunks)
  • tests/flip.test.tsx (5 hunks)
  • tests/flipShift.test.tsx (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/hooks/useAlign.ts
Additional comments not posted (8)
tests/arrow.test.jsx (1)

56-57: 增加了 lefttop 属性

在测试用例中增加了 lefttop 属性,与现有的 xy 值对齐,这可能会提高测试的全面性。请确保这些属性在测试中被正确使用,并且没有引入任何不一致。

代码更改已被批准。

运行以下脚本以验证 lefttop 在测试中的使用情况:

tests/flipShift.test.tsx (1)

88-89: 增加了 lefttop 属性

在测试用例中增加了 lefttop 属性,与现有的 xy 值对齐,这可能会提高测试的全面性。请确保这些属性在测试中被正确使用,并且没有引入任何不一致。

代码更改已被批准。

运行以下脚本以验证 lefttop 在测试中的使用情况:

tests/align.test.tsx (1)

29-30: 增加了 lefttop 属性

在测试用例中增加了 lefttop 属性,与现有的 xy 值对齐,这可能会提高测试的全面性。请确保这些属性在测试中被正确使用,并且没有引入任何不一致。

代码更改已被批准。

运行以下脚本以验证 lefttop 在测试中的使用情况:

Verification successful

lefttop 属性的使用已验证

在测试文件中,lefttop 属性被用于对象属性、样式属性以及测试描述中,表明它们在测试中被正确使用。这些属性与定位逻辑一致,符合测试的目标。

  • 确认 lefttop 在测试用例中被正确使用。
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:验证测试中 `left` 和 `top` 的使用情况。

# 测试:搜索函数使用情况。预期:只有新签名的出现。
rg --type typescript -A 5 $'left' $'top'

Length of output: 71


Script:

#!/bin/bash
# 描述:验证测试中 `left` 和 `top` 的使用情况。

# 测试:搜索 `left` 和 `top` 在 .tsx 文件中的使用情况。
rg --type tsx -A 5 'left' | rg 'top'

Length of output: 62


Script:

#!/bin/bash
# 描述:验证测试中 `left` 和 `top` 的使用情况。

# 使用 fd 查找 .tsx 文件,然后使用 rg 搜索 `left` 和 `top`。
fd --extension tsx --exec rg 'left' | rg 'top'

Length of output: 408

tests/flip-visibleFirst.test.tsx (3)

59-60: 批准添加 lefttop 属性。

添加 lefttop 属性有助于提高位置计算的精确性。建议验证这些属性在不同浏览器中的一致性。


68-69: 批准添加 lefttop 属性。

添加 lefttop 属性有助于提高位置计算的精确性。建议验证这些属性在不同浏览器中的一致性。


77-78: 批准添加 lefttop 属性。

添加 lefttop 属性有助于提高位置计算的精确性。建议验证这些属性在不同浏览器中的一致性。

tests/flip.test.tsx (2)

61-62: 批准添加 lefttop 属性。

添加 lefttop 属性有助于提高位置计算的精确性。建议验证这些属性在不同浏览器中的一致性。


70-71: 批准添加 lefttop 属性。

添加 lefttop 属性有助于提高位置计算的精确性。建议验证这些属性在不同浏览器中的一致性。

@consistent-k
Copy link
Contributor Author

ci 挂了

改了一下 给其他的测试用例也补充了top 和 left的值 应该可以跑过了

@@ -212,6 +212,8 @@ export default function useAlign(
};
} else {
const rect = target.getBoundingClientRect();
rect.x = rect.x || rect.left;
rect.y = rect.y || rect.top
Copy link
Member

@afc163 afc163 Aug 30, 2024

Choose a reason for hiding this comment

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

Suggested change
rect.y = rect.y || rect.top
rect.y = rect.y ?? rect.top;

Copy link
Member

Choose a reason for hiding this comment

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

|| 会导致 rect.y 为 0 也会短路。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的 修改了

Copy link

codecov bot commented Aug 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.72%. Comparing base (c629b9c) to head (a68a3db).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #481      +/-   ##
==========================================
+ Coverage   97.71%   97.72%   +0.01%     
==========================================
  Files          13       13              
  Lines         787      791       +4     
  Branches      234      238       +4     
==========================================
+ Hits          769      773       +4     
  Misses         18       18              

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

src/hooks/useAlign.ts Fixed Show fixed Hide fixed
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ad3d5e6 and a68a3db.

Files selected for processing (1)
  • src/hooks/useAlign.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/hooks/useAlign.ts

@@ -212,6 +212,8 @@
};
} else {
const rect = target.getBoundingClientRect();
rect.x = rect.x ?? rect.left;
rect.y = rect.y ?? rect.top

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (99% of all statements in
the enclosing function
have an explicit semicolon).
@afc163 afc163 merged commit f219948 into react-component:master Aug 30, 2024
8 checks passed
@consistent-k
Copy link
Contributor Author

@afc163 没参与过antd的贡献 求问这个改动预计啥时候能更新到antd里呀

@yoyo837
Copy link
Member

yoyo837 commented Sep 3, 2024

我去催个发布

zombieJ pushed a commit that referenced this pull request Sep 3, 2024
@afc163
Copy link
Member

afc163 commented Sep 3, 2024

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.

3 participants