diff --git a/packages/core/docs/api/models/Form.md b/packages/core/docs/api/models/Form.md index 5b89d80f8e6..ffe7e039acb 100644 --- a/packages/core/docs/api/models/Form.md +++ b/packages/core/docs/api/models/Form.md @@ -104,13 +104,13 @@ interface createVoidField { #### 描述 -设置表单值,可以设置合并策略 +设置表单值,可以设置合并策略 [IFormMergeStrategy](#IFormMergeStrategy) #### 签名 ```ts interface setValues { - (values: object, strategy: 'overwrite' | 'merge' = 'merge'): void + (values: object, strategy: IFormMergeStrategy = 'merge'): void } ``` @@ -124,7 +124,7 @@ interface setValues { ```ts interface setInitialValues { - (initialValues: object, strategy: 'overwrite' | 'merge' = 'merge'): void + (initialValues: object, strategy: IFormMergeStrategy = 'merge'): void } ``` @@ -762,6 +762,12 @@ interface IFormState { } ``` +### IFormMergeStrategy + +```ts +type IFormMergeStrategy = 'overwrite' | 'merge' | 'deepMerge' | 'shallowMerge' +``` + ### IFieldFactoryProps ```ts diff --git a/packages/core/src/models/Form.ts b/packages/core/src/models/Form.ts index 7140cca64fc..d09690f6e86 100644 --- a/packages/core/src/models/Form.ts +++ b/packages/core/src/models/Form.ts @@ -40,6 +40,7 @@ import { IFieldStateGetter, IFieldStateSetter, FormDisplayTypes, + IFormMergeStrategy, } from '../types' import { isVoidField, @@ -384,11 +385,13 @@ export class Form { /** 状态操作模型 **/ - setValues = (values: any, strategy: 'overwrite' | 'merge' = 'merge') => { + setValues = (values: any, strategy: IFormMergeStrategy = 'merge') => { if (!isPlainObj(values)) return untracked(() => { - if (strategy === 'merge') { + if (strategy === 'merge' || strategy === 'deepMerge') { this.values = defaults(this.values, values) + } else if (strategy === 'shallowMerge') { + this.values = Object.assign(this.values, values) } else { this.values = values as any } @@ -397,12 +400,14 @@ export class Form { setInitialValues = ( initialValues: any, - strategy: 'overwrite' | 'merge' = 'merge' + strategy: IFormMergeStrategy = 'merge' ) => { if (!isPlainObj(initialValues)) return untracked(() => { - if (strategy === 'merge') { + if (strategy === 'merge' || strategy === 'deepMerge') { this.initialValues = defaults(this.initialValues, initialValues) + } else if (strategy === 'shallowMerge') { + this.initialValues = Object.assign(this.initialValues, initialValues) } else { this.initialValues = initialValues as any } diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index ad99c8d2a03..5cbac5e04cd 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -231,6 +231,12 @@ export interface IFormProps { controlled?: boolean } +export type IFormMergeStrategy = + | 'overwrite' + | 'merge' + | 'deepMerge' + | 'shallowMerge' + export interface IFieldFactoryProps< Decorator extends JSXComponent, Component extends JSXComponent,