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: ✨ MessageBox 确认按钮增加 confirmButtonLoading 属性 #865

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

China-xiaoFang
Copy link

@China-xiaoFang China-xiaoFang commented Jan 22, 2025

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

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

🔗 相关 Issue

无。

💡 需求背景和解决方案

当我们使用 MessageBox 组件做一些确认提示的时候,因要请求API接口,如果接口未配置 Loading,并且接口延迟比较高,会导致用户立即点击 取消 按钮关闭弹窗,会使后续流程或逻辑错乱。
所以 MessageBox 组件增加 confirmButtonLoading 属性,当设置为 true 的时候,会自动隐藏 取消 按钮,并且禁止点击蒙层关闭。

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

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

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

Summary by CodeRabbit

  • 新特性

    • 为消息框添加了确认按钮加载状态功能
    • 支持在确认前显示加载状态,防止用户重复点击
  • 本地化

    • 为多语言版本添加了加载状态文案,包括中文、英文、日文等多个语言
  • 文档

    • 更新了文档,详细说明了confirmButtonLoading属性的使用方法
  • 版本

    • 最低支持版本:1.6.2

Copy link

coderabbitai bot commented Jan 22, 2025

Warning

Rate limit exceeded

@China-xiaoFang has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 23 minutes and 7 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 478afa0 and 875a2b9.

📒 Files selected for processing (2)
  • docs/component/message-box.md (2 hunks)
  • src/pages/messageBox/Index.vue (2 hunks)

概述

演练

此次更改为消息框(MessageBox)组件引入了一个新的加载按钮状态功能。通过添加 confirmButtonLoading 属性,开发者可以在确认操作前显示加载状态。这个特性允许在异步操作期间阻止对话框关闭,并为用户提供视觉反馈。变更涉及多个文件,包括文档、组件实现、类型定义和多语言本地化文件。

变更

文件路径 变更摘要
docs/component/message-box.md 新增"确认前置加载"部分,描述 confirmButtonLoading 属性
src/pages/messageBox/Index.vue 添加演示新加载功能的示例代码块
src/uni_modules/wot-design-uni/components/wd-message-box/types.ts 更新类型定义,添加 confirmButtonLoading 属性
src/uni_modules/wot-design-uni/components/wd-message-box/wd-message-box.vue 实现加载状态逻辑
src/uni_modules/wot-design-uni/locale/lang/* 为多种语言添加 loading 本地化消息

建议相关的 PR

建议的审阅者

  • Moonofweisheng

诗歌

🐰 消息框的魔法舞
加载按钮闪耀光芒
异步操作不再慌
用户体验更明朗
兔子为你点赞!🌟


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

vercel bot commented Jan 22, 2025

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 Jan 22, 2025 2:09pm

Copy link

netlify bot commented Jan 22, 2025

Deploy Preview for wot-design-uni ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 875a2b9
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/6790fbd28e8b360007594ce9
😎 Deploy Preview https://deploy-preview-865--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: 3

🧹 Nitpick comments (4)
src/uni_modules/wot-design-uni/components/wd-message-box/types.ts (1)

101-104: 建议完善属性文档说明

confirmButtonLoading 属性的注释应该包含:

  1. 使用场景说明
  2. beforeConfirm 的关联说明
  3. 对话框行为变化说明

建议更新注释为:

  /**
-  * 确认按钮加载
+  * 确认按钮加载状态
+  * @description 当设置为 true 时:
+  * 1. 确认按钮将显示加载状态
+  * 2. 取消按钮将被禁用
+  * 3. 点击遮罩层将不会关闭对话框
+  * @example
+  * // 在 API 请求期间显示加载状态
+  * beforeConfirm: ({ resolve }) => {
+  *   messageBox.show({ confirmButtonLoading: true })
+  *   apiRequest().finally(() => {
+  *     messageBox.show({ confirmButtonLoading: false })
+  *     resolve(true)
+  *   })
+  * }
   */
src/uni_modules/wot-design-uni/locale/lang/pt-PT.ts (1)

70-71: 葡萄牙语翻译和语法正确!

翻译准确且符合葡萄牙语的语言习惯。添加的逗号和新的 loading 文本格式正确。

建议:为保持一致性,可以考虑在其他使用 "Carregando..." 的地方(如 loadmore.loading)也使用相同的省略号样式。

