|
|
@@ -348,7 +348,6 @@ export class FormApi {
|
|
|
return true;
|
|
|
});
|
|
|
const filteredFields = fieldMergeFn(fields, form.values);
|
|
|
- this.handleStringToArrayFields(filteredFields);
|
|
|
form.setValues(filteredFields, shouldValidate);
|
|
|
}
|
|
|
|
|
|
@@ -358,7 +357,6 @@ export class FormApi {
|
|
|
const form = await this.getForm();
|
|
|
await form.submitForm();
|
|
|
const rawValues = toRaw(await this.getValues());
|
|
|
- this.handleArrayToStringFields(rawValues);
|
|
|
await this.state?.handleSubmit?.(rawValues);
|
|
|
|
|
|
return rawValues;
|
|
|
@@ -458,16 +456,31 @@ export class FormApi {
|
|
|
return this.form;
|
|
|
}
|
|
|
|
|
|
- private handleArrayToStringFields = (originValues: Record<string, any>) => {
|
|
|
+ private handleMultiFields = (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) =>
|
|
|
- Array.isArray(value) ? value.join(sep) : value,
|
|
|
- );
|
|
|
+ this.processFields(fields, separator, originValues, (value, sep) => {
|
|
|
+ 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']
|
|
|
@@ -503,8 +516,7 @@ export class FormApi {
|
|
|
const values = { ...originValues };
|
|
|
const fieldMappingTime = this.state?.fieldMappingTime;
|
|
|
|
|
|
- this.handleStringToArrayFields(values);
|
|
|
-
|
|
|
+ this.handleMultiFields(values);
|
|
|
if (!fieldMappingTime || !Array.isArray(fieldMappingTime)) {
|
|
|
return values;
|
|
|
}
|
|
|
@@ -550,65 +562,6 @@ export class FormApi {
|
|
|
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 = (
|
|
|
fields: string[],
|
|
|
separator: string,
|