Skip to content
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

fix(thought-chain): controlled mode not working #598

Merged
merged 1 commit into from
Mar 19, 2025

Conversation

whitexie
Copy link
Contributor

@whitexie whitexie commented Mar 14, 2025

close #590

Summary by CodeRabbit

  • Tests
    • 新增测试用例,验证组件在预设展开状态下点击操作能正确显示额外内容。
  • Refactor
    • 优化了折叠状态的管理逻辑,通过更明确的配置和回调机制,提升了组件行为的稳定性和一致性。

Copy link
Contributor

coderabbitai bot commented Mar 14, 2025

📝 Walkthrough

Walkthrough

该提交引入了针对 ThoughtChain 组件的新测试用例,用于验证其在受控模式下的行为。测试用例设置了初始展开 key,并检测点击项头后生成的展开体数量是否正确。同时,对 useCollapsible 钩子的状态初始化逻辑进行了调整,从直接使用 customizeExpandedKeys 改为用空数组初始化,并通过选项对象传递 valueonChange 回调,从而实现更明确的状态更新控制。此外,还调整了部分导入语句的顺序,均不影响功能。

Changes

文件路径 更改摘要
components/thought-chain/__tests__/index.test.tsx 新增受控模式下的测试用例;调整部分导入顺序
components/thought-chain/hooks/useCollapsible.ts 修改状态初始化逻辑:由直接传入改为空数组并传递 valueonChange;调整导入顺序

Sequence Diagram(s)

sequenceDiagram
    participant T as 测试用例
    participant C as ThoughtChain 组件
    participant H as useCollapsible 钩子
    participant U as 用户

    T->>C: 用 expandedKeys = ["test3"] 渲染组件
    Note over C: 调用 useCollapsible,初始化状态为 [],设置 value 与 onChange
    U->>C: 点击项头
    C->>H: 触发 onChange 回调
    H->>C: 更新 mergedExpandedKeys 状态
    C-->>T: 组件根据新状态重新渲染
Loading

Suggested reviewers

  • zombieJ

Poem

我是小兔CodeRabbit,跳跃在代码间,
测试新添功能稳,点击间见奇幻展。
状态更新井然有序,如草原上风轻拂,
每行代码皆似诗,闪耀着智慧的火花,
愿代码如草原畅快奔,快乐无忧漫天舞!
🐇✨

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 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 c468b22 and 9d5d6f7.

📒 Files selected for processing (2)
  • components/thought-chain/__tests__/index.test.tsx (2 hunks)
  • components/thought-chain/hooks/useCollapsible.ts (2 hunks)
🔇 Additional comments (5)
components/thought-chain/__tests__/index.test.tsx (3)

2-2: 导入语句的顺序调整

导入语句的顺序调整是一个风格上的变化,不会影响功能。将测试工具相关的导入放在更靠前的位置是合理的。


5-5: 图标导入移动到测试工具后面

CheckCircleOutlined图标导入移动到测试工具后面是一个风格上的调整,不会影响功能。


88-113: 新增控制模式测试用例

这个测试用例很好地验证了ThoughtChain组件在受控模式下的行为。通过设置初始展开key为['test3']并在点击后检查展开体数量,可以验证在受控模式下点击不会改变展开状态。测试逻辑清晰,覆盖了PR的主要修复点。

测试用例验证了在受控模式下,如果父组件不更新expandedKeys,则组件状态不会改变,这与受控模式的预期行为一致。

components/thought-chain/hooks/useCollapsible.ts (2)

2-2: React导入位置调整

将React导入移动到useMergedState导入后面是一个风格上的调整,不会影响功能。


56-61: 修复受控模式状态管理逻辑

这个修改很好地解决了受控模式不工作的问题。通过将mergedExpandedKeys初始化为空数组,并通过选项对象明确指定valueonChange,确保了在受控模式下组件能够正确响应外部状态变化。

原来的实现可能导致内部状态覆盖外部提供的状态,而新的实现尊重外部状态控制,符合React受控组件的设计模式。

-  const [mergedExpandedKeys, setMergedExpandedKeys] = useMergedState<
-    RequiredCollapsibleOptions['expandedKeys']
-  >(customizeExpandedKeys);
+  const [mergedExpandedKeys, setMergedExpandedKeys] = useMergedState<
+    RequiredCollapsibleOptions['expandedKeys']
+  >([], {
+    value: customizeExpandedKeys,
+    onChange: customizeOnExpand,
+  });
✨ 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.

Copy link

github-actions bot commented Mar 14, 2025

Preview is ready

Copy link

Walkthrough

This pull request addresses the issue of the controlled mode not working in the ThoughtChain component. It modifies the useCollapsible hook to properly handle controlled expandedKeys and adds a test case to ensure the controlled mode functions as expected.

Changes

File Summary
components/thought-chain/__tests__/index.test.tsx Added a test case for the ThoughtChain component to verify functionality in controlled mode.
components/thought-chain/hooks/useCollapsible.ts Modified the useCollapsible hook to correctly handle controlled expandedKeys by using useMergedState with value and onChange options.

RequiredCollapsibleOptions['expandedKeys']
>([], {
value: customizeExpandedKeys,
onChange: customizeOnExpand,

Choose a reason for hiding this comment

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

Ensure that customizeOnExpand is defined and properly handles changes to expandedKeys. If customizeOnExpand is not defined, it may lead to unexpected behavior when the expandedKeys change.

Copy link

codecov bot commented Mar 14, 2025

Bundle Report

Bundle size has no change ✅

Copy link

codecov bot commented Mar 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.88%. Comparing base (c468b22) to head (9d5d6f7).
Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #598      +/-   ##
==========================================
+ Coverage   91.81%   91.88%   +0.06%     
==========================================
  Files          67       67              
  Lines        1466     1466              
  Branches      388      388              
==========================================
+ Hits         1346     1347       +1     
+ Misses        120      119       -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.

@afc163 afc163 merged commit bbb2fae into ant-design:main Mar 19, 2025
11 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