UNPKG

@modular-forms/react

Version:

The modular and type-safe form library for React

41 lines (40 loc) 1.13 kB
import { signal } from '@preact/signals-react'; import { useMemo } from 'react'; /** * Creates and returns the store of the form. * * @param options The form options. * * @returns The reactive store. */ export function useFormStore({ initialValues = {}, validateOn = 'submit', revalidateOn = 'change', validate, } = {}) { return useMemo(() => ({ internal: { // Props initialValues, validate, validateOn, revalidateOn, // Signals fieldNames: signal([]), fieldArrayNames: signal([]), // Stores fields: {}, fieldArrays: {}, // Other validators: new Set(), }, // Signals element: signal(null), submitCount: signal(0), submitting: signal(false), submitted: signal(false), validating: signal(false), touched: signal(false), dirty: signal(false), invalid: signal(false), response: signal({}), }), // eslint-disable-next-line react-hooks/exhaustive-deps []); }