// @flow

import { reduxForm, SubmissionError } from 'redux-form';
import { handleSubmissionError } from './utils';
import { pick } from 'lodash';
type Props = {
  onSubmitFail: any => any,
  onSubmit: any => Promise<*>,
  /* Enable auto submissionError
   * This only works if 'onSubmit' is passed into legoForm,
   * and not if it is passed into 'handleSubmit()'*/
  enableSubmissionError?: boolean,
  /* Move the screen to the first error in the list on SubmissionError */
  enableFocusOnError?: boolean,
  /* Also pick these values based on the keys given on the array
   * Should be used when there is no visible filed for the value,
   * but that it should still pass the value further.
   * 
   * The id is always picked, together with all registered fields
   */
  enableValuePicking?: boolean,
  pickAdditionalValues?: Array<string>,
  ...any
};

const legoForm = ({
  onSubmitFail = () => {},
  onSubmit = () => {},
  enableSubmissionError = true,
  enableFocusOnError = true,
  enableValuePicking = false,
  pickAdditionalValues = [],
  ...rest
}: Props) =>
  reduxForm({
    ...rest,
    onSubmitFail: errors => {
      if (!enableFocusOnError) {
        return onSubmitFail(errors);
      }

      try {
        // We should instead check if error is SubmissionError. Does not work now
        const [firstErrorField] = Object.keys(errors);

        const field = document.querySelector(`[name="${firstErrorField}"]`);
        if (field && field.focus) {
          field.focus();
        }
      } catch (e) {
        //
      }
      return onSubmitFail(errors);
    },
    onSubmit: (values, dispatch, props) => {
      const pickedValues = enableValuePicking
        ? pick(
            values,
            Object.keys(props.registeredFields)
              .concat('id')
              .concat(pickAdditionalValues)
          )
        : values;

      return onSubmit(pickedValues, dispatch, props).catch(error => {
        if (error instanceof SubmissionError || !enableSubmissionError) {
          throw error;
        }

        return handleSubmissionError(error);
      });
    }
  });

export default legoForm;

@@ -356,17 +356,156 @@
 Submit()
-'
+%0A   *%0A   * This will add a %22generic%22 error message to the %22_error%22 value that can%0A   * be ectracted from the form props as %22props.error%22%0A   
 */%0A  ena
@@ -636,24 +636,511 @@
 ?: boolean,%0A
+  /* This will pick the values assosiated with only the visible%0A   * fields at the time of submitting. This makes it possible to add%0A   * default values to the %60initialValues%60 to redux form, and only send%0A   * them if a field is registered/visible. It therefore also allows you to%0A   * do object spreading into initialValues, without sending all the values%0A   * into the onSubmit action.%0A   *%0A   * about picking: https://lodash.com/docs/4.17.4#pick%0A   */%0A  enableValuePicking?: boolean,%0A
   /* Also pi
@@ -1225,19 +1225,21 @@
 there is
- no
+n't a
  visible
@@ -1241,18 +1241,18 @@
 sible fi
-l
 e
+l
 d for th
@@ -1269,56 +1269,33 @@
   * 
-bu
+a
 t th
-at it should still pass the value further.
+e time of submitting
 %0A   
@@ -1373,40 +1373,8 @@
  */%0A
-  enableValuePicking?: boolean,%0A
   pi

Add better documentation for legoForm