5.6.8 • Published 4 months ago

@cimom/vben-core-popup-ui v5.6.8

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Popup UI 组件

Popup UI 组件是一个基于 Vue 3 的弹出层组件库,包含模态框(Modal)、抽屉(Drawer)和警告框(Alert)等组件,提供了丰富的配置选项和 API 调用方式。

安装

npm install @cimom/vben-core-ui-kit-popup-ui

组件列表

  • Modal: 模态对话框
  • Drawer: 抽屉组件
  • Alert: 警告提示框

Modal 模态对话框

基本使用

<script setup lang="ts">
import { Modal } from '@cimom/vben-core-ui-kit-popup-ui';
import { ref } from 'vue';

const visible = ref(false);

const handleOpen = () => {
  visible.value = true;
};

const handleClose = () => {
  visible.value = false;
};

const handleConfirm = () => {
  console.log('确认');
  visible.value = false;
};
</script>

<template>
  <button @click="handleOpen">打开模态框</button>

  <Modal
    v-model:isOpen="visible"
    title="基本模态框"
    @confirm="handleConfirm"
    @cancel="handleClose"
  >
    <div>这是一个基本的模态框内容</div>
  </Modal>
</template>

使用 API 方式调用

<script setup lang="ts">
import { useModal } from '@cimom/vben-core-ui-kit-popup-ui';

const modal = useModal();

const openModal = () => {
  modal.open({
    title: 'API 调用模态框',
    content: '这是通过 API 方式打开的模态框',
    onConfirm: () => {
      console.log('确认');
      modal.close();
    },
  });
};
</script>

<template>
  <button @click="openModal">API 打开模态框</button>
</template>

Modal 属性

属性名类型默认值说明
isOpenbooleanfalse是否显示模态框
titlestring''标题
titleTooltipstring-标题提示信息
descriptionstring-描述信息
confirmTextstring'确认'确认按钮文本
cancelTextstring'取消'取消按钮文本
showConfirmButtonbooleantrue是否显示确认按钮
showCancelButtonbooleantrue是否显示取消按钮
confirmDisabledbooleanfalse确认按钮是否禁用
confirmLoadingbooleanfalse确认按钮是否加载中
closablebooleantrue是否显示关闭按钮
closeOnClickModalbooleantrue点击遮罩是否关闭
closeOnPressEscapebooleantrue按 ESC 是否关闭
centeredbooleanfalse是否居中显示
fullscreenbooleanfalse是否全屏显示
fullscreenButtonbooleantrue是否显示全屏按钮
draggablebooleanfalse是否可拖拽
borderedbooleantrue是否显示边框
destroyOnClosebooleantrue关闭时是否销毁子元素
loadingbooleanfalse是否显示加载状态
submittingbooleanfalse是否处于提交状态
modalbooleantrue是否显示遮罩
openAutoFocusbooleanfalse打开时是否自动聚焦
headerbooleantrue是否显示头部
footerbooleantrue是否显示底部
classstring''自定义类名
headerClassstring''头部自定义类名
contentClassstring''内容自定义类名
footerClassstring''底部自定义类名
zIndexnumber-z-index 层级
appendToMainbooleanfalse是否挂载到主内容区域
overlayBlurbooleanfalse是否模糊背景

Modal 事件

事件名参数说明
update:isOpen(isOpen: boolean) => void更新打开状态
confirm-点击确认按钮时触发
cancel-点击取消按钮时触发
close-关闭模态框时触发
closed-模态框关闭动画结束后触发
open-打开模态框时触发
opened-模态框打开动画结束后触发

Modal API 方法

方法名参数返回值说明
open(options?: ModalOptions) => void-打开模态框
close-Promise<void>关闭模态框
setData<T>(data: T) => ModalApiModalApi设置数据
getData-<T>() => T获取数据
setState(state: Partial<ModalState>) => ModalApiModalApi设置状态
lock(isLocked?: boolean) => ModalApiModalApi锁定模态框
unlock-ModalApi解锁模态框

Drawer 抽屉组件

基本使用

<script setup lang="ts">
import { Drawer } from '@cimom/vben-core-ui-kit-popup-ui';
import { ref } from 'vue';

const visible = ref(false);
const placement = ref('right');

const handleOpen = () => {
  visible.value = true;
};

const handleClose = () => {
  visible.value = false;
};
</script>

