From e65faa5ef8e0d347cbb3a0973f1c4416028bf83e Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Tue, 23 May 2023 21:56:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=87=BD=E6=95=B0=E5=BC=8F=E5=BC=B9?= =?UTF-8?q?=E6=A1=86=E6=B7=BB=E5=8A=A0`updateDialog`=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E5=BC=B9=E6=A1=86=E8=87=AA=E8=BA=AB=E5=B1=9E=E6=80=A7=E5=80=BC?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ReDialog/index.ts | 27 ++++++++++++- src/views/components/dialog/index.vue | 56 ++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/components/ReDialog/index.ts b/src/components/ReDialog/index.ts index 377f8409b..cd7a8a553 100644 --- a/src/components/ReDialog/index.ts +++ b/src/components/ReDialog/index.ts @@ -12,6 +12,7 @@ import type { const dialogStore = ref>([]); +/** 打开弹框 */ const addDialog = (options: DialogOptions) => { const open = () => dialogStore.value.push(Object.assign(options, { visible: true })); @@ -24,16 +25,40 @@ const addDialog = (options: DialogOptions) => { } }; +/** 关闭弹框 */ const closeDialog = (options: DialogOptions, index: number, args?: any) => { dialogStore.value.splice(index, 1); options.closeCallBack && options.closeCallBack({ options, index, args }); }; +/** + * @description 更改弹框自身属性值 + * @param value 属性值 + * @param key 属性,默认`title` + * @param index 弹框索引(默认`0`,代表只有一个弹框,对于嵌套弹框要改哪个弹框的属性值就把该弹框索引赋给`index`) + */ +const updateDialog = (value: any, key = "title", index = 0) => { + dialogStore.value[index][key] = value; +}; + +/** 关闭所有弹框 */ const closeAllDialog = () => { dialogStore.value = []; }; +/** 千万别忘了在下面这三处引入并注册下,放心注册,不使用`addDialog`调用就不会被挂载 + * https://github.com/pure-admin/vue-pure-admin/blob/main/src/App.vue#L4 + * https://github.com/pure-admin/vue-pure-admin/blob/main/src/App.vue#L13 + * https://github.com/pure-admin/vue-pure-admin/blob/main/src/App.vue#L18 + */ const ReDialog = withInstall(reDialog); export type { EventType, ArgsType, DialogProps, ButtonProps, DialogOptions }; -export { ReDialog, dialogStore, addDialog, closeDialog, closeAllDialog }; +export { + ReDialog, + dialogStore, + addDialog, + closeDialog, + updateDialog, + closeAllDialog +}; diff --git a/src/views/components/dialog/index.vue b/src/views/components/dialog/index.vue index 3a2f9839f..106535b9a 100644 --- a/src/views/components/dialog/index.vue +++ b/src/views/components/dialog/index.vue @@ -2,9 +2,14 @@ import { useRouter } from "vue-router"; import { h, createVNode, ref } from "vue"; import { message } from "@/utils/message"; -import { cloneDeep } from "@pureadmin/utils"; import forms, { type FormProps } from "./form.vue"; -import { addDialog, closeDialog, closeAllDialog } from "@/components/ReDialog"; +import { cloneDeep, debounce } from "@pureadmin/utils"; +import { + addDialog, + closeDialog, + updateDialog, + closeAllDialog +} from "@/components/ReDialog"; defineOptions({ name: "DialogPage" @@ -60,13 +65,16 @@ function onStyleClick() { }); } -function onoOpenDelayClick() { - addDialog({ - title: "延时2秒打开弹框", - openDelay: 2000, - contentRenderer: () =>

弹框内容-延时2秒打开弹框

- }); -} +// 添加 600ms 防抖 +const onoOpenDelayClick = debounce( + () => + addDialog({ + title: "延时2秒打开弹框", + openDelay: 2000 - 600, + contentRenderer: () =>

弹框内容-延时2秒打开弹框

+ }), + 600 +); function onCloseDelayClick() { addDialog({ @@ -240,6 +248,35 @@ function onNestingClick() { }); } +// 满足在 contentRenderer 内容区更改弹框自身属性值的场景 +function onUpdateClick() { + const curPage = ref(1); + addDialog({ + title: `第${curPage.value}页`, + contentRenderer: () => ( + <> + 1 ? false : true} + onClick={() => { + curPage.value -= 1; + updateDialog(`第${curPage.value}页`); + }} + > + 上一页 + + { + curPage.value += 1; + updateDialog(`第${curPage.value}页`); + }} + > + 下一页 + + + ) + }); +} + // 结合Form表单(第一种方式,弹框关闭立刻恢复初始值)通过 props 属性接收子组件的 prop 并赋值 function onFormOneClick() { addDialog({ @@ -421,6 +458,7 @@ function onBeforeSureClick() { 打开后的回调 关闭后的回调 嵌套的弹框 + 更改弹框自身属性值