(config: Config<FormValues, InitialFormValues>)
| 164 | } |
| 165 | |
| 166 | function createForm< |
| 167 | FormValues = Record<string, any>, |
| 168 | InitialFormValues extends Partial<FormValues> = Partial<FormValues> |
| 169 | >(config: Config<FormValues, InitialFormValues>): FormApi<FormValues, InitialFormValues> { |
| 170 | if (!config) { |
| 171 | throw new Error("No config specified"); |
| 172 | } |
| 173 | let { |
| 174 | debug, |
| 175 | destroyOnUnregister, |
| 176 | keepDirtyOnReinitialize, |
| 177 | initialValues, |
| 178 | mutators, |
| 179 | onSubmit, |
| 180 | validate, |
| 181 | validateOnBlur, |
| 182 | callbackScheduler, |
| 183 | ignoreUnregister, |
| 184 | } = config; |
| 185 | if (!onSubmit) { |
| 186 | throw new Error("No onSubmit function specified"); |
| 187 | } |
| 188 | |
| 189 | const state: InternalState<FormValues, InitialFormValues> = { |
| 190 | subscribers: { index: 0, entries: {} }, |
| 191 | fieldSubscribers: Object.create(null), |
| 192 | fields: Object.create(null), |
| 193 | formState: { |
| 194 | asyncErrors: {}, |
| 195 | dirtySinceLastSubmit: false, |
| 196 | modifiedSinceLastSubmit: false, |
| 197 | errors: {}, |
| 198 | initialValues: initialValues && { ...initialValues } as InitialFormValues, |
| 199 | |
| 200 | pristine: true, |
| 201 | submitting: false, |
| 202 | submitFailed: false, |
| 203 | submitSucceeded: false, |
| 204 | resetWhileSubmitting: false, |
| 205 | valid: true, |
| 206 | validating: 0, |
| 207 | values: (initialValues ? { ...initialValues } : {}) as FormValues, |
| 208 | }, |
| 209 | lastFormState: undefined, |
| 210 | }; |
| 211 | let inBatch = 0; |
| 212 | let validationPaused = false; |
| 213 | let validationBlocked = false; |
| 214 | let preventNotificationWhileValidationPaused = false; |
| 215 | let nextAsyncValidationKey = 0; |
| 216 | let nextFieldInstanceId = 0; |
| 217 | const asyncValidationPromises: { [key: number]: Promise<any> } = {}; |
| 218 | const pendingAsyncCallbacks: (() => void)[] = []; |
| 219 | let asyncCallbacksScheduled = false; |
| 220 | const clearAsyncValidationPromise = (key: number) => (result: any) => { |
| 221 | delete asyncValidationPromises[key]; |
| 222 | return result; |
| 223 | }; |
searching dependent graphs…