import { ObjectConfig, ObjectFieldConfig } from "../config"; import { FragmentField } from "./fragmentField"; import { ListFieldState } from "./listField"; import { FieldState, InternalSetOpts, SetOpts } from "./valueField"; import { Builtin } from "../utils"; /** * Wraps a given input/on-the-wire type `T` for editing in a form. * * We basically mimic every field in `T` (i.e. `firstName`, `lastName`, etc.) but decorate them * with form-specific state like `touched`, `dirty`, `errors`, etc. * * The intent is that, after ensuring all fields are `valid`/etc., callers can take the * result of this `objectState.value` (or `objectState.originalValue` for the non-proxy version) and * have exactly the on-the-wire type `T` that they need to submit to the backend, without doing the * manual mapping of "data that was in the form controls" into "data that the backend wants". * * Note that this can be hierarchical by having a field of `ListFieldState` that * themselves each wrap an `ObjectState`, i.e.: * * ``` * ObjectState for author * - firstName: FieldState * - lastName: FieldState * - rows: ListFieldState * - [0]: ObjectState for book 1 * - title: FieldState * - [1]: ObjectState for book 2 * - title: FieldState * ``` */ export type ObjectState = FieldStates & FieldState & { /** Sets the state of fields in `state`. */ set(state: Partial, opts?: SetOpts): void; /** Returns whether the object can be saved, i.e. is valid, but also as a side-effect marks touched. */ canSave(): boolean; }; export type ObjectStateInternal = ObjectState & { set(value: T, opts?: InternalSetOpts): void; isSameEntity(other: T): boolean; idKey: string | undefined; }; declare const fragmentSym: unique symbol; export type Fragment = V & { [fragmentSym]: true; }; export declare function fragment(value: V): Fragment; /** For a given input type `T`, decorate each field into the "field state" type that holds our form-relevant state, i.e. valid/touched/etc. */ type FieldStates = { [K in keyof T]-?: T[K] extends Fragment ? FragmentField : T[K] extends Array | null | undefined ? [U] extends [Builtin] ? FieldState : ListFieldState : T[K] extends Builtin | null | undefined ? FieldState : ObjectState; }; /** * Creates a new `ObjectState` for a given form object `T` given config rules in `config`. * * The returned `ObjectState` can be used in a mobx `useLocalObservable` to drive an * interactive form that tracks the current valid/touched/etc. state of both each * individual fields as well as the top-level form/object itself. */ export declare function createObjectState(config: ObjectConfig, instance: T, opts?: { maybeAutoSave?: () => void; }): ObjectState; /** A more internal version of `createObjectState`. */ export declare function newObjectState(config: ObjectFieldConfig, parentState: (() => ObjectState

) | undefined, parentInstance: P | undefined, parentListState: FieldState | undefined, instance: T, key: keyof T | undefined, maybeAutoSave: () => void, deepExhaustive: boolean): ObjectState; export {};