@conform-to/react
Version:
Conform view adapter for react
82 lines • 5.23 kB
TypeScript
import { type FieldName, FormError } from '@conform-to/dom/future';
import type { FieldMetadata, Fieldset, FormContext, FormMetadata, FormState, FormAction, UnknownIntent, IntentHandler, BaseFieldMetadata, BaseFormMetadata, DefineConditionalField } from './types';
export declare function initializeState<ErrorShape>(options?: {
defaultValue?: Record<string, unknown> | null | undefined;
resetKey?: string | undefined;
}): FormState<ErrorShape>;
/**
* Updates form state based on action type:
* - Client actions: update target value and client errors
* - Server actions: update server errors and clear client errors, with optional target value
* - Initialize: set initial server value
*/
export declare function updateState<ErrorShape>(state: FormState<ErrorShape>, action: FormAction<ErrorShape, UnknownIntent | null, {
handlers: Record<string, IntentHandler>;
cancelled: boolean;
reset: (defaultValue?: Record<string, unknown> | null | undefined) => FormState<ErrorShape>;
}>): FormState<ErrorShape>;
/**
* Removes list keys where array length has changed to force regeneration.
* Minimizes UI state loss by only invalidating keys when necessary.
*/
export declare function pruneListKeys(listKeys: Record<string, string[]>, targetValue: Record<string, unknown>): Record<string, string[]>;
export declare function getDefaultPayload(context: FormContext<any>, name: string): unknown;
export declare function getDefaultValue(context: FormContext<any>, name: string): string;
export declare function getDefaultOptions(context: FormContext<any>, name: string): string[];
export declare function isDefaultChecked(context: FormContext<any>, name: string): boolean;
/**
* Determine if the field is touched
*
* This checks if the field is in the list of touched fields,
* or if there is any child field that is touched, i.e. form / fieldset
*/
export declare function isTouched(state: FormState<any>, name?: string): boolean;
export declare function getDefaultListKey(prefix: string, initialValue: Record<string, unknown> | null, name: string): string[];
export declare function getListKey(context: FormContext<any>, name: string): string[];
export declare function getErrors<ErrorShape>(state: FormState<ErrorShape>, name?: string): ErrorShape | undefined;
export declare function getFieldErrors<ErrorShape>(state: FormState<ErrorShape>, name?: string): Record<string, ErrorShape>;
/**
* Checks if fieldErrors contains any errors at the given name or any child path.
*/
export declare function hasFieldError<ErrorShape>(error: FormError<ErrorShape>, name: string): boolean;
export declare function isValid(state: FormState<any>, name?: string): boolean;
export declare function getFormMetadata<ErrorShape, CustomFormMetadata extends Record<string, unknown> = {}, CustomFieldMetadata extends Record<string, unknown> = {}>(context: FormContext<ErrorShape>, options?: {
extendFormMetadata?: ((metadata: BaseFormMetadata<ErrorShape>) => CustomFormMetadata) | undefined;
extendFieldMetadata?: (<FieldShape>(metadata: BaseFieldMetadata<FieldShape, ErrorShape>, ctx: {
form: BaseFormMetadata<ErrorShape>;
when: DefineConditionalField;
}) => CustomFieldMetadata) | undefined;
}): FormMetadata<ErrorShape, CustomFormMetadata, CustomFieldMetadata>;
export declare function getField<FieldShape, ErrorShape = string, CustomFieldMetadata extends Record<string, unknown> = {}>(context: FormContext<ErrorShape>, options: {
name: FieldName<FieldShape>;
extendFieldMetadata?: (<F>(metadata: BaseFieldMetadata<F, ErrorShape>, ctx: {
form: BaseFormMetadata<ErrorShape>;
when: DefineConditionalField;
}) => CustomFieldMetadata) | undefined;
form?: BaseFormMetadata<ErrorShape, CustomFieldMetadata> | undefined;
key?: string | undefined;
}): FieldMetadata<FieldShape, ErrorShape, CustomFieldMetadata>;
/**
* Creates a proxy that dynamically generates field objects when properties are accessed.
*/
export declare function getFieldset<FieldShape = Record<string, any>, ErrorShape = string, CustomFieldMetadata extends Record<string, unknown> = {}>(context: FormContext<ErrorShape>, options: {
name?: FieldName<FieldShape> | undefined;
extendFieldMetadata?: (<F>(metadata: BaseFieldMetadata<F, ErrorShape>, ctx: {
form: BaseFormMetadata<ErrorShape>;
when: DefineConditionalField;
}) => CustomFieldMetadata) | undefined;
form?: BaseFormMetadata<ErrorShape, CustomFieldMetadata> | undefined;
}): Fieldset<FieldShape, ErrorShape, CustomFieldMetadata>;
/**
* Creates an array of field objects for list/array inputs
*/
export declare function getFieldList<FieldShape = Array<any>, ErrorShape = string, CustomFieldMetadata extends Record<string, unknown> = {}>(context: FormContext<ErrorShape>, options: {
name: FieldName<FieldShape>;
extendFieldMetadata?: (<F>(metadata: BaseFieldMetadata<F, ErrorShape>, ctx: {
form: BaseFormMetadata<ErrorShape>;
when: DefineConditionalField;
}) => CustomFieldMetadata) | undefined;
}): FieldMetadata<[
FieldShape
] extends [Array<infer ItemShape> | null | undefined] ? ItemShape : unknown, ErrorShape, CustomFieldMetadata>[];
//# sourceMappingURL=state.d.ts.map