Skip to content

Commit d39c426

Browse files
committed
fix(shared): fix defaults merge with null will get unexpect results #1644
1 parent d14c4eb commit d39c426

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/core/src/__tests__/form.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,34 @@ test('setValues/setInitialValues', () => {
122122
expect(form.values.aa).toBeUndefined()
123123
})
124124

125+
test('setValues with null', () => {
126+
const form = attach(createForm())
127+
form.setInitialValues({
128+
'object-1': {
129+
'array-1': null,
130+
},
131+
'object-2': {
132+
'array-2': null,
133+
},
134+
})
135+
form.setValues({
136+
'object-1': {
137+
'array-1': null,
138+
},
139+
'object-2': {
140+
'array-2': null,
141+
},
142+
})
143+
expect(form.values).toEqual({
144+
'object-1': {
145+
'array-1': null,
146+
},
147+
'object-2': {
148+
'array-2': null,
149+
},
150+
})
151+
})
152+
125153
test('observable values/initialValues', () => {
126154
const values: any = observable({
127155
aa: 123,

packages/shared/src/defaults.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { each } from './array'
22
import { isEmpty, isValid } from './isEmpty'
3-
import { getType, isArr } from './checkers'
3+
import { getType, isArr, isPlainObj } from './checkers'
44

55
const isUnNormalObject = (value: any) => {
66
if (value?._owner && value?.$$typeof) {
@@ -34,7 +34,11 @@ export const defaults = (defaults_: any, targets: any) => {
3434
) {
3535
return !isEmpty(targets) ? targets : defaults_
3636
} else {
37-
const results = isArr(defaults_) ? [] : {}
37+
const results = isArr(defaults_)
38+
? []
39+
: isPlainObj(defaults_)
40+
? {}
41+
: defaults_
3842
each(targets, (value, key) => {
3943
results[key] = defaults(defaults_[key], value)
4044
})

0 commit comments

Comments
 (0)