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: 修复Form表单开启submitOnChange、submitOnEnter后配置fieldMappingTime的数组值映射字… #5328

Closed
wants to merge 1 commit into from
Closed
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
43 changes: 41 additions & 2 deletions packages/@core/ui-kit/form-ui/src/form-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Store } from '@vben-core/shared/store';
import {
bindMethods,
createMerge,
formatDate,
isDate,
isDayjsObject,
isFunction,
Expand Down Expand Up @@ -97,6 +98,44 @@ export class FormApi {
return form.values;
}

handleRangeTimeValue(values: Record<string, any>) {
const fieldMappingTime = this.state?.fieldMappingTime;

if (!fieldMappingTime || !Array.isArray(fieldMappingTime)) {
return values;
}

fieldMappingTime.forEach(
([field, [startTimeKey, endTimeKey], format = 'YYYY-MM-DD']) => {
if (startTimeKey && endTimeKey && values[field] === null) {
values[startTimeKey] = undefined;
values[endTimeKey] = undefined;
}

if (!values[field]) {
values[field] = undefined;
return;
}

const [startTime, endTime] = values[field];
const [startTimeFormat, endTimeFormat] = Array.isArray(format)
? format
: [format, format];

values[startTimeKey] = startTime
? formatDate(startTime, startTimeFormat)
: undefined;
values[endTimeKey] = endTime
? formatDate(endTime, endTimeFormat)
: undefined;

values[field] = undefined;
},
);

return values;
}
Dengjunle marked this conversation as resolved.
Show resolved Hide resolved

async isFieldValid(fieldName: string) {
const form = await this.getForm();
return form.isFieldValid(fieldName);
Expand All @@ -122,7 +161,7 @@ export class FormApi {
if (!validateResult.valid) {
return;
}
const rawValues = toRaw(form.values || {});
const rawValues = api.handleRangeTimeValue(toRaw(form.values || {}));
Dengjunle marked this conversation as resolved.
Show resolved Hide resolved
return rawValues;
}),
);
Expand Down Expand Up @@ -253,7 +292,7 @@ export class FormApi {
e?.stopPropagation();
const form = await this.getForm();
await form.submitForm();
const rawValues = toRaw(form.values || {});
const rawValues = this.handleRangeTimeValue(toRaw(form.values || {}));
await this.state?.handleSubmit?.(rawValues);

return rawValues;
Expand Down
Loading