Skip to content

Commit

Permalink
fix(core): fix form unmount should not clear values (#2997)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Mar 30, 2022
1 parent b202c0d commit 18b91f8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
36 changes: 36 additions & 0 deletions packages/core/src/__tests__/form.spec.ts
Expand Up @@ -1561,3 +1561,39 @@ test('validator order with format', async () => {
['The field value is required'],
])
})

test('form unmount can not effect field values', () => {
const form = attach(
createForm({
values: {
aa: '123',
},
})
)
attach(
form.createField({
name: 'aa',
})
)
expect(form.values.aa).toEqual('123')
form.onUnmount()
expect(form.values.aa).toEqual('123')
})

test('form clearFormGraph need clear field values', () => {
const form = attach(
createForm({
values: {
aa: '123',
},
})
)
attach(
form.createField({
name: 'aa',
})
)
expect(form.values.aa).toEqual('123')
form.clearFormGraph('*')
expect(form.values.aa).toBeUndefined()
})
4 changes: 2 additions & 2 deletions packages/core/src/models/BaseField.ts
Expand Up @@ -291,8 +291,8 @@ export class BaseField<Decorator = any, Component = any, TextType = any> {
this.form.removeEffects(this)
}

destroy = () => {
destroy(this.form.fields, this.address.toString())
destroy = (forceClear = true) => {
destroy(this.form.fields, this.address.toString(), forceClear)
}

match = (pattern: FormPathPattern) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/models/Form.ts
Expand Up @@ -563,7 +563,7 @@ export class Form<ValueType extends object = any> {

onUnmount = () => {
this.notify(LifeCycleTypes.ON_FORM_UNMOUNT)
this.query('*').forEach((field) => field.destroy())
this.query('*').forEach((field) => field.destroy(false))
this.disposers.forEach((dispose) => dispose())
this.unmounted = true
this.indexes = {}
Expand Down

0 comments on commit 18b91f8

Please sign in to comment.