@@ -349,7 +349,9 @@ export interface FieldListeners<
349
349
TData extends DeepValue < TParentData , TName > = DeepValue < TParentData , TName > ,
350
350
> {
351
351
onChange ?: FieldListenerFn < TParentData , TName , TData >
352
+ onChangeDebounceMs ?: number
352
353
onBlur ?: FieldListenerFn < TParentData , TName , TData >
354
+ onBlurDebounceMs ?: number
353
355
onMount ?: FieldListenerFn < TParentData , TName , TData >
354
356
onSubmit ?: FieldListenerFn < TParentData , TName , TData >
355
357
}
@@ -1175,10 +1177,7 @@ export class FieldApi<
1175
1177
setValue = ( updater : Updater < TData > , options ?: UpdateMetaOptions ) => {
1176
1178
this . form . setFieldValue ( this . name , updater as never , options )
1177
1179
1178
- this . options . listeners ?. onChange ?.( {
1179
- value : this . state . value ,
1180
- fieldApi : this ,
1181
- } )
1180
+ this . triggerOnChangeListener ( )
1182
1181
1183
1182
this . validate ( 'change' )
1184
1183
}
@@ -1226,10 +1225,7 @@ export class FieldApi<
1226
1225
) => {
1227
1226
this . form . pushFieldValue ( this . name , value as any , opts )
1228
1227
1229
- this . options . listeners ?. onChange ?.( {
1230
- value : this . state . value ,
1231
- fieldApi : this ,
1232
- } )
1228
+ this . triggerOnChangeListener ( )
1233
1229
}
1234
1230
1235
1231
/**
@@ -1242,10 +1238,7 @@ export class FieldApi<
1242
1238
) => {
1243
1239
this . form . insertFieldValue ( this . name , index , value as any , opts )
1244
1240
1245
- this . options . listeners ?. onChange ?.( {
1246
- value : this . state . value ,
1247
- fieldApi : this ,
1248
- } )
1241
+ this . triggerOnChangeListener ( )
1249
1242
}
1250
1243
1251
1244
/**
@@ -1258,10 +1251,7 @@ export class FieldApi<
1258
1251
) => {
1259
1252
this . form . replaceFieldValue ( this . name , index , value as any , opts )
1260
1253
1261
- this . options . listeners ?. onChange ?.( {
1262
- value : this . state . value ,
1263
- fieldApi : this ,
1264
- } )
1254
+ this . triggerOnChangeListener ( )
1265
1255
}
1266
1256
1267
1257
/**
@@ -1270,10 +1260,7 @@ export class FieldApi<
1270
1260
removeValue = ( index : number , opts ?: UpdateMetaOptions ) => {
1271
1261
this . form . removeFieldValue ( this . name , index , opts )
1272
1262
1273
- this . options . listeners ?. onChange ?.( {
1274
- value : this . state . value ,
1275
- fieldApi : this ,
1276
- } )
1263
+ this . triggerOnChangeListener ( )
1277
1264
}
1278
1265
1279
1266
/**
@@ -1282,10 +1269,7 @@ export class FieldApi<
1282
1269
swapValues = ( aIndex : number , bIndex : number , opts ?: UpdateMetaOptions ) => {
1283
1270
this . form . swapFieldValues ( this . name , aIndex , bIndex , opts )
1284
1271
1285
- this . options . listeners ?. onChange ?.( {
1286
- value : this . state . value ,
1287
- fieldApi : this ,
1288
- } )
1272
+ this . triggerOnChangeListener ( )
1289
1273
}
1290
1274
1291
1275
/**
@@ -1294,10 +1278,7 @@ export class FieldApi<
1294
1278
moveValue = ( aIndex : number , bIndex : number , opts ?: UpdateMetaOptions ) => {
1295
1279
this . form . moveFieldValues ( this . name , aIndex , bIndex , opts )
1296
1280
1297
- this . options . listeners ?. onChange ?.( {
1298
- value : this . state . value ,
1299
- fieldApi : this ,
1300
- } )
1281
+ this . triggerOnChangeListener ( )
1301
1282
}
1302
1283
1303
1284
/**
@@ -1633,10 +1614,7 @@ export class FieldApi<
1633
1614
}
1634
1615
this . validate ( 'blur' )
1635
1616
1636
- this . options . listeners ?. onBlur ?.( {
1637
- value : this . state . value ,
1638
- fieldApi : this ,
1639
- } )
1617
+ this . triggerOnBlurListener ( )
1640
1618
}
1641
1619
1642
1620
/**
@@ -1654,6 +1632,50 @@ export class FieldApi<
1654
1632
} ) as never ,
1655
1633
)
1656
1634
}
1635
+
1636
+ private triggerOnBlurListener ( ) {
1637
+ const debounceMs = this . options . listeners ?. onBlurDebounceMs
1638
+
1639
+ if ( debounceMs && debounceMs > 0 ) {
1640
+ if ( this . timeoutIds . blur ) {
1641
+ clearTimeout ( this . timeoutIds . blur )
1642
+ }
1643
+
1644
+ this . timeoutIds . blur = setTimeout ( ( ) => {
1645
+ this . options . listeners ?. onBlur ?.( {
1646
+ value : this . state . value ,
1647
+ fieldApi : this ,
1648
+ } )
1649
+ } , debounceMs )
1650
+ } else {
1651
+ this . options . listeners ?. onBlur ?.( {
1652
+ value : this . state . value ,
1653
+ fieldApi : this ,
1654
+ } )
1655
+ }
1656
+ }
1657
+
1658
+ private triggerOnChangeListener ( ) {
1659
+ const debounceMs = this . options . listeners ?. onChangeDebounceMs
1660
+
1661
+ if ( debounceMs && debounceMs > 0 ) {
1662
+ if ( this . timeoutIds . change ) {
1663
+ clearTimeout ( this . timeoutIds . change )
1664
+ }
1665
+
1666
+ this . timeoutIds . change = setTimeout ( ( ) => {
1667
+ this . options . listeners ?. onChange ?.( {
1668
+ value : this . state . value ,
1669
+ fieldApi : this ,
1670
+ } )
1671
+ } , debounceMs )
1672
+ } else {
1673
+ this . options . listeners ?. onChange ?.( {
1674
+ value : this . state . value ,
1675
+ fieldApi : this ,
1676
+ } )
1677
+ }
1678
+ }
1657
1679
}
1658
1680
1659
1681
function normalizeError ( rawError ?: ValidationError ) {
0 commit comments