Skip to content

feat: Add attribute itemRender to 'segmented' #301

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

Merged
merged 9 commits into from
Jul 2, 2025

Conversation

EmilyyyLiu
Copy link
Contributor

@EmilyyyLiu EmilyyyLiu commented Jul 1, 2025

增加 itemRender 属性,用来渲染自定义label内容

Summary by CodeRabbit

  • 新功能

    • 为分段选择组件新增可选属性 itemRender,允许自定义每个选项内容的渲染方式。
  • 测试

    • 增加了针对 itemRender 属性的单元测试,确保自定义渲染和默认渲染均正常工作。

Copy link

coderabbitai bot commented Jul 1, 2025

"""

Walkthrough

新增了 Segmented 组件的可选 itemRender 属性,支持通过传入函数自定义每个选项的渲染内容。InternalSegmentedOption 组件接收该属性并应用于标签渲染。新增测试覆盖默认渲染及自定义渲染两种情况。

Changes

文件/分组 变更摘要
src/index.tsx SegmentedPropsInternalSegmentedOption 增加 itemRender 可选属性,实现选项内容自定义渲染。
tests/index.test.tsx 新增 itemRender 测试用例,验证默认渲染和自定义渲染函数的行为。

Suggested reviewers

  • zombieJ

Poem

🐰
小兔子写代码,
Segmented 迎新来。
itemRender 随心变,
标签内容任你改。
测试通过真愉快,
组件更灵活,大家都爱!

"""

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

tests/index.test.tsx

Oops! Something went wrong! :(

ESLint: 7.32.0

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.


📜 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 b876296 and 50e4846.

📒 Files selected for processing (1)
  • tests/index.test.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/index.test.tsx
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • 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
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests 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.

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

🧹 Nitpick comments (1)
src/index.tsx (1)

101-105: 建议优化类型定义结构

当前使用交叉类型的方式可以工作,但可以考虑更清晰的接口继承方式来提高可读性。

可以考虑以下重构:

-const InternalSegmentedOption: React.FC<
-  {
-    itemRender?: (node: React.ReactNode, itemInfo: ItemInfo) => React.ReactNode;
-  } & ItemInfo
-> = (props) => {
+interface InternalSegmentedOptionProps extends ItemInfo {
+  itemRender?: (node: React.ReactNode, itemInfo: ItemInfo) => React.ReactNode;
+}
+
+const InternalSegmentedOption: React.FC<InternalSegmentedOptionProps> = (props) => {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8ee5eef and 0fefb75.

📒 Files selected for processing (1)
  • src/index.tsx (6 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/index.tsx

[error] 168-168: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: WIP
🔇 Additional comments (6)
src/index.tsx (6)

47-47: itemRender 属性签名设计合理

新增的 itemRender 属性提供了 nodeitemInfo 两个参数,很好地解决了之前评论中提到的问题——如何知道要渲染的是哪个 item。通过 ItemInfo 对象提供了完整的上下文信息。


78-99: ItemInfo 接口设计完善

ItemInfo 接口包含了渲染自定义内容所需的所有必要信息,包括样式、状态、事件处理器等。这个设计有助于用户在自定义渲染时获得完整的上下文。


118-118: 默认实现符合最佳实践

itemRender 提供默认的恒等函数实现是很好的做法,确保了组件的向后兼容性。


132-166: 渲染逻辑重构合理

将 JSX 内容提取到 ItemContent 变量中,然后通过 itemRender 函数处理,这种模式符合 React 的渲染属性模式最佳实践。


189-189: 正确地转发 itemRender 属性

在主组件中正确地解构并准备转发 itemRender 属性给子组件。


314-314: 属性传递实现正确

正确地将 itemRender 属性传递给每个 InternalSegmentedOption 实例,确保自定义渲染功能能够正常工作。

Copy link

codecov bot commented Jul 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.39%. Comparing base (17b4ba3) to head (50e4846).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #301      +/-   ##
==========================================
+ Coverage   99.37%   99.39%   +0.01%     
==========================================
  Files           2        2              
  Lines         161      164       +3     
  Branches       40       39       -1     
==========================================
+ Hits          160      163       +3     
  Misses          1        1              

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

@EmilyyyLiu EmilyyyLiu changed the title [WIP]feat: Add attribute itemRender to 'segmented' feat: Add attribute itemRender to 'segmented' Jul 2, 2025
src/index.tsx Outdated
disabled?: boolean;
checked: boolean;
label: React.ReactNode;
title?: string;
Copy link
Member

Choose a reason for hiding this comment

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

直接用 SegmentedLabeledOption

@zombieJ zombieJ merged commit 23a6ea7 into react-component:master Jul 2, 2025
8 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