Skip to content

Commit

Permalink
fix(shared): fix defaults merge with null will get unexpect results #…
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jun 26, 2021
1 parent d14c4eb commit d39c426
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
28 changes: 28 additions & 0 deletions packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ test('setValues/setInitialValues', () => {
expect(form.values.aa).toBeUndefined()
})

test('setValues with null', () => {
const form = attach(createForm())
form.setInitialValues({
'object-1': {
'array-1': null,
},
'object-2': {
'array-2': null,
},
})
form.setValues({
'object-1': {
'array-1': null,
},
'object-2': {
'array-2': null,
},
})
expect(form.values).toEqual({
'object-1': {
'array-1': null,
},
'object-2': {
'array-2': null,
},
})
})

test('observable values/initialValues', () => {
const values: any = observable({
aa: 123,
Expand Down
8 changes: 6 additions & 2 deletions packages/shared/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { each } from './array'
import { isEmpty, isValid } from './isEmpty'
import { getType, isArr } from './checkers'
import { getType, isArr, isPlainObj } from './checkers'

const isUnNormalObject = (value: any) => {
if (value?._owner && value?.$$typeof) {
Expand Down Expand Up @@ -34,7 +34,11 @@ export const defaults = (defaults_: any, targets: any) => {
) {
return !isEmpty(targets) ? targets : defaults_
} else {
const results = isArr(defaults_) ? [] : {}
const results = isArr(defaults_)
? []
: isPlainObj(defaults_)
? {}
: defaults_
each(targets, (value, key) => {
results[key] = defaults(defaults_[key], value)
})
Expand Down

0 comments on commit d39c426

Please sign in to comment.