From d0a8f6691027f179d6d76f089d43b47d40b45c93 Mon Sep 17 00:00:00 2001 From: Kamil Kusy Date: Sat, 16 Aug 2025 19:00:53 +0200 Subject: [PATCH] feat(form-core): add new methods to FieldGroupApi --- packages/form-core/src/FieldGroupApi.ts | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/form-core/src/FieldGroupApi.ts b/packages/form-core/src/FieldGroupApi.ts index 32dcfe7a0..69fda71da 100644 --- a/packages/form-core/src/FieldGroupApi.ts +++ b/packages/form-core/src/FieldGroupApi.ts @@ -178,6 +178,7 @@ export class FieldGroupApi< store: Derived> get state() { + // this.store.values e.g {phone: '', fullName: ''} return this.store.state } @@ -347,6 +348,17 @@ export class FieldGroupApi< return this.form.deleteField(this.getFormFieldName(field)) } + /** + * Delete all fields and their subfields. + */ + deleteAllFields = () => { + const fieldGroupKeys = Object.keys(this.state.values || {}) + + fieldGroupKeys.forEach((key) => { + this.deleteField(key) + }) + } + /** * Pushes a value into an array field. */ @@ -409,6 +421,14 @@ export class FieldGroupApi< ) } + replaceAllFields = (newValues: TFieldGroupData) => { + this.deleteAllFields() + + Object.entries(newValues || {}).forEach(([field, value]) => { + this.setFieldValue(field, value as never) + }) + } + /** * Removes a value from an array field at the specified index. */ @@ -470,6 +490,17 @@ export class FieldGroupApi< return this.form.resetField(this.getFormFieldName(field)) } + /** + * Resets all fields values and meta to default state + */ + resetAllFields = () => { + const fieldGroupKeys = Object.keys(this.state.values || {}) + + fieldGroupKeys.forEach((key) => { + this.resetField(key) + }) + } + validateAllFields = (cause: ValidationCause) => this.form.validateAllFields(cause) }