Skip to content

Commit

Permalink
perf(BasicForm): fix invaild defaultValue && split-setdefault-setvalue (
Browse files Browse the repository at this point in the history
  • Loading branch information
electroluxcode committed May 8, 2024
1 parent 4348d21 commit 88e77db
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/components/Form/src/BasicForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
removeSchemaByField,
resetFields,
scrollToField,
resetDefaultField,
} = useFormEvents({
emit,
getProps,
Expand Down Expand Up @@ -305,6 +306,7 @@
validate,
submit: handleSubmit,
scrollToField: scrollToField,
resetDefaultField,
};
const getFormActionBindProps = computed(
Expand Down
3 changes: 3 additions & 0 deletions src/components/Form/src/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export function useForm(props?: Props): UseFormReturnType {
const form = await getForm();
return form.validateFields(nameList);
},
resetDefaultField:async (nameList?: NamePath[]) => {
unref(formRef)?.resetDefaultField(nameList);
},
};

return [register, methods];
Expand Down
33 changes: 28 additions & 5 deletions src/components/Form/src/hooks/useFormEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ComputedRef, Ref } from 'vue';
import type { FormProps, FormSchemaInner as FormSchema, FormActionType } from '../types/form';
import type { NamePath } from 'ant-design-vue/lib/form/interface';
import { unref, toRaw, nextTick } from 'vue';
import { isArray, isFunction, isObject, isString, isDef, isNil } from '@/utils/is';
import { isArray, isFunction, isObject, isString, isNil } from '@/utils/is';
import { deepMerge } from '@/utils';
import { dateItemType, defaultValueComponents, isIncludeSimpleComponents } from '../helper';
import { dateUtil } from '@/utils/dateUtil';
Expand Down Expand Up @@ -110,15 +110,37 @@ export function useFormEvents({
}
validKeys.push(key);
} else {
// key not exist
if (isDef(get(defaultValueRef.value, key))) {
unref(formModel)[key] = cloneDeep(unref(get(defaultValueRef.value, key)));
}
// key not exist
// refer:https://github.com/vbenjs/vue-vben-admin/issues/3795
}
});
validateFields(validKeys).catch((_) => {});
}

/**
* @description: Set form default value
*/
function resetDefaultField(nameList?: NamePath[]) {
if(!Array.isArray(nameList)){
return
}
if (Array.isArray(nameList) && nameList.length === 0) {
return;
}
const validKeys: string[] = [];
let keys = Object.keys(unref(formModel))
if(!keys){
return
}
nameList.forEach((key:any) => {
if(keys.includes(key)){
validKeys.push(key);
unref(formModel)[key] = cloneDeep(unref(get(defaultValueRef.value, key)));
}
});
validateFields(validKeys).catch((_) => {});
}

/**
* @description: Delete based on field name
*/
Expand Down Expand Up @@ -359,6 +381,7 @@ export function useFormEvents({
resetFields,
setFieldsValue,
scrollToField,
resetDefaultField
};
}

Expand Down
8 changes: 7 additions & 1 deletion src/components/Form/src/hooks/useFormValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function useFormValues({
const schemas = unref(getSchema);
const obj: Recordable = {};
schemas.forEach((item) => {
const { defaultValue, defaultValueObj } = item;
const { defaultValue, defaultValueObj, componentProps={} } = item;
const fieldKeys = Object.keys(defaultValueObj || {});
if (fieldKeys.length) {
fieldKeys.forEach((field) => {
Expand All @@ -152,6 +152,12 @@ export function useFormValues({
formModel[item.field] = defaultValue;
}
}
if (!isNil(componentProps?.defaultValue)) {
obj[item.field] = componentProps?.defaultValue;
if (formModel[item.field] === undefined) {
formModel[item.field] = componentProps?.defaultValue;
}
}
});
defaultValueRef.value = cloneDeep(obj);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/Form/src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface FormActionType {
validateFields: (nameList?: NamePath[]) => Promise<any>;
validate: <T = Recordable>(nameList?: NamePath[] | false) => Promise<T>;
scrollToField: (name: NamePath, options?: ScrollOptions) => Promise<void>;
resetDefaultField:(name?: NamePath[]) => void;
}

export type RegisterFn = (formInstance: FormActionType) => void;
Expand Down

0 comments on commit 88e77db

Please sign in to comment.