Skip to content

Conversation

@cncolder
Copy link
Contributor

@cncolder cncolder commented Dec 15, 2025

Summary by CodeRabbit

  • 修复

    • 提升对话框显示准备阶段的运行时稳健性:增加检查以避免在缺失底层元素时发生空值访问,从而减少因为此类情况导致的异常,提升稳定性和用户体验。
  • 测试

    • 新增测试用例,确保在底层元素不可用时触发准备流程不会抛出异常,验证修复的有效性。

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Dec 15, 2025

@cncolder is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Dec 15, 2025

Walkthrough

src/Dialog/Content/index.tsxonPrepare 内新增防卫检查:当 dialogRef.current?.nativeElement 不存在时立即返回,防止在随后计算 elementOffset 时访问 null/undefined。另新增测试以覆盖该行为。公共 API 或导出声明未变更。

Changes

内聚体 / 文件 变更摘要
运行时安全增强
src/Dialog/Content/index.tsx
onPrepare 中加入存在性检查:若 dialogRef.current?.nativeElement 不存在则提前返回,避免对 elementOffset 的 null/undefined 访问
测试覆盖新增
tests/index.spec.tsx
添加两条测试,分别在 dialog 和 mousePosition 上确认当 nativeElement 不可用时,调用 onAppearPrepare 不抛出异常(在渲染前后各验证一次)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 分钟

  • 关注点:
    • 验证防卫检查条件与提前返回逻辑是否恰当
    • 确认正常路径下 elementOffset 的计算不被阻断
    • 检查新增测试是否充分覆盖前后渲染场景及边缘条件

Poem

🐰 我在准备前静静守候,
若无足迹便轻轻退后,
不叫空值扰乱一场舞,
小小防护换来宁静秋,
代码林里又添一抹柔 🌿

Pre-merge checks and finishing touches

❌ 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 The title accurately summarizes the main change: adding a guard to prevent errors when nativeElement is unavailable. It is concise, specific, and directly related to the primary modification in the changeset.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • 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.

@cncolder
Copy link
Contributor Author

cncolder commented Dec 15, 2025

Demo

https://stackblitz.com/edit/mngzgsqm?file=src%2Fdemo.tsx

Dependencies

  • react 19.2.3
  • antd 6.1.0
  • @rc-component/dialog 1.5.1
image image

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/Dialog/Content/index.tsx (1)

40-44: 考虑为 dialogRef.current 添加空值检查以保持一致性。

onPrepare 中新增的保护措施类似,这里访问 dialogRef.current.inMotiondialogRef.current.enableMotion 时也可能遇到 dialogRef.current 为空的情况。建议添加空值检查以提高代码的健壮性。

可以考虑使用以下方式:

 React.useImperativeHandle(ref, () => ({
   ...panelRef.current,
-  inMotion: dialogRef.current.inMotion,
-  enableMotion: dialogRef.current.enableMotion,
+  inMotion: dialogRef.current?.inMotion,
+  enableMotion: dialogRef.current?.enableMotion,
 }));
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c3bbe74 and b3bd49d.

📒 Files selected for processing (1)
  • src/Dialog/Content/index.tsx (1 hunks)
🔇 Additional comments (1)
src/Dialog/Content/index.tsx (1)

55-56: 良好的防御性编程实践。

此保护措施有效防止了当 nativeElement 不可用时访问空引用的错误,逻辑正确。

}

function onPrepare() {
if (!dialogRef.current?.nativeElement) return;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!dialogRef.current?.nativeElement) return;
if (!dialogRef.current?.nativeElement) {
return;
}

什么情况下会没有,补充这个情况的测试用例,尝试跑到这一行。

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
Contributor Author

Choose a reason for hiding this comment

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

jsdom 环境下可能没有办法测试 iframe, 我只能添加测试用例覆盖到这一行.

@codecov
Copy link

codecov bot commented Dec 15, 2025

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #537      +/-   ##
==========================================
+ Coverage   98.41%   98.42%   +0.01%     
==========================================
  Files           8        8              
  Lines         189      191       +2     
  Branches       66       68       +2     
==========================================
+ Hits          186      188       +2     
  Misses          3        3              

☔ 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 (1)
tests/index.spec.tsx (1)

282-292: 新增用例方向正确,但建议减少对其他用例副作用的依赖

这个测试很好地抓住了“nativeElement 不可用时调用 onAppearPrepare 不应抛错”的核心行为,整体思路没问题。但当前有两点可以稍微收紧一下:

  1. 第一个 expect(() => { global.onAppearPrepare?.(); }).not.toThrow(); 在本用例内部没有任何 render,它隐式依赖于前面其它用例已经给 global.onAppearPrepare 赋值;如果这个用例单独执行或用例顺序调整,这一段要么什么都不做(onAppearPrepareundefined),要么行为取决于上一个用例的状态,可读性和稳健性都稍弱。
  2. 第二次调用前虽然有一次 render(<Dialog visible />),但可以考虑在这里也显式构造和本 bug 更接近的场景(例如带 mousePosition 的 Dialog),让测试意图更直接。

可以参考下面两种小改法择一:

  • 如果只关心“当前实现下不抛错”,建议去掉对前一个用例状态的依赖:
-    it('should not throw error when nativeElement is not available', () => {
-      expect(() => {
-        global.onAppearPrepare?.();
-      }).not.toThrow();
-
-      render(<Dialog visible />);
-
-      expect(() => {
-        global.onAppearPrepare?.();
-      }).not.toThrow();
-    });
+    it('should not throw error when nativeElement is not available', () => {
+      render(<Dialog visible />);
+
+      expect(() => {
+        global.onAppearPrepare?.();
+      }).not.toThrow();
+    });
  • 如果希望覆盖“前一次动画留下的 onAppearPrepare + 当前没有 nativeElement”的组合场景,可以在本用例内先显式渲染一次、再触发关闭或卸载,再做第一次调用,这样不用依赖其它测试的执行顺序,同时场景更自说明。

整体来说,用例已经能保护这次修复,以上只是为了让测试更加自洽、抗变更性更好一些,可按需要取舍。

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1ef532e and 3dad7dc.

📒 Files selected for processing (1)
  • tests/index.spec.tsx (1 hunks)

@afc163 afc163 merged commit e5625e3 into react-component:master Dec 15, 2025
9 of 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.

2 participants