Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/form-core/src/FieldGroupApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export class FieldGroupApi<
store: Derived<FieldGroupState<TFieldGroupData>>

get state() {
// this.store.values e.g {phone: '', fullName: ''}
return this.store.state
}

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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)
}