Skip to content

Commit

Permalink
fix: πŸ› patchFieldStates#destory>forceClear by initialValue
Browse files Browse the repository at this point in the history
βœ… Closes: alibaba#4024
  • Loading branch information
charlzyx committed Dec 28, 2023
1 parent ec5485f commit 3db61ad
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
58 changes: 58 additions & 0 deletions packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,64 @@ test('form lifecycle can be triggered after call form.setXXX', () => {
expect(valuesTriggerNum).toEqual(6)
})

test('reference initialValue should forceClear in destory', async () => {
const form = attach(createForm())
const arr1 = attach(
form.createArrayField({
name: 'aa',
initialValue: [{}],
})
)

const arr2 = attach(
form.createArrayField({
name: 'bb',
basePath: 'aa.0',
initialValue: [{}],
})
)

attach(
form.createField({
name: 'cc',
basePath: 'aa.0.bb.0',
initialValue: true,
})
)
// console.log('check init');
expect(form.initialValues).toEqual({
aa: [{ bb: [{ cc: true }] }],
})

attach(
form.createField({
name: 'cc',
basePath: 'aa.0.bb.1',
initialValue: true,
})
)
attach(
form.createField({
name: 'cc',
basePath: 'aa.0.bb.2',
initialValue: true,
})
)
// await arr2.push({...arr2.props.initialValue?.[0]})
// await arr2.push({...arr2.props.initialValue?.[0]})

expect(form.initialValues).toEqual({
aa: [{ bb: [{ cc: true }, { cc: true }, { cc: true }] }],
})

arr1.unshift()
arr1.push(arr2.props.initialValue)

expect(form.initialValues).toEqual({
aa: [{ bb: [{ cc: true }] }],
})
})

test('form values change with array field(default value)', async () => {
const handler = jest.fn()
const form = attach(
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export const patchFieldStates = (
) => {
patches.forEach(({ type, address, oldAddress, payload }) => {
if (type === 'remove') {
destroy(target, address, false)
const forceClear = isUndef(target?.initialValue)
destroy(target, address, forceClear)
} else if (type === 'update') {
if (payload) {
target[address] = payload
Expand Down

0 comments on commit 3db61ad

Please sign in to comment.