Skip to content

Modal对话框

Modal对话框是一种模态对话框,用于在不跳转页面、不打断用户工作流程的情况下,让用户处理事务。此外,Modal.confirm()等语法糖方法可用于快速创建简洁的确认框询问用户。

代码演示

  1. 基本用法:通过按钮点击控制对话框的显示与隐藏。
vue
<template>
  <div>
    <a-button type="primary" @click="showModal">Open Modal</a-button>
    <a-modal v-model:open="open" title="Basic Modal" @ok="handleOk">
      <p>Some contents...</p>
      <p>Some contents...</p>
      <p>Some contents...</p>
    </a-modal>
  </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
const open = ref<boolean>(false);

const showModal = () => {
  open.value = true;
};

const handleOk = (e: MouseEvent) => {
  console.log(e);
  open.value = false;
};
</script>
  1. 确认对话框:使用confirm()方法快速弹出确认框,可定制标题、图标、内容、按钮文本及按钮点击回调等。
vue
<template>
  <a-space wrap>
    <a-button @click="showConfirm">Confirm</a-button>
    <a-button @click="showPromiseConfirm">With promise</a-button>
    <a-button type="dashed" @click="showDeleteConfirm">Delete</a-button>
    <a-button type="dashed" @click="showPropsConfirm">With extra props</a-button>
  </a-space>
</template>
<script lang="ts" setup>
import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
import { createVNode } from 'vue'
import { Modal } from '@design-middle-center/fuse-design-vue'
const showConfirm = () => {
  Modal.confirm({
    title: 'Do you Want to delete these items?',
    icon: createVNode(ExclamationCircleOutlined),
    content: createVNode('div', { style: 'color:red;' }, 'Some descriptions'),
    onOk() {
      console.log('OK')
    },
    onCancel() {
      console.log('Cancel')
    },
    class: 'test'
  })
}
const showDeleteConfirm = () => {
  Modal.confirm({
    title: 'Are you sure delete this task?',
    icon: createVNode(ExclamationCircleOutlined),
    content: 'Some descriptions',
    okText: 'Yes',
    okType: 'danger',
    cancelText: 'No',
    onOk() {
      console.log('OK')
    },
    onCancel() {
      console.log('Cancel')
    }
  })
}
const showPropsConfirm = () => {
  Modal.confirm({
    title: 'Are you sure delete this task?',
    icon: createVNode(ExclamationCircleOutlined),
    content: 'Some descriptions',
    okText: 'Yes',
    okType: 'danger',
    okButtonProps: {
      disabled: true
    },
    cancelText: 'No',
    onOk() {
      console.log('OK')
    },
    onCancel() {
      console.log('Cancel')
    }
  })
}

function showPromiseConfirm() {
  Modal.confirm({
    title: 'Do you want to delete these items?',
    icon: createVNode(ExclamationCircleOutlined),
    content: 'When clicked the OK button, this dialog will be closed after 1 second',
    async onOk() {
      try {
        return await new Promise((resolve, reject) => {
          setTimeout(Math.random() > 0.5 ? resolve : reject, 1000)
        })
      } catch {
        return console.log('Oops errors!')
      }
    },
    onCancel() {}
  })
}
</script>
  1. 信息提示:展示不同类型的信息提示对话框,只包含一个关闭按钮。
vue
<template>
  <a-space wrap>
    <a-button @click="info">Info</a-button>
    <a-button @click="success">Success</a-button>
    <a-button @click="error">Error</a-button>
    <a-button @click="warning">Warning</a-button>
  </a-space>
</template>
<script lang="ts" setup>
import { Modal } from '@design-middle-center/fuse-design-vue'
import { h } from 'vue'
const info = () => {
  Modal.info({
    title: 'This is a notification message',
    content: h('div', {}, [
      h('p', 'some messages...some messages...'),
      h('p', 'some messages...some messages...')
    ]),
    onOk() {
      console.log('ok')
    }
  })
}
const success = () => {
  Modal.success({
    title: 'This is a success message',
    content: h('div', {}, [
      h('p', 'some messages...some messages...'),
      h('p', 'some messages...some messages...')
    ])
  })
}

const error = () => {
  Modal.error({
    title: 'This is an error message',
    content: 'some messages...some messages...'
  })
}

const warning = () => {
  Modal.warning({
    title: 'This is a warning message',
    content: 'some messages...some messages...'
  })
}
</script>
  1. 手动更新和移除:手动更新和关闭通过Modal.method方式创建的对话框。
vue
<template>
  <a-button @click="countDown">Open modal to close in 5s</a-button>
</template>
<script lang="ts" setup>
import { Modal } from '@design-middle-center/fuse-design-vue'
const countDown = () => {
  let secondsToGo = 5
  const modal = Modal.success({
    title: 'This is a notification message',
    content: `This modal will be destroyed after ${secondsToGo} second.`
  })
  const interval = setInterval(() => {
    secondsToGo -= 1
    modal.update({
      content: `This modal will be destroyed after ${secondsToGo} second.`
    })
  }, 1000)
  setTimeout(() => {
    clearInterval(interval)
    modal.destroy()
  }, secondsToGo * 1000)
}
</script>

API

