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 @@