import { r as UnionVariants } from "./union-schema-B-Lsy588.mjs"; import { Validator } from "typebox/schema"; import { Static, TLiteral, TObject, TSchema, TUnion } from "typebox"; //#region src/form/form-field.model.d.ts declare class FormFieldModel { readonly name: string; readonly schema: T; readonly config: FormFieldConfig; readonly validator: Validator; value: T.Static | undefined; touched: boolean; /** * An error set by hand rather than derived from the schema — typically a server response, or a rule * the schema can't express, applied from inside `handleSubmit`. Use `setError`. * * Distinct from `errorMessage`, which is what to *display*: this one when set, the schema's otherwise. */ error: string | undefined; get valid(): boolean; get errorMessage(): string; constructor(config: FormFieldConfig); setValue(value?: T.Static): void; /** Set (or with `undefined`, clear) a manual error. Cleared automatically by an edit, a reset, or the next submit. */ setError(error: string | undefined): void; private convertValue; setTouched(touched: boolean): void; reset(): void; props(): any; toJSON(): T.Static | undefined; } //#endregion //#region src/form/form.types.d.ts type FormSchema = TObject | TUnion; interface FormConfig { handleSubmit: (data: Static) => Promise; /** * Called when `handleSubmit` throws — for a toast, a report to your error tracker, or mapping an API * error onto fields with `form.fields.x.setError(...)`. * * `submitError` is already set by the time this runs, so a handler that wants to own presentation * entirely can clear it with `form.setSubmitError(undefined)` and render nothing from the form. * * Providing this **replaces the default `console.error`**. Without it, the rejection is consumed by * the form's own `catch` and a thrown error would otherwise be completely invisible — so if you take * over, logging unexpected failures becomes yours too. */ handleError?: (error: unknown, form: FormModel) => void; initialValues?: FormInitialValues; } interface FormFieldConfig { name: string; schema: T; initialValue?: Static; } type UnionToIntersection = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never; /** Keys present on the variant `O` (a single union member). */ type VariantKeys = O extends TObject ? keyof O["properties"] : never; /** Keys present on *every* variant — the shared base + discriminator. */ type SharedKeys = UnionToIntersection extends { k: infer K extends PropertyKey; } ? K : never; /** The schema of key `K` across the variants that declare it (unioned when they differ). */ type VariantSchema = O extends TObject ? K extends keyof O["properties"] ? O["properties"][K] : never : never; /** The static value type of key `K` across the variants that declare it. */ type VariantValue = O extends TObject ? K extends keyof O["properties"] ? Static : never : never; type FormFields = T extends TUnion ? { [K in SharedKeys>]: FormFieldModel, K>> } : T extends TObject ? { [K in keyof T["properties"]]: FormFieldModel } : never; type RawFormFields = T extends TUnion ? { [K in VariantKeys>]: FormFieldModel, K>> } : T extends TObject ? { [K in keyof T["properties"]]: FormFieldModel } : never; type FormInitialValues = T extends TUnion ? { [K in VariantKeys>]?: VariantValue, K> } : T extends TObject ? Partial> : never; /** Keys that are typed as a literal in at least one variant — valid discriminators. */ type DiscriminatorKeys = T extends TUnion ? LiteralKeys> : never; type LiteralKeys = O extends TObject ? { [K in keyof O["properties"]]: O["properties"][K] extends TLiteral ? K : never }[keyof O["properties"]] : never; /** The literal values a given discriminator key can take across all variants. */ type DiscriminatorValue = T extends TUnion ? LiteralValue, D> : never; type LiteralValue = O extends TObject ? D extends keyof O["properties"] ? O["properties"][D] extends TLiteral ? L : never : never : never; /** The variant object whose discriminator `D` equals the literal `V`. */ type MatchVariant = T extends TUnion ? Distribute, D, V> : never; type Distribute = O extends TObject ? D extends keyof O["properties"] ? O["properties"][D] extends TLiteral ? V extends L ? O : never : never : never : never; //#endregion //#region src/form/form.model.d.ts declare class FormModel { /** Shared fields only (for unions); every field for a plain object schema. */ readonly fields: FormFields; /** Every field across all variants — escape hatch for reaching variant fields. */ readonly rawFields: RawFormFields; readonly config: FormConfig; readonly schema: T; readonly validator: Validator; submitting: boolean; submitted: boolean; /** * Whatever `handleSubmit` last threw. `unknown` rather than `any`: it may be an `Error`, an API * error object, or a string, and narrowing at the render site is the only safe way to read it. */ submitError: unknown; constructor(schema: T, config: FormConfig); get valid(): boolean; props(): any; setSubmitError(error: unknown): void; protected setSubmitting(submitting: boolean): void; protected setSubmitted(submitted: boolean): void; reset(): void; validate(): boolean; toJSON(): Partial>; } //#endregion //#region src/form/components/form.d.ts interface MobxFormProps extends Omit, "form" | "action" | "method"> { form: FormModel; } declare const MobxForm: import("react").ForwardRefExoticComponent & import("react").RefAttributes>; //#endregion //#region src/form/components/form-when.d.ts interface FormWhenProps, V extends string> { form: FormModel; /** The discriminator field name. */ field: D; /** Render the children only while the discriminator field equals this value. */ value: V & DiscriminatorValue; /** Receives the matching variant's fields. The form itself is already in scope. */ children: (fields: FormFields>) => React.ReactNode; } /** * Renders its children only when the form's discriminator field currently holds * `value`, passing the fields narrowed to that variant. Stack one per variant to * lay out a discriminated-union form without manual conditionals or casts. */ declare function FormWhen, V extends string>({ form, field, value, children }: FormWhenProps): import("react/jsx-runtime").JSX.Element; //#endregion //#region src/form/form.context.d.ts declare const formContext: import("react").Context> | undefined>; declare const useFormContext: () => FormModel>; declare const FormProvider: import("react").Provider> | undefined>; declare const useFormContextIfAvailable: () => FormModel> | undefined; //#endregion //#region src/form/use-form.d.ts declare const useForm: (schema: T, config: FormConfig) => FormModel; //#endregion export { DiscriminatorKeys, DiscriminatorValue, FormConfig, FormFieldConfig, FormFieldModel, FormFields, FormModel, FormProvider, FormSchema, FormWhen, FormWhenProps, MatchVariant, MobxForm, MobxFormProps, RawFormFields, formContext, useForm, useFormContext, useFormContextIfAvailable }; //# sourceMappingURL=form.d.mts.map