Skip to content

Modal 对话框

在当前页面打开一个浮层,承载相关操作。

基本用法

对话框的基本用法。


代码事例
vue
<template>
  <fk-button @click="handleClick">Open Modal</fk-button>
  <fk-modal v-model:visible="visible" :defaultPosition="[100, 100]" @ok="handleOk" @cancel="handleCancel">
    <template #title>
      Title
    </template>
    <div>You can customize modal body text by the current situation. This modal will be closed immediately once you press the OK button.</div>
  </fk-modal>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      handleClick,
      handleOk,
      handleCancel
    }
  },
}
</script>

异步关闭

可以通过 on-before-ok 更简洁的实现异步关闭功能


代码事例
vue

<template>
  <fk-button @click="handleClick">Open Modal</fk-button>
  <fk-modal v-model:visible="visible" :on-before-ok="handleBeforeOk" unmount-on-close @cancel="handleCancel">
    <template #title>
      Title
    </template>
    <div>You can customize modal body text by the current situation. This modal will be closed immediately once you
      press the OK button.
    </div>
  </fk-modal>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);

    const handleClick = () => {
      visible.value = true;
    };
    const handleBeforeOk = async () => {
      await new Promise(resolve => setTimeout(resolve, 3000));
      return true;
      // prevent close
      // return false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      handleClick,
      handleBeforeOk,
      handleCancel
    }
  },
}
</script>

函数调用

通过函数的方式使用对话框。


代码事例
vue
<template>
  <fk-button @click="handleClick">Open Modal</fk-button>
</template>

<script>
import { h } from 'vue';
import { Button, Modal } from '@erp/common';

const ModalContent = {
  setup() {
    const onClick = () => {
      Modal.info({
        title: 'Info Title',
        content: 'This is an nest info message'
      });
    };

    return () => h('div', {class: 'info-modal-content'}, [
      h('span', {style: 'margin-bottom: 10px;'}, 'This is an info message'),
      h(Button, {size: 'mini', onClick}, 'Open Nest Modal')
    ])
  },
}

export default {
  setup() {
    const handleClick = () => {
      Modal.info({
        title: 'Info Title',
        content: () => h(ModalContent)
      });
    };

    return {
      handleClick
    }
  },
}
</script>

<style>
.info-modal-content {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
</style>

消息提示

info, success, warning, error四种类型的消息提示,仅提供一个确认按钮用于关闭消息提示对话框。 消息默认会默认开启 simplehideCancel,如果想要取消可以再次设置。


代码事例
vue
<template>
  <fk-space>
    <fk-button @click="handleClickInfo">Info</fk-button>
    <fk-button status="success" @click="handleClickSuccess">Success</fk-button>
    <fk-button status="warning" @click="handleClickWarning">Warning</fk-button>
    <fk-button status="danger" @click="handleClickError">Error</fk-button>
  </fk-space>
</template>

<script>
import { Modal } from '@erp/common';

export default {
  setup() {
    const handleClickInfo = () => {
      Modal.info({
        title: 'Info Notification',
        content: 'This is an info description which directly indicates a neutral informative change or action.'
      });
    };
    const handleClickSuccess = () => {
      Modal.success({
        title: 'Success Notification',
        content: 'This is a success notification'
      });
    };
    const handleClickWarning = () => {
      Modal.warning({
        title: 'Warning Notification',
        content: 'This is a warning description which directly indicates a warning that might need attention.'
      });
    };
    const handleClickError = () => {
      Modal.error({
        title: 'Error Notification',
        content: 'This is an error description which directly indicates a dangerous or potentially negative action.'
      });
    };

    return {
      handleClickInfo,
      handleClickSuccess,
      handleClickWarning,
      handleClickError
    }
  },
}
</script>

对话框的宽度

设置 width="auto" 可以让对话框自适应宽度


代码事例
vue
<template>
  <fk-button @click="handleClick">Open Modal</fk-button>
  <fk-modal v-model:visible="visible" width="auto" @ok="handleOk" @cancel="handleCancel">
    <template #title>
      Title
    </template>
    <div>You can customize modal body text by the current situation. This modal will be closed immediately once you press the OK button.</div>
  </fk-modal>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      handleClick,
      handleOk,
      handleCancel
    }
  },
}
</script>

定制按钮文字

设置 okTextcancelText 可以自定义按钮文字。