<template>
  <button @click="handleOpen">打开抽屉</button>

  <Drawer
    v-model:isOpen="visible"
    :placement="placement"
    title="基本抽屉"
    @close="handleClose"
  >
    <div>这是一个基本的抽屉内容</div>
  </Drawer>
</template>

使用 API 方式调用

<script setup lang="ts">
import { useDrawer } from '@cimom/vben-core-ui-kit-popup-ui';

const drawer = useDrawer();

const openDrawer = () => {
  drawer.open({
    title: 'API 调用抽屉',
    content: '这是通过 API 方式打开的抽屉',
    placement: 'right',
    width: '30%',
    onClose: () => {
      console.log('关闭');
    },
  });
};
</script>

<template>
  <button @click="openDrawer">API 打开抽屉</button>
</template>

Drawer 属性

属性名类型默认值说明
isOpenbooleanfalse是否显示抽屉
titlestring''标题
placement'top' \| 'right' \| 'bottom' \| 'left''right'抽屉位置
widthstring \| number'30%'宽度(placement 为 left 或 right 时有效)
heightstring \| number'30%'高度(placement 为 top 或 bottom 时有效)
closablebooleantrue是否显示关闭按钮
closeOnClickModalbooleantrue点击遮罩是否关闭
closeOnPressEscapebooleantrue按 ESC 是否关闭
destroyOnClosebooleantrue关闭时是否销毁子元素
loadingbooleanfalse是否显示加载状态
submittingbooleanfalse是否处于提交状态
modalbooleantrue是否显示遮罩
headerbooleantrue是否显示头部
footerbooleanfalse是否显示底部
showConfirmButtonbooleanfalse是否显示确认按钮
showCancelButtonbooleanfalse是否显示取消按钮
confirmTextstring'确认'确认按钮文本
cancelTextstring'取消'取消按钮文本
classstring''自定义类名
headerClassstring''头部自定义类名
contentClassstring''内容自定义类名
footerClassstring''底部自定义类名
zIndexnumber-z-index 层级

Drawer 事件

事件名参数说明
update:isOpen(isOpen: boolean) => void更新打开状态
confirm-点击确认按钮时触发
cancel-点击取消按钮时触发
close-关闭抽屉时触发
closed-抽屉关闭动画结束后触发
open-打开抽屉时触发
opened-抽屉打开动画结束后触发

Drawer API 方法

方法名参数返回值说明
open(options?: DrawerOptions) => void-打开抽屉
close-Promise<void>关闭抽屉
setData<T>(data: T) => DrawerApiDrawerApi设置数据
getData-<T>() => T获取数据
setState(state: Partial<DrawerState>) => DrawerApiDrawerApi设置状态
lock(isLocked?: boolean) => DrawerApiDrawerApi锁定抽屉
unlock-DrawerApi解锁抽屉

Alert 警告提示框

基本使用

<script setup lang="ts">
import { useAlert } from '@cimom/vben-core-ui-kit-popup-ui';

const alert = useAlert();

const showSuccess = () => {
  alert.success('操作成功', '成功提示');
};

const showError = () => {
  alert.error('操作失败', '错误提示');
};

const showWarning = () => {
  alert.warning('注意事项', '警告提示');
};

const showInfo = () => {
  alert.info('提示信息', '信息提示');
};
</script>

<template>
  <div class="space-x-2">
    <button @click="showSuccess">成功提示</button>
    <button @click="showError">错误提示</button>
    <button @click="showWarning">警告提示</button>
    <button @click="showInfo">信息提示</button>
  </div>
</template>

Alert API 方法

方法名参数返回值说明
success(message: string, title?: string, options?: AlertOptions) => void-显示成功提示
error(message: string, title?: string, options?: AlertOptions) => void-显示错误提示
warning(message: string, title?: string, options?: AlertOptions) => void-显示警告提示
info(message: string, title?: string, options?: AlertOptions) => void-显示信息提示
confirm(message: string, title?: string, options?: AlertOptions) => Promise<boolean>Promise<boolean>显示确认提示框

AlertOptions 配置项

属性名类型默认值说明
confirmTextstring'确认'确认按钮文本
cancelTextstring'取消'取消按钮文本
showCancelButtonbooleanfalse是否显示取消按钮
centeredbooleantrue是否居中显示
closablebooleantrue是否显示关闭按钮
closeOnClickModalbooleantrue点击遮罩是否关闭
closeOnPressEscapebooleantrue按 ESC 是否关闭
durationnumber3000自动关闭的延时(毫秒),设为 0 则不自动关闭

