UNPKG

@matthew.ngo/reform

Version:

A flexible and powerful React form management library with advanced validation, state observation, and multi-group support

95 lines (94 loc) 3.37 kB
/** * Main types module for Reform * Exports all types used throughout the library */ import { Control, FieldPath, UseFormRegister, UseFormReturn, UseFormSetValue } from 'react-hook-form'; import { FormErrorsState } from './core/errors/state/types'; import { FormStateManager } from './core/form/form-state'; import { FieldArrayHelpers } from './features/field-array/types'; import { ConditionalFieldsManager } from './features/conditional/types'; import { FieldConfig, ReformYupIntegration, ValidationConfig, YupSchemaConfig } from './core/validation'; import { FormGroup } from './core/form/form-groups'; import { Maybe, AnyObject } from 'yup'; import { ReformGroupHandler } from './typessss'; /** * Main return type for the useReform hook * Combines all functionality from various modules * * @template T - The type of form data */ export interface ReformReturn<T extends Record<string, any>> extends FormErrorsState, FormStateManager<T>, ReformGroupHandler<T>, FieldArrayHelpers<T>, ConditionalFieldsManager<T> { /** React Hook Form methods */ formMethods: UseFormReturn<{ groups: FormGroup<T>[]; }>; /** Form configuration */ config: DynamicFormConfig<T>; /** Yup integration utilities */ yup: ReformYupIntegration<T>; /** Get all form groups */ getGroups: () => FormGroup<T>[]; /** Get data for a specific group */ getGroupData: (index: number) => T | undefined; } export declare type DeepPartial<T> = { [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]; }; export declare type InferFormData<T> = T & Record<string, unknown>; export declare type FormDataPath<T> = FieldPath<{ groups: FormGroup<T>[]; }>; export declare type FormControl<T> = Control<{ groups: FormGroup<T>[]; }>; export interface FormState { isDirty: boolean; touched: boolean; submitCount: number; isSubmitting: boolean; } export interface YupIntegrationConfig<T extends Maybe<AnyObject>> { /** * Configuration for Yup integration */ yupConfig?: Partial<YupSchemaConfig<T>>; } export declare type DynamicFormConfig<T extends AnyObject> = { value?: FormGroup<T>[]; minGroups?: number; maxGroups?: number; defaultData?: Partial<T>; fields?: { [K in keyof T]: FieldConfig<T, K>; }; onChange?: (groups: FormGroup<T>[]) => void; } & ValidationConfig<T> & YupIntegrationConfig<T>; export interface DynamicFormProps<T extends AnyObject> extends DynamicFormConfig<T> { disabled?: boolean; className?: string; } export declare type FieldKey<T> = keyof T; export declare type FieldValue<T, K extends FieldKey<T>> = T[K]; export declare type FieldData<T> = T; export declare type FieldErrorMessage = string; export declare type FieldErrors = Record<FieldKey<any>, FieldErrorMessage>; export interface FieldState { isDirty: boolean; touched: boolean; initialValue: any; } export interface FieldRenderProps<T, K extends keyof T> { name: string; value: T[K]; error?: string; group: FormGroup<T>; index: number; onChange: (value: T[K]) => void; register: UseFormRegister<{ groups: FormGroup<T>[]; }>; setValue: UseFormSetValue<{ groups: FormGroup<T>[]; }>; fieldPath: string; }