UNPKG

12 kBTypeScriptView Raw
1import { Component, ComponentType, Context, SyntheticEvent } from "react";
2import { Dispatch } from "redux";
3import { FieldType, FormErrors, FormStateMap, FormWarnings, RegisteredFieldState } from "../index";
4import { Validator } from "./Field";
5import { FormState } from "./reducer";
6
7export type FormSubmitHandler<FormData = {}, P = {}, ErrorType = string> = (
8 values: FormData,
9 dispatch: Dispatch<any>,
10 props: DecoratedFormProps<FormData, P, ErrorType>,
11 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
12) => void | FormErrors<FormData, ErrorType> | Promise<any>;
13
14export type GetFormState = (state: any) => FormStateMap;
15export interface SubmitHandler<FormData = {}, P = {}, ErrorType = string> {
16 (
17 submit: FormSubmitHandler<FormData, P, ErrorType>,
18 props?: DecoratedFormProps<FormData, P, ErrorType>,
19 valid?: boolean,
20 asyncValidate?: any,
21 fields?: string[],
22 ): any;
23 (event: SyntheticEvent<any>): void;
24}
25
26export interface ValidateCallback<FormData, P, ErrorType> {
27 values: FormData;
28 nextProps: DecoratedFormProps<FormData, P, ErrorType>;
29 props: DecoratedFormProps<FormData, P, ErrorType>;
30 initialRender: boolean;
31 lastFieldValidatorKeys: string[];
32 fieldValidatorKeys: string[];
33 structure: any;
34}
35
36export interface AsyncValidateCallback<FormData, ErrorType> {
37 asyncErrors?: FormErrors<FormData, ErrorType> | undefined;
38 initialized: boolean;
39 trigger: "blur" | "submit";
40 blurredField?: string | undefined;
41 pristine: boolean;
42 syncValidationPasses: boolean;
43}
44
45export interface InjectedArrayProps {
46 insert(field: string, index: number, value: any): void;
47 move(field: string, from: number, to: number): void;
48 pop(field: string): void;
49 push(field: string, value: any): void;
50 remove(field: string, index: number): void;
51 removeAll(field: string): void;
52 shift(field: string): void;
53 splice(field: string, index: number, removeNum: number, value: any): void;
54 swap(field: string, indexA: number, indexB: number): void;
55 unshift(field: string, value: any): void;
56}
57
58export interface RegisteredField {
59 count: number;
60 name: string;
61 type: "Field" | "FieldArray";
62}
63
64export interface InjectedFormProps<FormData = {}, P = {}, ErrorType = string> {
65 anyTouched: boolean;
66 array: InjectedArrayProps;
67 asyncValidate(): void;
68 asyncValidating: string | boolean;
69 autofill(field: string, value: any): void;
70 blur(field: string, value: any): void;
71 change(field: string, value: any): void;
72 clearAsyncError(field: string): void;
73 clearSubmit(): void;
74 destroy(): void;
75 dirty: boolean;
76 error: ErrorType;
77 form: string;
78 handleSubmit: SubmitHandler<FormData, P, ErrorType>;
79 initialize(data: Partial<FormData>): void;
80 initialized: boolean;
81 initialValues: Partial<FormData>;
82 invalid: boolean;
83 pristine: boolean;
84 reset(): void;
85 submitFailed: boolean;
86 submitSucceeded: boolean;
87 submitting: boolean;
88 touch(...field: string[]): void;
89 untouch(...field: string[]): void;
90 valid: boolean;
91 warning: any;
92}
93
94export interface ConfigProps<FormData = {}, P = {}, ErrorType = string> {
95 form: string;
96 asyncBlurFields?: string[] | undefined;
97 asyncChangeFields?: string[] | undefined;
98 asyncValidate?(
99 values: FormData,
100 dispatch: Dispatch<any>,
101 props: DecoratedFormProps<FormData, P, ErrorType>,
102 blurredField: string,
103 ): Promise<any>;
104 destroyOnUnmount?: boolean | undefined;
105 enableReinitialize?: boolean | undefined;
106 forceUnregisterOnUnmount?: boolean | undefined;
107 getFormState?: GetFormState | undefined;
108 immutableProps?: string[] | undefined;
109 initialValues?: Partial<FormData> | undefined;
110 keepDirtyOnReinitialize?: boolean | undefined;
111 updateUnregisteredFields?: boolean | undefined;
112 keepValues?: boolean | undefined;
113 onChange?(
114 values: Partial<FormData>,
115 dispatch: Dispatch<any>,
116 props: DecoratedFormProps<FormData, P, ErrorType>,
117 previousValues: Partial<FormData>,
118 ): void;
119 onSubmit?: FormSubmitHandler<FormData, P, ErrorType> | SubmitHandler<FormData, P, ErrorType> | undefined;
120 onSubmitFail?(
121 errors: FormErrors<FormData, ErrorType> | undefined,
122 dispatch: Dispatch<any>,
123 submitError: any,
124 props: DecoratedFormProps<FormData, P, ErrorType>,
125 ): void;
126 onSubmitSuccess?(result: any, dispatch: Dispatch<any>, props: DecoratedFormProps<FormData, P, ErrorType>): void;
127 propNamespace?: string | undefined;
128 pure?: boolean | undefined;
129 shouldValidate?(params: ValidateCallback<FormData, P, ErrorType>): boolean;
130 shouldError?(params: ValidateCallback<FormData, P, ErrorType>): boolean;
131 shouldWarn?(params: ValidateCallback<FormData, P, ErrorType>): boolean;
132 shouldAsyncValidate?(params: AsyncValidateCallback<FormData, ErrorType>): boolean;
133 submitAsSideEffect?: boolean | undefined;
134 touchOnBlur?: boolean | undefined;
135 touchOnChange?: boolean | undefined;
136 persistentSubmitErrors?: boolean | undefined;
137 validate?(values: FormData, props: DecoratedFormProps<FormData, P, ErrorType>): FormErrors<FormData, ErrorType>;
138 warn?(values: FormData, props: DecoratedFormProps<FormData, P, ErrorType>): FormWarnings<FormData>;
139}
140
141export interface FormContext {
142 form: string;
143 getFormState: GetFormState;
144 asyncValidate: {
145 (name?: string, value?: any, trigger?: "blur" | "change"): Promise<any>;
146 };
147 getValues: { (): any };
148 sectionPrefix?: string | undefined;
149 prefixName?: string | undefined;
150 register: (
151 name: string,
152 type: string,
153 getValidator?: () => Validator | Validator[],
154 getWarner?: () => Validator | Validator[],
155 ) => void;
156 unregister: (name: string) => void;
157 registerInnerOnSubmit: (innerOnSubmit: () => void) => void;
158 focus: (name: string) => void;
159 change: (name: string, value: any) => void;
160 blur: (name: string, value: any) => void;
161}
162
163export interface WrappedReduxFormContext {
164 _reduxForm: FormContext;
165}
166
167export declare const ReduxFormContext: Context<FormContext>;
168
169export interface FormInstance<FormData, P> extends Component<P> {
170 dirty: boolean;
171 invalid: boolean;
172 pristine: boolean;
173 registeredFields: RegisteredFieldState[];
174 reset(): void;
175 resetSection(...sections: string[]): void;
176 submit(): Promise<any>;
177 valid: boolean;
178 values: Partial<FormData>;
179 wrappedInstance?: HTMLElement | undefined;
180}
181
182export type SubmitAction = () => void;
183export type InitializeAction<FormData> = (
184 initialValues: Partial<FormData>,
185 keepDirty: boolean,
186 otherMeta?: any,
187) => void;
188export type AutoFillAction = (field: string, value: any) => void;
189export type BlurAction = (field: string, value: any) => void;
190export type ChangeAction = (field: string, value: any) => void;
191export type FocusAction = (field: string) => void;
192export type ArrayUnshiftAction = (field: string, value: any) => void;
193export type ArrayShiftAction = (field: string) => void;
194export type ArraySpliceAction = (
195 field: string,
196 index: number,
197 removeNum: number,
198 value: any,
199) => void;
200export type ArrayInsertAction = (field: string, index: number, value: any) => void;
201export type ArrayMoveAction = (field: string, from: number, to: number) => void;
202export type ArrayPopAction = (field: string) => void;
203export type ArrayPushAction = (field: string, value: any) => void;
204export type ArrayRemoveAction = (field: string, index: number) => void;
205export type ArrayRemoveAllAction = (field: string) => void;
206export type ArraySwapAction = (field: string, indexA: number, indexB: number) => void;
207export type ClearSubmitAction = () => void;
208export type ClearSubmitErrorsAction = () => void;
209export type ClearAsyncErrorAction = (field: string) => void;
210export type ClearFieldsAction = (keepTouched: boolean, persistentSubmitErrors: boolean, ...fields: string[]) => void;
211export type DestroyAction = () => void;
212export type RegisterFieldAction = (name: string, type: FieldType) => void;
213export type UnregisterFieldAction = (name: string, destroyOnUnmount?: boolean) => void;
214export type ResetAction = () => void;
215export type ResetSectionAction = () => void;
216export type SetSubmitFailedAction = (...fields: string[]) => void;
217export type SetSubmitSucceededAction = (...fields: string[]) => void;
218export type StartAsyncValidationAction = (field: string) => void;
219export type StopAsyncValidationAction<ErrorType> = (errors?: FormErrors<ErrorType>) => void;
220export type StopSubmitAction<ErrorType> = (errors?: FormErrors<ErrorType>) => void;
221export type StartSubmitAction = () => void;
222export type TouchAction = (...fields: string[]) => void;
223export type UntouchAction = (...fields: string[]) => void;
224export type UpdateSyncErrorsAction<ErrorType> = (syncErrors?: FormErrors<ErrorType>, error?: any) => void;
225export type UpdateSyncWarningsAction<ErrorType> = (syncErrors?: FormErrors<ErrorType>, error?: any) => void;
226
227export type DecoratedFormState<FormData, ErrorType> = FormState & {
228 asyncErrors?: FormErrors<FormData, ErrorType> | undefined;
229 asyncValidating: boolean;
230 dirty: boolean;
231 error?: any;
232 initialized: boolean;
233 invalid: boolean;
234 pristine: boolean;
235 submitSucceeded: boolean;
236 syncErrors?: FormErrors<FormData, ErrorType> | undefined;
237 syncWarnings?: FormWarnings<any, any> | undefined;
238 triggerSubmit?: boolean | undefined;
239 valid: boolean;
240 validExceptSubmit: boolean;
241 warning?: any;
242};
243
244export interface DecoratedFormActions<ErrorType> {
245 arrayInsert: ArrayInsertAction;
246 arrayMove: ArrayMoveAction;
247 arrayPop: ArrayPopAction;
248 arrayPush: ArrayPushAction;
249 arrayRemove: ArrayRemoveAction;
250 arrayRemoveAll: ArrayRemoveAllAction;
251 arrayShift: ArrayShiftAction;
252 arraySplice: ArraySpliceAction;
253 arraySwap: ArraySwapAction;
254 arrayUnshift: ArrayUnshiftAction;
255 autofill: AutoFillAction;
256 clearSubmit: ClearSubmitAction;
257 clearSubmitErrors: ClearSubmitErrorsAction;
258 clearAsyncError: ClearAsyncErrorAction;
259 clearFields: ClearFieldsAction;
260 destroy: DestroyAction;
261 initialize: InitializeAction<ErrorType>;
262 registerField: RegisterFieldAction;
263 reset: ResetAction;
264 resetSection: ResetSectionAction;
265 startAsyncValidation: StartAsyncValidationAction;
266 startSubmit: StartSubmitAction;
267 stopAsyncValidation: StopAsyncValidationAction<ErrorType>;
268 stopSubmit: StopSubmitAction<ErrorType>;
269 submit: SubmitAction;
270 setSubmitFailed: SetSubmitFailedAction;
271 setSubmitSucceeded: SetSubmitSucceededAction;
272 touch: TouchAction;
273 unregisterField: UnregisterFieldAction;
274 untouch: UntouchAction;
275 updateSyncErrors: UpdateSyncErrorsAction<ErrorType>;
276 updateSyncWarnings: UpdateSyncWarningsAction<ErrorType>;
277 blur: BlurAction;
278 change: ChangeAction;
279 focus: FocusAction;
280 array: InjectedArrayProps;
281 dispatch: Dispatch;
282}
283
284export type DecoratedFormProps<FormData = {}, P = {}, ErrorType = string> =
285 & P
286 & Partial<ConfigProps<FormData, P, ErrorType>>
287 & Partial<DecoratedFormState<FormData, ErrorType>>
288 & Partial<DecoratedFormActions<ErrorType>>
289 & { _reduxForm?: WrappedReduxFormContext | undefined };
290
291export interface DecoratedComponentClass<FormData, P> {
292 new(props?: P, context?: any): FormInstance<FormData, P>;
293}
294
295export type FormDecorator<FormData, P, ErrorType = string> = (
296 component: ComponentType<P & InjectedFormProps<FormData, P, ErrorType>>,
297) => DecoratedComponentClass<FormData, DecoratedFormProps<FormData, P, ErrorType>>;
298
299export declare function reduxForm<FormData = {}, P = {}, ErrorType = string>(
300 config:
301 | ConfigProps<FormData, P, ErrorType>
302 | Partial<ConfigProps<FormData, P, ErrorType>>,
303): FormDecorator<FormData, P, ErrorType>;
304
305export default reduxForm;