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

feat: ✨ Sidebar 侧边栏组件增加before-change属性支持异步更新 #721

Merged
merged 1 commit into from
Nov 18, 2024

Conversation

Moonofweisheng
Copy link
Owner

@Moonofweisheng Moonofweisheng commented Nov 18, 2024

Closes: #711

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

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

🔗 相关 Issue

#711

💡 需求背景和解决方案

为Sidebar组件添加before-change属性,用于处理异步更新的场景

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

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

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

Summary by CodeRabbit

  • 新功能

    • wd-sidebar 组件中添加了异步切换功能,允许在切换选项卡之前执行特定逻辑。
    • 新增 before-change 属性,支持在切换之前进行自定义处理。
    • 新增 badge-props 属性,允许传递自定义徽章属性。
  • 文档

    • 更新了 wd-sidebar 组件的文档,增加了异步切换部分及相关示例,提升了可读性和一致性。

Copy link

vercel bot commented Nov 18, 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 18, 2024 3:29pm

Copy link

coderabbitai bot commented Nov 18, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

本次更改主要针对 wd-sidebar 组件的功能和文档进行了更新。新增了 before-change 属性,允许在切换侧边栏项之前执行特定逻辑,并在文档中详细说明了其用法。此外,更新了 badge-props 属性以支持自定义徽章属性,增强了组件的功能性和可读性。相关的 Vue 组件和类型定义也进行了相应的修改,以支持新的异步切换功能。

Changes

文件路径 更改摘要
docs/component/sidebar.md 更新文档,新增“异步切换”部分,添加 before-change 属性及其功能描述,扩展 badge-props 属性说明。
src/pages/sidebar/Index.vue 添加 :before-change 属性,定义 beforeChange 函数以处理异步切换,更新导入语句。
src/uni_modules/wot-design-uni/components/wd-sidebar/types.ts 新增 SidebarBeforeChangeOptionSidebarBeforeChange 类型,更新 sidebarProps 以包含 beforeChange 属性。
src/uni_modules/wot-design-uni/components/wd-sidebar/wd-sidebar.vue 更新 setChange 函数以支持 beforeChange,新增 updateValue 函数,更新文档注释。

Assessment against linked issues

