@@ -108,6 +108,7 @@ export class FormStore {
108
108
registerField : this . registerField ,
109
109
useSubscribe : this . useSubscribe ,
110
110
setInitialValues : this . setInitialValues ,
111
+ destroyForm : this . destroyForm ,
111
112
setCallbacks : this . setCallbacks ,
112
113
setValidateMessages : this . setValidateMessages ,
113
114
getFields : this . getFields ,
@@ -124,18 +125,47 @@ export class FormStore {
124
125
this . subscribable = subscribable ;
125
126
} ;
126
127
128
+ /**
129
+ * Record prev Form unmount fieldEntities which config preserve false.
130
+ * This need to be refill with initialValues instead of store value.
131
+ */
132
+ private prevWithoutPreserves : NameMap < boolean > | null = null ;
133
+
127
134
/**
128
135
* First time `setInitialValues` should update store with initial value
129
136
*/
130
137
private setInitialValues = ( initialValues : Store , init : boolean ) => {
131
138
this . initialValues = initialValues || { } ;
132
139
if ( init ) {
133
- this . store = setValues ( { } , this . store , initialValues ) ;
140
+ let nextStore = setValues ( { } , initialValues , this . store ) ;
141
+
142
+ // We will take consider prev form unmount fields.
143
+ // When the field is not `preserve`, we need fill this with initialValues instead of store.
144
+ this . prevWithoutPreserves ?. map ( ( { key : namePath } ) => {
145
+ nextStore = setValue ( nextStore , namePath , getValue ( initialValues , namePath ) ) ;
146
+ } ) ;
147
+ this . prevWithoutPreserves = null ;
148
+
149
+ this . updateStore ( nextStore ) ;
134
150
}
135
151
} ;
136
152
153
+ private destroyForm = ( ) => {
154
+ const prevWithoutPreserves = new NameMap < boolean > ( ) ;
155
+ this . getFieldEntities ( true ) . forEach ( entity => {
156
+ if ( ! entity . isPreserve ( ) ) {
157
+ prevWithoutPreserves . set ( entity . getNamePath ( ) , true ) ;
158
+ }
159
+ } ) ;
160
+
161
+ this . prevWithoutPreserves = prevWithoutPreserves ;
162
+ } ;
163
+
137
164
private getInitialValue = ( namePath : InternalNamePath ) => {
138
- return cloneDeep ( getValue ( this . initialValues , namePath ) ) ;
165
+ const initValue = getValue ( this . initialValues , namePath ) ;
166
+
167
+ // Not cloneDeep when without `namePath`
168
+ return namePath . length ? cloneDeep ( initValue ) : initValue ;
139
169
} ;
140
170
141
171
private setCallbacks = ( callbacks : Callbacks ) => {
@@ -168,6 +198,11 @@ export class FormStore {
168
198
}
169
199
} ;
170
200
201
+ // ============================ Store =============================
202
+ private updateStore = ( nextStore : Store ) => {
203
+ this . store = nextStore ;
204
+ } ;
205
+
171
206
// ============================ Fields ============================
172
207
/**
173
208
* Get registered field entities.
@@ -428,7 +463,7 @@ export class FormStore {
428
463
const originValue = this . getFieldValue ( namePath ) ;
429
464
// Set `initialValue`
430
465
if ( ! info . skipExist || originValue === undefined ) {
431
- this . store = setValue ( this . store , namePath , [ ...records ] [ 0 ] . value ) ;
466
+ this . updateStore ( setValue ( this . store , namePath , [ ...records ] [ 0 ] . value ) ) ;
432
467
}
433
468
}
434
469
}
@@ -460,7 +495,7 @@ export class FormStore {
460
495
461
496
const prevStore = this . store ;
462
497
if ( ! nameList ) {
463
- this . store = setValues ( { } , this . initialValues ) ;
498
+ this . updateStore ( setValues ( { } , this . initialValues ) ) ;
464
499
this . resetWithFieldInitialValue ( ) ;
465
500
this . notifyObservers ( prevStore , null , { type : 'reset' } ) ;
466
501
return ;
@@ -470,7 +505,7 @@ export class FormStore {
470
505
const namePathList : InternalNamePath [ ] = nameList . map ( getNamePath ) ;
471
506
namePathList . forEach ( namePath => {
472
507
const initialValue = this . getInitialValue ( namePath ) ;
473
- this . store = setValue ( this . store , namePath , initialValue ) ;
508
+ this . updateStore ( setValue ( this . store , namePath , initialValue ) ) ;
474
509
} ) ;
475
510
this . resetWithFieldInitialValue ( { namePathList } ) ;
476
511
this . notifyObservers ( prevStore , namePathList , { type : 'reset' } ) ;
@@ -487,7 +522,7 @@ export class FormStore {
487
522
488
523
// Value
489
524
if ( 'value' in data ) {
490
- this . store = setValue ( this . store , namePath , data . value ) ;
525
+ this . updateStore ( setValue ( this . store , namePath , data . value ) ) ;
491
526
}
492
527
493
528
this . notifyObservers ( prevStore , [ namePath ] , {
@@ -531,7 +566,7 @@ export class FormStore {
531
566
const prevValue = getValue ( this . store , namePath ) ;
532
567
533
568
if ( prevValue === undefined ) {
534
- this . store = setValue ( this . store , namePath , initialValue ) ;
569
+ this . updateStore ( setValue ( this . store , namePath , initialValue ) ) ;
535
570
}
536
571
}
537
572
} ;
@@ -570,7 +605,7 @@ export class FormStore {
570
605
)
571
606
) {
572
607
const prevStore = this . store ;
573
- this . store = setValue ( prevStore , namePath , defaultValue , true ) ;
608
+ this . updateStore ( setValue ( prevStore , namePath , defaultValue , true ) ) ;
574
609
575
610
// Notify that field is unmount
576
611
this . notifyObservers ( prevStore , [ namePath ] , { type : 'remove' } ) ;
@@ -638,7 +673,7 @@ export class FormStore {
638
673
private updateValue = ( name : NamePath , value : StoreValue ) => {
639
674
const namePath = getNamePath ( name ) ;
640
675
const prevStore = this . store ;
641
- this . store = setValue ( this . store , namePath , value ) ;
676
+ this . updateStore ( setValue ( this . store , namePath , value ) ) ;
642
677
643
678
this . notifyObservers ( prevStore , [ namePath ] , {
644
679
type : 'valueUpdate' ,
@@ -666,7 +701,7 @@ export class FormStore {
666
701
const prevStore = this . store ;
667
702
668
703
if ( store ) {
669
- this . store = setValues ( this . store , store ) ;
704
+ this . updateStore ( setValues ( this . store , store ) ) ;
670
705
}
671
706
672
707
this . notifyObservers ( prevStore , null , {
0 commit comments