参数说明类型默认值
afterClose关闭动画结束后的回调函数() => void-
centered是否垂直居中显示booleanfalse
cancelButtonProps取消按钮的额外属性object-
cancelText取消按钮的文本stringCancel
closable是否显示关闭按钮booleantrue
closeIcon自定义关闭图标VNode-
confirmButtonProps确定按钮的额外属性object-
confirmLoading确定按钮是否显示加载状态booleanfalse
destroyOnClose关闭时是否销毁 Modal 里的子元素booleanfalse
footer自定义页脚,设为 null 时不显示页脚VNode | (() => VNode) | null-
getContainer设置 Modal 挂载的 HTML 节点string | (() => HTMLElement)() => document.body
keyboard按下 ESC 时是否关闭 Modalbooleantrue
mask是否展示遮罩层booleantrue
maskClosable点击遮罩层是否可关闭 Modalbooleantrue
maskStyle遮罩层的样式object-
okButtonProps确定按钮的额外属性(与 confirmButtonProps 相同,优先使用 okButtonPropsobject-
okText确定按钮的文本stringOK
okType确定按钮的类型,如 primarydangerstringprimary
open(v - model)是否显示 Modalboolean-
titleModal 的标题string | VNode-
visibleopen 作用相同,是否显示 Modalboolean-
widthModal 的宽度number | string520
wrapClassName外层容器的类名string-
zIndexModal 的 z - indexnumber-
事件名称说明回调参数
cancel点击取消按钮的回调函数(e: MouseEvent) => void
closeModal 关闭时的回调函数(e: MouseEvent) => void
ok点击确定按钮的回调函数(e: MouseEvent) => void
参数说明类型默认值
title确认框的标题string | VNode-
icon确认框的图标VNode-
content确认框的内容string | VNode-
okText确定按钮的文本stringOK
okType确定按钮的类型,如 primarydangerstringprimary
cancelText取消按钮的文本stringCancel
okButtonProps确定按钮的额外属性object-
cancelButtonProps取消按钮的额外属性object-
onOk点击确定按钮的回调函数,返回 Promise 可延迟关闭(() => void) | (() => Promise<void>)-
onCancel点击取消按钮的回调函数,返回 Promise 可延迟关闭(() => void) | (() => Promise<void>)-
getContainer设置确认框挂载的 HTML 节点string | (() => HTMLElement)() => document.body
keyboard按下 ESC 时是否关闭确认框booleantrue
maskClosable点击遮罩层是否可关闭确认框booleantrue
maskStyle遮罩层的样式object-
zIndex确认框的 z - indexnumber-
class自定义确认框的类名string-
参数说明类型默认值
title信息提示框的标题string | VNode-
content信息提示框的内容string | VNode-
okText确定按钮的文本stringOK
onOk点击确定按钮的回调函数() => void-
getContainer设置信息提示框挂载的 HTML 节点string | (() => HTMLElement)() => document.body
keyboard按下 ESC 时是否关闭信息提示框booleantrue
maskClosable点击遮罩层是否可关闭信息提示框booleantrue
maskStyle遮罩层的样式object-
zIndex信息提示框的 z - indexnumber-
参数说明类型默认值
title成功提示框的标题string | VNode-
content成功提示框的内容string | VNode-
okText确定按钮的文本stringOK
onOk点击确定按钮的回调函数() => void-
getContainer设置成功提示框挂载的 HTML 节点string | (() => HTMLElement)() => document.body
keyboard按下 ESC 时是否关闭成功提示框booleantrue
maskClosable点击遮罩层是否可关闭成功提示框booleantrue
maskStyle遮罩层的样式object-
zIndex成功提示框的 z - indexnumber-
参数说明类型默认值
title错误提示框的标题string | VNode-
content错误提示框的内容string | VNode-
okText确定按钮的文本stringOK
onOk点击确定按钮的回调函数() => void-
getContainer设置错误提示框挂载的 HTML 节点string | (() => HTMLElement)() => document.body
keyboard按下 ESC 时是否关闭错误提示框booleantrue
maskClosable点击遮罩层是否可关闭错误提示框booleantrue
maskStyle遮罩层的样式object-
zIndex错误提示框的 z - indexnumber-
参数说明类型默认值
title警告提示框的标题string | VNode-
content警告提示框的内容string | VNode-
okText确定按钮的文本stringOK
onOk点击确定按钮的回调函数() => void-
getContainer设置警告提示框挂载的 HTML 节点string | (() => HTMLElement)() => document.body
keyboard按下 ESC 时是否关闭警告提示框booleantrue
maskClosable点击遮罩层是否可关闭警告提示框booleantrue
maskStyle遮罩层的样式object-
zIndex警告提示框的 z - indexnumber-

返回一个包含 modalcontextHolder 的数组。modal 包含 confirminfosuccesserrorwarning 等方法,用于创建各种类型的模态框。contextHolder 是一个 VNode,用于提供上下文,在模板中直接使用。

注意事项

  1. 受控与非受控openvisible 用于控制 Modal 的显示与隐藏,使用时需确保其状态的正确更新,以避免出现意外行为。
  2. 销毁与性能:当 destroyOnClose 设为 true 时,关闭 Modal 会销毁其中的子元素,有助于释放资源,特别是在子元素包含大量数据或复杂逻辑时。
  3. 样式定制:通过 wrapClassNamemaskStyle 等属性可以方便地对 Modal 的外层样式和遮罩层样式进行定制,以满足不同的设计需求。