代码事例
vue
<template>
  <fk-button @click="handleClick">Open Modal</fk-button>
  <fk-modal :visible="visible" ok-text="Confirm" cancel-text="Exit" unmount-on-close @ok="handleOk" @cancel="handleCancel">
    <template #title>
      Title
    </template>
    <div>You can customize modal body text by the current situation. This modal will be closed immediately once you press the OK button.</div>
  </fk-modal>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      handleClick,
      handleOk,
      handleCancel
    }
  },
}
</script>

弹出层表单

在对话框中使用表单


代码事例
vue

<template>
  <fk-button @click="handleClick">Open Form Modal</fk-button>
  <fk-modal v-model:visible="visible" title="Modal Form" @cancel="handleCancel" @before-ok="handleBeforeOk">
    <fk-form :model="form">
      <fk-form-item field="name" label="Name">
        <fk-input v-model="form.name" />
      </fk-form-item>
      <fk-form-item field="post" label="Post">
        <fk-select v-model="form.post">
          <fk-option value="post1">Post1</fk-option>
          <fk-option value="post2">Post2</fk-option>
          <fk-option value="post3">Post3</fk-option>
          <fk-option value="post4">Post4</fk-option>
        </fk-select>
      </fk-form-item>
    </fk-form>
  </fk-modal>
</template>

<script>
import { reactive, ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);
    const form = reactive({
      name: '',
      post: ''
    });

    const handleClick = () => {
      visible.value = true;
    };
    const handleBeforeOk = (done) => {
      console.log(form)
      window.setTimeout(() => {
        done()
        // prevent close
        // done(false)
      }, 3000)
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      form,
      handleClick,
      handleBeforeOk,
      handleCancel
    }
  },
}
</script>

可拖动

开启 draggable 属性,允许用户拖动对话框。


代码事例
vue
<template>
  <fk-button @click="handleClick">Open Draggable Modal</fk-button>
  <fk-modal v-model:visible="visible" draggable @ok="handleOk" @cancel="handleCancel">
    <template #title>
      Title
    </template>
    <div>You can customize modal body text by the current situation. This modal will be closed immediately once you press the OK button.</div>
  </fk-modal>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      handleClick,
      handleOk,
      handleCancel
    }
  },
}
</script>

全屏

开启 fullscreen 属性,可以让对话框占满整个容器。


代码事例
vue
<template>
  <fk-button @click="handleClick">Open Modal</fk-button>
  <fk-modal v-model:visible="visible" fullscreen @ok="handleOk" @cancel="handleCancel">
    <template #title>
      Title
    </template>
    <div>You can customize modal body text by the current situation. This modal will be closed immediately once you press the OK button.</div>
  </fk-modal>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      handleClick,
      handleOk,
      handleCancel
    }
  },
}
</script>

API

参数名描述类型默认值版本
visible (v-model)对话框是否可见boolean-
default-visible对话框默认是否可见(非受控状态)booleanfalse
width对话框的宽度,不设置的情况下会使用样式中的宽度值number|string-1.0.0
top对话框的距离顶部的高度,居中显示开启的情况下不生效number|string-1.0.0
mask是否显示遮罩层booleantrue
title标题string-
title-align标题的水平对齐方向'start' | 'center''start'1.0.0
align-center对话框是否居中显示booleantrue
unmount-on-close关闭时是否卸载节点booleantrue
mask-closable是否点击遮罩层可以关闭对话框booleantrue
hide-cancel是否隐藏取消按钮booleanfalse
simple是否开启简单模式boolean(props: any) => { return props.notice;}
closable是否显示关闭按钮booleantrue
ok-text确认按钮的内容string-
cancel-text取消按钮的内容string-
ok-loading确认按钮是否为加载中状态booleanfalse
ok-button-props确认按钮的PropsButtonProps-
cancel-button-props取消按钮的PropsButtonProps-
footer是否展示页脚部分booleantrue
render-to-body对话框是否挂载在 body 元素下booleantrue
popup-container弹出框的挂载容器string | HTMLElement'body'
mask-style蒙层的样式CSSProperties-
modal-class对话框的类名string | any[]-
modal-style对话框的样式CSSProperties-
on-before-ok触发 ok 事件前的回调函数。如果返回 false 则不会触发后续事件,也可使用 done 进行异步关闭。(done: (closed: boolean | any) => void) => void | boolean | Promise<void | boolean>-1.0.0
on-before-cancel触发 cancel 事件前的回调函数。如果返回 false 则不会触发后续事件。() => boolean-1.0.0
esc-to-close是否支持 ESC 键关闭对话框booleantrue1.0.0
draggable是否支持拖动booleantrue1.0.0
fullscreen是否开启全屏booleanfalse1.0.0
mask-animation-name遮罩层动画名字string-1.0.0
modal-animation-name对话框动画名字string-1.0.0
body-class对话框内容部分的类名string | any[]-1.0.0
body-style对话框内容部分的样式StyleValue-1.0.0
hide-title是否隐藏标题booleanfalse1.0.0
事件名描述参数版本
ok点击确定按钮时触发ev: MouseEvent
cancel点击取消、关闭按钮时触发ev: MouseEvent | KeyboardEvent
open对话框打开后(动画结束)触发-
close对话框关闭后(动画结束)触发-
before-open对话框打开前触发-1.0.0
before-close对话框关闭前触发-1.0.0
插槽名描述参数
title标题-
footer页脚-

