feat(switch): loading 态受控
🤔 这个变动的性质是?
- [ ] 新特性提交
- [ ] 日常 bug 修复
- [ ] 站点、文档改进
- [ ] 演示代码改进
- [ ] 组件样式/交互改进
- [ ] TypeScript 定义更新
- [ ] 包体积优化
- [ ] 性能优化
- [ ] 功能增强
- [ ] 国际化改进
- [ ] 重构
- [ ] 代码风格优化
- [ ] 测试用例
- [ ] 分支合并
- [ ] 其他改动(是关于什么的改动?)
🔗 相关 Issue
💡 需求背景和解决方案
☑️ 请求合并前的自查清单
⚠️ 请自检并全部勾选全部选项。⚠️
- [ ] 文档已补充或无须补充
- [ ] 代码演示已提供或无须提供
- [ ] TypeScript 定义已补充或无须补充
- [ ] fork仓库代码是否为最新避免文件冲突
- [ ] Files changed 没有 package.json lock 等无关文件
Summary by CodeRabbit
-
新功能
- 新增 Demo8 演示组件,展示异步状态更新和交互提示效果。
- Switch 组件现支持受控 loading 状态,可通过新增属性进行加载提示控制。
-
文档
- 文档中新增“loading 态受控”章节,详细介绍了相关新属性和事件的使用。
- 增加中英文及繁体中文翻译,提升多语言用户体验。
Walkthrough
本次变更在开关组件中引入了新的 Demo8 演示组件,并增加了新翻译条目 loadingControl,用于描述受控 loading 状态。同时,更新了相关文档,对 loadingIcon、loading、onLoadingChange 等属性进行了补充说明和描述调整。开关组件内部也增加了状态变量和默认属性,以支持受控 loading 状态,并对异步交互流程进行了处理。此外,类型定义与迁移文档也相应进行了更新,确保 API 定义的一致性。
Changes
| 文件/文件组 | 变更摘要 |
|---|---|
src/packages/switch/demo.taro.tsxsrc/packages/switch/demo.tsx |
为 SwitchDemo 组件增加了对 Demo8 的引用,并添加新翻译 loadingControl(中英文)以展示 loading 状态。 |
src/packages/switch/demos/h5/demo8.tsxsrc/packages/switch/demos/taro/demo8.tsx |
新增 Demo8 组件,采用异步请求模拟延时更新开关状态,并在状态切换时展示 toast 通知。 |
src/packages/switch/doc.en-US.mdsrc/packages/switch/doc.mdsrc/packages/switch/doc.taro.mdsrc/packages/switch/doc.zh-TW.md |
文档中新增 “loading 态受控” 节,引入 demo 代码块,更新了 loadingIcon 属性描述,并增加了 loading 与 onLoadingChange 新属性说明。 |
src/packages/switch/switch.taro.tsxsrc/packages/switch/switch.tsx |
在 Switch 组件中新增 loading 与 onLoadingChange 默认属性,调整内部状态管理逻辑,更新 onClick 及异步状态处理逻辑。 |
src/sites/sites-react/doc/docs/react/migrate-from-v2.mdsrc/sites/sites-react/doc/docs/taro/migrate-from-v2.md |
更新迁移文档,说明新增了开关组件的 loading 属性和 onLoadingChange 回调,明确 API 变化。 |
src/types/spec/switch/base.ts |
为 BaseSwitch 接口增加了 loading (boolean |
Sequence Diagram(s)
sequenceDiagram
participant U as 用户
participant D as Demo8组件
participant M as mockRequest
participant T as Toast
U->>D: 切换开关状态
D->>T: 显示加载提示
D->>M: 发起异步请求(2秒延时)
M-->>D: 请求完成结果
D->>T: 更新并显示结果通知
D->>D: 更新状态(checkedAsync / externalLoading)
Possibly related PRs
- jdf2e/nutui-react#2779: 此 PR 与本次变更相关,均引入了新的 Demo8 组件,并为
SwitchDemo组件添加了loadingControl翻译条目。 - jdf2e/nutui-react#3122: 此 PR 与本次变更密切相关,同样引入了新的 Demo8 演示组件和
loadingControl翻译条目,同时调整了开关组件的异步行为处理。 - jdf2e/nutui-react#3143: 此 PR 涉及开关组件的异步操作和 loading 状态管理的修改,与本次变更在功能上有相关性。
Suggested reviewers
- xiaoyatong
- oasis-cloud
Poem
跳跃在代码田野间的我,
小兔子见证新 Demo 的出现在路上,🐰
受控 loading 状态轻轻开启,
文档清晰如晨光照亮每一步。
异步请求悄然传来温柔的回响,
更新后的组件,令代码世界更加生动。
[!TIP]
⚡💬 Agentic Chat (Pro Plan, General Availability)
- We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
📜 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 04aee9a38a085352af883f8320e1a2e17bfc30b2 and fe2c80f197aa62b98257a4fbe05fae6b341dd387.
📒 Files selected for processing (6)
src/packages/switch/demos/h5/demo8.tsx(1 hunks)src/packages/switch/switch.taro.tsx(5 hunks)src/packages/switch/switch.tsx(5 hunks)src/sites/sites-react/doc/docs/react/migrate-from-v2.md(1 hunks)src/sites/sites-react/doc/docs/react/official-theme-react.md(0 hunks)src/sites/sites-react/doc/docs/taro/migrate-from-v2.md(1 hunks)
💤 Files with no reviewable changes (1)
- src/sites/sites-react/doc/docs/react/official-theme-react.md
🚧 Files skipped from review as they are similar to previous changes (4)
- src/sites/sites-react/doc/docs/taro/migrate-from-v2.md
- src/sites/sites-react/doc/docs/react/migrate-from-v2.md
- src/packages/switch/switch.taro.tsx
- src/packages/switch/demos/h5/demo8.tsx
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/packages/switch/switch.tsx
[warning] 50-50: src/packages/switch/switch.tsx#L50 Added line #L50 was not covered by tests
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: test
🔇 Additional comments (5)
src/packages/switch/switch.tsx (5)
14-16: 新增的默认属性设计合理新增的
loading和onLoadingChange默认属性遵循了 React 受控组件的最佳实践。将loading默认值设为undefined可以区分受控和非受控模式,而onLoadingChange的空函数实现避免了调用时的空值检查。
29-31: 属性解构命名规范合理将
loading重命名为propLoading是一个良好的实践,可以清晰地区分外部传入的属性和内部使用的状态变量。
45-47: 内部状态管理实现完善针对 loading 状态的内部管理逻辑实现完善,通过条件判断
propLoading !== undefined来区分受控和非受控模式,符合 React 组件设计模式。
72-76: 关于 loadingIcon 与 loading 状态的关联设计当前实现中,只有在提供了
loadingIcon的情况下才会在onChange回调前设置 loading 状态(第75行)。如果没有提供图标,即使异步操作正在进行,loading 状态也不会被激活。请确认这是否是有意的设计决策。如果希望无论是否有图标都能反映异步操作的状态,可以考虑将这两个条件解耦。
100-102: 组件渲染逻辑更新合理更新后的渲染条件
loading && loadingIcon确保只有在组件处于加载状态且提供了加载图标时才会渲染图标,与组件的预期行为一致。
✨ Finishing Touches
- [ ] 📝 Generate Docstrings
🪧 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
@coderabbitaiin 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
@coderabbitaiin 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 pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai planto trigger planning for file edits and PR creation.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile 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.
Codecov Report
Attention: Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
Project coverage is 87.20%. Comparing base (
794a53f) to head (fe2c80f). Report is 25 commits behind head on feat_v3.x.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/packages/switch/switch.tsx | 94.44% | 1 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## feat_v3.x #3149 +/- ##
==========================================
Coverage 87.19% 87.20%
==========================================
Files 279 279
Lines 18544 18556 +12
Branches 2822 2825 +3
==========================================
+ Hits 16170 16181 +11
- Misses 2369 2370 +1
Partials 5 5
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
同 switch 另外一个 pr 中的建议,可以参考,不需要增加额外状态