fix: 解决搜索表单中arrayToStringFields属性无法在搜索时生效的问题 (#6591)
- 合并handleArrayToStringFields与handleStringToArrayFields为handleMultiFields - 在handleMultiFields判断arrayToString或stringToArray - 删除两个方法在其他地方的引用 统一为在handleRangeTimeValue内调用handleMultiFields实现数据转换 Co-authored-by: 宛晴 <wanqing@mengtaigroup.com>
This commit is contained in:
parent
482ce981ce
commit
a8b848d367
@ -348,7 +348,6 @@ export class FormApi {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
const filteredFields = fieldMergeFn(fields, form.values);
|
const filteredFields = fieldMergeFn(fields, form.values);
|
||||||
this.handleStringToArrayFields(filteredFields);
|
|
||||||
form.setValues(filteredFields, shouldValidate);
|
form.setValues(filteredFields, shouldValidate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,7 +357,6 @@ export class FormApi {
|
|||||||
const form = await this.getForm();
|
const form = await this.getForm();
|
||||||
await form.submitForm();
|
await form.submitForm();
|
||||||
const rawValues = toRaw(await this.getValues());
|
const rawValues = toRaw(await this.getValues());
|
||||||
this.handleArrayToStringFields(rawValues);
|
|
||||||
await this.state?.handleSubmit?.(rawValues);
|
await this.state?.handleSubmit?.(rawValues);
|
||||||
|
|
||||||
return rawValues;
|
return rawValues;
|
||||||
@ -458,16 +456,31 @@ export class FormApi {
|
|||||||
return this.form;
|
return this.form;
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleArrayToStringFields = (originValues: Record<string, any>) => {
|
private handleMultiFields = (originValues: Record<string, any>) => {
|
||||||
const arrayToStringFields = this.state?.arrayToStringFields;
|
const arrayToStringFields = this.state?.arrayToStringFields;
|
||||||
if (!arrayToStringFields || !Array.isArray(arrayToStringFields)) {
|
if (!arrayToStringFields || !Array.isArray(arrayToStringFields)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const processFields = (fields: string[], separator: string = ',') => {
|
const processFields = (fields: string[], separator: string = ',') => {
|
||||||
this.processFields(fields, separator, originValues, (value, sep) =>
|
this.processFields(fields, separator, originValues, (value, sep) => {
|
||||||
Array.isArray(value) ? value.join(sep) : value,
|
if (Array.isArray(value)) {
|
||||||
|
return value.join(sep);
|
||||||
|
} else if (typeof value === 'string') {
|
||||||
|
// 处理空字符串的情况
|
||||||
|
if (value === '') {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
// 处理复杂分隔符的情况
|
||||||
|
const escapedSeparator = sep.replaceAll(
|
||||||
|
/[.*+?^${}()|[\]\\]/g,
|
||||||
|
String.raw`\$&`,
|
||||||
);
|
);
|
||||||
|
return value.split(new RegExp(escapedSeparator));
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// 处理简单数组格式 ['field1', 'field2', ';'] 或 ['field1', 'field2']
|
// 处理简单数组格式 ['field1', 'field2', ';'] 或 ['field1', 'field2']
|
||||||
@ -503,8 +516,7 @@ export class FormApi {
|
|||||||
const values = { ...originValues };
|
const values = { ...originValues };
|
||||||
const fieldMappingTime = this.state?.fieldMappingTime;
|
const fieldMappingTime = this.state?.fieldMappingTime;
|
||||||
|
|
||||||
this.handleStringToArrayFields(values);
|
this.handleMultiFields(values);
|
||||||
|
|
||||||
if (!fieldMappingTime || !Array.isArray(fieldMappingTime)) {
|
if (!fieldMappingTime || !Array.isArray(fieldMappingTime)) {
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
@ -550,65 +562,6 @@ export class FormApi {
|
|||||||
return values;
|
return values;
|
||||||
};
|
};
|
||||||
|
|
||||||
private handleStringToArrayFields = (originValues: Record<string, any>) => {
|
|
||||||
const arrayToStringFields = this.state?.arrayToStringFields;
|
|
||||||
if (!arrayToStringFields || !Array.isArray(arrayToStringFields)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const processFields = (fields: string[], separator: string = ',') => {
|
|
||||||
this.processFields(fields, separator, originValues, (value, sep) => {
|
|
||||||
if (typeof value !== 'string') {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
// 处理空字符串的情况
|
|
||||||
if (value === '') {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
// 处理复杂分隔符的情况
|
|
||||||
const escapedSeparator = sep.replaceAll(
|
|
||||||
/[.*+?^${}()|[\]\\]/g,
|
|
||||||
String.raw`\$&`,
|
|
||||||
);
|
|
||||||
return value.split(new RegExp(escapedSeparator));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 处理简单数组格式 ['field1', 'field2', ';'] 或 ['field1', 'field2']
|
|
||||||
if (arrayToStringFields.every((item) => typeof item === 'string')) {
|
|
||||||
const lastItem =
|
|
||||||
arrayToStringFields[arrayToStringFields.length - 1] || '';
|
|
||||||
const fields =
|
|
||||||
lastItem.length === 1
|
|
||||||
? arrayToStringFields.slice(0, -1)
|
|
||||||
: arrayToStringFields;
|
|
||||||
const separator = lastItem.length === 1 ? lastItem : ',';
|
|
||||||
processFields(fields, separator);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理嵌套数组格式 [['field1'], ';']
|
|
||||||
arrayToStringFields.forEach((fieldConfig) => {
|
|
||||||
if (Array.isArray(fieldConfig)) {
|
|
||||||
const [fields, separator = ','] = fieldConfig;
|
|
||||||
if (Array.isArray(fields)) {
|
|
||||||
processFields(fields, separator);
|
|
||||||
} else if (typeof originValues[fields] === 'string') {
|
|
||||||
const value = originValues[fields];
|
|
||||||
if (value === '') {
|
|
||||||
originValues[fields] = [];
|
|
||||||
} else {
|
|
||||||
const escapedSeparator = separator.replaceAll(
|
|
||||||
/[.*+?^${}()|[\]\\]/g,
|
|
||||||
String.raw`\$&`,
|
|
||||||
);
|
|
||||||
originValues[fields] = value.split(new RegExp(escapedSeparator));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
private processFields = (
|
private processFields = (
|
||||||
fields: string[],
|
fields: string[],
|
||||||
separator: string,
|
separator: string,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user