src/uni_modules/wot-design-uni/components/wd-message-box/wd-message-box.vue (1)

102-119: 建议添加 TypeScript 接口定义

当前的状态对象使用了 JSDoc 注释来描述属性,建议创建专门的 TypeScript 接口来增强类型安全性。

建议添加以下接口定义:

interface MessageBoxState {
  /** 确认按钮加载 */
  confirmButtonLoading: boolean
  /** 是否展示取消按钮 */
  showCancelButton: boolean
  /** 是否支持点击蒙层关闭 */
  closeOnClickModal: boolean
  /** 确定按钮文案 */
  confirmButtonText: string
}
docs/component/message-box.md (1)

192-232: 建议补充更多使用场景示例

当前文档已经很好地介绍了基本用法,但建议添加更多实际应用场景的示例,如:

  • API 请求失败时的处理
  • 多个连续确认操作的处理
  • 自定义加载提示文案的使用方法

建议在文档中添加以下示例:

// API 请求失败的处理示例
function handleApiError() {
  message.confirm({
    msg: '是否提交数据?',
    title: '提示',
    confirmButtonLoading: true,
    beforeConfirm: async ({ resolve }) => {
      try {
        toast.loading('提交中...')
        await apiRequest() // 可能失败的 API 调用
        toast.success('提交成功')
        resolve(true)
      } catch (error) {
        toast.error('提交失败')
        resolve(false)
      }
    }
  })
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e5a24c4 and 478afa0.

📒 Files selected for processing (19)
  • docs/component/message-box.md (2 hunks)
  • src/pages/messageBox/Index.vue (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-message-box/types.ts (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-message-box/wd-message-box.vue (3 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/ar-SA.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/de-DE.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/en-US.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/es-ES.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/fr-FR.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/ja-JP.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/ko-KR.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/pt-PT.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/ru-RU.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/th-TH.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/tr-TR.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/vi-VN.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/zh-CN.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/zh-HK.ts (1 hunks)
  • src/uni_modules/wot-design-uni/locale/lang/zh-TW.ts (1 hunks)
🔇 Additional comments (17)
src/uni_modules/wot-design-uni/locale/lang/zh-HK.ts (1)

39-39: 翻译和格式正确!

传统繁体中文(香港)的翻译准确,格式规范。

src/uni_modules/wot-design-uni/locale/lang/zh-TW.ts (1)

39-39: 翻译和格式正确!

传统繁体中文(台湾)的翻译准确,与香港繁体中文保持一致,符合语言规范。

src/uni_modules/wot-design-uni/locale/lang/zh-CN.ts (1)

70-71: 翻译和格式正确!

简体中文的翻译准确,格式规范,与其他语言版本保持一致性。

src/uni_modules/wot-design-uni/locale/lang/ko-KR.ts (1)

70-71: 建议验证韩文翻译的准确性

韩文翻译格式正确,但建议由韩语专家确认"로딩 중..."的用词是否最准确。

src/uni_modules/wot-design-uni/locale/lang/ja-JP.ts (1)

70-71: 翻译和格式正确

日语翻译准确,格式保持一致。

src/uni_modules/wot-design-uni/locale/lang/vi-VN.ts (1)

46-52: 翻译和格式正确

越南语翻译准确,格式保持一致。

src/uni_modules/wot-design-uni/locale/lang/en-US.ts (1)

74-75: 翻译和格式正确

英语翻译准确,格式保持一致。

src/uni_modules/wot-design-uni/locale/lang/th-TH.ts (1)

74-75: 代码变更正确!

泰语翻译正确添加,符合语言规范。

src/uni_modules/wot-design-uni/locale/lang/ar-SA.ts (1)

74-75: 代码变更正确!

阿拉伯语翻译正确添加,符合语言规范。

src/uni_modules/wot-design-uni/locale/lang/tr-TR.ts (1)

70-71: 代码变更正确!

土耳其语翻译正确添加,符合语言规范。

src/uni_modules/wot-design-uni/locale/lang/ru-RU.ts (1)

70-71: 代码变更正确!建议添加验证。

俄语翻译正确添加,符合语言规范。建议验证所有语言文件是否都已添加相应的翻译。

运行以下脚本以验证所有语言文件是否都包含 loading 翻译:

✅ Verification successful

所有语言文件的 loading 翻译已完整验证!

经过检查,所有语言文件(包括 zh-TW、ko-KR、pt-BR、th-TH、zh-CN、ru-RU、tr-TR、vi-VN、en-US、fr-FR、de-DE、ja-JP、es-ES、ar-SA)都已正确包含 messageBox.loading 的翻译,结构完整且格式统一。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查所有语言文件中是否都包含 messageBox.loading 翻译
fd -e ts -x grep -l "messageBox" {} | xargs grep -L "loading:"

Length of output: 202


Script:

#!/bin/bash
# List all language files and their content
fd . "src/uni_modules/wot-design-uni/locale/lang" -e ts -x cat {}

Length of output: 42849

src/uni_modules/wot-design-uni/locale/lang/es-ES.ts (1)

70-71: 西班牙语翻译和语法正确!

翻译准确且符合西班牙语的语言习惯。添加的逗号和新的 loading 文本格式正确。

src/uni_modules/wot-design-uni/locale/lang/de-DE.ts (1)

74-75: 德语翻译和语法正确!

翻译准确且符合德语的语言习惯。添加的逗号和新的 loading 文本格式正确。

src/uni_modules/wot-design-uni/locale/lang/fr-FR.ts (1)

70-71: 法语翻译和语法正确!

翻译准确且符合法语的语言习惯。添加的逗号和新的 loading 文本格式正确。

src/pages/messageBox/Index.vue (1)

36-38: 示例代码结构清晰!

新增的演示区块标题准确描述了功能,并保持了与其他示例一致的代码结构。

src/uni_modules/wot-design-uni/components/wd-message-box/wd-message-box.vue (1)

98-100: 属性定义合理!

confirmButtonLoading 属性的默认值设置正确,位置放置恰当。

docs/component/message-box.md (1)

295-314: 参数文档完整规范!

新增的 confirmButtonLoading 参数文档包含了所有必要信息:

  • 参数说明清晰
  • 类型、默认值准确
  • 版本信息完整

@@ -14,7 +14,7 @@ import { type InputSize, type InputType } from '../wd-input/types'
export type MessageType = 'alert' | 'confirm' | 'prompt'

export type MessageBeforeConfirmOption = {
resolve: (isPass: boolean) => void
resolve: (isPass?: boolean) => void
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

注意:这是一个破坏性更改

isPass 参数设为可选可能会影响现有代码。建议:

  1. 在更新日志中标注这是一个破坏性更改
  2. 确保所有使用 beforeConfirm 的代码都已经过测试

Comment on lines 116 to 139
function beforeConfirmLoading() {
message
.confirm({
msg: '是否删除',
title: '提示',
confirmButtonLoading: true,
beforeConfirm: ({ resolve }) => {
try {
toast.loading('删除中...')
setTimeout(() => {
toast.close()
resolve(true)
toast.success('删除成功')
}, 3000)
} finally {
resolve()
}
}
})
.then(() => {})
.catch((error) => {
console.log(error)
})
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

修复 resolve() 被多次调用的问题

当前实现中,resolve() 在两个地方被调用:

  1. finally 块中无条件调用
  2. setTimeout 回调中在 toast.success() 之前调用

这可能导致确认对话框在操作完成之前就被关闭。

建议修改实现方式:

 function beforeConfirmLoading() {
   message
     .confirm({
       msg: '是否删除',
       title: '提示',
       confirmButtonLoading: true,
       beforeConfirm: ({ resolve }) => {
-        try {
           toast.loading('删除中...')
           setTimeout(() => {
             toast.close()
             resolve(true)
             toast.success('删除成功')
-          }, 3000)
-        } finally {
-          resolve()
-        }
+          }, 3000)
       }
     })
     .then(() => {})
     .catch((error) => {
       console.log(error)
     })
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function beforeConfirmLoading() {
message
.confirm({
msg: '是否删除',
title: '提示',
confirmButtonLoading: true,
beforeConfirm: ({ resolve }) => {
try {
toast.loading('删除中...')
setTimeout(() => {
toast.close()
resolve(true)
toast.success('删除成功')
}, 3000)
} finally {
resolve()
}
}
})
.then(() => {})
.catch((error) => {
console.log(error)
})
}
function beforeConfirmLoading() {
message
.confirm({
msg: '是否删除',
title: '提示',
confirmButtonLoading: true,
beforeConfirm: ({ resolve }) => {
toast.loading('删除中...')
setTimeout(() => {
toast.close()
resolve(true)
toast.success('删除成功')
}, 3000)
}
})
.then(() => {})
.catch((error) => {
console.log(error)
})
}

Comment on lines +188 to +204
if (messageState.confirmButtonLoading) {
state.confirmButtonText = messageState.confirmButtonText
state.showCancelButton = messageState.showCancelButton
state.closeOnClickModal = messageState.closeOnClickModal
messageState.showCancelButton = false
messageState.confirmButtonText = translate('loading')
state.confirmButtonLoading = true
}
messageState.beforeConfirm({
resolve: (isPass) => {
if (isPass) {
if (messageState.confirmButtonLoading) {
state.confirmButtonLoading = false
messageState.showCancelButton = state.showCancelButton
messageState.confirmButtonText = state.confirmButtonText
messageState.closeOnClickModal = state.closeOnClickModal
}
if (isPass === null || isPass === undefined || isPass) {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

建议增强错误处理机制

当前实现在处理加载状态时没有考虑异常情况,建议添加错误处理以确保状态能够正确恢复。

建议修改实现方式:

 if (messageState.confirmButtonLoading) {
   state.confirmButtonText = messageState.confirmButtonText
   state.showCancelButton = messageState.showCancelButton
   state.closeOnClickModal = messageState.closeOnClickModal
   messageState.showCancelButton = false
   messageState.confirmButtonText = translate('loading')
   state.confirmButtonLoading = true
 }
 messageState.beforeConfirm({
   resolve: (isPass) => {
+    try {
       if (messageState.confirmButtonLoading) {
         state.confirmButtonLoading = false
         messageState.showCancelButton = state.showCancelButton
         messageState.confirmButtonText = state.confirmButtonText
         messageState.closeOnClickModal = state.closeOnClickModal
       }
       if (isPass === null || isPass === undefined || isPass) {
         handleConfirm({
           action: action,
           value: messageState.inputValue
         })
       }
+    } catch (error) {
+      // 确保在发生错误时恢复原始状态
+      if (messageState.confirmButtonLoading) {
+        state.confirmButtonLoading = false
+        messageState.showCancelButton = state.showCancelButton
+        messageState.confirmButtonText = state.confirmButtonText
+        messageState.closeOnClickModal = state.closeOnClickModal
+      }
+      throw error
+    }
   }
 })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (messageState.confirmButtonLoading) {
state.confirmButtonText = messageState.confirmButtonText
state.showCancelButton = messageState.showCancelButton
state.closeOnClickModal = messageState.closeOnClickModal
messageState.showCancelButton = false
messageState.confirmButtonText = translate('loading')
state.confirmButtonLoading = true
}
messageState.beforeConfirm({
resolve: (isPass) => {
if (isPass) {
if (messageState.confirmButtonLoading) {
state.confirmButtonLoading = false
messageState.showCancelButton = state.showCancelButton
messageState.confirmButtonText = state.confirmButtonText
messageState.closeOnClickModal = state.closeOnClickModal
}
if (isPass === null || isPass === undefined || isPass) {
if (messageState.confirmButtonLoading) {
state.confirmButtonText = messageState.confirmButtonText
state.showCancelButton = messageState.showCancelButton
state.closeOnClickModal = messageState.closeOnClickModal
messageState.showCancelButton = false
messageState.confirmButtonText = translate('loading')
state.confirmButtonLoading = true
}
messageState.beforeConfirm({
resolve: (isPass) => {
try {
if (messageState.confirmButtonLoading) {
state.confirmButtonLoading = false
messageState.showCancelButton = state.showCancelButton
messageState.confirmButtonText = state.confirmButtonText
messageState.closeOnClickModal = state.closeOnClickModal
}
if (isPass === null || isPass === undefined || isPass) {
handleConfirm({
action: action,
value: messageState.inputValue
})
}
} catch (error) {
// 确保在发生错误时恢复原始状态
if (messageState.confirmButtonLoading) {
state.confirmButtonLoading = false
messageState.showCancelButton = state.showCancelButton
messageState.confirmButtonText = state.confirmButtonText
messageState.closeOnClickModal = state.closeOnClickModal
}
throw error
}
}
})

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.

1 participant