MCPcopy Create free account
hub / github.com/RtlZeroMemory/Rezi / useFieldArray

Function useFieldArray

packages/core/src/forms/internal/arrayState.ts:154–375  ·  view source on GitHub ↗
(field: K)

Source from the content-addressed store, hash-verified

152 useFieldArray: <K extends ArrayFieldName<T>>(field: K) => UseFieldArrayReturn<T, K>;
153}> {
154 const useFieldArray = <K extends ArrayFieldName<T>>(field: K): UseFieldArrayReturn<T, K> => {
155 const fieldKey = field as keyof T;
156 const values = getArrayFieldValues(options.state.values, field);
157 const keys = ensureFieldArrayKeys(
158 fieldKey,
159 values.length,
160 options.fieldArrayKeysRef,
161 options.fieldArrayKeyCounterRef,
162 );
163
164 const append = (item: ArrayFieldItem<T, K>): void => {
165 options.pendingAsyncValuesRef.current = null;
166
167 options.updateFormState((prev) => {
168 if (prev.isSubmitting || !options.isFieldEditableInternal(fieldKey, prev)) {
169 return prev;
170 }
171
172 const currentValues = getArrayFieldValues(prev.values, field);
173 const nextValuesArray = [...currentValues, item];
174 const nextValues = {
175 ...prev.values,
176 [field]: nextValuesArray as unknown as T[K],
177 };
178 options.pendingAsyncValuesRef.current = nextValues;
179
180 const nextTouched = [
181 ...normalizeBooleanArray(prev.touched[fieldKey], currentValues.length, false),
182 false,
183 ];
184 const nextDirty = computeFieldDirty(
185 fieldKey,
186 nextValuesArray as T[keyof T],
187 options.initialValuesRef.current,
188 );
189
190 let nextErrors = prev.errors;
191 if (options.validateOnChange) {
192 nextErrors = options.runSyncValidationFiltered(nextValues, prev);
193 } else {
194 nextErrors = {
195 ...prev.errors,
196 [fieldKey]: remapFieldArrayErrors(
197 prev.errors[fieldKey],
198 currentValues.length,
199 (errs) => [...errs, undefined],
200 ),
201 };
202 }
203
204 appendFieldArrayKey(fieldKey, options.fieldArrayKeysRef, options.fieldArrayKeyCounterRef);
205
206 return {
207 ...prev,
208 values: nextValues,
209 errors: nextErrors,
210 submitError: undefined,
211 touched: {

Callers

nothing calls this directly

Calls 2

getArrayFieldValuesFunction · 0.85
ensureFieldArrayKeysFunction · 0.85

Tested by

no test coverage detected