UNPKG

10.2 kBTypeScriptView Raw
1import * as React from 'react';
2/**
3 * Values of fields in the form
4 */
5export interface FormikValues {
6 [field: string]: any;
7}
8/**
9 * An object containing error messages whose keys correspond to FormikValues.
10 * Should always be an object of strings, but any is allowed to support i18n libraries.
11 */
12export declare type FormikErrors<Values> = {
13 [K in keyof Values]?: Values[K] extends any[] ? Values[K][number] extends object ? FormikErrors<Values[K][number]>[] | string | string[] : string | string[] : Values[K] extends object ? FormikErrors<Values[K]> : string;
14};
15/**
16 * An object containing touched state of the form whose keys correspond to FormikValues.
17 */
18export declare type FormikTouched<Values> = {
19 [K in keyof Values]?: Values[K] extends any[] ? Values[K][number] extends object ? FormikTouched<Values[K][number]>[] : boolean : Values[K] extends object ? FormikTouched<Values[K]> : boolean;
20};
21/**
22 * Formik state tree
23 */
24export interface FormikState<Values> {
25 /** Form values */
26 values: Values;
27 /** map of field names to specific error for that field */
28 errors: FormikErrors<Values>;
29 /** map of field names to whether the field has been touched */
30 touched: FormikTouched<Values>;
31 /** whether the form is currently submitting */
32 isSubmitting: boolean;
33 /** whether the form is currently validating (prior to submission) */
34 isValidating: boolean;
35 /** Top level status state, in case you need it */
36 status?: any;
37 /** Number of times user tried to submit the form */
38 submitCount: number;
39}
40/**
41 * Formik computed properties. These are read-only.
42 */
43export interface FormikComputedProps<Values> {
44 /** True if any input has been touched. False otherwise. */
45 readonly dirty: boolean;
46 /** True if state.errors is empty */
47 readonly isValid: boolean;
48 /** The initial values of the form */
49 readonly initialValues: Values;
50 /** The initial errors of the form */
51 readonly initialErrors: FormikErrors<Values>;
52 /** The initial visited fields of the form */
53 readonly initialTouched: FormikTouched<Values>;
54 /** The initial status of the form */
55 readonly initialStatus?: any;
56}
57/**
58 * Formik state helpers
59 */
60export interface FormikHelpers<Values> {
61 /** Manually set top level status. */
62 setStatus: (status?: any) => void;
63 /** Manually set errors object */
64 setErrors: (errors: FormikErrors<Values>) => void;
65 /** Manually set isSubmitting */
66 setSubmitting: (isSubmitting: boolean) => void;
67 /** Manually set touched object */
68 setTouched: (touched: FormikTouched<Values>, shouldValidate?: boolean) => void;
69 /** Manually set values object */
70 setValues: (values: React.SetStateAction<Values>, shouldValidate?: boolean) => void;
71 /** Set value of form field directly */
72 setFieldValue: (field: string, value: any, shouldValidate?: boolean) => void;
73 /** Set error message of a form field directly */
74 setFieldError: (field: string, message: string | undefined) => void;
75 /** Set whether field has been touched directly */
76 setFieldTouched: (field: string, isTouched?: boolean, shouldValidate?: boolean) => void;
77 /** Validate form values */
78 validateForm: (values?: any) => Promise<FormikErrors<Values>>;
79 /** Validate field value */
80 validateField: (field: string) => void;
81 /** Reset form */
82 resetForm: (nextState?: Partial<FormikState<Values>>) => void;
83 /** Submit the form imperatively */
84 submitForm: () => Promise<void>;
85 /** Set Formik state, careful! */
86 setFormikState: (f: FormikState<Values> | ((prevState: FormikState<Values>) => FormikState<Values>), cb?: () => void) => void;
87}
88/**
89 * Formik form event handlers
90 */
91export interface FormikHandlers {
92 /** Form submit handler */
93 handleSubmit: (e?: React.FormEvent<HTMLFormElement>) => void;
94 /** Reset form event handler */
95 handleReset: (e?: React.SyntheticEvent<any>) => void;
96 handleBlur: {
97 /** Classic React blur handler, keyed by input name */
98 (e: React.FocusEvent<any>): void;
99 /** Preact-like linkState. Will return a handleBlur function. */
100 <T = string | any>(fieldOrEvent: T): T extends string ? (e: any) => void : void;
101 };
102 handleChange: {
103 /** Classic React change handler, keyed by input name */
104 (e: React.ChangeEvent<any>): void;
105 /** Preact-like linkState. Will return a handleChange function. */
106 <T = string | React.ChangeEvent<any>>(field: T): T extends React.ChangeEvent<any> ? void : (e: string | React.ChangeEvent<any>) => void;
107 };
108 getFieldProps: <Value = any>(props: any) => FieldInputProps<Value>;
109 getFieldMeta: <Value>(name: string) => FieldMetaProps<Value>;
110 getFieldHelpers: <Value = any>(name: string) => FieldHelperProps<Value>;
111}
112/**
113 * Base formik configuration/props shared between the HoC and Component.
114 */
115export interface FormikSharedConfig<Props = {}> {
116 /** Tells Formik to validate the form on each input's onChange event */
117 validateOnChange?: boolean;
118 /** Tells Formik to validate the form on each input's onBlur event */
119 validateOnBlur?: boolean;
120 /** Tells Formik to validate upon mount */
121 validateOnMount?: boolean;
122 /** Tell Formik if initial form values are valid or not on first render */
123 isInitialValid?: boolean | ((props: Props) => boolean);
124 /** Should Formik reset the form when new initialValues change */
125 enableReinitialize?: boolean;
126}
127/**
128 * <Formik /> props
129 */
130export interface FormikConfig<Values> extends FormikSharedConfig {
131 /**
132 * Form component to render
133 */
134 component?: React.ComponentType<FormikProps<Values>> | React.ReactNode;
135 /**
136 * Render prop (works like React router's <Route render={props =>} />)
137 * @deprecated
138 */
139 render?: (props: FormikProps<Values>) => React.ReactNode;
140 /**
141 * React children or child render callback
142 */
143 children?: ((props: FormikProps<Values>) => React.ReactNode) | React.ReactNode;
144 /**
145 * Initial values of the form
146 */
147 initialValues: Values;
148 /**
149 * Initial status
150 */
151 initialStatus?: any;
152 /** Initial object map of field names to specific error for that field */
153 initialErrors?: FormikErrors<Values>;
154 /** Initial object map of field names to whether the field has been touched */
155 initialTouched?: FormikTouched<Values>;
156 /**
157 * Reset handler
158 */
159 onReset?: (values: Values, formikHelpers: FormikHelpers<Values>) => void;
160 /**
161 * Submission handler
162 */
163 onSubmit: (values: Values, formikHelpers: FormikHelpers<Values>) => void | Promise<any>;
164 /**
165 * A Yup Schema or a function that returns a Yup schema
166 */
167 validationSchema?: any | (() => any);
168 /**
169 * Validation function. Must return an error object or promise that
170 * throws an error object where that object keys map to corresponding value.
171 */
172 validate?: (values: Values) => void | object | Promise<FormikErrors<Values>>;
173 /** Inner ref */
174 innerRef?: React.Ref<FormikProps<Values>>;
175}
176/**
177 * State, handlers, and helpers made available to form component or render prop
178 * of <Formik/>.
179 */
180export declare type FormikProps<Values> = FormikSharedConfig & FormikState<Values> & FormikHelpers<Values> & FormikHandlers & FormikComputedProps<Values> & FormikRegistration & {
181 submitForm: () => Promise<any>;
182};
183/** Internal Formik registration methods that get passed down as props */
184export interface FormikRegistration {
185 registerField: (name: string, fns: {
186 validate?: FieldValidator;
187 }) => void;
188 unregisterField: (name: string) => void;
189}
190/**
191 * State, handlers, and helpers made available to Formik's primitive components through context.
192 */
193export declare type FormikContextType<Values> = FormikProps<Values> & Pick<FormikConfig<Values>, 'validate' | 'validationSchema'>;
194export interface SharedRenderProps<T> {
195 /**
196 * Field component to render. Can either be a string like 'select' or a component.
197 */
198 component?: string | React.ComponentType<T | void>;
199 /**
200 * Render prop (works like React router's <Route render={props =>} />)
201 */
202 render?: (props: T) => React.ReactNode;
203 /**
204 * Children render function <Field name>{props => ...}</Field>)
205 */
206 children?: (props: T) => React.ReactNode;
207}
208export declare type GenericFieldHTMLAttributes = JSX.IntrinsicElements['input'] | JSX.IntrinsicElements['select'] | JSX.IntrinsicElements['textarea'];
209/** Field metadata */
210export interface FieldMetaProps<Value> {
211 /** Value of the field */
212 value: Value;
213 /** Error message of the field */
214 error?: string;
215 /** Has the field been visited? */
216 touched: boolean;
217 /** Initial value of the field */
218 initialValue?: Value;
219 /** Initial touched state of the field */
220 initialTouched: boolean;
221 /** Initial error message of the field */
222 initialError?: string;
223}
224/** Imperative handles to change a field's value, error and touched */
225export interface FieldHelperProps<Value> {
226 /** Set the field's value */
227 setValue: (value: Value, shouldValidate?: boolean) => void;
228 /** Set the field's touched value */
229 setTouched: (value: boolean, shouldValidate?: boolean) => void;
230 /** Set the field's error value */
231 setError: (value: string | undefined) => void;
232}
233/** Field input value, name, and event handlers */
234export interface FieldInputProps<Value> {
235 /** Value of the field */
236 value: Value;
237 /** Name of the field */
238 name: string;
239 /** Multiple select? */
240 multiple?: boolean;
241 /** Is the field checked? */
242 checked?: boolean;
243 /** Change event handler */
244 onChange: FormikHandlers['handleChange'];
245 /** Blur event handler */
246 onBlur: FormikHandlers['handleBlur'];
247}
248export declare type FieldValidator = (value: any) => string | void | Promise<string | void>;