Merge pull request #113 from cshaptx4869/patch-72

watch函数增加配置项参数返回
This commit is contained in:
Ray Hao
2024-05-24 10:29:22 +08:00
committed by GitHub
3 changed files with 31 additions and 32 deletions

View File

@@ -106,46 +106,45 @@ const formRef = ref<FormInstance>();
const formItems = reactive(props.formItems); const formItems = reactive(props.formItems);
const formData = reactive<IObject>({}); const formData = reactive<IObject>({});
const formRules: FormRules = {}; const formRules: FormRules = {};
const watchArr = []; const prepareFuncs = [];
const computedArr = [];
const watchEffectArr = [];
// 初始化 // 初始化
for (const item of formItems) { for (const item of formItems) {
item.initFn && item.initFn(item); item.initFn && item.initFn(item);
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) {
watchArr.push({ field: item.prop, func: item.watch }); prepareFuncs.push(() => {
}
if (item.computed !== undefined) {
computedArr.push({ field: item.prop, func: item.computed });
}
if (item.watchEffect !== undefined) {
watchEffectArr.push(item.watchEffect);
}
}
watchArr.forEach(({ field, func }) => {
watch( watch(
() => formData[field], () => formData[item.prop],
(newValue, oldValue) => { (newValue, oldValue) => {
func(newValue, oldValue, formData); item.watch && item.watch(newValue, oldValue, formData, formItems);
} }
); );
}); });
computedArr.forEach(({ field, func }) => { }
formData[field] = computed({
if (item.computed !== undefined) {
prepareFuncs.push(() => {
formData[item.prop] = computed({
get() { get() {
return func(formData); return item.computed ? item.computed(formData) : undefined;
}, },
// TODO // TODO
set() {}, set() {},
}); });
});
watchEffectArr.forEach((func) => {
watchEffect(() => {
func(formData);
}); });
}); }
if (item.watchEffect !== undefined) {
prepareFuncs.push(() => {
watchEffect(() => {
item.watchEffect && item.watchEffect(formData);
});
});
}
}
prepareFuncs.forEach((func) => func());
// 获取表单数据 // 获取表单数据
function getFormData(key?: string) { function getFormData(key?: string) {
return key === undefined ? formData : formData[key] ?? undefined; return key === undefined ? formData : formData[key] ?? undefined;

View File

@@ -113,11 +113,11 @@ const handleSubmit = useThrottleFn(() => {
let msg = "操作成功"; let msg = "操作成功";
if (props.modalConfig.component === "drawer") { if (props.modalConfig.component === "drawer") {
if (props.modalConfig.drawer?.title) { if (props.modalConfig.drawer?.title) {
msg = props.modalConfig.drawer?.title; msg = `${props.modalConfig.drawer?.title}成功`;
} }
} else { } else {
if (props.modalConfig.dialog?.title) { if (props.modalConfig.dialog?.title) {
msg = props.modalConfig.dialog?.title; msg = `${props.modalConfig.dialog?.title}成功`;
} }
} }
ElMessage.success(msg); ElMessage.success(msg);

View File

@@ -54,11 +54,11 @@ 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, items: IObject[]) => void;
// 计算属性函数 // 计算属性函数
computed?: (data: T) => any; computed?: (data: T) => any;
// 监听收集函数 // 监听收集函数
watchEffect?: (data: T) => void; watchEffect?: (data: T) => void;
// 初始化数据函数扩展 // 初始化数据函数扩展
initFn?: (formItem: IObject) => void; initFn?: (item: IObject) => void;
}>; }>;