Skip to content

feat(suggestion): onSelect 回调增加当前 item 信息 #718

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 3 commits into
base: main
Choose a base branch
from

Conversation

HomyeeKing
Copy link

@HomyeeKing HomyeeKing commented Apr 10, 2025

中文版模板 / Chinese template

🤔 This is a ...

  • 🆕 New feature
  • 🐞 Bug fix
  • 📝 Site / documentation improvement
  • 📽️ Demo improvement
  • 💄 Component style improvement
  • 🤖 TypeScript definition improvement
  • 📦 Bundle size optimization
  • ⚡️ Performance optimization
  • ⭐️ Feature enhancement
  • 🌐 Internationalization
  • 🛠 Refactoring
  • 🎨 Code style optimization
  • ✅ Test Case
  • 🔀 Branch merge
  • ⏩ Workflow
  • ⌨️ Accessibility improvement
  • ❓ Other (about what?)

🔗 Related Issues

  • Describe the source of related requirements, such as links to relevant issue discussions.
  • For example: close #xxxx, fix #xxxx

💡 Background and Solution

  • The specific problem to be addressed.
  • List the final API implementation and usage if needed.
  • If there are UI/interaction changes, consider providing screenshots or GIFs.

onSelect 方法回调一个当前选中的 item,此外开发者可以在 item对象上扩展额外信息,增加泛型

 <Suggestion
      items={suggestions}
      onSelect={(
		itemVal, 
+		item
		) => {
      		  setValue(`[${itemVal}]:`);
+				console.log('item', item)
     	}}
    /> 

📝 Change Log

Language Changelog
🇺🇸 English
🇨🇳 Chinese

Summary by CodeRabbit

  • 新功能
    • 建议组件现在在选项选择时提供额外的详细信息,增强了交互体验。
  • 文档
    • 更新了相关文档,确保说明最新接口使用方式。
  • 重构
    • 优化了组件接口,提升了类型安全性和灵活性。

Copy link
Contributor

coderabbitai bot commented Apr 10, 2025

📝 Walkthrough

Walkthrough

此次修改主要在 Suggestion 组件中增强了类型安全性和灵活性。更新引入了泛型参数,用于定义 SuggestionItemitemschildrenonSelect 等属性的类型。在文档(英语和中文)和代码实现中,对传递参数的签名进行了调整,使得回调函数能够接收更多的上下文信息,例如在 onSelect 中增加了选中的项,从而使组件在处理选择事件时具有更全面的信息支持。

Changes

