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: 🐛 修复 Collapse v-model绑定数据变化时未更新折叠面板状态的问题 #744

Merged
merged 1 commit into from
Nov 28, 2024

Conversation

Moonofweisheng
Copy link
Owner

@Moonofweisheng Moonofweisheng commented Nov 28, 2024

Closes: #741

🤔 这个 PR 的性质是?(至少选择一个)

  • 日常 bug 修复
  • 新特性提交
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • CI/CD 改进
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 代码重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

#741

💡 需求背景和解决方案

将更新CollapseItem状态的逻辑转到CollapseItem组件中,父组件只关注更新选中项。

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充

Summary by CodeRabbit

  • 新功能

    • 增强了折叠项的响应性,支持根据父组件的选择状态自动更新展开状态。
    • 更新了折叠组件的切换功能,简化了控制逻辑,移除了异步行为。
  • bug 修复

    • 修复了折叠组件在状态切换时的控制流问题,确保更顺畅的用户体验。

Copy link

vercel bot commented Nov 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wot-design-uni ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 28, 2024 4:39am

Copy link

coderabbitai bot commented Nov 28, 2024

Walkthrough

本次更改主要涉及 wd-collapse-item.vuewd-collapse.vue 组件的功能增强和控制流简化。在 wd-collapse-item.vue 中,新增了一个 watch 函数来监控 isSelected 计算属性的变化,并在变化时调用 updateExpand 函数以调整折叠项的展开状态。同时,updateExpand 函数被修改以接受一个参数 useBeforeExpand,以便根据用户定义的逻辑执行预展开处理。在 wd-collapse.vue 中,移除了某些方法的异步行为,简化了组件的控制流。

Changes

文件路径 更改摘要
src/uni_modules/wot-design-uni/components/wd-collapse-item/wd-collapse-item.vue 添加 watch 函数监控 isSelected,修改 updateExpand 函数以接受参数 useBeforeExpand
src/uni_modules/wot-design-uni/components/wd-collapse/wd-collapse.vue 移除 updateChildren 函数,简化 toggletoggleAll 函数,移除 watchimmediate: true 选项。

Assessment against linked issues

Objective Addressed Explanation
期望 v-model 支持数组赋空收起功能 (741)

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

netlify bot commented Nov 28, 2024

Deploy Preview for wot-design-uni ready!

Name Link
🔨 Latest commit 5456e3d
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/6747f3f86e90b10008699103
😎 Deploy Preview https://deploy-preview-744--wot-design-uni.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

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 (2)
src/uni_modules/wot-design-uni/components/wd-collapse/wd-collapse.vue (1)

Line range hint 112-132: toggleAll 方法的改进符合预期

移除异步操作简化了代码流程,同时保持了完整的功能:

  • 正确处理了 accordion 模式
  • 支持跳过禁用项
  • 维护了展开/折叠状态

建议补充以下场景的错误处理:

const toggleAll = (options: CollapseToggleAllOptions = {}) => {
  if (props.accordion) {
+   console.warn('toggleAll 方法在手风琴模式下无效')
    return
  }
+  if (!Array.isArray(props.modelValue)) {
+    console.error('非手风琴模式下 modelValue 必须为数组类型')
+    return
+  }
  // ... 其余代码保持不变
}
src/uni_modules/wot-design-uni/components/wd-collapse-item/wd-collapse-item.vue (1)

83-88: 建议增加错误处理机制

当前的 watch 实现直接调用 updateExpand,建议添加错误处理以提高组件的健壮性。

建议修改为:

 watch(
   () => isSelected.value,
   (newVal) => {
-    updateExpand(newVal)
+    updateExpand(newVal).catch(error => {
+      console.warn('折叠面板状态更新失败:', error)
+    })
   }
 )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between fb7580d and 5456e3d.

📒 Files selected for processing (2)
  • src/uni_modules/wot-design-uni/components/wd-collapse-item/wd-collapse-item.vue (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-collapse/wd-collapse.vue (3 hunks)
🔇 Additional comments (4)
src/uni_modules/wot-design-uni/components/wd-collapse/wd-collapse.vue (2)

72-72: 移除 immediate 选项是正确的做法

移除 modelValue 的 watch 函数中的 immediate 选项是解决问题的关键。这样可以避免在组件初始化时过早触发监听器,让折叠面板的状态更新更加可控。


Line range hint 97-106: 优化了状态更新逻辑

移除了异步操作,简化了状态更新流程。当数组被设置为空时的处理也更加准确。建议添加单元测试以验证这些场景:

  1. 设置数组为空的情况
  2. 手风琴模式切换
  3. 多选模式下的状态变化
✅ Verification successful

状态更新逻辑已得到充分验证

示例页面中包含了完整的测试场景,涵盖了所有关键功能点:

  • 空数组状态:通过 toggleAll(false) 和手动收起实现
  • 手风琴模式:accordion 属性的切换测试
  • 多选模式:基础用法中的数组状态管理

示例代码同时也验证了类型定义的正确性,状态更新逻辑的改进是安全的。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查是否存在相关测试用例
rg -l "describe.*wd-collapse" --type vue --type ts

Length of output: 80


Script:

#!/bin/bash
# 搜索测试文件
fd "test|spec" -e js -e ts -e jsx -e tsx

# 搜索组件相关的测试内容
rg -l "collapse" --type js --type ts

Length of output: 1476


Script:

#!/bin/bash
# 检查示例页面中的测试用例
cat src/pages/collapse/Index.vue

# 检查组件的类型定义
cat src/uni_modules/wot-design-uni/components/wd-collapse/types.ts

Length of output: 9340

src/uni_modules/wot-design-uni/components/wd-collapse-item/wd-collapse-item.vue (2)

34-34: 导入变更正确实现!

从 'vue' 中导入 watch 函数是合理的,这与新增的响应式行为相符。


Line range hint 91-157: 实现方案合理且符合需求!

代码变更很好地解决了 v-model 绑定数据变化时未更新折叠面板状态的问题:

  1. updateExpand 函数通过 useBeforeExpand 参数提供了更灵活的状态更新控制
  2. initRect 函数确保了组件状态与选中状态的同步
  3. 错误处理得当,保证了组件的稳定性

执行以下脚本验证组件的使用方式:

✅ Verification successful

代码变更验证通过,实现合理且安全!

通过分析代码使用情况,我可以确认这些变更是安全的:

  1. 组件的使用场景完全符合预期,在折叠面板中被正确使用,包括基础用法、自定义标题、手风琴模式等场景
  2. updateExpand 函数的新参数 useBeforeExpand 不会影响现有功能,因为:
    • 默认值保持为 true,保证了向后兼容性
    • 在 v-model 数据变化时,可以跳过 beforeExpand 钩子,避免不必要的验证
  3. 状态更新逻辑保持了一致性,不会破坏父组件 wd-collapse 的控制流
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:验证组件的使用方式和依赖关系

# 检查组件的使用示例
rg -A 10 "wd-collapse-item" --type vue

# 检查相关的测试用例
rg -A 10 "describe.*CollapseItem" --type typescript

Length of output: 155


Script:

#!/bin/bash
# Search for collapse item component usage and tests
rg -A 10 "wd-collapse-item"

# Search for component registration
rg "components/wd-collapse-item"

# Search for any test files containing CollapseItem
fd "collapse.*test" --type f --exec cat {}

Length of output: 20541

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.

[Bug 上报] 1.4.0版本 collapse折叠面板 v-model 不支持 数组赋空 收起,1.3.11版本支持。
1 participant