Skip to content

feat(radio): 支持自定义icon的classname#3196

Merged
Miles-hxy merged 2 commits intojdf2e:feat_v3.xfrom
xiaoyatong:feat3/radio-0421
Apr 22, 2025
Merged

feat(radio): 支持自定义icon的classname#3196
Miles-hxy merged 2 commits intojdf2e:feat_v3.xfrom
xiaoyatong:feat3/radio-0421

Conversation

@xiaoyatong
Copy link
Collaborator

@xiaoyatong xiaoyatong commented Apr 21, 2025

🤔 这个变动的性质是?

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

🔗 相关 Issue

💡 需求背景和解决方案

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

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

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

Summary by CodeRabbit

  • Bug Fixes
    • 修复了单选框图标的样式问题,现在在切换状态时会保留并合并原有的图标样式类,确保自定义图标的样式不会被覆盖。
    • 增加了测试用例,验证单选框选中状态下自定义图标样式的正确应用。

@coderabbitai
Copy link

coderabbitai bot commented Apr 21, 2025

Walkthrough

本次变更主要调整了在克隆 React 元素(activeIcon 和 icon)时 className 的赋值方式。现在,组件在合并 className 时,会保留原有元素上的 className,并与动态计算的颜色相关 class 一同合并,而不是仅覆盖为 color() 的结果。这一修改确保了原有图标上的样式不会被覆盖,同时依然应用了根据状态变化的颜色类。

Changes

文件/分组路径 变更摘要
src/packages/radio/radio.taro.tsx
src/packages/radio/radio.tsx
修改 renderIcon 函数中克隆 activeIcon 和 icon 时 className 的合并方式,保留原有 className 并合并动态颜色类
src/packages/radio/test/radio.spec.tsx 修改测试用例,新增带自定义 className 的 icon 和 activeIcon 测试,验证合并后 className 的正确应用

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant RadioComponent
    participant IconElement

    User ->> RadioComponent: 渲染 Radio 组件
    RadioComponent ->> IconElement: 克隆 activeIcon/icon,合并原 className 与 color() class
    IconElement -->> RadioComponent: 返回合并后的元素
    RadioComponent -->> User: 显示带有正确 className 的图标
Loading

Possibly related PRs

  • jdf2e/nutui-react#2443: 该 PR 同样修改了图标元素 className 的合并方式,增强了图标渲染时 CSS 类的保留与叠加。

Suggested reviewers

  • oasis-cloud

Poem

🐰
合并 className,样式不再丢,
颜色与原样,图标更自由。
React 元素巧克隆,
兔子跳跃乐无穷。
代码整洁又一秋!


📜 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 d6f019d and 7844f6e.

📒 Files selected for processing (1)
  • src/packages/radio/__test__/radio.spec.tsx (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (2)
src/packages/radio/__test__/radio.spec.tsx (2)

31-41: 支持自定义图标类名的测试用例已正确实现

测试用例现在正确地验证了Radio组件能够保留自定义图标的className属性。通过为icon属性传递带有test-class类名的Check图标,测试确保了未选中状态下图标的自定义类名被正确保留。

-      <Radio
-        icon={<Check />}
-        activeIcon={<Check />}
-      >
+      <Radio
+        icon={<Check className="test-class" />}
+        activeIcon={<Check className="test-active-class" />}
+      >

测试断言也相应更新,验证了类名的存在:

-    expect(container.querySelector('.nut-icon')).toBeInTheDocument()
+    expect(container.querySelector('.nut-icon')).toHaveClass('test-class')

43-56: 新增测试:验证选中状态下自定义图标类名正确性

这个新测试用例很好地补充了功能验证,确保在选中状态下,activeIcon的自定义类名test-active-class也能被正确保留和应用。通过设置defaultChecked={true},测试模拟了选中状态下的组件行为。

此测试与前一个测试形成了完整的测试覆盖:

  1. 未选中状态下验证icon的类名保留
  2. 选中状态下验证activeIcon的类名保留

这对确保功能的完整性非常重要,很好的改进。

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

@xiaoyatong xiaoyatong requested a review from Miles-hxy April 21, 2025 06:20
@github-actions github-actions bot added action:review This PR needs more reviews (less than 2 approvals) 3.x Target branch 3.x labels Apr 21, 2025
@codecov
Copy link

codecov bot commented Apr 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.55%. Comparing base (8ddee7c) to head (7844f6e).
Report is 6 commits behind head on feat_v3.x.

Additional details and impacted files
@@              Coverage Diff              @@
##           feat_v3.x    #3196      +/-   ##
=============================================
+ Coverage      86.52%   86.55%   +0.02%     
=============================================
  Files            289      289              
  Lines          18725    18725              
  Branches        2818     2820       +2     
=============================================
+ Hits           16202    16207       +5     
+ Misses          2518     2513       -5     
  Partials           5        5              

☔ 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)
src/packages/radio/radio.tsx (1)

82-82: ✅ 优化了图标类名的处理方式

这个改动使得组件可以保留用户为 activeIcon 传递的自定义 className,同时仍然应用内部的样式类。这样能让用户更灵活地自定义图标样式,而不会丢失组件的默认样式。

建议添加测试用例来验证这个功能,静态分析显示这一行没有测试覆盖。可以添加一个测试用例,验证自定义 className 是否被正确保留:

it('应保留自定义图标的 className', () => {
  const activeIcon = <div className="custom-active-icon" />;
  const wrapper = mount(<Radio activeIcon={activeIcon} checked />);
  expect(wrapper.find('.custom-active-icon')).toHaveLength(1);
  expect(wrapper.find('.nut-radio-icon-checked')).toHaveLength(1);
});
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 82-82: src/packages/radio/radio.tsx#L82
Added line #L82 was not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b7a6a20 and d6f019d.

📒 Files selected for processing (2)
  • src/packages/radio/radio.taro.tsx (2 hunks)
  • src/packages/radio/radio.tsx (2 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/packages/radio/radio.tsx

[warning] 82-82: src/packages/radio/radio.tsx#L82
Added line #L82 was not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (3)
src/packages/radio/radio.tsx (1)

91-91: ✅ 优化了未选中状态下图标类名的处理方式

修改后可以保留 icon 元素上的原始 className,同时添加组件内部样式类。这个改动与 activeIcon 的处理方式保持一致,使 API 行为更加一致和可预测。

src/packages/radio/radio.taro.tsx (2)

87-87: ✅ Taro 版本同步支持自定义 activeIcon 的 className

这个改动与 Web 版本保持一致,使 Taro 版本也支持保留用户为 activeIcon 提供的自定义 className,增强了组件的可定制性。这确保了 Web 和 Taro 实现的 API 行为一致性。


96-96: ✅ Taro 版本同步支持自定义 icon 的 className

修改后可以保留 icon 元素上的原始 className,这与 Web 版本的改动一致,保证了两个实现的行为一致性。这样的统一处理方式有助于开发者在不同平台间无缝切换。

@Miles-hxy Miles-hxy merged commit f80257b into jdf2e:feat_v3.x Apr 22, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.x Target branch 3.x action:review This PR needs more reviews (less than 2 approvals) size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants