<commit_before>import { FormikConfig, FormikProvider, FormikValues, useFormik } from 'formik';
import React, { useEffect, useImperativeHandle, useState } from 'react';

export function FormikOnBlur<
  Values extends FormikValues = FormikValues,
  ExtraProps = {}
>(props: FormikConfig<Values> & ExtraProps) {
  const formikBag = useFormik<Values>({ ...props, validateOnBlur: true });
  const [submitting, setSubmitting] = useState(false);

  useEffect(() => {
    if (
      Object.keys(formikBag.errors || {}).length === 0 &&
      formikBag.dirty && 
      !formikBag.isSubmitting &&
      !submitting
    ) {
      setSubmitting(true);
      const { values } = formikBag;
      formikBag.submitForm().then(() => {
        formikBag.resetForm({ values })
        setSubmitting(false);
      });
    }
  }, [
    formikBag.errors,
    formikBag.dirty,
    submitting,
    formikBag.isSubmitting
  ]);

  const { children, innerRef } = props;

  useImperativeHandle(innerRef, () => formikBag);

  return <FormikProvider value={formikBag}>{children}</FormikProvider>;
}<commit_msg>FormikOnBlur: update form on initialValues change<commit_after>import { FormikConfig, FormikProvider, FormikValues, useFormik } from 'formik';
import React, { useEffect, useImperativeHandle, useState } from 'react';

export function FormikOnBlur<
  Values extends FormikValues = FormikValues,
  ExtraProps = {}
>(props: FormikConfig<Values> & ExtraProps) {
  const formikBag = useFormik<Values>({ ...props, validateOnBlur: true });
  const [submitting, setSubmitting] = useState(false);

  useEffect(() => {
    if (
      Object.keys(formikBag.errors || {}).length === 0 &&
      formikBag.dirty &&
      !formikBag.isSubmitting &&
      !submitting
    ) {
      setSubmitting(true);
      const { values } = formikBag;
      formikBag.submitForm().then(() => {
        formikBag.resetForm({ values });
        setSubmitting(false);
      });
    }
  }, [
    formikBag.errors,
    formikBag.dirty,
    submitting,
    formikBag.isSubmitting
  ]);

  useEffect(() => {
    formikBag.resetForm({ values: props.initialValues });
  }, [props.initialValues]);

  const { children, innerRef } = props;

  useImperativeHandle(innerRef, () => formikBag);

  return <FormikProvider value={formikBag}>{children}</FormikProvider>;
}
{'fraction_non_alphanumeric': 0.1401909722222222, 'fraction_numerical': 0.0008680555555555555, 'mean_word_length': 3.6471774193548385, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4886832', 'n_tokens_mistral': 744, 'n_tokens_neox': 718, 'n_words': 138}