feat: 添加确认对话框组件,优化提示和对话框服务的交互逻辑
Some checks failed
/ playwright (push) Successful in 1m30s
/ depcheck (push) Successful in 1m19s
/ build-and-deploy-to-vercel (push) Failing after 1m50s

This commit is contained in:
严浩
2024-12-14 20:01:38 +08:00
parent 9ed80478e6
commit b1c0efb853
7 changed files with 215 additions and 180 deletions

View File

@ -9,5 +9,6 @@ const VITE_BUILD_COMMIT = import.meta.env.VITE_BUILD_COMMIT;
<RouterView />
<DynamicDialog />
<ConfirmDialog></ConfirmDialog>
<Toast />
</template>

View File

@ -1,7 +1,7 @@
<script lang="ts"></script>
<script setup lang="tsx">
import { DialogService, ToastService } from '@/utils/primevue';
import { ConfirmationService, DialogService, ToastService } from '@/utils/primevue';
import dialogContent from './dialog-content.vue';
const dynamicComponent = defineComponent({
@ -29,13 +29,13 @@ const dynamicComponent = defineComponent({
const openDialog = async () => {
const dialog1 = DialogService.open(dynamicComponent, {
props: {
header: 'Header1 可以拖动',
header: '对话框1 可以拖动',
position: 'bottomleft',
draggable: true,
pt: { mask: { class: 'backdrop-blur-sm' } }, // 相当于: pt:mask:class="backdrop-blur-sm"
},
data: {
用inject接收: '定义在-DynamicDialogOptions.data-的数据',
用inject接收: '通过 DynamicDialogOptions.data 传递的数据',
},
emits: {
// https://github.com/primefaces/primevue/blob/bd7161298a472c8cd954e35e6a538a8bd1b1b386/packages/primevue/src/dynamicdialog/DynamicDialog.vue#L5
@ -54,27 +54,48 @@ const openDialog = async () => {
await new Promise((resolve) => setTimeout(resolve, 300));
DialogService.open(dialogContent, {
props: {
header: 'Header2',
header: '对话框2',
position: 'bottomright',
// draggable: false, // Header1的 draggable: true 会影响 Header2如果指定 draggable: false则不会受到影响。
},
});
};
const openToast = () => {
ToastService.add({ severity: 'info', summary: 'Info', detail: 'Message Content', life: 3000 });
ToastService.add({ severity: 'info', summary: '提示', detail: '消息内容', life: 3000 });
};
const openConfirm = async () => {
ConfirmationService.require({
message: '确定要继续吗?',
header: '确认',
icon: 'pi pi-exclamation-triangle',
rejectProps: {
label: '取消',
severity: 'secondary',
outlined: true,
},
acceptProps: {
label: '确定',
},
accept: () => {
ToastService.add({ severity: 'info', summary: '已确认', detail: '您已同意操作', life: 3000 });
},
reject: () => {
ToastService.add({ severity: 'error', summary: '已取消', detail: '您已取消操作', life: 3000 });
},
});
};
</script>
<template>
<div class="primevue">
<InputText />
<InputText placeholder="请输入" />
<Select
:options="[
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' },
{ name: '纽约', code: 'NY' },
{ name: '罗马', code: 'RM' },
{ name: '伦敦', code: 'LDN' },
{ name: '伊斯坦布尔', code: 'IST' },
{ name: '巴黎', code: 'PRS' },
]"
optionLabel="name"
placeholder="选择城市"
@ -83,16 +104,8 @@ const openToast = () => {
<DatePicker dateFormat="dd/mm/yy" />
<Button @click="openToast"> ToastService </Button>
<Button @click="openDialog"> DialogService </Button>
<Button @click="openToast">提示服务</Button>
<Button @click="openDialog">对话框服务</Button>
<Button @click="openConfirm">确认服务</Button>
</div>
</template>
<style>
.p-toast {
max-width: calc(100% - 50px);
}
.p-toast .p-toast-message-text {
margin-top: -0.2rem;
}
</style>

View File

@ -13,7 +13,7 @@ import { router } from './router';
import Aura from '@primevue/themes/aura';
import zhCN from 'primelocale/zh-CN.json';
import PrimeVue from 'primevue/config';
// import 'primeicons/primeicons.css';
import 'primeicons/primeicons.css';
/* https://github.com/intlify/bundle-tools/tree/main/packages/unplugin-vue-i18n#static-bundle-importing
* All i18n resources specified in the plugin `include` option can be loaded

View File

@ -1,17 +1,29 @@
/**
* 需要把 <Toast /> 放在 App.vue 的 template 中
*/
import '../assets/reset-primevue.css';
// ========================================================================
// ========================== ConfirmationService =========================
// ========================================================================
// @ts-expect-error - Ignore missing types
import ConfirmationEventBus from 'primevue/confirmationeventbus';
import type { ConfirmationServiceMethods } from 'primevue/confirmationservice';
export const ConfirmationService: ConfirmationServiceMethods = {
require: (options) => {
ConfirmationEventBus.emit('confirm', options);
},
close: () => {
ConfirmationEventBus.emit('close');
},
};
// ========================================================================
// ============================= ToastService =============================
// ========================================================================
// @ts-expect-error - Ignore missing types
import ToastEventBus from 'primevue/toasteventbus';
import type { ToastServiceMethods } from 'primevue/toastservice';
// @ts-expect-error - Ignore missing types
import DynamicDialogEventBus from 'primevue/dynamicdialogeventbus';
import type { DialogServiceMethods } from 'primevue/dialogservice';
import '../assets/reset-primevue.css';
// https://github.com/primefaces/primevue/blob/61929eae7526015af0362fc5889f2af7527403d1/packages/primevue/src/toastservice/ToastService.js
export const ToastService: ToastServiceMethods = {
add: (message) => {
@ -28,6 +40,12 @@ export const ToastService: ToastServiceMethods = {
},
};
// ========================================================================
// ============================= DialogService ============================
// ========================================================================
// @ts-expect-error - Ignore missing types
import DynamicDialogEventBus from 'primevue/dynamicdialogeventbus';
import type { DialogServiceMethods } from 'primevue/dialogservice';
// https://github.com/primefaces/primevue/blob/18367429f624285ff32d0ef775c1825a43a02fb1/packages/primevue/src/dialogservice/DialogService.js#L7
export const DialogService: DialogServiceMethods = {
open: (content, options) => {