UNPKG

@conform-to/dom

Version:

A set of opinionated helpers built on top of the Constraint Validation API

139 lines (138 loc) 5.68 kB
import type { DefaultValue, FieldName, FormValue } from './form'; export type SubmissionState = { validated: Record<string, boolean>; }; export type SubmissionPayload<Entry extends FormDataEntryValue> = Entry | SubmissionPayload<Entry>[] | { [key: string]: SubmissionPayload<Entry>; }; export type SubmissionContext<Value = null, FormError = string[]> = { intent: Intent | null; payload: Record<string, SubmissionPayload<FormDataEntryValue>>; fields: Set<string>; value?: Value; error?: Record<string, FormError | null> | null; state?: SubmissionState; }; export type Submission<Schema, FormError = string[], FormValue = Schema> = { status: 'success'; payload: Record<string, SubmissionPayload<FormDataEntryValue>>; value: FormValue; reply(options?: ReplyOptions<FormError>): SubmissionResult<FormError>; } | { status: 'error' | undefined; payload: Record<string, SubmissionPayload<FormDataEntryValue>>; error: Record<string, FormError | null> | null; reply(options?: ReplyOptions<FormError>): SubmissionResult<FormError>; }; export type SubmissionResult<FormError = string[]> = { status?: 'error' | 'success'; intent?: Intent; initialValue?: Record<string, SubmissionPayload<string>> | null; fields?: string[]; error?: Record<string, FormError | null>; state?: SubmissionState; }; export type ReplyOptions<FormError> = { resetForm?: boolean; } | { formErrors?: FormError; fieldErrors?: Record<string, FormError>; hideFields?: string[]; }; /** * The name to be used when submitting a form control */ export declare const INTENT = "__intent__"; /** * The name to be used when submitting a state */ export declare const STATE = "__state__"; export declare function getSubmissionContext(body: FormData | URLSearchParams): SubmissionContext; export declare function parse<FormValue, FormError>(payload: FormData | URLSearchParams, options: { resolve: (payload: Record<string, any>, intent: Intent | null) => { value?: FormValue; error?: Record<string, FormError | null> | null; }; }): Submission<FormValue, FormError>; export declare function parse<FormValue, FormError>(payload: FormData | URLSearchParams, options: { resolve: (payload: Record<string, any>, intent: Intent | null) => Promise<{ value?: FormValue; error?: Record<string, FormError | null> | null; }>; }): Promise<Submission<FormValue, FormError>>; export declare function parse<FormValue, FormError>(payload: FormData | URLSearchParams, options: { resolve: (payload: Record<string, any>, intent: Intent | null) => { value?: FormValue; error?: Record<string, FormError | null> | null; } | Promise<{ value?: FormValue; error?: Record<string, FormError | null> | null; }>; }): Submission<FormValue, FormError> | Promise<Submission<FormValue, FormError>>; export declare function createSubmission<FormValue, FormError>(context: SubmissionContext<FormValue, FormError>): Submission<FormValue, FormError>; export declare function replySubmission<FormError>(context: SubmissionContext<unknown, FormError>, options?: ReplyOptions<FormError>): SubmissionResult<FormError>; export type ValidateIntent<Schema = any> = { type: 'validate'; payload: { name?: FieldName<Schema>; }; }; export type ResetIntent<Schema = any> = { type: 'reset'; payload: { name?: FieldName<Schema>; index?: never; } | { name: FieldName<Schema>; index: Schema extends Array<unknown> ? number : never; }; }; export type UpdateIntent<Schema = any> = { type: 'update'; payload: { name?: FieldName<Schema>; index?: never; value?: NonNullable<DefaultValue<Schema>>; validated?: boolean; } | { name: FieldName<Schema>; index: Schema extends Array<unknown> ? number : never; value?: Schema extends Array<infer Item> ? NonNullable<DefaultValue<Item>> : never; validated?: boolean; }; }; export type RemoveIntent<Schema = any> = { type: 'remove'; payload: { name: FieldName<Schema>; index: number; }; }; export type InsertIntent<Schema = any> = { type: 'insert'; payload: { name: FieldName<Schema>; defaultValue?: Schema extends Array<infer Item> ? DefaultValue<Item> : never; index?: number; }; }; export type ReorderIntent<Schema = any> = { type: 'reorder'; payload: { name: FieldName<Schema>; from: number; to: number; }; }; export type Intent<Schema = any> = ValidateIntent<Schema> | ResetIntent<Schema> | UpdateIntent<Schema> | ReorderIntent<Schema> | RemoveIntent<Schema> | InsertIntent<Schema>; export declare function getIntent(serializedIntent: string | null | undefined): Intent | null; export declare function serializeIntent<Schema>(intent: Intent<Schema>): string; export declare function updateList(list: unknown, intent: InsertIntent | RemoveIntent | ReorderIntent): void; export declare function setListValue(data: Record<string, unknown>, intent: InsertIntent | RemoveIntent | ReorderIntent): void; /** * A placeholder symbol for the root value of a nested object */ export declare const root: unique symbol; export declare function setState(state: Record<string, unknown>, name: string, valueFn: (value: unknown) => unknown): void; export declare function setListState(state: Record<string, unknown>, intent: InsertIntent | RemoveIntent | ReorderIntent, getDefaultValue?: (defaultValue: any) => any): void; export declare function serialize<Schema>(defaultValue: Schema): FormValue<Schema>;