@@ -25,7 +25,6 @@ import {
25
25
defaultGetValueFromEvent ,
26
26
getNamePath ,
27
27
getValue ,
28
- isSimilar ,
29
28
} from './utils/valueUtil' ;
30
29
31
30
const EMPTY_ERRORS : any [ ] = [ ] ;
@@ -77,7 +76,7 @@ export interface InternalFieldProps<Values = any> {
77
76
messageVariables ?: Record < string , string > ;
78
77
initialValue ?: any ;
79
78
onReset ?: ( ) => void ;
80
- onError ?: ( errors : string [ ] , warnings : string [ ] ) => void ;
79
+ onMetaChange ?: ( meta : Meta ) => void ;
81
80
preserve ?: boolean ;
82
81
83
82
/** @private Passed by Form.List props. Do not use since it will break by path check. */
@@ -216,20 +215,11 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
216
215
} ) ) ;
217
216
} ;
218
217
219
- /** Update `this.error`. If `onError` provided, trigger it */
220
- public updateError (
221
- prevErrors : string [ ] ,
222
- nextErrors : string [ ] ,
223
- prevWarnings : string [ ] ,
224
- nextWarnings : string [ ] ,
225
- ) {
226
- const { onError } = this . props ;
227
- if ( onError && ( ! isSimilar ( prevErrors , nextErrors ) || ! isSimilar ( prevWarnings , nextWarnings ) ) ) {
228
- onError ( nextErrors , nextWarnings ) ;
229
- }
230
- this . errors = nextErrors ;
231
- this . warnings = nextWarnings ;
232
- }
218
+ public triggerMetaEvent = ( ) => {
219
+ const { onMetaChange } = this . props ;
220
+
221
+ onMetaChange ?.( this . getMeta ( ) ) ;
222
+ } ;
233
223
234
224
// ========================= Field Entity Interfaces =========================
235
225
// Trigger by store update. Check if need update the component
@@ -242,15 +232,14 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
242
232
243
233
const namePathMatch = namePathList && containsNamePath ( namePathList , namePath ) ;
244
234
245
- const prevErrors = this . errors ;
246
- const prevWarnings = this . warnings ;
247
-
248
235
// `setFieldsValue` is a quick access to update related status
249
236
if ( info . type === 'valueUpdate' && info . source === 'external' && prevValue !== curValue ) {
250
237
this . touched = true ;
251
238
this . dirty = true ;
252
239
this . validatePromise = null ;
253
- this . updateError ( prevErrors , EMPTY_ERRORS , prevWarnings , EMPTY_ERRORS ) ;
240
+ this . errors = EMPTY_ERRORS ;
241
+ this . warnings = EMPTY_ERRORS ;
242
+ this . triggerMetaEvent ( ) ;
254
243
}
255
244
256
245
switch ( info . type ) {
@@ -260,7 +249,9 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
260
249
this . touched = false ;
261
250
this . dirty = false ;
262
251
this . validatePromise = null ;
263
- this . updateError ( prevErrors , EMPTY_ERRORS , prevWarnings , EMPTY_ERRORS ) ;
252
+ this . errors = EMPTY_ERRORS ;
253
+ this . warnings = EMPTY_ERRORS ;
254
+ this . triggerMetaEvent ( ) ;
264
255
265
256
onReset ?.( ) ;
266
257
@@ -272,22 +263,23 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
272
263
case 'setField' : {
273
264
if ( namePathMatch ) {
274
265
const { data } = info ;
266
+
275
267
if ( 'touched' in data ) {
276
268
this . touched = data . touched ;
277
269
}
278
270
if ( 'validating' in data && ! ( 'originRCField' in data ) ) {
279
271
this . validatePromise = data . validating ? Promise . resolve ( [ ] ) : null ;
280
272
}
281
-
282
- const hasError = 'errors' in data ;
283
- const hasWarning = 'warnings' in data ;
284
- if ( hasError || hasWarning ) {
285
- const nextErrors = hasError ? data . errors || EMPTY_ERRORS : prevErrors ;
286
- const nextWarnings = hasWarning ? data . warnings || EMPTY_ERRORS : prevWarnings ;
287
- this . updateError ( prevErrors , nextErrors , prevWarnings , nextWarnings ) ;
273
+ if ( 'errors' in data ) {
274
+ this . errors = data . errors || EMPTY_ERRORS ;
275
+ }
276
+ if ( 'warnings' in data ) {
277
+ this . warnings = data . warnings || EMPTY_ERRORS ;
288
278
}
289
279
this . dirty = true ;
290
280
281
+ this . triggerMetaEvent ( ) ;
282
+
291
283
this . reRender ( ) ;
292
284
return ;
293
285
}
@@ -347,9 +339,6 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
347
339
} ;
348
340
349
341
public validateRules = ( options ?: ValidateOptions ) : Promise < RuleError [ ] > => {
350
- const prevErrors = this . errors ;
351
- const prevWarnings = this . warnings ;
352
-
353
342
// We should fixed namePath & value to avoid developer change then by form function
354
343
const namePath = this . getNamePath ( ) ;
355
344
const currentValue = this . getValue ( ) ;
@@ -401,7 +390,9 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
401
390
}
402
391
} ) ;
403
392
404
- this . updateError ( prevErrors , nextErrors , prevWarnings , nextWarnings ) ;
393
+ this . errors = nextErrors ;
394
+ this . warnings = nextWarnings ;
395
+ this . triggerMetaEvent ( ) ;
405
396
406
397
this . reRender ( ) ;
407
398
}
@@ -414,6 +405,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
414
405
this . dirty = true ;
415
406
this . errors = EMPTY_ERRORS ;
416
407
this . warnings = EMPTY_ERRORS ;
408
+ this . triggerMetaEvent ( ) ;
417
409
418
410
// Force trigger re-render since we need sync renderProps with new meta
419
411
this . reRender ( ) ;
@@ -519,6 +511,8 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
519
511
this . touched = true ;
520
512
this . dirty = true ;
521
513
514
+ this . triggerMetaEvent ( ) ;
515
+
522
516
let newValue : StoreValue ;
523
517
if ( getValueFromEvent ) {
524
518
newValue = getValueFromEvent ( ...args ) ;
0 commit comments