Objective Addressed Explanation
增加 before-change 钩子以允许开发者控制切换 (#[711])

Possibly related PRs

🐇 在侧边栏里跳跃欢,
新功能让人乐开颜。
异步切换真方便,
开发者心中有主见。
badge属性也更新,
让我们的组件更出色! 🌟


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 18, 2024

Deploy Preview for wot-design-uni ready!

Name Link
🔨 Latest commit 7c83f70
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/673b5d1b8125850008fdbbcb
😎 Deploy Preview https://deploy-preview-721--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

cloudflare-workers-and-pages bot commented Nov 18, 2024

Deploying wot-design-uni with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7c83f70
Status:⚡️  Build in progress...

View logs

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: 2

🧹 Outside diff range and nitpick comments (5)
src/uni_modules/wot-design-uni/components/wd-sidebar/types.ts (1)

33-37: 完善属性文档和类型引用

建议更新以下内容:

  1. 使用更详细的属性文档注释
  2. 更新类型引用以匹配修正后的类型名称
   /**
    * 当前导航项的索引
+   * @default 0
    */
   modelValue: makeNumericProp(0),
   /**
-   * 在改变前执行的函数
+   * 切换前的钩子函数,返回 false 可阻止切换,支持异步
+   * @param option.value 目标值
+   * @param option.resolve 接收一个 boolean 参数,true 表示允许切换,false 表示阻止切换
    */
-  beforeChange: Function as PropType<SiderbarBeforeChange>
+  beforeChange: Function as PropType<SidebarBeforeChange>
src/uni_modules/wot-design-uni/components/wd-sidebar/wd-sidebar.vue (1)

32-33: 建议完善参数文档说明

建议在文档注释中添加参数类型信息,使文档更加完整。

/**
 * 子项状态变更
- * @param value 目标值
- * @param label 目标值标题
+ * @param value {number | string} 目标值
+ * @param label {string} 目标值标题
 */
src/pages/sidebar/Index.vue (2)

Line range hint 24-28: 新增的 before-change 示例实现完善!

建议为该示例添加注释说明异步切换的功能,以便其他开发者更好地理解使用场景。

 <wd-sidebar v-model="active3" :before-change="beforeChange">
+  <!-- 演示异步切换功能,点击后会有2秒loading状态 -->
   <wd-sidebar-item :value="0" label="标签名称" />
   <wd-sidebar-item :value="1" label="标签名称" disabled />
   <wd-sidebar-item :value="2" label="标签名称" />
 </wd-sidebar>

61-67: 建议增强错误处理机制

当前实现仅考虑了成功场景,建议增加错误处理逻辑。

 const beforeChange: SiderbarBeforeChange = ({ value, resolve }) => {
   showLoading('切换中')
   setTimeout(() => {
-    closeLoading()
-    resolve(true)
+    try {
+      // 这里可以添加实际的异步逻辑
+      closeLoading()
+      resolve(true)
+    } catch (error) {
+      closeLoading()
+      uni.showToast({
+        title: '切换失败',
+        icon: 'error'
+      })
+      resolve(false)
+    }
   }, 2000)
 }
docs/component/sidebar.md (1)

70-97: 建议补充异步切换的错误处理说明

示例代码展示了基本的异步切换功能,建议补充以下内容:

  1. resolve(false) 时的行为说明
  2. 异步操作失败时的错误处理建议
  3. 补充 before-change 的返回值类型说明

建议在示例代码中添加错误处理:

 const beforeChange: SiderbarBeforeChange = ({ value, resolve }) => {
   showLoading('切换中')
-  setTimeout(() => {
-    closeLoading()
-    resolve(true)
-  }, 2000)
+  try {
+    setTimeout(() => {
+      closeLoading()
+      resolve(true)
+    }, 2000)
+  } catch (error) {
+    closeLoading()
+    toast.show('切换失败')
+    resolve(false)
+  }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 08e7d77 and e1e5e3a.

📒 Files selected for processing (4)
  • docs/component/sidebar.md (6 hunks)
  • src/pages/sidebar/Index.vue (3 hunks)
  • src/uni_modules/wot-design-uni/components/wd-sidebar/types.ts (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-sidebar/wd-sidebar.vue (2 hunks)
🔇 Additional comments (7)
src/uni_modules/wot-design-uni/components/wd-sidebar/types.ts (1)

Line range hint 1-42: 整体实现符合需求

类型定义和属性声明完全支持了 before-change 异步更新的功能需求,提供了良好的 TypeScript 类型支持。建议在修复上述问题后合并此PR。

src/uni_modules/wot-design-uni/components/wd-sidebar/wd-sidebar.vue (3)

20-20: 导入实用函数看起来不错!

引入 isFunction 工具函数来验证 beforeChange 属性的类型是一个很好的做法。


50-58: 更新逻辑封装得很好

将更新逻辑抽取为独立的 updateValue 函数是个很好的做法,提高了代码的可维护性。事件发送的实现也很规范。


36-48: 异步切换功能实现正确,建议验证使用场景

beforeChange 的实现符合需求,支持异步控制切换行为。代码结构清晰,使用 resolve 回调处理异步结果的方式很合理。

建议运行以下脚本检查组件的使用示例:

✅ Verification successful

Let me check the content of the example and documentation files to verify the implementation details.


异步切换功能实现完善,示例和文档覆盖充分

示例和文档中已经完整展示了 beforeChange 的使用方法:

  • 在示例代码 src/pages/sidebar/Index.vue 中提供了完整的异步切换实现
  • 文档 docs/component/sidebar.md 中详细说明了 beforeChange 的用法和参数说明
  • 包含了加载状态展示的最佳实践示例

无需进一步验证,当前实现和文档完全符合要求。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:查找组件使用示例和测试用例,确保新功能有适当的示例和测试覆盖

# 搜索示例代码
echo "查找使用示例..."
rg -l "wd-sidebar.*before-change" 

# 搜索测试用例
echo "查找测试用例..."
fd -e spec.ts -e spec.js -e test.ts -e test.js | xargs rg "beforeChange"

Length of output: 1786


Script:

#!/bin/bash
# Check the content of example and documentation files
echo "检查示例文件内容..."
cat src/pages/sidebar/Index.vue

echo "\n检查文档内容..."
cat docs/component/sidebar.md

Length of output: 16561

src/pages/sidebar/Index.vue (2)

4-4: 文件元数据更新正确!

文件路径和最后编辑时间已更新。

Also applies to: 7-7


52-53: 类型导入和使用规范!

正确引入了必要的类型定义和工具函数。

docs/component/sidebar.md (1)

505-505: 文档更新完善

badge-props 属性的文档说明清晰,并正确链接到了 Badge 组件文档。

docs/component/sidebar.md Show resolved Hide resolved
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.

[新功能需求] Sidebar组件新增钩子
1 participant