From d59428d2d63cd8131f169f1c2b002fd5f4934636 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Thu, 16 Oct 2025 21:03:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor(form):=20=E4=BF=9D=E6=8C=81=E5=8D=95?= =?UTF-8?q?=E5=90=91=E6=95=B0=E6=8D=AE=E6=B5=81,=E8=A1=A8=E5=8D=95?= =?UTF-8?q?=E5=86=85=E9=83=A8=E7=9A=84=E7=BB=84=E4=BB=B6=E4=B8=8D=E5=8E=BB?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A1=A8=E5=8D=95=E7=9A=84=E5=80=BC,?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E9=80=9A=E8=BF=87chang=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E8=A1=A8=E5=8D=95=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/form-schema/src/base.ts | 6 +- packages/form/src/Form.vue | 15 +- packages/form/src/containers/Container.vue | 192 ++++++++------------- packages/form/src/containers/Fieldset.vue | 2 +- packages/form/src/fields/Cascader.vue | 41 +++-- packages/form/src/fields/Checkbox.vue | 4 +- packages/form/src/fields/CheckboxGroup.vue | 2 +- packages/form/src/fields/ColorPicker.vue | 4 +- packages/form/src/fields/Date.vue | 4 +- packages/form/src/fields/DateTime.vue | 4 +- packages/form/src/fields/Daterange.vue | 4 +- packages/form/src/fields/Number.vue | 4 +- packages/form/src/fields/NumberRange.vue | 12 +- packages/form/src/fields/RadioGroup.vue | 20 +-- packages/form/src/fields/Select.vue | 4 +- packages/form/src/fields/Switch.vue | 4 +- packages/form/src/fields/Text.vue | 4 +- packages/form/src/fields/Textarea.vue | 4 +- packages/form/src/fields/Time.vue | 4 +- packages/form/src/fields/Timerange.vue | 4 +- packages/form/src/theme/container.scss | 17 ++ packages/form/tests/unit/Form.spec.ts | 190 +++++++++++++++++++- 22 files changed, 360 insertions(+), 185 deletions(-) diff --git a/packages/form-schema/src/base.ts b/packages/form-schema/src/base.ts index e2326be4..57a8ea43 100644 --- a/packages/form-schema/src/base.ts +++ b/packages/form-schema/src/base.ts @@ -15,13 +15,14 @@ export interface ChangeRecord { export interface OnChangeHandlerData { model: FormValue; - values?: FormValue; + values?: Readonly; parent?: FormValue; formValue?: FormValue; - config: any; + config: Readonly; prop: string; changeRecords: ChangeRecord[]; setModel: (prop: string, value: any) => void; + setFromValue: (prop: string, value: any) => void; } export type FormValue = Record; @@ -385,6 +386,7 @@ export interface NumberConfig extends FormItem { */ export interface NumberRangeConfig extends FormItem { type?: 'number-range'; + clearable?: boolean; } /** diff --git a/packages/form/src/Form.vue b/packages/form/src/Form.vue index 34c20ab7..ee3e7431 100644 --- a/packages/form/src/Form.vue +++ b/packages/form/src/Form.vue @@ -32,6 +32,7 @@ import { provide, reactive, ref, shallowRef, toRaw, watch, watchEffect } from 'v import { cloneDeep, isEqual } from 'lodash-es'; import { TMagicForm, tMagicMessage, tMagicMessageBox } from '@tmagic/design'; +import { setValueByKeyPath } from '@tmagic/utils'; import Container from './containers/Container.vue'; import { getConfig } from './utils/config'; @@ -173,7 +174,18 @@ watch( const changeHandler = (v: FormValue, eventData: ContainerChangeEventData) => { if (eventData.changeRecords?.length) { - changeRecords.value.push(...eventData.changeRecords); + for (const record of eventData.changeRecords) { + if (record.propPath) { + const index = changeRecords.value.findIndex((item) => item.propPath === record.propPath); + if (index > -1) { + changeRecords.value[index] = record; + } else { + changeRecords.value.push(record); + } + + setValueByKeyPath(record.propPath, record.value, values.value); + } + } } emit('change', values.value, eventData); }; @@ -201,6 +213,7 @@ defineExpose({ submitForm: async (native?: boolean): Promise => { try { await tMagicForm.value?.validate(); + changeRecords.value = []; return native ? values.value : cloneDeep(toRaw(values.value)); } catch (invalidFields: any) { emit('error', invalidFields); diff --git a/packages/form/src/containers/Container.vue b/packages/form/src/containers/Container.vue index 55a1e21c..744db643 100644 --- a/packages/form/src/containers/Container.vue +++ b/packages/form/src/containers/Container.vue @@ -1,32 +1,18 @@