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 ofFieldPaths for each element of the arrayerror- the validation error message, if anydirty- whether thevalueis not equal toinitialValuepristine- opposite ofdirtyvalid- whether thevalueis validinvalid- oppposite ofvalidtouched- whether the field has been blurred or the form submittedvisited- whether the field has been focusedsetMeta- method to set theFieldMetafor this fieldsetValue- method to set the value for this fieldsetParsedValue- method to set the parsed value for this fieldinsertParsed- method to insert a parsed value into the arrayinsert- method to insert a value into the arraymove- method to move a value from one index to anotherpop- method to remove the last value from the arraypushParsed- method to add a parsed value to the end of the arraypush- method to add a value to the end of the arrayremove- method to remove a value at an indexremoveAll- method to remove all valuesspliceParsed- method to remove and/or insert parsed values, likeArray.splicesplice- method to remove and/or insert values, likeArray.spliceswap- method to swap values at two indicesunshiftParsed- method to add a parsed value to the beginning of the arrayunshift- method to add a value to the beginning of the array