UNPKG

@modular-forms/solid

Version:

The modular and type-safe form library for SolidJS

48 lines (47 loc) 1.67 kB
import { createSignal } from '../primitives'; import { getFieldArrayStore } from './getFieldArrayStore'; import { getPathValue } from './getPathValue'; import { getUniqueId } from './getUniqueId'; /** * Initializes and returns the store of a field array. * * @param form The form of the field array. * @param name The name of the field array. * * @returns The reactive store. */ export function initializeFieldArrayStore(form, name) { // Initialize store on first request if (!getFieldArrayStore(form, name)) { // Create initial items of field array const initial = getPathValue(name, form.internal.initialValues)?.map(() => getUniqueId()) || []; // Create signals of field array store const initialItems = createSignal(initial); const startItems = createSignal(initial); const items = createSignal(initial); const error = createSignal(''); const active = createSignal(false); const touched = createSignal(false); const dirty = createSignal(false); // Add store of field array to form form.internal.fieldArrays[name] = { // Signals initialItems, startItems, items, error, active, touched, dirty, // Other validate: [], validateOn: undefined, revalidateOn: undefined, consumers: new Set(), }; // Add name of field array to form form.internal.fieldArrayNames.set((names) => [...names, name]); } // Return store of field array return getFieldArrayStore(form, name); }