Skip to content

Commit 2828e7a

Browse files
authored
fix: form fieldMappingTime is not working (#5333)
* fix: form option `fieldMappingTime` is not working * fix: form merge support `fieldMappingTime`
1 parent 16162c0 commit 2828e7a

File tree

7 files changed

+72
-68
lines changed

7 files changed

+72
-68
lines changed

packages/@core/ui-kit/form-ui/src/components/form-actions.vue

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import { computed, toRaw, unref, watch } from 'vue';
33
44
import { useSimpleLocale } from '@vben-core/composables';
55
import { VbenExpandableArrow } from '@vben-core/shadcn-ui';
6-
import {
7-
cn,
8-
formatDate,
9-
isFunction,
10-
triggerWindowResize,
11-
} from '@vben-core/shared/utils';
6+
import { cn, isFunction, triggerWindowResize } from '@vben-core/shared/utils';
127
138
import { COMPONENT_MAP } from '../config';
149
import { injectFormProps } from '../use-form-context';
@@ -58,7 +53,7 @@ async function handleSubmit(e: Event) {
5853
return;
5954
}
6055
61-
const values = handleRangeTimeValue(toRaw(form.values));
56+
const values = toRaw(await unref(rootProps).formApi?.getValues());
6257
await unref(rootProps).handleSubmit?.(values);
6358
}
6459
@@ -67,13 +62,7 @@ async function handleReset(e: Event) {
6762
e?.stopPropagation();
6863
const props = unref(rootProps);
6964
70-
const values = toRaw(form.values);
71-
// 清理时间字段
72-
props.fieldMappingTime &&
73-
props.fieldMappingTime.forEach(([_, [startTimeKey, endTimeKey]]) => {
74-
delete values[startTimeKey];
75-
delete values[endTimeKey];
76-
});
65+
const values = toRaw(props.formApi?.getValues());
7766
7867
if (isFunction(props.handleReset)) {
7968
await props.handleReset?.(values);
@@ -82,44 +71,6 @@ async function handleReset(e: Event) {
8271
}
8372
}
8473
85-
function handleRangeTimeValue(values: Record<string, any>) {
86-
const fieldMappingTime = unref(rootProps).fieldMappingTime;
87-
88-
if (!fieldMappingTime || !Array.isArray(fieldMappingTime)) {
89-
return values;
90-
}
91-
92-
fieldMappingTime.forEach(
93-
([field, [startTimeKey, endTimeKey], format = 'YYYY-MM-DD']) => {
94-
if (startTimeKey && endTimeKey && values[field] === null) {
95-
delete values[startTimeKey];
96-
delete values[endTimeKey];
97-
}
98-
99-
if (!values[field]) {
100-
delete values[field];
101-
return;
102-
}
103-
104-
const [startTime, endTime] = values[field];
105-
const [startTimeFormat, endTimeFormat] = Array.isArray(format)
106-
? format
107-
: [format, format];
108-
109-
values[startTimeKey] = startTime
110-
? formatDate(startTime, startTimeFormat)
111-
: undefined;
112-
values[endTimeKey] = endTime
113-
? formatDate(endTime, endTimeFormat)
114-
: undefined;
115-
116-
delete values[field];
117-
},
118-
);
119-
120-
return values;
121-
}
122-
12374
watch(
12475
() => collapsed.value,
12576
() => {

packages/@core/ui-kit/form-ui/src/form-api.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Store } from '@vben-core/shared/store';
1515
import {
1616
bindMethods,
1717
createMerge,
18+
formatDate,
1819
isDate,
1920
isDayjsObject,
2021
isFunction,
@@ -94,7 +95,7 @@ export class FormApi {
9495

9596
async getValues() {
9697
const form = await this.getForm();
97-
return form.values;
98+
return this.handleRangeTimeValue(form.values);
9899
}
99100

100101
async isFieldValid(fieldName: string) {
@@ -117,12 +118,11 @@ export class FormApi {
117118
try {
118119
const results = await Promise.all(
119120
chain.map(async (api) => {
120-
const form = await api.getForm();
121121
const validateResult = await api.validate();
122122
if (!validateResult.valid) {
123123
return;
124124
}
125-
const rawValues = toRaw(form.values || {});
125+
const rawValues = toRaw((await api.getValues()) || {});
126126
return rawValues;
127127
}),
128128
);
@@ -147,7 +147,9 @@ export class FormApi {
147147
if (!this.isMounted) {
148148
Object.assign(this.form, formActions);
149149
this.stateHandler.setConditionTrue();
150-
this.setLatestSubmissionValues({ ...toRaw(this.form.values) });
150+
this.setLatestSubmissionValues({
151+
...toRaw(this.handleRangeTimeValue(this.form.values)),
152+
});
151153
this.isMounted = true;
152154
}
153155
}
@@ -253,7 +255,7 @@ export class FormApi {
253255
e?.stopPropagation();
254256
const form = await this.getForm();
255257
await form.submitForm();
256-
const rawValues = toRaw(form.values || {});
258+
const rawValues = toRaw(await this.getValues());
257259
await this.state?.handleSubmit?.(rawValues);
258260

259261
return rawValues;
@@ -342,6 +344,48 @@ export class FormApi {
342344
return this.form;
343345
}
344346

347+
private handleRangeTimeValue = (originValues: Record<string, any>) => {
348+
const values = { ...originValues };
349+
const fieldMappingTime = this.state?.fieldMappingTime;
350+
351+
if (!fieldMappingTime || !Array.isArray(fieldMappingTime)) {
352+
return values;
353+
}
354+
355+
fieldMappingTime.forEach(
356+
([field, [startTimeKey, endTimeKey], format = 'YYYY-MM-DD']) => {
357+
if (startTimeKey && endTimeKey && values[field] === null) {
358+
Reflect.deleteProperty(values, startTimeKey);
359+
Reflect.deleteProperty(values, endTimeKey);
360+
// delete values[startTimeKey];
361+
// delete values[endTimeKey];
362+
}
363+
364+
if (!values[field]) {
365+
Reflect.deleteProperty(values, field);
366+
// delete values[field];
367+
return;
368+
}
369+
370+
const [startTime, endTime] = values[field];
371+
const [startTimeFormat, endTimeFormat] = Array.isArray(format)
372+
? format
373+
: [format, format];
374+
375+
values[startTimeKey] = startTime
376+
? formatDate(startTime, startTimeFormat)
377+
: undefined;
378+
values[endTimeKey] = endTime
379+
? formatDate(endTime, endTimeFormat)
380+
: undefined;
381+
382+
// delete values[field];
383+
Reflect.deleteProperty(values, field);
384+
},
385+
);
386+
return values;
387+
};
388+
345389
private updateState() {
346390
const currentSchema = this.state?.schema ?? [];
347391
const prevSchema = this.prevState?.schema ?? [];

packages/@core/ui-kit/form-ui/src/use-form-context.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ZodRawShape } from 'zod';
22

33
import type { ComputedRef } from 'vue';
44

5-
import type { FormActions, VbenFormProps } from './types';
5+
import type { ExtendedFormApi, FormActions, VbenFormProps } from './types';
66

77
import { computed, unref, useSlots } from 'vue';
88

@@ -13,8 +13,10 @@ import { useForm } from 'vee-validate';
1313
import { object } from 'zod';
1414
import { getDefaultsForSchema } from 'zod-defaults';
1515

16+
type ExtendFormProps = VbenFormProps & { formApi: ExtendedFormApi };
17+
1618
export const [injectFormProps, provideFormProps] =
17-
createContext<[ComputedRef<VbenFormProps> | VbenFormProps, FormActions]>(
19+
createContext<[ComputedRef<ExtendFormProps> | ExtendFormProps, FormActions]>(
1820
'VbenFormProps',
1921
);
2022

packages/@core/ui-kit/form-ui/src/vben-use-form.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ function handleKeyDownEnter(event: KeyboardEvent) {
5353
forward.value.formApi.validateAndSubmitForm();
5454
}
5555
56-
const handleValuesChangeDebounced = useDebounceFn((newVal) => {
57-
forward.value.handleValuesChange?.(cloneDeep(newVal));
56+
const handleValuesChangeDebounced = useDebounceFn(async () => {
57+
forward.value.handleValuesChange?.(
58+
cloneDeep(await forward.value.formApi.getValues()),
59+
);
5860
state.value.submitOnChange && forward.value.formApi?.validateAndSubmitForm();
5961
}, 300);
6062

packages/effects/plugins/src/echarts/use-echarts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function useEcharts(chartRef: Ref<EchartsUIType>) {
109109
return {
110110
renderEcharts,
111111
resize,
112-
chartInstance
112+
chartInstance,
113113
};
114114
}
115115

packages/effects/plugins/src/vxe-table/use-vxe-grid.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type {
88
VxeToolbarPropTypes,
99
} from 'vxe-table';
1010
11+
import type { SetupContext } from 'vue';
12+
1113
import type { VbenFormProps } from '@vben-core/form-ui';
1214
1315
import type { ExtendedVxeGridApi, VxeGridProps } from './types';
@@ -68,18 +70,18 @@ const {
6870
6971
const { isMobile } = usePreferences();
7072
71-
const slots = useSlots();
73+
const slots: SetupContext['slots'] = useSlots();
7274
7375
const [Form, formApi] = useTableForm({
7476
compact: true,
7577
handleSubmit: async () => {
76-
const formValues = formApi.form.values;
78+
const formValues = await formApi.getValues();
7779
formApi.setLatestSubmissionValues(toRaw(formValues));
7880
props.api.reload(formValues);
7981
},
8082
handleReset: async () => {
8183
await formApi.resetForm();
82-
const formValues = formApi.form.values;
84+
const formValues = await formApi.getValues();
8385
formApi.setLatestSubmissionValues(formValues);
8486
props.api.reload(formValues);
8587
},
@@ -246,7 +248,7 @@ async function init() {
246248
const autoLoad = defaultGridOptions.proxyConfig?.autoLoad;
247249
const enableProxyConfig = options.value.proxyConfig?.enabled;
248250
if (enableProxyConfig && autoLoad) {
249-
props.api.grid.commitProxy?.('_init', formApi.form?.values ?? {});
251+
props.api.grid.commitProxy?.('_init', (await formApi.getValues()) ?? {});
250252
// props.api.reload(formApi.form?.values ?? {});
251253
}
252254

playground/src/views/examples/vxe-table/form.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { VxeTableGridOptions } from '#/adapter/vxe-table';
55
import { Page } from '@vben/common-ui';
66
77
import { message } from 'ant-design-vue';
8+
import dayjs from 'dayjs';
89
910
import { useVbenVxeGrid } from '#/adapter/vxe-table';
1011
import { getExampleTableApi } from '#/api';
@@ -21,6 +22,7 @@ interface RowType {
2122
const formOptions: VbenFormProps = {
2223
// 默认展开
2324
collapsed: false,
25+
fieldMappingTime: [['date', ['start', 'end']]],
2426
schema: [
2527
{
2628
component: 'Input',
@@ -58,8 +60,9 @@ const formOptions: VbenFormProps = {
5860
label: 'Color',
5961
},
6062
{
61-
component: 'DatePicker',
62-
fieldName: 'datePicker',
63+
component: 'RangePicker',
64+
defaultValue: [dayjs().subtract(-7, 'days'), dayjs()],
65+
fieldName: 'date',
6366
label: 'Date',
6467
},
6568
],

0 commit comments

Comments
 (0)