示例

自定义内容的模态框

<template>
  <button @click="openCustomModal">打开自定义模态框</button>
</template>

<script setup lang="ts">
import { useModal } from '@cimom/vben-core-ui-kit-popup-ui';
import { h } from 'vue';

const modal = useModal();

const openCustomModal = () => {
  modal.open({
    title: '自定义内容',
    content: h('div', { class: 'p-4' }, [
      h('h3', { class: 'text-lg font-bold mb-2' }, '用户信息'),
      h('div', { class: 'mb-2' }, [
        h('span', { class: 'font-medium' }, '用户名: '),
        h('span', 'admin'),
      ]),
      h('div', { class: 'mb-2' }, [
        h('span', { class: 'font-medium' }, '邮箱: '),
        h('span', 'admin@example.com'),
      ]),
      h('div', { class: 'mb-2' }, [
        h('span', { class: 'font-medium' }, '角色: '),
        h('span', '管理员'),
      ]),
    ]),
    width: '500px',
    centered: true,
  });
};
</script>

异步操作的模态框

<template>
  <button @click="openAsyncModal">打开异步操作模态框</button>
</template>

<script setup lang="ts">
import { useModal } from '@cimom/vben-core-ui-kit-popup-ui';

const modal = useModal();

const openAsyncModal = () => {
  const modalInstance = modal.open({
    title: '异步操作',
    content: '确认执行此操作吗?此操作需要一些时间完成。',
    onConfirm: async () => {
      try {
        // 锁定模态框,防止重复提交
        modalInstance.lock();

        // 模拟异步操作
        await new Promise((resolve) => setTimeout(resolve, 2000));

        // 操作成功后关闭模态框
        modalInstance.close();

        // 显示成功提示
        modal.success('操作成功完成');
      } catch (error) {
        // 解锁模态框,允许用户重试
        modalInstance.unlock();

        // 显示错误提示
        modal.error('操作失败: ' + error.message);
      }
    },
  });
};
</script>

表单抽屉

<template>
  <button @click="openFormDrawer">打开表单抽屉</button>
</template>

<script setup lang="ts">
import { useDrawer } from '@cimom/vben-core-ui-kit-popup-ui';
import { ref, h } from 'vue';

const drawer = useDrawer();

const openFormDrawer = () => {
  const formData = ref({
    name: '',
    email: '',
    description: '',
  });

  const drawerInstance = drawer.open({
    title: '添加用户',
    content: h('div', { class: 'p-4' }, [
      h('div', { class: 'mb-4' }, [
        h('label', { class: 'block mb-1' }, '姓名'),
        h('input', {
          class: 'w-full p-2 border rounded',
          value: formData.value.name,
          onInput: (e) => (formData.value.name = e.target.value),
        }),
      ]),
      h('div', { class: 'mb-4' }, [
        h('label', { class: 'block mb-1' }, '邮箱'),
        h('input', {
          class: 'w-full p-2 border rounded',
          value: formData.value.email,
          onInput: (e) => (formData.value.email = e.target.value),
        }),
      ]),
      h('div', { class: 'mb-4' }, [
        h('label', { class: 'block mb-1' }, '描述'),
        h('textarea', {
          class: 'w-full p-2 border rounded',
          rows: 4,
          value: formData.value.description,
          onInput: (e) => (formData.value.description = e.target.value),
        }),
      ]),
    ]),
    footer: true,
    showConfirmButton: true,
    showCancelButton: true,
    confirmText: '保存',
    cancelText: '取消',
    width: '400px',
    onConfirm: async () => {
      // 验证表单
      if (!formData.value.name || !formData.value.email) {
        drawer.error('请填写必填字段');
        return;
      }

      // 锁定抽屉
      drawerInstance.lock();

      try {
        // 模拟提交数据
        await new Promise((resolve) => setTimeout(resolve, 1000));

        // 关闭抽屉
        drawerInstance.close();

        // 显示成功提示
        drawer.success('用户添加成功');
      } catch (error) {
        // 解锁抽屉
        drawerInstance.unlock();

        // 显示错误提示
        drawer.error('保存失败: ' + error.message);
      }
    },
  });
};
</script>