Skip to content

Modal 对话框

简陋的模态对话框。 版本要求 >= 1.0.21

代码演示

基本用法

<template>
  <z-button type="primary" @click="showModal">Open Modal</z-button>

  <z-modal v-model:open="open" title="Basic Modal">
    <p>Some contents...</p>
    <p>Some contents...</p>
    <p>Some contents...</p>
  </z-modal>
</template>

<script setup lang="ts">
import {ref} from "vue";

const open = ref<boolean>(false);
const showModal = () => {
  open.value = true;
};
</script>

<style lang="less" scoped>
.box {
  margin-left: 124px;
}
</style>

自定义页脚或头部

使用 headerfooter 可自定义头部或底部,当你不需要它们时可传入null。

<template>
  <z-button type="primary" @click="showModal">Open Modal</z-button>

  <z-modal v-model:open="open" title="Basic Modal">
    <p>Some contents...</p>
    <p>Some contents...</p>
    <p>Some contents...</p>

    <template #header>
      <div>🌎️ 头部</div>
    </template>

    <template #footer>
      <div class="footer">
        <z-button @click="closeModal">自定义页脚</z-button>
      </div>
    </template>
  </z-modal>
</template>

<script setup lang="ts">
import {ref} from "vue";

const open = ref<boolean>(false);
const showModal = () => {
  open.value = true;
};

const closeModal = () => {
  open.value = false;
}
</script>

<style lang="less" scoped>

</style>

取消内边距

当你想自定义padding时,可设置 isPadding 为true,取消自带的内边距。

<template>
  <z-button type="primary" @click="showModal">Open Modal</z-button>

  <z-modal v-model:open="open" :header="null" :footer="null" title="Basic Modal" is-padding>
    <div class="box">
      <div class="box-content">
        <p>Some contents...</p>
        <p>Some contents...</p>
        <p>Some contents...</p>
      </div>

      <div class="btnGroup">
        <z-button type="danger" @click="closeModal">关闭</z-button>
      </div>
    </div>
  </z-modal>
</template>

<script setup lang="ts">
import {ref} from "vue";

const open = ref<boolean>(false);
const showModal = () => {
  open.value = true;
};

const closeModal = () => {
  open.value = false;
}
</script>

<style lang="less" scoped>
.box {
  padding: 12px;
  .btnGroup {
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
}
</style>

异步关闭

点击确定后,异步关闭对话框。

<template>
  <z-button type="primary" @click="showModal">Open Modal</z-button>

  <z-modal v-model:open="open" :loading="loading" title="Basic Modal" @ok="handleOk">
    <p>Some contents...</p>
    <p>Some contents...</p>
    <p>Some contents...</p>
  </z-modal>
</template>

<script setup lang="ts">
import {ref} from "vue";

const open = ref<boolean>(false);
const loading = ref<boolean>(false);

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

const handleOk = () => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 4000);
}
</script>

<style lang="less" scoped>

</style>

信息提示

简介的提示框,使用 info 只提供一个按钮用于关闭。

<template>
  <z-button type="primary" @click="showModal">Open Modal</z-button>
</template>

<script setup lang="ts">
import { h } from "vue";
import { Modals } from "@d7ee/z-base-ui";

const showModal = () => {
  Modals.info({
    title: "标题",
    content: h('div', {}, [
      h('p', {style: {color: 'red'}},'some messages...some messages...'),
      h('p', 'some messages...some messages...'),
    ]),
    onOk: () => {
      console.log("ok")
    }
  })
};
</script>

<style lang="less" scoped>
.box {
  margin-left: 124px;
}
</style>

确认对话框

确认对话框,使用 confirm 创建一个对话框。

<template>
  <z-button type="primary" @click="showModal">Open Modal</z-button>
</template>

<script setup lang="ts">
import { h } from "vue";
import { Modals } from "@d7ee/z-base-ui";

const showModal = () => {
  Modals.confirm({
    title: "标题",
    content: h('div', {}, [
      h('p', {style: {color: 'red'}},'some messages...some messages...'),
      h('p', 'some messages...some messages...'),
    ]),
    onOk: () => {
      console.log("ok")
    }
  })
};
</script>

<style lang="less" scoped>
.box {
  margin-left: 124px;
}
</style>

Api

属性名说明类型默认值
open控制显示隐藏(v-model)boolean
width宽度string
title标题string
loading异步关闭boolean
header隐藏顶部boolean
footer隐藏底部boolean
okText确认按钮文字string确认
cancelText取消按钮文字string确认
closeIcon显示关闭图标booleantrue
okType确认按钮类型ButtonType
cancelType取消按钮类型ButtonType
isOk显示确认按钮booleantrue
isCancel显示取消按钮booleantrue
top对话框位置string
wrapClassName设置最外层的类名string
isPadding取消所有内边距booleanfalse
border取消内容层对header和footer的边框booleanfalse
zIndex设置z-index的层级number1000
mask显示遮罩层booleantrue
maskClosable点击遮罩层能关闭对话框booleantrue
bodyStyle设置内容层的样式Record<string, any>
maskStyle设置遮罩层样式Record<string, any>

Event

事件说明

事件名称说明回调参数
ok点击确定回调(e) => void
cancel点击取消或遮罩层时回调(e) => void

Info or Confirm

  • Modals.info
  • Modals.confirm

以上均为一个 函数,参数为Object,具体属性为如下属性。

属性名说明类型默认值
width宽度string
content内容string | VNode
title标题string | VNode
onOk点击确认回调function
onCancel点击取消回调function