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 formData = reactive<IObject>({});
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, formItems);
}
);
});
}
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;

View File

@@ -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);

View File

@@ -54,11 +54,11 @@ export type IFormItems<T = any> = 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;
}>;