From a492146a16359a13dfca89f752109a8916f3c55a Mon Sep 17 00:00:00 2001 From: cshaptx4869 <994774638@qq.com> Date: Thu, 23 May 2024 19:55:03 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor(PageModal):=20:recycle:=20?= =?UTF-8?q?=E9=87=8D=E6=9E=84watch=E3=80=81computed=E3=80=81watchEffect?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/PageModal/Form.vue | 55 +++++++++++++++---------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/components/PageModal/Form.vue b/src/components/PageModal/Form.vue index fe3ba1ee..0a030b7b 100644 --- a/src/components/PageModal/Form.vue +++ b/src/components/PageModal/Form.vue @@ -106,46 +106,45 @@ const formRef = ref(); const formItems = reactive(props.formItems); const formData = reactive({}); const formRules: FormRules = {}; -const watchArr = []; -const computedArr = []; -const watchEffectArr = []; +const prepareFuncs = []; // 初始化 for (const item of formItems) { item.initFn && item.initFn(item); formData[item.prop] = item.initialValue ?? ""; formRules[item.prop] = item.rules ?? []; + if (item.watch !== undefined) { - watchArr.push({ field: item.prop, func: item.watch }); + prepareFuncs.push(() => { + watch( + () => formData[item.prop], + (newValue, oldValue) => { + item.watch && item.watch(newValue, oldValue, formData); + } + ); + }); } + if (item.computed !== undefined) { - computedArr.push({ field: item.prop, func: item.computed }); + prepareFuncs.push(() => { + formData[item.prop] = computed({ + get() { + return item.computed ? item.computed(formData) : undefined; + }, + // TODO + set() {}, + }); + }); } + if (item.watchEffect !== undefined) { - watchEffectArr.push(item.watchEffect); + prepareFuncs.push(() => { + watchEffect(() => { + item.watchEffect && item.watchEffect(formData); + }); + }); } } -watchArr.forEach(({ field, func }) => { - watch( - () => formData[field], - (newValue, oldValue) => { - func(newValue, oldValue, formData); - } - ); -}); -computedArr.forEach(({ field, func }) => { - formData[field] = computed({ - get() { - return func(formData); - }, - // TODO - set() {}, - }); -}); -watchEffectArr.forEach((func) => { - watchEffect(() => { - func(formData); - }); -}); +prepareFuncs.forEach((func) => func()); // 获取表单数据 function getFormData(key?: string) { return key === undefined ? formData : formData[key] ?? undefined; From 45ca47457158e493dc52ea77c6de61be212d3496 Mon Sep 17 00:00:00 2001 From: cshaptx4869 <994774638@qq.com> Date: Thu, 23 May 2024 20:47:58 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat(PageModal):=20:sparkles:=20watch?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE=E9=A1=B9?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/PageModal/Form.vue | 2 +- src/components/PageModal/types.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/PageModal/Form.vue b/src/components/PageModal/Form.vue index 0a030b7b..a36d0655 100644 --- a/src/components/PageModal/Form.vue +++ b/src/components/PageModal/Form.vue @@ -118,7 +118,7 @@ for (const item of formItems) { watch( () => formData[item.prop], (newValue, oldValue) => { - item.watch && item.watch(newValue, oldValue, formData); + item.watch && item.watch(newValue, oldValue, formData, formItems); } ); }); diff --git a/src/components/PageModal/types.ts b/src/components/PageModal/types.ts index cddca5fb..4846b0af 100644 --- a/src/components/PageModal/types.ts +++ b/src/components/PageModal/types.ts @@ -54,11 +54,11 @@ export type IFormItems = Array<{ // 是否隐藏 hidden?: boolean; // 监听函数 - watch?: (newValue: any, oldValue: any, data: T) => void; + watch?: (newValue: any, oldValue: any, data: T, items: IObject[]) => void; // 计算属性函数 computed?: (data: T) => any; // 监听收集函数 watchEffect?: (data: T) => void; // 初始化数据函数扩展 - initFn?: (formItem: IObject) => void; + initFn?: (item: IObject) => void; }>; From c8bf3927c9c2130f683018fc7397e21155aafaea Mon Sep 17 00:00:00 2001 From: cshaptx4869 <994774638@qq.com> Date: Fri, 24 May 2024 10:27:26 +0800 Subject: [PATCH 3/3] =?UTF-8?q?refactor(PageModal):=20:recycle:=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=93=8D=E4=BD=9C=E6=88=90=E5=8A=9F=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/PageModal/index.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/PageModal/index.vue b/src/components/PageModal/index.vue index 998304c5..74fad1d6 100644 --- a/src/components/PageModal/index.vue +++ b/src/components/PageModal/index.vue @@ -113,11 +113,11 @@ const handleSubmit = useThrottleFn(() => { let msg = "操作成功"; if (props.modalConfig.component === "drawer") { if (props.modalConfig.drawer?.title) { - msg = props.modalConfig.drawer?.title; + msg = `${props.modalConfig.drawer?.title}成功`; } } else { if (props.modalConfig.dialog?.title) { - msg = props.modalConfig.dialog?.title; + msg = `${props.modalConfig.dialog?.title}成功`; } } ElMessage.success(msg);