Skip to content

Conversation

EmilyyyLiu
Copy link
Contributor

@EmilyyyLiu EmilyyyLiu commented Sep 10, 2025

关联issue:ant-design/ant-design#54854
替换 useMergedState 为 useControlledState

Summary by CodeRabbit

  • 代码重构
    • 调整内部状态管理方式以提升一致性与稳定性;对外行为与 API 保持不变。
  • 杂务
    • 升级依赖至较新版本,带来最新修复与兼容性改进。

Copy link

coderabbitai bot commented Sep 10, 2025

Walkthrough

将依赖 @rc-component/util^1.1.0 升至 ^1.3.0;在 src/index.tsx 中以 useControlledState 替换 useMergedState,并将默认值初始化调整为 defaultValue ?? segmentedOptions[0]?.value;其余公开 API 未改动。

Changes

Cohort / File(s) Summary
依赖版本更新
package.json
将依赖 @rc-component/util^1.1.0 更新为 ^1.3.0
状态管理钩子替换
src/index.tsx
useMergedState 替换为 useControlledState;初始化逻辑改为使用 defaultValue ?? segmentedOptions[0]?.value;简化了钩子入参形式,保持受控值传入方式不变。

Sequence Diagram(s)

sequenceDiagram
    autonumber
    actor User as 用户
    participant Seg as Segmented组件
    participant Hook as useControlledState(新)
    participant Props as Props(value, defaultValue, options)

    User->>Seg: 渲染(传入 Props)
    Seg->>Hook: 初始化(value, defaultValue ?? options[0]?.value)
    Hook-->>Seg: [state, setState]
    note over Seg,Hook: 状态由受控值或默认值驱动
    User->>Seg: 交互/选择项
    Seg->>Hook: setState(新值)
    Hook-->>Seg: 更新后的 state
    Seg-->>User: 触发 onChange / 重渲染
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • zombieJ

Pre-merge checks (3 passed)

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed 标题简洁明确地反映了主要改动,即升级 @rc-component/util 依赖并替换 useMergedState 为 useControlledState,与更改内容紧密对应。
Description Check ✅ Passed PR 描述关联了相关 issue 并说明了替换 useMergedState,为本次更改的重要部分且与实际改动一致,因此与更改内容相关。
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

Poem

兔子敲键轻轻跳,依赖版本往上跑。
钩子一换脉络俏,受控默认各归巢。
候选首项来报到,状态流转更明了。
嗅嗅胡萝卜,提交已过桥。 🐇🥕

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.

✨ 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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @EmilyyyLiu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此 PR 主要进行了一项重构工作,通过升级核心工具库依赖并替换内部使用的状态管理 Hook,以优化代码结构和保持与最新库版本的兼容性。

Highlights

  • 依赖升级: 将 @rc-component/util 依赖从 ^1.1.0 升级到 ^1.3.0。
  • 状态管理 Hook 替换: 在 src/index.tsx 中,将 useMergedState 替换为 useControlledState。
  • useControlledState 初始化逻辑调整: 调整了 useControlledState 的初始化值逻辑,现在优先使用 defaultValue,如果不存在则回退到 segmentedOptions[0]?.value。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

Warning

Review the following alerts detected in dependencies.

According to your organization's Security Policy, it is recommended to resolve "Warn" alerts. Learn more about Socket for GitHub.

Action Severity Alert  (click "▶" to expand/collapse)
Warn Critical
[email protected] has a Critical CVE.

CVE: GHSA-fjxv-7rqg-78g4 form-data uses unsafe random function in form-data for choosing boundary (CRITICAL)

Affected versions: < 2.5.4; >= 3.0.0 < 3.0.4; >= 4.0.0 < 4.0.4

Patched version: 2.5.4

From: ?npm/[email protected]npm/[email protected]

ℹ Read more on: This package | This alert | What is a critical CVE?

Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at [email protected].

Suggestion: Remove or replace dependencies that include known critical CVEs. Consumers can use dependency overrides or npm audit fix --force to remove vulnerable dependencies.

Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment @SocketSecurity ignore npm/[email protected]. You can also ignore all packages with @SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.

View full report

Copy link

codecov bot commented Sep 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.39%. Comparing base (13dcd71) to head (5235b13).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #303   +/-   ##
=======================================
  Coverage   99.38%   99.39%           
=======================================
  Files           2        2           
  Lines         163      164    +1     
  Branches       39       40    +1     
=======================================
+ Hits          162      163    +1     
  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.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 升级了 @rc-component/util 依赖,并使用 useControlledState 替换了 useMergedState。相关代码改动正确地保留了原有的逻辑,确保了组件在受控和非受控模式下的行为一致。这是一次不错的重构,提升了代码的可维护性。

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

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

203-207: 默认值仅在首次渲染读取首项;若 options 后置变更,初始未选中可能无法回填

这改变了与 useMergedState(带默认值配置对象)可能略有不同的时机。若希望在“非受控且当前值为 undefined”时,在 options 异步更新后仍自动选中第一项,可考虑添加同步逻辑(不会违背“不要在值不在 options 时强切换”的注释):

@@
-    const [rawValue, setRawValue] = useControlledState(
-      defaultValue ?? segmentedOptions[0]?.value,
-      value,
-    );
+    const [rawValue, setRawValue] = useControlledState(
+      defaultValue ?? segmentedOptions[0]?.value,
+      value,
+    );
+    // 当为非受控且当前值为 undefined,options 更新后回填首项
+    React.useEffect(() => {
+      if (value === undefined && rawValue === undefined && segmentedOptions.length > 0) {
+        setRawValue(segmentedOptions[0].value);
+      }
+    }, [value, rawValue, segmentedOptions]);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 13dcd71 and 5235b13.

📒 Files selected for processing (2)
  • package.json (1 hunks)
  • src/index.tsx (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: preview
🔇 Additional comments (2)
package.json (1)

52-52: 依赖已升级至 @rc-component/util@^1.3.0,hooks 已全部切换为 useControlledState,无 useMergedState 残留且方法签名未变更,LGTM。

src/index.tsx (1)

1-1: 确认已移除所有 useMergedState 导入
扫描 src/** 目录未发现任何 useMergedState 导入,已替换为 useControlledState 并与项目风格保持一致。

@zombieJ zombieJ merged commit 4b00c44 into react-component:master Sep 10, 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