Skip to content

feat: support labelRender prop #643

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

inottn
Copy link

@inottn inottn commented Apr 28, 2025

Allows custom rendering of the currently selected label, which can be used for value backfill but the corresponding option is missing and does not want to directly render the value.

Summary by CodeRabbit

  • 新功能

    • 现在支持通过 labelRender 属性自定义 TreeSelect 组件已选项的标签显示内容。
  • 测试

    • 新增了针对 labelRender 属性的用例,确保自定义标签渲染功能的正确性。

Copy link

vercel bot commented Apr 28, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tree-select ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 28, 2025 6:52am

Copy link

coderabbitai bot commented Apr 28, 2025

Walkthrough

本次变更为 TreeSelect 组件引入了一个新的可选属性 labelRender,允许开发者自定义已选项的标签渲染方式。在组件内部,displayValues 的计算逻辑被调整:如提供了 labelRender,则使用其返回值作为标签,否则回退为原有的 label 或 value。测试文件新增了两组用例,分别验证了 labelRender 的基本功能及其在 value 不在 treeData 时的表现。

Changes

文件路径 变更摘要
src/TreeSelect.tsx 新增 labelRender 可选属性至 TreeSelectProps,并在渲染已选项标签时调用该函数。
tests/Select.spec.tsx 新增两组测试用例,验证 labelRender 的渲染逻辑及其在 value 不在 treeData 时的表现。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant TreeSelect
    participant labelRender

    User->>TreeSelect: 选择节点
    TreeSelect->>labelRender: 调用 labelRender(item)
    labelRender-->>TreeSelect: 返回自定义标签
    TreeSelect-->>User: 显示自定义标签
Loading

Suggested reviewers

  • zombieJ

Poem

🐇
小树枝上选标签,
labelRender 现身边。
自定义的名字闪,
测试用例保平安。
代码更新新体验,
兔子挥笔乐无边!

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.

src/TreeSelect.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

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.

tests/Select.spec.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

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 6fa2d68 and e0a5e7d.

📒 Files selected for processing (2)
  • src/TreeSelect.tsx (4 hunks)
  • tests/Select.spec.tsx (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/TreeSelect.tsx (1)
src/interface.ts (1)
  • LabeledValueType (14-20)
🔇 Additional comments (6)
src/TreeSelect.tsx (4)

97-97: 清晰的类型定义!

新增的 labelRender 属性类型定义恰当,使用 (props: LabeledValueType) => React.ReactNode 允许开发者自定义已选项的标签渲染方式。这与 PR 目标完全吻合。


165-165: 正确解构获取 labelRender 属性

属性解构位置正确,放在了 Options 相关属性的区域内。


400-400: 优雅的标签渲染实现

此处实现逻辑清晰:当 labelRender 函数存在时,使用它来渲染标签;否则使用原有的 label 或 value 作为后备方案。这种实现方式保持了向后兼容性,同时提供了更大的灵活性。


411-411: 依赖数组已正确更新

labelRender 添加到 useMemo 的依赖数组中确保了当 labelRender 函数变化时,displayValues 会被重新计算,避免了潜在的过时闭包问题。

tests/Select.spec.tsx (2)

709-726: 测试用例覆盖了基本功能

这个测试用例验证了 labelRender 的基本功能:

  1. 确认 labelRender 函数被调用
  2. 验证渲染结果正确地组合了标签和值

测试用例设计合理,确保了新功能的正常工作。


728-748: 测试用例覆盖了重要的边缘情况

这个测试用例验证了值不在 treeData 中的情况(即 PR 描述中提到的"值回填场景"):

  1. 确认即使值不在 treeData 中,labelRender 函数仍然被调用
  2. 验证在没有标签时使用默认标签的回退逻辑
  3. 确保最终渲染结果符合预期

这个测试用例对确保功能在各种场景下的正确性非常重要。

✨ 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 generate sequence diagram to generate a sequence diagram of the changes in 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.

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.

1 participant