diff --git a/src/atoms/list-atom/listItemForm.ts b/src/atoms/list-atom/listItemForm.ts index 161d1a5..f1c7b6f 100644 --- a/src/atoms/list-atom/listItemForm.ts +++ b/src/atoms/list-atom/listItemForm.ts @@ -1,109 +1,24 @@ -import { - FormAtom, - FormFieldErrors, - FormFieldValues, - FormFields, - RESET, - SubmitStatus, - TouchedFields, - ValidateOn, - ValidateStatus, - formAtom, - walkFields, -} from "form-atoms"; -import { - Atom, - Getter, - SetStateAction, - Setter, - WritableAtom, - atom, -} from "jotai"; +import { FormAtom, FormFields, RESET, formAtom, walkFields } from "form-atoms"; +import { Atom, Getter, SetStateAction, WritableAtom, atom } from "jotai"; import { atomEffect } from "jotai-effect"; import { extendAtom } from "../extendAtom"; import { PrimitiveFormAtom } from "../types"; -export type ExtendFormAtom = - FormAtom extends Atom - ? Atom - : never; +type FormAtomState = + FormAtom extends Atom ? State : never; -// TODO(types): ExtendFormAtom does not work -// The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. -type NamedFormAtom = Atom<{ +type NamedFormAtomState = FormAtomState & { nameAtom: Atom; +}; - /** - * An atom containing an object of nested field atoms - */ - fields: WritableAtom< - Fields, - [Fields | typeof RESET | ((prev: Fields) => Fields)], - void - >; - /** - * An read-only atom that derives the form's values from - * its nested field atoms. - */ - values: Atom>; - /** - * An read-only atom that derives the form's errors from - * its nested field atoms. - */ - errors: Atom>; - /** - * A read-only atom that returns `true` if any of the fields in - * the form are dirty. - */ - dirty: Atom; - /** - * A read-only atom derives the touched state of its nested field atoms. - */ - touchedFields: Atom>; - /** - * A write-only atom that resets the form's nested field atoms - */ - reset: WritableAtom; - /** - * A write-only atom that validates the form's nested field atoms - */ - validate: WritableAtom; - /** - * A read-only atom that derives the form's validation status - */ - validateStatus: Atom; - /** - * A write-only atom for submitting the form - */ - submit: WritableAtom< - null, - [(value: FormFieldValues) => void | Promise], - void - >; - /** - * A read-only atom that reads the number of times the form has - * been submitted - */ - submitCount: Atom; - /** - * An atom that contains the form's submission status - */ - submitStatus: WritableAtom; - _validateFields: ( - get: Getter, - set: Setter, - event: ValidateOn, - ) => Promise; -}>; +type NamedFormAtom = Atom< + NamedFormAtomState +>; export type ListItemForm = NamedFormAtom; -export function listItemForm({ - fields, - formListAtom, - getListNameAtom, -}: { +type ListItemFormConfig = { /** * The fields of the item form. */ @@ -128,7 +43,13 @@ export function listItemForm({ [string | undefined | typeof RESET], void >; -}) { +}; + +export function listItemForm({ + fields, + formListAtom, + getListNameAtom, +}: ListItemFormConfig) { const itemFormAtom: ListItemForm = extendAtom( formAtom(fields) as unknown as PrimitiveFormAtom, (base, get) => {