Skip to content

Commit 18b91f8

Browse files
authored
fix(core): fix form unmount should not clear values (#2997)
1 parent b202c0d commit 18b91f8

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

packages/core/src/__tests__/form.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,3 +1561,39 @@ test('validator order with format', async () => {
15611561
['The field value is required'],
15621562
])
15631563
})
1564+
1565+
test('form unmount can not effect field values', () => {
1566+
const form = attach(
1567+
createForm({
1568+
values: {
1569+
aa: '123',
1570+
},
1571+
})
1572+
)
1573+
attach(
1574+
form.createField({
1575+
name: 'aa',
1576+
})
1577+
)
1578+
expect(form.values.aa).toEqual('123')
1579+
form.onUnmount()
1580+
expect(form.values.aa).toEqual('123')
1581+
})
1582+
1583+
test('form clearFormGraph need clear field values', () => {
1584+
const form = attach(
1585+
createForm({
1586+
values: {
1587+
aa: '123',
1588+
},
1589+
})
1590+
)
1591+
attach(
1592+
form.createField({
1593+
name: 'aa',
1594+
})
1595+
)
1596+
expect(form.values.aa).toEqual('123')
1597+
form.clearFormGraph('*')
1598+
expect(form.values.aa).toBeUndefined()
1599+
})

packages/core/src/models/BaseField.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ export class BaseField<Decorator = any, Component = any, TextType = any> {
291291
this.form.removeEffects(this)
292292
}
293293

294-
destroy = () => {
295-
destroy(this.form.fields, this.address.toString())
294+
destroy = (forceClear = true) => {
295+
destroy(this.form.fields, this.address.toString(), forceClear)
296296
}
297297

298298
match = (pattern: FormPathPattern) => {

packages/core/src/models/Form.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ export class Form<ValueType extends object = any> {
563563

564564
onUnmount = () => {
565565
this.notify(LifeCycleTypes.ON_FORM_UNMOUNT)
566-
this.query('*').forEach((field) => field.destroy())
566+
this.query('*').forEach((field) => field.destroy(false))
567567
this.disposers.forEach((dispose) => dispose())
568568
this.unmounted = true
569569
this.indexes = {}

0 commit comments

Comments
 (0)