Skip to content

feat(form-core): add array method field.clearValues and form.clearFieldValues #1404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -1288,6 +1288,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 @@ -1962,6 +1962,32 @@ export class FormApi<
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
}

/**
* Clear all values within an array field.
*/
clearFieldValues = <TField extends DeepKeys<TFormData>>(
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 @@ -1195,6 +1195,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 @@ -3015,4 +3015,26 @@ 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',
},
] as const

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([])
})
})