Modal提供的全局方法,可以通过以下三种方法使用:

  1. 通过this.$modal调用
  2. 在Composition API中,通过getCurrentInstance().appContext.config.globalProperties.$modal调用
  3. 导入Modal,通过Modal本身调用

当通过 import 方式使用时,组件没有办法获取当前的 Vue Context,如 i18n 或 route 等注入在 AppContext 上的内容无法在内部使用,需要在调用时手动传入 AppContext,或者为组件全局指定 AppContext

ts
import { createApp } from 'vue'
import { Modal } from '@erp/common';

const app = createApp(App);
Modal._context = app._context;

ModalConfig

参数名描述类型默认值版本
title标题RenderContent-
content内容RenderContent-
footer页脚boolean | RenderContenttrue
closable是否显示关闭按钮booleantrue
okText确认按钮的内容string-
cancelText取消按钮的内容string-
okButtonProps确认按钮的PropsButtonProps-
cancelButtonProps取消按钮的PropsButtonProps-
okLoading确认按钮是否为加载中状态booleanfalse
hideCancel是否隐藏取消按钮booleanfalse
mask是否显示遮罩层booleantrue
simple是否开启简单模式booleanfalse
maskClosable是否点击遮罩层可以关闭对话框booleantrue
maskStyle蒙层的样式CSSProperties-
alignCenter对话框是否居中显示booleantrue
escToClose是否支持 ESC 键关闭对话框booleantrue1.0.0
draggable是否支持拖动booleanfalse1.0.0
fullscreen是否开启全屏booleanfalse1.0.0
onOk点击确定按钮的回调函数(e?: any) => void-
onCancel点击取消按钮的回调函数(e?: any) => void-
onBeforeOk触发 ok 事件前的回调函数。如果返回 false 则不会触发后续事件,也可使用 done 进行异步关闭。(done: (closed: boolean) => void) => void | boolean | Promise<void | boolean>-1.0.0
onBeforeCancel触发 cancel 事件前的回调函数。如果返回 false 则不会触发后续事件。() => boolean-1.0.0
onOpen对话框打开后(动画结束)触发() => void-
onClose对话框关闭后(动画结束)触发() => void-
onBeforeOpen对话框打开前触发() => void-1.0.0
onBeforeClose对话框关闭前触发() => void-1.0.0
width对话框的宽度,不设置的情况下会使用样式中的宽度值number | string-1.0.0
top对话框的距离顶部的高度,居中显示开启的情况下不生效number | string-1.0.0
titleAlign标题的水平对齐方向'start' | 'center''center'1.0.0
renderToBody对话框是否挂载在 body 元素下booleantrue
popupContainer弹出框的挂载容器string | HTMLElement'body'
modalClass对话框的类名string | any[]-
modalStyle对话框的样式CSSProperties-
maskAnimationName遮罩层动画名字string-1.0.0
modalAnimationName对话框动画名字string-1.0.0
hideTitle是否隐藏标题booleanfalse1.0.0
bodyClass对话框内容部分的类名string | any[]-
bodyStyle对话框内容部分的样式StyleValue-
model数据模型any-

ModalReturn

参数名描述类型默认值版本
close关闭对话框() => void-
update更新对话框(config: ModalUpdateConfig) => void-1.0.0
containerover htmlHTMLElement-

ModalMethod

参数名描述类型默认值
open打开对话框(config: ModalConfig, appContext?: AppContext) => ModalReturn-
confirm打开对话框(简单模式)(config: ModalConfig, appContext?: AppContext) => ModalReturn-
info打开信息对话框(config: ModalConfig, appContext?: AppContext) => ModalReturn-
success打开成功对话框(config: ModalConfig, appContext?: AppContext) => ModalReturn-
warning打开警告对话框(config: ModalConfig, appContext?: AppContext) => ModalReturn-
error打开错误对话框(config: ModalConfig, appContext?: AppContext) => ModalReturn-

基于 MIT 许可发布