Skip to content

Commit

Permalink
fix(core): fix setValues/setInitialValues array merge strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jul 30, 2021
1 parent 1e2df7c commit 0773b06
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ test('setValues deep merge', () => {
aa: {
bb: 123,
cc: 321,
dd: [11, 22, 33],
},
},
})
Expand All @@ -1131,18 +1132,21 @@ test('setValues deep merge', () => {
aa: {
bb: 123,
cc: 321,
dd: [11, 22, 33],
},
})
form.setValues({
aa: {
bb: '',
cc: '',
dd: [44, 55, 66],
},
})
expect(form.values).toEqual({
aa: {
bb: '',
cc: '',
dd: [44, 55, 66],
},
})
})
8 changes: 6 additions & 2 deletions packages/core/src/models/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,9 @@ export class Form<ValueType extends object = any> {
if (!isPlainObj(values)) return
untracked(() => {
if (strategy === 'merge' || strategy === 'deepMerge') {
this.values = merge(this.values, values)
this.values = merge(this.values, values, {
arrayMerge: (target, source) => source,
})
} else if (strategy === 'shallowMerge') {
this.values = Object.assign(this.values, values)
} else {
Expand All @@ -415,7 +417,9 @@ export class Form<ValueType extends object = any> {
if (!isPlainObj(initialValues)) return
untracked(() => {
if (strategy === 'merge' || strategy === 'deepMerge') {
this.initialValues = merge(this.initialValues, initialValues)
this.initialValues = merge(this.initialValues, initialValues, {
arrayMerge: (target, source) => source,
})
} else if (strategy === 'shallowMerge') {
this.initialValues = Object.assign(this.initialValues, initialValues)
} else {
Expand Down

0 comments on commit 0773b06

Please sign in to comment.