useArrayField
React custom hook for subscribing to the value and validation state of an array form field and getting methods to programmatically manipulate it.
import { useArrayField } from '@jcoreio/zod-forms'
export function useArrayField(path): UseArrayFieldProps`
path
may be a FieldPath
. If you cast to TypedUseArrayField
or use the useArrayField
returned from createZodForm
,
path
may be a pathstring or path array.
The full method signature extracts the type of the subschema at the given path, and should produce a TS error if the path is invalid, not array valued, or doesn't exist in the schema.
Returns UseArrayFieldProps
An object containing the following properties. Causes a rerender when any of these properties changes.
elements
- an array ofFieldPath
s for each element of the arrayerror
- the validation error message, if anydirty
- whether thevalue
is not equal toinitialValue
pristine
- opposite ofdirty
valid
- whether therawValue
is validinvalid
- oppposite ofvalid
touched
- whether the field has been blurred or the form submittedvisited
- whether the field has been focusedsetMeta
- method to set theFieldMeta
for this fieldsetRawValue
- method to set the raw value for this fieldsetValue
- method to set the value for this fieldinsert
- method to insert a value into the arrayinsertRaw
- method to insert a raw value into the arraymove
- method to move a value from one index to anotherpop
- method to remove the last value from the arraypush
- method to add a value to the end of the arraypushRaw
- method to add a raw value to the end of the arrayremove
- method to remove a value at an indexremoveAll
- method to remove all valuessplice
- method to remove and/or insert values, likeArray.splice
spliceRaw
- method to remove and/or insert raw values, likeArray.splice
swap
- method to swap values at two indicesunshift
- method to add a value to the beginning of the arrayunshiftRaw
- method to add a raw value to the beginning of the