fix(BasicForm): when value is 0 or false resetFields is not work (#3828)
* fix(BasicForm): when value is 0 or false resetFields is not work * fix(BasicForm): when value is 0 or false resetFields is not work
This commit is contained in:
parent
478802b426
commit
22052f10f9
|
|
@ -121,7 +121,7 @@ export function useForm(props?: Props): UseFormReturnType {
|
||||||
const form = await getForm();
|
const form = await getForm();
|
||||||
return form.validateFields(nameList);
|
return form.validateFields(nameList);
|
||||||
},
|
},
|
||||||
resetDefaultField:async (nameList?: NamePath[]) => {
|
resetDefaultField: async (nameList?: NamePath[]) => {
|
||||||
unref(formRef)?.resetDefaultField(nameList);
|
unref(formRef)?.resetDefaultField(nameList);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -100,16 +100,15 @@ export function useFormEvents({
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let constructValue
|
let constructValue;
|
||||||
const setDateFieldValue = (v) => {
|
const setDateFieldValue = (v) => {
|
||||||
return v ? (_props?.valueFormat ? v : dateUtil(v)) : null;
|
return v ? (_props?.valueFormat ? v : dateUtil(v)) : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Adapt date component
|
||||||
// Adapt date component
|
if (itemIsDateComponent(schema?.component)) {
|
||||||
if(itemIsDateComponent(schema?.component)){
|
constructValue = tryConstructArray(key, values);
|
||||||
constructValue = tryConstructArray(key, values) ;
|
if (constructValue) {
|
||||||
if(!!constructValue){
|
|
||||||
const fieldValue = constructValue || value;
|
const fieldValue = constructValue || value;
|
||||||
if (Array.isArray(fieldValue)) {
|
if (Array.isArray(fieldValue)) {
|
||||||
const arr: any[] = [];
|
const arr: any[] = [];
|
||||||
|
|
@ -124,8 +123,8 @@ export function useFormEvents({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adapt common component
|
// Adapt common component
|
||||||
if (hasKey) {
|
if (hasKey) {
|
||||||
constructValue = get(value, key);
|
constructValue = get(value, key);
|
||||||
const fieldValue = constructValue || value;
|
const fieldValue = constructValue || value;
|
||||||
|
|
@ -135,7 +134,7 @@ export function useFormEvents({
|
||||||
}
|
}
|
||||||
validKeys.push(key);
|
validKeys.push(key);
|
||||||
} else {
|
} else {
|
||||||
// key not exist
|
// key not exist
|
||||||
// refer:https://github.com/vbenjs/vue-vben-admin/issues/3795
|
// refer:https://github.com/vbenjs/vue-vben-admin/issues/3795
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -145,27 +144,27 @@ export function useFormEvents({
|
||||||
/**
|
/**
|
||||||
* @description: Set form default value
|
* @description: Set form default value
|
||||||
*/
|
*/
|
||||||
function resetDefaultField(nameList?: NamePath[]) {
|
function resetDefaultField(nameList?: NamePath[]) {
|
||||||
if(!Array.isArray(nameList)){
|
if (!Array.isArray(nameList)) {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
if (Array.isArray(nameList) && nameList.length === 0) {
|
if (Array.isArray(nameList) && nameList.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const validKeys: string[] = [];
|
const validKeys: string[] = [];
|
||||||
let keys = Object.keys(unref(formModel))
|
const keys = Object.keys(unref(formModel));
|
||||||
if(!keys){
|
if (!keys) {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
nameList.forEach((key:any) => {
|
nameList.forEach((key: any) => {
|
||||||
if(keys.includes(key)){
|
if (keys.includes(key)) {
|
||||||
validKeys.push(key);
|
validKeys.push(key);
|
||||||
unref(formModel)[key] = cloneDeep(unref(get(defaultValueRef.value, key)));
|
unref(formModel)[key] = cloneDeep(unref(get(defaultValueRef.value, key)));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
validateFields(validKeys).catch((_) => {});
|
validateFields(validKeys).catch((_) => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: Delete based on field name
|
* @description: Delete based on field name
|
||||||
*/
|
*/
|
||||||
|
|
@ -175,9 +174,9 @@ export function useFormEvents({
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let fieldList: string[] = isString(fields) ? [fields] : fields;
|
let fieldList = (isString(fields) ? [fields] : fields) as string[];
|
||||||
if (isString(fields)) {
|
if (isString(fields)) {
|
||||||
fieldList = [fields];
|
fieldList = [fields as string];
|
||||||
}
|
}
|
||||||
for (const field of fieldList) {
|
for (const field of fieldList) {
|
||||||
_removeSchemaByField(field, schemaList);
|
_removeSchemaByField(field, schemaList);
|
||||||
|
|
@ -404,7 +403,7 @@ export function useFormEvents({
|
||||||
resetFields,
|
resetFields,
|
||||||
setFieldsValue,
|
setFieldsValue,
|
||||||
scrollToField,
|
scrollToField,
|
||||||
resetDefaultField
|
resetDefaultField,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -416,7 +415,7 @@ function getDefaultValue(
|
||||||
let defaultValue = cloneDeep(defaultValueRef.value[key]);
|
let defaultValue = cloneDeep(defaultValueRef.value[key]);
|
||||||
const isInput = checkIsInput(schema);
|
const isInput = checkIsInput(schema);
|
||||||
if (isInput) {
|
if (isInput) {
|
||||||
return defaultValue || undefined;
|
return !isNil(defaultValue) ? defaultValue : undefined;
|
||||||
}
|
}
|
||||||
if (!defaultValue && schema && checkIsRangeSlider(schema)) {
|
if (!defaultValue && schema && checkIsRangeSlider(schema)) {
|
||||||
defaultValue = [0, 0];
|
defaultValue = [0, 0];
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,7 @@ export function useFormValues({
|
||||||
const schemas = unref(getSchema);
|
const schemas = unref(getSchema);
|
||||||
const obj: Recordable = {};
|
const obj: Recordable = {};
|
||||||
schemas.forEach((item) => {
|
schemas.forEach((item) => {
|
||||||
const { defaultValue, defaultValueObj, componentProps={} } = item;
|
const { defaultValue, defaultValueObj, componentProps = {} } = item;
|
||||||
const fieldKeys = Object.keys(defaultValueObj || {});
|
const fieldKeys = Object.keys(defaultValueObj || {});
|
||||||
if (fieldKeys.length) {
|
if (fieldKeys.length) {
|
||||||
fieldKeys.forEach((field) => {
|
fieldKeys.forEach((field) => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue