UNPKG

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