MessageBox 对话框组件

MessageBox 对话框组件
BreezliMessageBox 对话框组件
MessageBox.vue
script
1 | <script setup lang="ts"> |
关键解释
defineOptions
- 定义组件名称为
VrMessageBox
,禁用属性继承:
1
2
3
4 defineOptions({
name: 'VrMessageBox',
inheritAttrs: false,
});
withDefaults
为
props
设置默认值:
1
2
3
4
5
6
7
8
9
10
11
12
13 const props = withDefaults(defineProps<MessageBoxProps>(), {
lockScroll: true,
showClose: true,
closeOnClickModal: true,
confirmButtonType: 'primary',
roundButton: false,
boxType: '',
inputValue: '',
inputPlaceholder: 'Please input...',
confirmButtonText: 'Ok',
cancelButtonText: 'Cancel',
showConfirmButton: true,
});
useZIndex
通过
useZIndex
管理对话框层级:
1
2 const { nextZIndex } = useZIndex();
const state = reactive({ ...props, zIndex: nextZIndex() });
handleAction
统一处理按钮点击和输入回车操作:
1
2
3
4
5
6
7 function handleAction(action: MessageBoxAction) {
if (isFunction(props.beforeClose)) {
props.beforeClose(action, state, () => doAction(action, state.inputValue));
} else {
doAction(action, state.inputValue);
}
}
template
1 | <template> |
关键解释
Transition
- 使用
fade-in-linear
动画,动画结束后触发destroy
移除 DOM。
VrOverlay
- 包裹对话框的遮罩层,支持
mask
属性控制遮罩是否可点击。
输入框与按钮
state.showInput
控制输入框显示(如prompt
类型)。- 按钮根据
showCancelButton
/showConfirmButton
动态显示。
标题与内容
center
模式下图标居中显示在标题栏内。hasMessage
检测是否存在消息内容。
methods.ts
1 | import { |
关键解释
messageInstanceMap
存储所有对话框实例,支持批量操作:
1
2
3
4
5
6
7
8 const messageInstanceMap = new Map<
ComponentPublicInstance<{ doClose: () => void }>,
{
options: MessageBoxOptions;
resolve: (res: any) => void;
reject: (res: any) => void;
}
>();
showMessage
- 根据
options
生成对话框实例。doAction
处理用户操作并触发resolve/reject
。
预定义对话框类型
alert
:默认禁用遮罩层关闭。confirm
:默认显示取消按钮。prompt
:默认显示输入框和取消按钮。
types.ts
1 | import { VNode } from 'vue'; |
关键解释
MessageBoxOptions
boxType
:指定对话框类型(alert/confirm/prompt
)。showInput
:是否显示输入框(通常用于prompt
类型)。beforeClose
:关闭前的钩子函数,需调用done()
完成关闭。
使用示例
1 | <script setup> |
核心 API
Props
属性 | 类型 | 默认值 | 说明 |
---|---|---|---|
title |
string |
- | 对话框标题 |
message |
`string | VNode | (() => VNode)` |
type |
messageType |
'' |
消息类型(info/success/warning/danger/error ) |
boxType |
MessageBoxType |
'' |
对话框类型(alert/confirm/prompt ) |
showClose |
boolean |
true |
是否显示关闭按钮 |
showInput |
boolean |
false |
是否显示输入框(prompt 类型默认开启) |
showCancelButton |
boolean |
false |
是否显示取消按钮 |
showConfirmButton |
boolean |
true |
是否显示确认按钮 |
confirmButtonText |
string |
'Ok' |
确认按钮文字 |
cancelButtonText |
string |
'Cancel' |
取消按钮文字 |
lockScroll |
boolean |
true |
是否锁定页面滚动 |
closeOnClickModal |
boolean |
true |
是否允许点击遮罩层关闭 |
方法
方法名 | 参数 | 说明 |
---|---|---|
MessageBox() |
options: MessageBoxOptions |
显示自定义对话框 |
alert() |
`message: string | VNode, title?: string, options?: MessageBoxOptions` |
confirm() |
`message: string | VNode, title?: string, options?: MessageBoxOptions` |
prompt() |
`message: string | VNode, title?: string, options?: MessageBoxOptions` |
close() |
- | 关闭所有对话框 |
回调参数
场景 | 返回值类型 | 说明 |
---|---|---|
alert |
string ('confirm' /'close' ) |
用户操作类型 |
confirm/prompt |
{ value: string; action: 'confirm' } 或 'cancel' /'close' |
包含输入值和操作类型(prompt 时返回输入值) |
注意事项
类型与默认配置:
alert
默认禁用遮罩层关闭,confirm
默认显示取消按钮,prompt
默认显示输入框和取消按钮。
异步处理:
MessageBox
返回Promise
,需通过.then()
或await
处理用户操作结果。
输入验证:
prompt
类型可通过beforeClose
钩子验证输入内容,阻止无效提交。
样式扩展:
- 按钮样式支持通过
confirmButtonType
/cancelButtonType
指定类型(如'primary'
/'danger'
)。
- 按钮样式支持通过
锁屏滚动:
lockScroll
启用时,页面滚动将被禁用,对话框居中显示。