Skip to content

Commit d0a8f66

Browse files
committed
feat(form-core): add new methods to FieldGroupApi
1 parent 1e58e57 commit d0a8f66

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

packages/form-core/src/FieldGroupApi.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export class FieldGroupApi<
178178
store: Derived<FieldGroupState<TFieldGroupData>>
179179

180180
get state() {
181+
// this.store.values e.g {phone: '', fullName: ''}
181182
return this.store.state
182183
}
183184

@@ -347,6 +348,17 @@ export class FieldGroupApi<
347348
return this.form.deleteField(this.getFormFieldName(field))
348349
}
349350

351+
/**
352+
* Delete all fields and their subfields.
353+
*/
354+
deleteAllFields = () => {
355+
const fieldGroupKeys = Object.keys(this.state.values || {})
356+
357+
fieldGroupKeys.forEach((key) => {
358+
this.deleteField(key)
359+
})
360+
}
361+
350362
/**
351363
* Pushes a value into an array field.
352364
*/
@@ -409,6 +421,14 @@ export class FieldGroupApi<
409421
)
410422
}
411423

424+
replaceAllFields = (newValues: TFieldGroupData) => {
425+
this.deleteAllFields()
426+
427+
Object.entries(newValues || {}).forEach(([field, value]) => {
428+
this.setFieldValue(field, value as never)
429+
})
430+
}
431+
412432
/**
413433
* Removes a value from an array field at the specified index.
414434
*/
@@ -470,6 +490,17 @@ export class FieldGroupApi<
470490
return this.form.resetField(this.getFormFieldName(field))
471491
}
472492

493+
/**
494+
* Resets all fields values and meta to default state
495+
*/
496+
resetAllFields = () => {
497+
const fieldGroupKeys = Object.keys(this.state.values || {})
498+
499+
fieldGroupKeys.forEach((key) => {
500+
this.resetField(key)
501+
})
502+
}
503+
473504
validateAllFields = (cause: ValidationCause) =>
474505
this.form.validateAllFields(cause)
475506
}

0 commit comments

Comments
 (0)