Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,15 @@ export class FieldApi<
this.triggerOnChangeListener()
}

/**
* Clear all values from the array.
*/
clearValues = (opts?: UpdateMetaOptions) => {
this.form.clearFieldValues(this.name, opts)

this.triggerOnChangeListener()
}

/**
* @private
*/
Expand Down
26 changes: 26 additions & 0 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,32 @@ export class FormApi<
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
}

/**
* Clear all values within an array field.
*/
clearFieldValues = <TField extends DeepKeysOfType<TFormData, any[]>>(
field: TField,
opts?: UpdateMetaOptions,
) => {
const fieldValue = this.getFieldValue(field)

const lastIndex = Array.isArray(fieldValue)
? Math.max((fieldValue as unknown[]).length - 1, 0)
: null

this.setFieldValue(field, [] as any, opts)

if (lastIndex !== null) {
for (let i = 0; i <= lastIndex; i++) {
const fieldKey = `${field}[${i}]`
this.deleteField(fieldKey as never)
}
}

// validate array change
this.validateField(field, 'change')
}

/**
* Resets the field value and meta to default state
*/
Expand Down
22 changes: 22 additions & 0 deletions packages/form-core/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,28 @@ describe('field api', () => {

field.moveValue(0, 1)
expect(arr).toStrictEqual(['middle', 'end', 'start'])

field.clearValues()
expect(arr).toStrictEqual([])
})

it('should not break when clearValues is called on a non-array field', () => {
const form = new FormApi({
defaultValues: {
name: 'foo',
},
})

form.mount()

const field = new FieldApi({
form,
name: 'name',
})

field.mount()

expect(() => field.clearValues()).not.toThrow()
})

it('should reset the form on a listener', () => {
Expand Down
22 changes: 22 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3267,6 +3267,28 @@ describe('form api', () => {
form.parseValuesWithSchemaAsync(z.any())
}).not.toThrowError()
})

it('should delete fields when resetting an array field to an empty array', () => {
const employees = [
{
firstName: 'Darcy',
},
]

const form = new FormApi({
defaultValues: {
employees,
},
})
form.mount()

form.clearFieldValues('employees')

expect(form.getFieldValue('employees')).toEqual([])
expect(form.getFieldValue(`employees[0]`)).toBeUndefined()
expect(form.getFieldMeta(`employees[0]`)).toBeUndefined()
expect(form.state.values.employees).toStrictEqual([])
})
})

it('should reset the errorSourceMap for the field when the form is reset', () => {
Expand Down