Skip to content

Commit

Permalink
feat(core): add extra strategy for merge form value (#1448)
Browse files Browse the repository at this point in the history
* feat(core):  add extra strategy for merge form value

* docs(core): update form api docs
  • Loading branch information
liuweiGL authored May 18, 2021
1 parent c45c751 commit 0b5606d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 9 additions & 3 deletions packages/core/docs/api/models/Form.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ interface createVoidField {

#### 描述

设置表单值,可以设置合并策略
设置表单值,可以设置合并策略 [IFormMergeStrategy](#IFormMergeStrategy)

#### 签名

```ts
interface setValues {
(values: object, strategy: 'overwrite' | 'merge' = 'merge'): void
(values: object, strategy: IFormMergeStrategy = 'merge'): void
}
```

Expand All @@ -124,7 +124,7 @@ interface setValues {

```ts
interface setInitialValues {
(initialValues: object, strategy: 'overwrite' | 'merge' = 'merge'): void
(initialValues: object, strategy: IFormMergeStrategy = 'merge'): void
}
```

Expand Down Expand Up @@ -762,6 +762,12 @@ interface IFormState {
}
```

### IFormMergeStrategy

```ts
type IFormMergeStrategy = 'overwrite' | 'merge' | 'deepMerge' | 'shallowMerge'
```
### IFieldFactoryProps
```ts
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/models/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
IFieldStateGetter,
IFieldStateSetter,
FormDisplayTypes,
IFormMergeStrategy,
} from '../types'
import {
isVoidField,
Expand Down Expand Up @@ -384,11 +385,13 @@ export class Form<ValueType extends object = any> {

/** 状态操作模型 **/

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
}
Expand All @@ -397,12 +400,14 @@ export class Form<ValueType extends object = any> {

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
}
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ export interface IFormProps<T extends object = any> {
controlled?: boolean
}

export type IFormMergeStrategy =
| 'overwrite'
| 'merge'
| 'deepMerge'
| 'shallowMerge'

export interface IFieldFactoryProps<
Decorator extends JSXComponent,
Component extends JSXComponent,
Expand Down

0 comments on commit 0b5606d

Please sign in to comment.