UNPKG

opti-form

Version:

Form logic for React with immutable controls and value

80 lines 3.33 kB
import { TControlExternalErrorFlat } from './TControlExternalErrorFlat'; import { TControlValue } from './TControlValue'; import { TControlDataFields } from './control-data'; import { TFormFields, TResetProps } from './types'; import { FormValidationType } from './validation'; import { TControlObjectValue } from './values'; interface IProps<Value extends TControlObjectValue, Context = never> { /** * A callback that must return form tree structure info. * Each node is created by methods createBasic, createObject and createArray depending on control type. * It is called only once on mount */ getFieldsData: () => TControlDataFields<Value, Context>; /** * Validation context. Required if Context generic param is provided. */ context: Context; /** * Validation type that will be used across the whole form */ validationType?: FormValidationType; /** * Default value for form. It's value matters only on mount, subsequent changes to this prop will be ignored. * If you need to change defaultValue later, use reset() method. */ defaultValue?: TControlValue<Value>; /** * Value for form. It's value matters only on mount, subsequent changes to this prop will be ignored. * If you need to change value later, use reset() method. */ value?: TControlValue<Value>; } interface IResult<Value extends TControlObjectValue> { /** * Current form value. Is immutable and only changes instance if form value changed */ value: TControlValue<Value>; /** * Map of top-level form controls */ fields: TFormFields<Value>; /** * Whether or not form contains validation errors */ isValid: boolean; /** * Whether or not form is currently validating, can be true only if any control has async validation */ isValidating: boolean; /** * Whether or not any control is touched */ isTouched: boolean; /** * Whether or not current form value differs from default value */ isDirty: boolean; /** * Applies errors list to form. Useful if you have backend validation and need to apply validation result to form */ applyFlatErrorsList: (errors: TControlExternalErrorFlat[]) => void; /** * Returns promise that is resolved either with form value that fits the Value param (not TControlValue wrapper) or null. * Will be resolved with null if form contains any validation errors and with value otherwise. * If async validation is in progress, will be resolved after validation finishes, otherwise will be resolved instantly. */ getValidValue: () => Promise<Value | null>; /** * Resets the form. * If no arg is provided will reset current value to current default value and mark all controls as not touched. */ reset: (props?: TResetProps<Value>) => void; } /** * Creates form tree based on provided structure and provides all necessary data and methods */ export declare function useOptiForm<Value extends TControlObjectValue>(props: Omit<IProps<Value, never>, 'context'>): IResult<Value>; export declare function useOptiForm<Value extends TControlObjectValue, Context>(props: IProps<Value, Context>): IResult<Value>; export {}; //# sourceMappingURL=useOptiForm.d.ts.map