文件 变更摘要
components/suggestion/index.en-US.md
components/suggestion/index.zh-CN.md
更新 SuggestionItem 类型定义:将 items 从 `SuggestionItem[]
components/suggestion/index.tsx SuggestionItem 引入泛型参数 T(默认 Record<string, any>);更新 onSelect 属性功能,使其接收两个参数(valueitem);调整 onInternalChange 函数接收选项数组参数,从而传递更多选项上下文。
components/suggestion/useActive.ts 修改 onSelect 函数签名,由单一的 string[] 参数改为 (value: string[], options: SuggestionItem[]) => void;在 onKeyDown 事件中调用时增加对选项(getItems)的传入,确保选中值与选项数据同时传递。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant SC as Suggestion组件
    participant IS as 内部逻辑
    participant CB as onSelect回调

    U->>SC: 触发键盘事件
    SC->>IS: 调用 getValues(activePaths) 获取值
    SC->>IS: 调用 getItems(activePaths) 获取选项
    SC->>CB: 调用 onSelect(value, selectedOptions) 传递值和选项
Loading

Suggested reviewers

  • YumoImer

Poem

我是一只快乐的小兔子,
在代码森林中奔跑无忧,
泛型如同新鲜的萝卜,
回调携带信息更周全,
代码跃动,梦想正飞扬!
🐇💻🌟

✨ 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 plan to trigger planning for file edits and PR creation.
  • @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
Contributor

Prepare preview

Copy link

Walkthrough

This pull request introduces a new feature to the onSelect method by adding a callback that returns the currently selected item. Additionally, developers can extend extra information on the item object by using generics.

Changes

Files Summary
components/suggestion/index.en-US.md, components/suggestion/index.zh-CN.md Updated documentation to reflect changes in the onSelect method and SuggestionItem type.
components/suggestion/index.tsx Modified SuggestionItem type to support generics and updated onSelect callback to include the selected item.
components/suggestion/useActive.ts Adjusted onSelect function to pass the selected options along with the value.

@HomyeeKing HomyeeKing changed the title feat(suggestion): onSelect 增加回调 feat(suggestion): onSelect 回调增加当前 item 信息 Apr 10, 2025
Copy link
Contributor

@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 (3)
components/suggestion/index.zh-CN.md (3)

34-34: 建议项类型定义更新:检查泛型参数设计
在第 34 行中,将 items 的类型更新为 SuggestionItem<T>[] | ((info: T) => SuggestionItem<K>[]),通过引入两个泛型参数提升了类型安全性和灵活性。请确认此处 T 与 K 在组件应用场景中的用途和区别,并在文档中简要说明,以便开发者了解何时应使用函数形式返回建议项以及两者的语义差异。


37-37: onSelect 回调更新:参数扩展说明建议
第 37 行中,onSelect 的类型由原来的 (value: string) => void 更新为 (value: string, item: SuggestionItem<T>) => void,这增强了回调的可用信息。建议在文档中补充一个简单示例,说明如何利用新增加的 item 参数来获得更多上下文信息,从而帮助开发者更好地理解和使用此特性。


50-57: SuggestionItem 接口更新:增强子项目类型安全性
在第 50 至 57 行中,对 SuggestionItem 接口进行了更新,将 children 属性的类型改为 SuggestionItem<T>[]。此调整使得子项目可以继承泛型,提高类型一致性。建议在文档中增加一小段描述,展示如何利用此扩展来定制子项目的结构,从而让用户更直观地了解修改前后的区别和优势。

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 87246f3 and 095bdb3.

📒 Files selected for processing (4)
  • components/suggestion/index.en-US.md (2 hunks)
  • components/suggestion/index.tsx (4 hunks)
  • components/suggestion/index.zh-CN.md (2 hunks)
  • components/suggestion/useActive.ts (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
components/suggestion/index.tsx (1)
components/x-provider/demo/use.tsx (1)
  • value (24-143)
🔇 Additional comments (10)
components/suggestion/useActive.ts (3)

3-3: 导入类型声明无误
导入 SuggestionItem 类型的写法正确,符合当前文件的使用需求。


12-12: 回调签名更新需要确认调用处
本次将 onSelect 的参数从单一字符串数组扩展为 (value: string[], options: SuggestionItem[]),功能更强大。建议确认所有调用处、类型声明和文档能正确匹配此新签名。


106-106: 在回调中传入完整选项信息
调用 onSelect 时,新增了对被选择项的数组传递,可提供更丰富的上下文数据。实现看起来合理。

components/suggestion/index.tsx (4)

11-20: 泛型扩展设计合理
SuggestionItem 定义为可扩展的泛型 { ... } & T,在复用性与自定义场景下更具灵活性,设计良好。


36-36: onSelect 回调参数扩展
onSelect?: (value: string, item: SuggestionItem) => void; 新增第二个参数可提供更多信息,增强了可用性。


110-110: onInternalChange 方法签名变化
新增 selectedOptions: SuggestionItem[],确保在最终回调中传递完整的选项数据。此变更与之前的泛型调整保持一致。


112-112: 回调传参保持一致性
在此处使用 valuePath[valuePath.length - 1] 和相应的 selectedOptions 项生成更直观的单个值与选项。实现无问题。

components/suggestion/index.en-US.md (3)

33-33: items 属性类型扩展
items 定义为 SuggestionItem<T>[] | ((info: T) => SuggestionItem<K>[]),强化在传入不同泛型时的灵活度,文档更新正确。


36-36: onSelect 文档更新
onSelect 回调新增 (value: string, item: SuggestionItem<T>),可同时获取所选值与对应对象,有利于更精细的处理。


49-55: SuggestionItem 表格说明完善
childrenextraiconlabelvalue 等属性在表格中进行了更详尽的列举,且结合了泛型声明,一目了然。

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