Created
June 27, 2018 18:24
-
-
Save slightlytyler/c356dd94feeb17b01b3a23dd6f1311d0 to your computer and use it in GitHub Desktop.
withForm HOC that wraps withFormik and includes the event in handleSubmit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import { withFormik } from 'formik'; | |
import { compose, pick } from 'lodash/fp'; | |
import PropTypes from 'prop-types'; | |
import { withContext, withHandlers } from 'recompose'; | |
type Config = Object; | |
const withForm = (config: Config) => { | |
const HOC = compose( | |
withFormik(config), | |
withHandlers({ | |
handleSubmit: props => e => { | |
e.preventDefault(); | |
return config.handleSubmit(props.values, { props, ...props }, e); | |
}, | |
}), | |
withContext( | |
{ | |
formik: PropTypes.object, | |
}, | |
props => ({ | |
formik: pick( | |
[ | |
'values', | |
'errors', | |
'touched', | |
'isSubmitting', | |
'submitCount', | |
'resetForm', | |
'submitForm', | |
'validateForm', | |
'setError', | |
'setErrors', | |
'setFieldError', | |
'setFieldTouched', | |
'setFieldValue', | |
'setStatus', | |
'setSubmitting', | |
'setTouched', | |
'setValues', | |
'setFormikState', | |
'dirty', | |
'isValid', | |
'initialValues', | |
'registerField', | |
'unregisterField', | |
'handleBlur', | |
'handleChange', | |
'handleReset', | |
'handleSubmit', | |
'validateOnChange', | |
'validateOnBlue', | |
], | |
props | |
), | |
}) | |
) | |
); | |
return HOC; | |
}; | |
export default withForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment