Skip to content

fix(Anchor): render children when items is undefined - #59211

Closed
dogledogle wants to merge 1 commit into
ant-design:masterfrom
dogledogle:fix/anchor-undefined-items
Closed

dogledogle wants to merge 1 commit into
ant-design:masterfrom
dogledogle:fix/anchor-undefined-items

Conversation

@dogledogle

Copy link
Copy Markdown
Contributor

🤔 这个变动的性质是?

  • 🆕 新特性提交
  • 🐞 Bug 修复
  • 📝 站点、文档改进
  • 📽️ 演示代码改进
  • 💄 组件样式/交互改进
  • 🤖 TypeScript 定义更新
  • 📦 包体积优化
  • ⚡️ 性能优化
  • ⭐️ 功能增强
  • 🌐 国际化改进
  • 🛠 重构
  • 🎨 代码风格优化
  • ✅ 测试用例
  • 🔀 分支合并
  • ⏩ 工作流程
  • ⌨️ 无障碍改进
  • ❓ 其他改动(是关于什么的改动?)

🔗 相关 Issue

N/A

💡 需求背景和解决方案

Anchor 使用属性是否存在来决定渲染 items 还是 children,导致显式传入 items={undefined} 时合法的 fallback children 被静默丢弃。

本次调整为仅在 items 不为 undefined 时使用数据配置,并补充回归测试。空数组仍会明确覆盖 children,不涉及公开 API 变更。

📝 更新日志

语言 更新描述
🇺🇸 英文 🐞 Fix Anchor dropping children when items={undefined} is explicitly provided.
🇨🇳 中文 🐞 修复 Anchor 显式传入 items={undefined} 时丢弃 children 的问题。

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@github-actions

github-actions Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

👁 Visual Regression Report for PR #59211 Passed ✅

🎯 Target branch: master (d0776d0)
⚠️ Baseline may be stale: no snapshot for the PR's base commit was found, so an older branch snapshot was used. Some diffs below may come from changes already merged into master rather than this PR.
📖 View Full Report ↗︎
📖 Alternative Report ↗︎

🎊 Congrats! No visual-regression diff found.

📖 中文报告

👁 PR #59211 视觉回归报告 通过 ✅

🎯 目标分支: master (d0776d0)
⚠️ 基线可能已过期: 未找到该 PR 基准提交对应的快照,因此回退使用了较旧的分支快照。下方部分差异可能来自已合并到 master 的改动,而非本 PR。

🎊 恭喜!未发现视觉回归差异。

@github-actions

github-actions Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Preview is ready

@coderabbitai

coderabbitai Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: d6e3b1e8-0608-49a3-b3f7-3e112c58c845

📥 Commits

Reviewing files that changed from the base of the PR and between 4e86b57 and 8090af7.

📒 Files selected for processing (2)
  • components/anchor/Anchor.tsx
  • components/anchor/__tests__/Anchor.test.tsx

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Summary

Summary by CodeRabbit

  • Bug Fixes

    • 修复 Anchor 在未提供链接项(显式为 undefined)时无法显示子内容的问题;现在会正确渲染传入的子元素。
  • Tests

    • 新增测试,验证上述场景下子元素能够正常显示。

Walkthrough

Anchor 现在仅在 items 不为 undefined 时创建嵌套链接。显式传入 items={undefined} 时,组件会渲染 children。新增测试覆盖该行为。

Changes

Anchor 子元素回退

Layer / File(s) Summary
Anchor 渲染分支与回归测试
components/anchor/Anchor.tsx, components/anchor/__tests__/Anchor.test.tsx
渲染条件改为 items !== undefined。新增测试,确认 items={undefined} 时显示 children 内容。

Estimated code review effort: 1 (简单) | ~5 分钟

Suggested reviewers: afc163, qdyanbing

Merge Risk: ⚪ Minimal · up to ade17

Anchor now renders child links when items is undefined while preserving empty-array override behavior. The targeted regression coverage supports merge readiness.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确描述了主要修复内容:当 items 为 undefined 时,Anchor 渲染 children。标题简洁且与变更一致。
Description check ✅ Passed 描述说明了 Anchor 的缺陷、修复方案、空数组行为和回归测试,与变更内容一致。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

小兔检查 Anchor 旁
undefined 不再藏
children 轻轻登场
测试守住新方向
萝卜庆祝渲染亮

Comment @coderabbitai help to get the list of available commands.

@coderabbitai
coderabbitai Bot requested review from QDyanbing and afc163 September 4, 2026 10:14
@pkg-pr-new

pkg-pr-new Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
More templates

npm i https://pkg.pr.new/antd@59211

commit: ade177f

@codecov

codecov Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Bundle Report

Changes will decrease total bundle size by 2 bytes (-0.0%) ⬇️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
antd.min-array-push 3.24MB -2 bytes (-0.0%) ⬇️

Affected Assets, Files, and Routes:

view changes for bundle: antd.min-array-push

Assets Changed:

Asset Name Size Change Total Size Change (%)
antd-with-locales.min.js -1 bytes 1.81MB -0.0%
antd.min.js -1 bytes 1.43MB -0.0%

@codecov

codecov Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (6a642e3) to head (ade177f).

Additional details and impacted files
@@            Coverage Diff            @@
##            master    #59211   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          834       834           
  Lines        16022     16022           
  Branches      4206      4206           
=========================================
  Hits         16022     16022           

☔ View full report in Codecov by Harness.
📢 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.

@nrps9909 nrps9909 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed exact head 8090af7960402f5da80751dd95a6a942a0b30224.

The added regression fails with the base 4e86b57c2d2fd6c9ca6532c37e22da9c536d5b62 implementation because the fallback child is absent. Restoring this change passes the complete Anchor Jest suite: 8 suites, 96 tests and 41 snapshots, including two independent review probes.

Those probes use real Anchor.Link children and cover omitted/undefined items, explicit empty-array precedence, data-item precedence, nested children, and repeated transitions from undefined to empty array to data and back to undefined. Existing rendering, click, scroll, accessibility and demo coverage also passes. ESLint, Biome and diff checks pass for the two changed files; all 42 applicable remote checks pass (two workflow checks are skipped).

The condition preserves an explicit items={[]} as an intentional override while restoring the children fallback for an absent value.

@dogledogle
dogledogle force-pushed the fix/anchor-undefined-items branch from 8090af7 to 7cd0237 Compare September 10, 2026 05:29
<div className={anchorClass}>
<span className={inkClass} ref={spanLinkNodeRef} style={mergedStyles.indicator} />
{'items' in props ? createNestedLink(items) : children}
{items !== undefined ? createNestedLink(items) : children}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Image

不过我倾向于暂不合入这个改动。items 与 JSX children 是新旧两套 API,children 已标记为 deprecated;目前实现自 data-driven API 引入后便以 items 属性是否存在来决定渲染来源。
items={undefined} 同时传入 legacy children 的场景比较边缘,目前也没有对应的实际用户问题或复现。对于需要兜底 UI 的业务,建议在调用侧明确处理,例如:

condition ? <Anchor items={anchorItems} /> : <Fallback />

如果兜底内容本身也是一组 Anchor 链接,也可以统一转换为 items 数据后传入,避免混用两套 API。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK,学习了。那这个 PR 是关掉还是等等其他人的看法?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

都可以。如果你也觉得没必要的话,可以关了。 觉得有必要的话,可以等其他人建议。

@dogledogle
dogledogle force-pushed the fix/anchor-undefined-items branch from 7cd0237 to ade177f Compare September 12, 2026 10:30
@dogledogle

Copy link
Copy Markdown
Contributor Author

边缘场景,若后面有实际用户反馈再做处理。

@dogledogle dogledogle closed this Sep 12, 2026
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