File tree Expand file tree Collapse file tree 4 files changed +79
-0
lines changed Expand file tree Collapse file tree 4 files changed +79
-0
lines changed Original file line number Diff line number Diff line change @@ -1313,6 +1313,15 @@ export class FieldApi<
1313
1313
this . triggerOnChangeListener ( )
1314
1314
}
1315
1315
1316
+ /**
1317
+ * Clear all values from the array.
1318
+ */
1319
+ clearValues = ( opts ?: UpdateMetaOptions ) => {
1320
+ this . form . clearFieldValues ( this . name , opts )
1321
+
1322
+ this . triggerOnChangeListener ( )
1323
+ }
1324
+
1316
1325
/**
1317
1326
* @private
1318
1327
*/
Original file line number Diff line number Diff line change @@ -2125,6 +2125,32 @@ export class FormApi<
2125
2125
this . validateField ( `${ field } [${ index2 } ]` as DeepKeys < TFormData > , 'change' )
2126
2126
}
2127
2127
2128
+ /**
2129
+ * Clear all values within an array field.
2130
+ */
2131
+ clearFieldValues = < TField extends DeepKeysOfType < TFormData , any [ ] > > (
2132
+ field : TField ,
2133
+ opts ?: UpdateMetaOptions ,
2134
+ ) => {
2135
+ const fieldValue = this . getFieldValue ( field )
2136
+
2137
+ const lastIndex = Array . isArray ( fieldValue )
2138
+ ? Math . max ( ( fieldValue as unknown [ ] ) . length - 1 , 0 )
2139
+ : null
2140
+
2141
+ this . setFieldValue ( field , [ ] as any , opts )
2142
+
2143
+ if ( lastIndex !== null ) {
2144
+ for ( let i = 0 ; i <= lastIndex ; i ++ ) {
2145
+ const fieldKey = `${ field } [${ i } ]`
2146
+ this . deleteField ( fieldKey as never )
2147
+ }
2148
+ }
2149
+
2150
+ // validate array change
2151
+ this . validateField ( field , 'change' )
2152
+ }
2153
+
2128
2154
/**
2129
2155
* Resets the field value and meta to default state
2130
2156
*/
Original file line number Diff line number Diff line change @@ -1346,6 +1346,28 @@ describe('field api', () => {
1346
1346
1347
1347
field . moveValue ( 0 , 1 )
1348
1348
expect ( arr ) . toStrictEqual ( [ 'middle' , 'end' , 'start' ] )
1349
+
1350
+ field . clearValues ( )
1351
+ expect ( arr ) . toStrictEqual ( [ ] )
1352
+ } )
1353
+
1354
+ it ( 'should not break when clearValues is called on a non-array field' , ( ) => {
1355
+ const form = new FormApi ( {
1356
+ defaultValues : {
1357
+ name : 'foo' ,
1358
+ } ,
1359
+ } )
1360
+
1361
+ form . mount ( )
1362
+
1363
+ const field = new FieldApi ( {
1364
+ form,
1365
+ name : 'name' ,
1366
+ } )
1367
+
1368
+ field . mount ( )
1369
+
1370
+ expect ( ( ) => field . clearValues ( ) ) . not . toThrow ( )
1349
1371
} )
1350
1372
1351
1373
it ( 'should reset the form on a listener' , ( ) => {
Original file line number Diff line number Diff line change @@ -3267,6 +3267,28 @@ describe('form api', () => {
3267
3267
form . parseValuesWithSchemaAsync ( z . any ( ) )
3268
3268
} ) . not . toThrowError ( )
3269
3269
} )
3270
+
3271
+ it ( 'should delete fields when resetting an array field to an empty array' , ( ) => {
3272
+ const employees = [
3273
+ {
3274
+ firstName : 'Darcy' ,
3275
+ } ,
3276
+ ]
3277
+
3278
+ const form = new FormApi ( {
3279
+ defaultValues : {
3280
+ employees,
3281
+ } ,
3282
+ } )
3283
+ form . mount ( )
3284
+
3285
+ form . clearFieldValues ( 'employees' )
3286
+
3287
+ expect ( form . getFieldValue ( 'employees' ) ) . toEqual ( [ ] )
3288
+ expect ( form . getFieldValue ( `employees[0]` ) ) . toBeUndefined ( )
3289
+ expect ( form . getFieldMeta ( `employees[0]` ) ) . toBeUndefined ( )
3290
+ expect ( form . state . values . employees ) . toStrictEqual ( [ ] )
3291
+ } )
3270
3292
} )
3271
3293
3272
3294
it ( 'should reset the errorSourceMap for the field when the form is reset' , ( ) => {
You can’t perform that action at this time.
0 commit comments