Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

FormApiArrayMethods.md

228 lines6.1 KB

Defined in: FormApi/FormApiArrayMethods.types.public.ts:322

Methods for adding, removing, moving, and filtering array field elements.

Extended by

Type Parameters

TFormData

TFormData

Library-managed. Do not specify explicitly.

Properties

clearFieldValues

clearFieldValues: ClearFieldValuesFn<TFormData>;

Defined in: FormApi/FormApiArrayMethods.types.public.ts:410

Removes every element from an array field.

A runtime value that is not an array produces a warning and is left unchanged. By default, the update marks the array field as touched and dirty, notifies change listeners, and runs change validation.

Example

// items: ['first', 'second']
formApi.clearFieldValues('items')
// items: []

filterFieldValues

filterFieldValues: FilterFieldValuesFn<TFormData>;

Defined in: FormApi/FormApiArrayMethods.types.public.ts:465

Keeps the elements that satisfy a predicate.

options.thisArg sets the predicate's this value. A runtime value that is not an array produces a warning and is left unchanged. By default, the update marks the array field as touched and dirty, notifies change listeners, and runs change validation.

Example

// items: [1, 2, 3, 4]
formApi.filterFieldValues('items', (item) => item % 2 === 0)
// items: [2, 4]

insertFieldValue

insertFieldValue: InsertFieldValueFn<TFormData>;

Defined in: FormApi/FormApiArrayMethods.types.public.ts:394

Inserts an element at an index in an array field.

The index must be between 0 and array.length; passing array.length appends the element. An out-of-range index or a runtime value that is not an array produces a warning and leaves the value unchanged.

By default, the update marks the array field as touched and dirty, notifies change listeners, and runs change validation.

Example

// items: ['first', 'second']
formApi.insertFieldValue('items', 1, 'new item')
// items: ['first', 'new item', 'second']

moveFieldValue

moveFieldValue: MoveFieldValueFn<TFormData>;

Defined in: FormApi/FormApiArrayMethods.types.public.ts:359

Moves an element to another index in an array field.

Both indices must be between 0 and array.length - 1. Passing equal indices does nothing. Out-of-range indices or a runtime value that is not an array produce a warning and leave the value unchanged.

By default, the update marks the array field as touched and dirty, notifies change listeners, and runs change validation.

Example

// items: ['first', 'second', 'third']
formApi.moveFieldValue('items', 0, 2)
// items: ['second', 'third', 'first']

pushFieldValue

pushFieldValue: PushFieldValueFn<TFormData>;

Defined in: FormApi/FormApiArrayMethods.types.public.ts:375

Appends an element to an array field.

A runtime value that is not an array produces a warning and is left unchanged. By default, the update marks the array field as touched and dirty, notifies change listeners, and runs change validation.

Example

// items: ['first', 'second']
formApi.pushFieldValue('items', 'new item')
// items: ['first', 'second', 'new item']

removeFieldValue

removeFieldValue: RemoveFieldValueFn<TFormData>;

Defined in: FormApi/FormApiArrayMethods.types.public.ts:429

Removes an element from an array field.

The index must be between 0 and array.length - 1. An out-of-range index or a runtime value that is not an array produces a warning and leaves the value unchanged.

By default, the update marks the array field as touched and dirty, notifies change listeners, and runs change validation.

Example

// items: ['first', 'second', 'third']
formApi.removeFieldValue('items', 1)
// items: ['first', 'third']

replaceFieldValue

replaceFieldValue: ReplaceFieldValueFn<TFormData>;

Defined in: FormApi/FormApiArrayMethods.types.public.ts:448

Replaces the element at an index in an array field.

The index must be between 0 and array.length - 1. An out-of-range index or a runtime value that is not an array produces a warning and leaves the value unchanged.

By default, the update marks the array field as touched and dirty, notifies change listeners, and runs change validation.

Example

// items: ['first', 'second', 'third']
formApi.replaceFieldValue('items', 1, 'new item')
// items: ['first', 'new item', 'third']

swapFieldValues

swapFieldValues: SwapFieldValuesFn<TFormData>;

Defined in: FormApi/FormApiArrayMethods.types.public.ts:340

Swaps two elements in an array field.

Both indices must be between 0 and array.length - 1. Passing equal indices does nothing. Out-of-range indices or a runtime value that is not an array produce a warning and leave the value unchanged.

By default, the update marks the array field as touched and dirty, notifies change listeners, and runs change validation.

Example

// items: ['first', 'second', 'third']
formApi.swapFieldValues('items', 0, 2)
// items: ['third', 'second', 'first']