Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(BasicForm): when value is 0 or false resetFields is not work #3828

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/Form/src/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function useForm(props?: Props): UseFormReturnType {
const form = await getForm();
return form.validateFields(nameList);
},
resetDefaultField:async (nameList?: NamePath[]) => {
resetDefaultField: async (nameList?: NamePath[]) => {
unref(formRef)?.resetDefaultField(nameList);
},
};
Expand Down
43 changes: 21 additions & 22 deletions src/components/Form/src/hooks/useFormEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,15 @@ export function useFormEvents({
});
}

let constructValue
let constructValue;
const setDateFieldValue = (v) => {
return v ? (_props?.valueFormat ? v : dateUtil(v)) : null;
};


// Adapt date component
if(itemIsDateComponent(schema?.component)){
constructValue = tryConstructArray(key, values) ;
if(!!constructValue){
// Adapt date component
if (itemIsDateComponent(schema?.component)) {
constructValue = tryConstructArray(key, values);
if (constructValue) {
const fieldValue = constructValue || value;
if (Array.isArray(fieldValue)) {
const arr: any[] = [];
Expand All @@ -124,8 +123,8 @@ export function useFormEvents({
}
}
}
// Adapt common component

// Adapt common component
if (hasKey) {
constructValue = get(value, key);
const fieldValue = constructValue || value;
Expand All @@ -135,7 +134,7 @@ export function useFormEvents({
}
validKeys.push(key);
} else {
// key not exist
// key not exist
// refer:https://github.com/vbenjs/vue-vben-admin/issues/3795
}
});
Expand All @@ -145,27 +144,27 @@ export function useFormEvents({
/**
* @description: Set form default value
*/
function resetDefaultField(nameList?: NamePath[]) {
if(!Array.isArray(nameList)){
return
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
const keys = Object.keys(unref(formModel));
if (!keys) {
return;
}
nameList.forEach((key:any) => {
if(keys.includes(key)){
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 All @@ -175,9 +174,9 @@ export function useFormEvents({
return;
}

let fieldList: string[] = isString(fields) ? [fields] : fields;
let fieldList = (isString(fields) ? [fields] : fields) as string[];
if (isString(fields)) {
fieldList = [fields];
fieldList = [fields as string];
}
for (const field of fieldList) {
_removeSchemaByField(field, schemaList);
Expand Down Expand Up @@ -404,7 +403,7 @@ export function useFormEvents({
resetFields,
setFieldsValue,
scrollToField,
resetDefaultField
resetDefaultField,
};
}

Expand All @@ -416,7 +415,7 @@ function getDefaultValue(
let defaultValue = cloneDeep(defaultValueRef.value[key]);
const isInput = checkIsInput(schema);
if (isInput) {
return defaultValue || undefined;
return !isNil(defaultValue) ? defaultValue : undefined;
}
if (!defaultValue && schema && checkIsRangeSlider(schema)) {
defaultValue = [0, 0];
Expand Down
2 changes: 1 addition & 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, componentProps={} } = item;
const { defaultValue, defaultValueObj, componentProps = {} } = item;
const fieldKeys = Object.keys(defaultValueObj || {});
if (fieldKeys.length) {
fieldKeys.forEach((field) => {
Expand Down
Loading