Merge pull request #99 from cshaptx4869/patch-61
feat(PageModal): ✨ 表单项增加computed和watchEffect配置
This commit is contained in:
@@ -88,7 +88,7 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import type { FormInstance, FormRules } from "element-plus";
|
import type { FormInstance, FormRules } from "element-plus";
|
||||||
import { reactive, ref, watch } from "vue";
|
import { reactive, ref, watch, computed, watchEffect } from "vue";
|
||||||
import { IForm, IFormItems, IObject } from "./types";
|
import { IForm, IFormItems, IObject } from "./types";
|
||||||
|
|
||||||
// 定义接收的属性
|
// 定义接收的属性
|
||||||
@@ -109,19 +109,45 @@ const props = withDefaults(
|
|||||||
const formRef = ref<FormInstance>();
|
const formRef = ref<FormInstance>();
|
||||||
const formData = reactive<IObject>({});
|
const formData = reactive<IObject>({});
|
||||||
const formRules: FormRules = {};
|
const formRules: FormRules = {};
|
||||||
|
const watchArr = [];
|
||||||
|
const computedArr = [];
|
||||||
|
const watchEffectArr = [];
|
||||||
// 初始化
|
// 初始化
|
||||||
for (const item of props.formItems) {
|
for (const item of props.formItems) {
|
||||||
formData[item.prop] = item.initialValue ?? "";
|
formData[item.prop] = item.initialValue ?? "";
|
||||||
formRules[item.prop] = item.rules ?? [];
|
formRules[item.prop] = item.rules ?? [];
|
||||||
if (item.watch !== undefined) {
|
if (item.watch !== undefined) {
|
||||||
watch(
|
watchArr.push({ field: item.prop, func: item.watch });
|
||||||
() => formData[item.prop],
|
}
|
||||||
(newValue, oldValue) => {
|
if (item.computed !== undefined) {
|
||||||
item.watch?.(newValue, oldValue, formData);
|
computedArr.push({ field: item.prop, func: item.computed });
|
||||||
}
|
}
|
||||||
);
|
if (item.watchEffect !== undefined) {
|
||||||
|
watchEffectArr.push(item.watchEffect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
// 获取表单数据
|
// 获取表单数据
|
||||||
function getFormData(key?: string) {
|
function getFormData(key?: string) {
|
||||||
return key === undefined ? formData : formData[key] ?? undefined;
|
return key === undefined ? formData : formData[key] ?? undefined;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
<!-- 表单 -->
|
<!-- 表单 -->
|
||||||
<page-form
|
<page-form
|
||||||
ref="pageFormRef"
|
ref="pageFormRef"
|
||||||
|
:pk="modalConfig.pk"
|
||||||
:form="modalConfig.form"
|
:form="modalConfig.form"
|
||||||
:form-items="modalConfig.formItems"
|
:form-items="modalConfig.formItems"
|
||||||
/>
|
/>
|
||||||
@@ -40,6 +41,7 @@
|
|||||||
<!-- 表单 -->
|
<!-- 表单 -->
|
||||||
<page-form
|
<page-form
|
||||||
ref="pageFormRef"
|
ref="pageFormRef"
|
||||||
|
:pk="modalConfig.pk"
|
||||||
:form="modalConfig.form"
|
:form="modalConfig.form"
|
||||||
:form-items="modalConfig.formItems"
|
:form-items="modalConfig.formItems"
|
||||||
style="padding-right: var(--el-dialog-padding-primary)"
|
style="padding-right: var(--el-dialog-padding-primary)"
|
||||||
@@ -94,7 +96,7 @@ const emit = defineEmits<{
|
|||||||
const modalVisible = ref(false);
|
const modalVisible = ref(false);
|
||||||
const pageFormRef = ref<InstanceType<typeof PageForm>>();
|
const pageFormRef = ref<InstanceType<typeof PageForm>>();
|
||||||
let initialFormData = {};
|
let initialFormData = {};
|
||||||
// 显示dialog
|
// 显示modal
|
||||||
function setModalVisible(data: IObject = {}) {
|
function setModalVisible(data: IObject = {}) {
|
||||||
modalVisible.value = true;
|
modalVisible.value = true;
|
||||||
initialFormData = data;
|
initialFormData = data;
|
||||||
|
|||||||
@@ -54,4 +54,8 @@ export type IFormItems<T = any> = Array<{
|
|||||||
hidden?: boolean;
|
hidden?: boolean;
|
||||||
// 监听函数
|
// 监听函数
|
||||||
watch?: (newValue: any, oldValue: any, data: T) => void;
|
watch?: (newValue: any, oldValue: any, data: T) => void;
|
||||||
|
// 计算属性函数
|
||||||
|
computed?: (data: T) => any;
|
||||||
|
// 监听收集函数
|
||||||
|
watchEffect?: (data: T) => void;
|
||||||
}>;
|
}>;
|
||||||
|
|||||||
Reference in New Issue
Block a user