({
debug,
decorators = [],
destroyOnUnregister,
form: alternateFormApi,
initialValues,
initialValuesEqual,
keepDirtyOnReinitialize,
mutators,
onSubmit,
subscription = all,
validate,
validateOnBlur,
...rest
}: FormProps<FormValues>)
| 38 | ); |
| 39 | |
| 40 | function ReactFinalForm<FormValues = Record<string, any>>({ |
| 41 | debug, |
| 42 | decorators = [], |
| 43 | destroyOnUnregister, |
| 44 | form: alternateFormApi, |
| 45 | initialValues, |
| 46 | initialValuesEqual, |
| 47 | keepDirtyOnReinitialize, |
| 48 | mutators, |
| 49 | onSubmit, |
| 50 | subscription = all, |
| 51 | validate, |
| 52 | validateOnBlur, |
| 53 | ...rest |
| 54 | }: FormProps<FormValues>) { |
| 55 | const config: Config<FormValues> = { |
| 56 | debug, |
| 57 | destroyOnUnregister, |
| 58 | initialValues, |
| 59 | keepDirtyOnReinitialize, |
| 60 | mutators, |
| 61 | onSubmit, |
| 62 | validate, |
| 63 | validateOnBlur, |
| 64 | }; |
| 65 | |
| 66 | const form: FormApi<FormValues> = useConstant(() => { |
| 67 | const f = alternateFormApi || createForm<FormValues>(config); |
| 68 | // pause validation until children register all fields on first render (unpaused in useEffect() below) |
| 69 | f.pauseValidation(); |
| 70 | return f; |
| 71 | }); |
| 72 | |
| 73 | // Get initial state without triggering callbacks during render |
| 74 | const [state, setState] = React.useState<FormState<FormValues>>(() => { |
| 75 | // Get initial state synchronously but without callbacks |
| 76 | return form.getState(); |
| 77 | }); |
| 78 | |
| 79 | // save a copy of state that can break through the closure |
| 80 | // on the shallowEqual() line below. |
| 81 | const stateRef = React.useRef<FormState<FormValues>>(state); |
| 82 | stateRef.current = state; |
| 83 | |
| 84 | React.useEffect(() => { |
| 85 | // We have rendered, so all fields are now registered, so we can unpause validation |
| 86 | form.isValidationPaused() && form.resumeValidation(); |
| 87 | const unsubscriptions: Unsubscribe[] = [ |
| 88 | form.subscribe((s) => { |
| 89 | setState((prevState) => { |
| 90 | if (!shallowEqual(s, prevState)) { |
| 91 | return s; |
| 92 | } |
| 93 | return prevState; |
| 94 | }); |
| 95 | }, subscription), |
| 96 | ...(decorators ? decorators.map((decorator) => decorator(form)) : []), |
| 97 | ]; |
nothing calls this directly
no test coverage detected
searching dependent graphs…