Skip to content

Commit

Permalink
fix(core): fix array remove can not auto clean initialValue (#2638)
Browse files Browse the repository at this point in the history
* test(core): add some tests

* fix(core): fix array remove can not auto clean initialValue
  • Loading branch information
janryWang authored Dec 17, 2021
1 parent fe10bfd commit 2e6db17
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/src/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ test('nest array remove', async () => {
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined()
await metrics.remove(1)
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined()
expect(form.initialValues.metrics?.[1]?.content?.[0]?.attr).toBeUndefined()
})

test('incomplete insertion of array elements', async () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/models/BaseField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
FieldDecorator,
FieldComponent,
} from '../types'
import { buildNodeIndexes, initFieldUpdate } from '../shared/internals'
import { buildNodeIndexes, destroy, initFieldUpdate } from '../shared/internals'
import { Form } from './Form'
import { Query } from './Query'

Expand Down Expand Up @@ -282,8 +282,7 @@ export class BaseField<Decorator = any, Component = any, TextType = any> {
}

destroy = () => {
this.dispose()
delete this.form.fields[this.address.toString()]
destroy(this.form.fields, this.address.toString())
}

match = (pattern: FormPathPattern) => {
Expand Down
17 changes: 14 additions & 3 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ export const patchFieldStates = (
) => {
patches.forEach(({ type, address, oldAddress, payload }) => {
if (type === 'remove') {
target[address]?.dispose()
delete target[address]
destroy(target, address)
} else if (type === 'update') {
if (payload) {
target[address] = payload
Expand All @@ -164,6 +163,14 @@ export const patchFieldStates = (
})
}

export const destroy = (
target: Record<string, GeneralField>,
address: string
) => {
target[address]?.dispose()
delete target[address]
}

export const patchFormValues = (
form: Form,
path: Array<string | number>,
Expand Down Expand Up @@ -333,7 +340,8 @@ export const spliceArrayState = (
}
const address = field.address.toString()
const addrLength = address.length
const fields = field.form.fields
const form = field.form
const fields = form.fields
const fieldPatches: INodePatch<GeneralField>[] = []
const offset = insertCount - deleteCount
const isArrayChildren = (identifier: string) => {
Expand Down Expand Up @@ -389,6 +397,9 @@ export const spliceArrayState = (
})
}
if (isInsertNode(identifier) || isDeleteNode(identifier)) {
if (isDataField(field)) {
form.deleteInitialValuesIn(field.path)
}
fieldPatches.push({ type: 'remove', address: identifier })
}
}
Expand Down

0 comments on commit 2e6db17

Please sign in to comment.