@@ -96,6 +96,12 @@ export function createForm<FieldProps, VirtualFieldProps>(
96
96
state . initialValue = initialValue
97
97
if ( ! isValid ( state . value ) ) {
98
98
state . value = initialValue
99
+ } else if (
100
+ / a r r a y / gi. test ( state . dataType ) &&
101
+ state . value &&
102
+ state . value . length === 0
103
+ ) {
104
+ state . value = initialValue
99
105
}
100
106
}
101
107
}
@@ -112,7 +118,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
112
118
const updateFields = ( field : IField | IVirtualField ) => {
113
119
if ( isField ( field ) ) {
114
120
field . setState ( state => {
115
- if ( state . visible ) {
121
+ if ( state . visible || state . unmounted ) {
116
122
if ( valuesChanged ) {
117
123
syncFieldValues ( state )
118
124
}
@@ -122,14 +128,14 @@ export function createForm<FieldProps, VirtualFieldProps>(
122
128
} else {
123
129
//缓存变化,等字段重新显示的时候再执行
124
130
if ( valuesChanged ) {
125
- env . visiblePendingFields [ state . name ] =
126
- env . visiblePendingFields [ state . name ] || { }
127
- env . visiblePendingFields [ state . name ] . values = true
131
+ env . hiddenPendingFields [ state . name ] =
132
+ env . hiddenPendingFields [ state . name ] || { }
133
+ env . hiddenPendingFields [ state . name ] . values = true
128
134
}
129
135
if ( initialValuesChanged ) {
130
- env . visiblePendingFields [ state . name ] =
131
- env . visiblePendingFields [ state . name ] || { }
132
- env . visiblePendingFields [ state . name ] . initialValues = true
136
+ env . hiddenPendingFields [ state . name ] =
137
+ env . hiddenPendingFields [ state . name ] || { }
138
+ env . hiddenPendingFields [ state . name ] . initialValues = true
133
139
}
134
140
}
135
141
} )
@@ -227,6 +233,28 @@ export function createForm<FieldProps, VirtualFieldProps>(
227
233
const errorsChanged = field . isDirty ( 'errors' )
228
234
const userUpdateFieldPath =
229
235
env . userUpdateFields [ env . userUpdateFields . length - 1 ]
236
+
237
+ const syncField = ( ) => {
238
+ if ( env . hiddenPendingFields [ published . name ] ) {
239
+ field . setState ( ( state : IFieldState ) => {
240
+ if ( env . hiddenPendingFields [ state . name ] . values ) {
241
+ syncFieldValues ( state )
242
+ }
243
+ if ( env . hiddenPendingFields [ state . name ] . initialValues ) {
244
+ syncFieldIntialValues ( state )
245
+ }
246
+ delete env . hiddenPendingFields [ state . name ]
247
+ } )
248
+ }
249
+ }
250
+
251
+ const notifyFormValuesChange = ( ) => {
252
+ if ( isFn ( options . onChange ) ) {
253
+ options . onChange ( state . getSourceState ( state => clone ( state . values ) ) )
254
+ }
255
+ heart . publish ( LifeCycleTypes . ON_FORM_VALUES_CHANGE , state )
256
+ }
257
+
230
258
if ( initializedChanged ) {
231
259
heart . publish ( LifeCycleTypes . ON_FIELD_INIT , field )
232
260
const isEmptyValue = ! isValid ( published . value )
@@ -239,34 +267,34 @@ export function createForm<FieldProps, VirtualFieldProps>(
239
267
} )
240
268
}
241
269
}
270
+ const wasHidden =
271
+ published . visible == false || published . unmounted === true
242
272
if ( valueChanged ) {
243
- userUpdating ( field , ( ) => {
244
- setFormValuesIn ( path , published . value )
245
- } )
273
+ if ( ! wasHidden ) {
274
+ userUpdating ( field , ( ) => {
275
+ setFormValuesIn ( path , published . value )
276
+ } )
277
+ }
246
278
heart . publish ( LifeCycleTypes . ON_FIELD_VALUE_CHANGE , field )
247
279
}
248
280
if ( initialValueChanged ) {
249
- setFormInitialValuesIn ( path , published . initialValue )
281
+ if ( ! wasHidden ) {
282
+ setFormInitialValuesIn ( path , published . initialValue )
283
+ }
250
284
heart . publish ( LifeCycleTypes . ON_FIELD_INITIAL_VALUE_CHANGE , field )
251
285
}
252
286
if ( displayChanged || visibleChanged ) {
253
287
if ( visibleChanged ) {
254
- if ( ! published . visible ) {
255
- deleteFormValuesIn ( path , true )
256
- } else {
257
- setFormValuesIn ( path , published . value )
258
- if ( env . visiblePendingFields [ published . name ] ) {
259
- field . setState ( ( state : IFieldState ) => {
260
- if ( env . visiblePendingFields [ state . name ] . values ) {
261
- syncFieldValues ( state )
262
- }
263
- if ( env . visiblePendingFields [ state . name ] . initialValues ) {
264
- syncFieldIntialValues ( state )
265
- }
266
- delete env . visiblePendingFields [ state . name ]
267
- } )
288
+ userUpdating ( field , ( ) => {
289
+ if ( ! published . visible ) {
290
+ deleteFormValuesIn ( path , true )
291
+ //考虑到隐藏删值,不应该同步子树,但是需要触发表单变化事件
292
+ notifyFormValuesChange ( )
293
+ } else {
294
+ setFormValuesIn ( path , published . value )
295
+ syncField ( )
268
296
}
269
- }
297
+ } )
270
298
}
271
299
graph . eachChildren ( path , childState => {
272
300
childState . setState ( ( state : IFieldState < FieldProps > ) => {
@@ -286,8 +314,11 @@ export function createForm<FieldProps, VirtualFieldProps>(
286
314
userUpdating ( field , ( ) => {
287
315
if ( published . unmounted ) {
288
316
deleteFormValuesIn ( path , true )
317
+ //考虑到隐藏删值,不应该同步子树,但是需要触发表单变化事件
318
+ notifyFormValuesChange ( )
289
319
} else {
290
320
setFormValuesIn ( path , published . value )
321
+ syncField ( )
291
322
}
292
323
} )
293
324
heart . publish ( LifeCycleTypes . ON_FIELD_UNMOUNT , field )
@@ -416,6 +447,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
416
447
visible,
417
448
display,
418
449
computeState,
450
+ dataType,
419
451
useDirty,
420
452
props
421
453
} : Exclude < IFieldStateProps , 'dataPath' | 'nodePath' > ) : IField {
@@ -430,6 +462,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
430
462
nodePath,
431
463
dataPath,
432
464
computeState,
465
+ dataType,
433
466
useDirty : isValid ( useDirty ) ? useDirty : options . useDirty
434
467
} )
435
468
field . subscription = {
@@ -780,7 +813,10 @@ export function createForm<FieldProps, VirtualFieldProps>(
780
813
return arr
781
814
} ,
782
815
validate ( opts ?: IFormExtendedValidateFieldOptions ) {
783
- return validate ( field . getSourceState ( state => state . path ) , opts )
816
+ return validate (
817
+ field . getSourceState ( state => state . path ) ,
818
+ opts
819
+ )
784
820
}
785
821
}
786
822
}
@@ -1041,9 +1077,8 @@ export function createForm<FieldProps, VirtualFieldProps>(
1041
1077
1042
1078
function userUpdating ( field : IField | IVirtualField , fn ?: ( ) => void ) {
1043
1079
if ( ! field ) return
1044
- const nodePath = field . getSourceState ( state => state . path )
1045
- if ( nodePath )
1046
- env . userUpdateFields . push ( field . getSourceState ( state => state . path ) )
1080
+ const nodePath = field . state . path
1081
+ if ( nodePath ) env . userUpdateFields . push ( nodePath )
1047
1082
if ( isFn ( fn ) ) {
1048
1083
fn ( )
1049
1084
}
@@ -1223,7 +1258,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
1223
1258
userUpdateFields : [ ] ,
1224
1259
taskIndexes : { } ,
1225
1260
removeNodes : { } ,
1226
- visiblePendingFields : { } ,
1261
+ hiddenPendingFields : { } ,
1227
1262
lastShownStates : { } ,
1228
1263
submittingTask : undefined
1229
1264
}
0 commit comments