Skip to content

feat: support name prop to enable grouping #284

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 2 commits into from
Nov 21, 2024

Conversation

thinkasany
Copy link
Contributor

@thinkasany thinkasany commented Nov 20, 2024

close ant-design/ant-design#51698

image

Summary by CodeRabbit

Summary by CodeRabbit

  • 新功能
    • 新增了 App 组件,包含分段选择功能,支持选择 'iOS'、'Android' 和 'Web' 选项。
    • Segmented 组件现在支持可选的 name 属性,以便对单选按钮进行分组。
    • 文档中新增了名为 "name" 的部分,引用了新组件的代码示例。
  • 文档
    • 更新了文档 docs/example.md,增加了新部分和代码引用。
  • 测试
    • 新增测试用例,验证 Segmented 组件生成的所有单选输入元素是否具有指定的 name 属性。

Copy link

coderabbitai bot commented Nov 20, 2024

Walkthrough

此次更改引入了一个新的 React 组件 App,该组件位于 docs/demo/name.tsx 文件中,包含一个配置了 name 属性和选项的 Segmented 组件。文档文件 docs/example.md 中新增了一个名为 "name" 的部分,引用了新组件的代码片段。同时,在 src/index.tsx 文件中,为 SegmentedProps 接口和 InternalSegmentedOption 组件添加了一个可选的 name 属性,以支持分组的单选输入元素。

Changes

文件路径 更改摘要
docs/demo/name.tsx 新增组件 App,包含 Segmented 组件,配置了 name 和选项。
docs/example.md 新增部分 ## name,并添加了对 ./demo/name.tsx 的代码引用。
src/index.tsx SegmentedProps 接口和 InternalSegmentedOption 组件添加了可选属性 name

Assessment against linked issues

Objective Addressed Explanation
支持 name 属性以启用分组 (#[51698])
Segmented 组件的子输入添加 name 属性 (#[51698])

Possibly related PRs

Poem

在草地上跳跃的小兔子,
新组件来啦真欢喜,
选项分组更方便,
文档更新齐欢颜。
让我们一起欢庆吧,
代码世界更精彩! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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 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

🧹 Outside diff range and nitpick comments (2)
docs/demo/name.tsx (1)

5-17: 建议增强示例的实用性

当前示例虽然展示了 name 属性的基本用法,但建议通过以下方式增强演示效果:

  1. 添加多个 Segmented 组件来展示分组功能
  2. 展示 name 属性在表单中的实际应用场景
 export default function App() {
   return (
     <div>
       <div className="wrapper">
         <Segmented
           name="group"
           options={['iOS', 'Android', 'Web']}
           onChange={(value) => console.log(value, typeof value)}
         />
+        <Segmented
+          name="group"
+          options={['开发版', '测试版', '正式版']}
+          onChange={(value) => console.log(value, typeof value)}
+        />
       </div>
     </div>
   );
 }
docs/example.md (1)

35-38: 文档结构清晰,格式规范!

新增的 name 示例部分保持了与其他示例相同的文档结构和格式规范,便于用户查阅和理解。

建议在该部分添加简要说明,以帮助用户更好地理解 name 属性的用途和使用场景。

示例:

 ## name
+
+用于设置分组的名称,使多个 Segmented 组件能够进行分组管理。
 
 <code src="./demo/name.tsx"></code>
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6bedd52 and 9e6b3e8.

📒 Files selected for processing (3)
  • docs/demo/name.tsx (1 hunks)
  • docs/example.md (1 hunks)
  • src/index.tsx (6 hunks)
🔇 Additional comments (3)
docs/demo/name.tsx (1)

1-3: 导入声明看起来很合理!

导入语句简洁明了,包含了必要的依赖项。

src/index.tsx (2)

43-43: 接口定义正确且完整

name 属性的添加符合 TypeScript 接口定义规范,使用可选属性标记很恰当。


82-82: 验证 name 属性的传递链路

name 属性在组件层级间的传递实现正确,并且成功应用到了 radio input 元素上。建议验证以下几点:

  1. 在表单提交时,确认 name 分组功能正常工作
  2. 测试多个 Segmented 组件使用相同或不同 name 值的场景
  3. 确保与现有表单库的兼容性

Also applies to: 95-95, 112-112, 142-142, 216-216

<Segmented
name="group"
options={['iOS', 'Android', 'Web']}
onChange={(value) => console.log(value, typeof value)}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

优化 onChange 处理函数

建议移除 console.log,改用更实用的状态管理方式来展示选中值的变化。

-          onChange={(value) => console.log(value, typeof value)}
+          onChange={(value) => {
+            // 在实际应用中更新状态
+            setSelectedPlatform(value);
+          }}

Committable suggestion skipped: line range outside the PR's diff.

@thinkasany
Copy link
Contributor Author

@zombieJ 佬,空的时候帮忙review一下~

@afc163
Copy link
Member

afc163 commented Nov 20, 2024

加个测试用例

Copy link

codecov bot commented Nov 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.21%. Comparing base (6bedd52) to head (3a5906d).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #284   +/-   ##
=======================================
  Coverage   99.21%   99.21%           
=======================================
  Files           2        2           
  Lines         127      127           
  Branches       29       29           
=======================================
  Hits          126      126           
  Misses          1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@thinkasany
Copy link
Contributor Author

thinkasany commented Nov 20, 2024

加个测试用例

@afc163 done

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

🧹 Outside diff range and nitpick comments (1)
tests/index.test.tsx (1)

654-665: 测试用例实现正确,建议补充边界场景测试

测试用例正确验证了 name 属性的基本功能,确保所有单选输入框都能正确接收 name 属性值。

建议添加以下测试场景以提高覆盖率:

  1. 验证 name 属性为空字符串的情况
  2. 验证动态更新 name 属性的情况
  3. 验证带有禁用选项时的 name 属性传递

示例代码:

it('should handle empty name property', () => {
  const { container } = render(
    <Segmented options={['iOS', 'Android', 'Web']} name="" />,
  );
  container
    .querySelectorAll<HTMLInputElement>('input[type="radio"]')
    .forEach((el) => {
      expect(el.name).toEqual('');
    });
});

it('should handle name property updates', () => {
  const { container, rerender } = render(
    <Segmented options={['iOS', 'Android', 'Web']} name="group1" />,
  );
  
  rerender(<Segmented options={['iOS', 'Android', 'Web']} name="group2" />);
  
  container
    .querySelectorAll<HTMLInputElement>('input[type="radio"]')
    .forEach((el) => {
      expect(el.name).toEqual('group2');
    });
});

it('should handle name property with disabled options', () => {
  const { container } = render(
    <Segmented 
      options={[
        'iOS',
        { label: 'Android', value: 'Android', disabled: true },
        'Web'
      ]} 
      name="group"
    />,
  );
  
  container
    .querySelectorAll<HTMLInputElement>('input[type="radio"]')
    .forEach((el) => {
      expect(el.name).toEqual('group');
    });
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9e6b3e8 and 3a5906d.

📒 Files selected for processing (1)
  • tests/index.test.tsx (1 hunks)

@afc163 afc163 merged commit daedf88 into react-component:master Nov 21, 2024
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.

Segmented: add support for name prop to enable grouping
2 participants