|
@@ -295,6 +295,7 @@ export class FormApi {
|
|
|
return true;
|
|
|
});
|
|
|
const filteredFields = fieldMergeFn(fields, form.values);
|
|
|
+ this.handleStringToArrayFields(filteredFields);
|
|
|
form.setValues(filteredFields, shouldValidate);
|
|
|
}
|
|
|
|
|
@@ -304,6 +305,7 @@ 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;
|
|
@@ -392,10 +394,53 @@ export class FormApi {
|
|
|
return this.form;
|
|
|
}
|
|
|
|
|
|
+ private handleArrayToStringFields = (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,
|
|
|
+ );
|
|
|
+ };
|
|
|
+
|
|
|
+ // 处理简单数组格式 ['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;
|
|
|
+ // 根据类型定义,fields 应该始终是字符串数组
|
|
|
+ if (!Array.isArray(fields)) {
|
|
|
+ console.warn(
|
|
|
+ `Invalid field configuration: fields should be an array of strings, got ${typeof fields}`,
|
|
|
+ );
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ processFields(fields, separator);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
private handleRangeTimeValue = (originValues: Record<string, any>) => {
|
|
|
const values = { ...originValues };
|
|
|
const fieldMappingTime = this.state?.fieldMappingTime;
|
|
|
|
|
|
+ this.handleStringToArrayFields(values);
|
|
|
+
|
|
|
if (!fieldMappingTime || !Array.isArray(fieldMappingTime)) {
|
|
|
return values;
|
|
|
}
|
|
@@ -441,6 +486,80 @@ 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,
|
|
|
+ originValues: Record<string, any>,
|
|
|
+ transformFn: (value: any, separator: string) => any,
|
|
|
+ ) => {
|
|
|
+ fields.forEach((field) => {
|
|
|
+ const value = originValues[field];
|
|
|
+ if (value === undefined || value === null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ originValues[field] = transformFn(value, separator);
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
private updateState() {
|
|
|
const currentSchema = this.state?.schema ?? [];
|
|
|
const prevSchema = this.prevState?.schema ?? [];
|