Skip to content

Commit

Permalink
fix(core): fix initValues when values is empty Array or Object (#3583)
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyunwan committed Dec 1, 2022
1 parent 8602fe2 commit c538d0d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
68 changes: 68 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,74 @@ test('setValue/setInitialValue', () => {
expect(form.values.ddd).toEqual('222')
})

test('initialValue when value is empty array or object', () => {
const form = attach(
createForm({
values: {
aaa: [],
bbb: {},
ddd: {},
fff: {
ggg: '',
},
},
})
)
const aaa = attach(
form.createField({
name: 'aaa',
initialValue: ['aaa'],
})
)
const bbb = attach(
form.createField({
name: 'bbb',
initialValue: {
ccc: 'ccc',
},
})
)

const ddd = attach(
form.createObjectField({
name: 'ddd',
})
)

attach(
form.createField({
name: 'ddd.eee',
initialValue: 'eee',
})
)

const fff = attach(
form.createObjectField({
name: 'fff',
initialValue: {
ggg: 'ggg',
},
})
)

attach(
form.createField({
name: 'fff.hhh',
initialValue: 'hhh',
})
)

expect(aaa.value).toEqual([])
expect(bbb.value).toEqual({})
expect(ddd.value).toEqual({
eee: 'eee',
})
expect(fff.value).toEqual({
ggg: '',
hhh: 'hhh',
})
})

test('setLoading/setValidating', async () => {
const form = attach(createForm())
const field = attach(
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,8 @@ export const allowAssignDefaultValue = (target: any, source: any) => {
if (typeof target === typeof source) {
if (target === '') return false
if (target === 0) return false
if (Array.isArray(target)) return false

This comment has been minimized.

Copy link
@cn-xufei

cn-xufei Dec 19, 2022

This is a destructive change!
The default value of the array is no longer valid...

if (isPlainObj(target)) return false
}

const isEmptyTarget = target !== null && isEmpty(target)
Expand Down

0 comments on commit c538d0d

Please sign in to comment.