Skip to content

Commit

Permalink
fix(core): fix unexpect patch initialValues (#3147)
Browse files Browse the repository at this point in the history
* fix(core): fix unexpect patch initialValues

* test(core): add tests
  • Loading branch information
janryWang authored May 25, 2022
1 parent c8485c0 commit 9ade22c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
54 changes: 54 additions & 0 deletions packages/core/src/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,57 @@ test('array field remove can not memory leak', async () => {
expect(bb.visible).toBeTruthy()
expect(handler).toBeCalledTimes(1)
})

test('array field patch values', async () => {
const form = attach(createForm())

const arr = attach(
form.createArrayField({
name: 'a',
})
)

await arr.unshift({})
attach(
form.createObjectField({
name: '0',
basePath: 'a',
})
)
attach(
form.createField({
name: 'c',
initialValue: 'A',
basePath: 'a.0',
})
)
expect(form.values).toEqual({ a: [{ c: 'A' }] })
await arr.unshift({})
attach(
form.createObjectField({
name: '0',
basePath: 'a',
})
)
attach(
form.createField({
name: 'c',
initialValue: 'A',
basePath: 'a.0',
})
)
attach(
form.createObjectField({
name: '1',
basePath: 'a',
})
)
attach(
form.createField({
name: 'c',
initialValue: 'A',
basePath: 'a.1',
})
)
expect(form.values).toEqual({ a: [{ c: 'A' }, { c: 'A' }] })
})
5 changes: 2 additions & 3 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ export const triggerFormInitialValuesChange = (
if (Array.isArray(change.object) && change.key === 'length') return
if (
contains(form.initialValues, change.object) ||
contains(form.initialValues, change.value)
form.initialValues === change.value
) {
if (change.type === 'add' || change.type === 'set') {
patchFormValues(form, path.slice(1), change.value)
Expand All @@ -721,8 +721,7 @@ export const triggerFormInitialValuesChange = (
export const triggerFormValuesChange = (form: Form, change: DataChange) => {
if (Array.isArray(change.object) && change.key === 'length') return
if (
(contains(form.values, change.object) ||
contains(form.values, change.value)) &&
(contains(form.values, change.object) || form.values === change.value) &&
form.initialized
) {
form.notify(LifeCycleTypes.ON_FORM_VALUES_CHANGE)
Expand Down

0 comments on commit 9ade22c

Please sign in to comment.