Skip to content

Commit e17c1a5

Browse files
authored
Merge pull request youlaitech#99 from cshaptx4869/patch-61
feat(PageModal): ✨ 表单项增加computed和watchEffect配置
2 parents e581ed2 + f181597 commit e17c1a5

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

Diff for: src/components/PageModal/Form.vue

+33-7
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
<script setup lang="ts">
9090
import type { FormInstance, FormRules } from "element-plus";
91-
import { reactive, ref, watch } from "vue";
91+
import { reactive, ref, watch, computed, watchEffect } from "vue";
9292
import { IForm, IFormItems, IObject } from "./types";
9393
9494
// 定义接收的属性
@@ -109,19 +109,45 @@ const props = withDefaults(
109109
const formRef = ref<FormInstance>();
110110
const formData = reactive<IObject>({});
111111
const formRules: FormRules = {};
112+
const watchArr = [];
113+
const computedArr = [];
114+
const watchEffectArr = [];
112115
// 初始化
113116
for (const item of props.formItems) {
114117
formData[item.prop] = item.initialValue ?? "";
115118
formRules[item.prop] = item.rules ?? [];
116119
if (item.watch !== undefined) {
117-
watch(
118-
() => formData[item.prop],
119-
(newValue, oldValue) => {
120-
item.watch?.(newValue, oldValue, formData);
121-
}
122-
);
120+
watchArr.push({ field: item.prop, func: item.watch });
121+
}
122+
if (item.computed !== undefined) {
123+
computedArr.push({ field: item.prop, func: item.computed });
124+
}
125+
if (item.watchEffect !== undefined) {
126+
watchEffectArr.push(item.watchEffect);
123127
}
124128
}
129+
watchArr.forEach(({ field, func }) => {
130+
watch(
131+
() => formData[field],
132+
(newValue, oldValue) => {
133+
func(newValue, oldValue, formData);
134+
}
135+
);
136+
});
137+
computedArr.forEach(({ field, func }) => {
138+
formData[field] = computed({
139+
get() {
140+
return func(formData);
141+
},
142+
// TODO
143+
set() {},
144+
});
145+
});
146+
watchEffectArr.forEach((func) => {
147+
watchEffect(() => {
148+
func(formData);
149+
});
150+
});
125151
// 获取表单数据
126152
function getFormData(key?: string) {
127153
return key === undefined ? formData : formData[key] ?? undefined;

Diff for: src/components/PageModal/index.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<!-- 表单 -->
1212
<page-form
1313
ref="pageFormRef"
14+
:pk="modalConfig.pk"
1415
:form="modalConfig.form"
1516
:form-items="modalConfig.formItems"
1617
/>
@@ -40,6 +41,7 @@
4041
<!-- 表单 -->
4142
<page-form
4243
ref="pageFormRef"
44+
:pk="modalConfig.pk"
4345
:form="modalConfig.form"
4446
:form-items="modalConfig.formItems"
4547
style="padding-right: var(--el-dialog-padding-primary)"
@@ -94,7 +96,7 @@ const emit = defineEmits<{
9496
const modalVisible = ref(false);
9597
const pageFormRef = ref<InstanceType<typeof PageForm>>();
9698
let initialFormData = {};
97-
// 显示dialog
99+
// 显示modal
98100
function setModalVisible(data: IObject = {}) {
99101
modalVisible.value = true;
100102
initialFormData = data;

Diff for: src/components/PageModal/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ export type IFormItems<T = any> = Array<{
5454
hidden?: boolean;
5555
// 监听函数
5656
watch?: (newValue: any, oldValue: any, data: T) => void;
57+
// 计算属性函数
58+
computed?: (data: T) => any;
59+
// 监听收集函数
60+
watchEffect?: (data: T) => void;
5761
}>;

0 commit comments

Comments
 (0)