Created
April 15, 2019 18:46
-
-
Save jgcmarins/5e68ee1b6911a0bd09033cada97021f0 to your computer and use it in GitHub Desktop.
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
| import { connect, FormikProps, withFormik } from "formik"; | |
| import * as React from "react"; | |
| import * as yup from "yup"; | |
| import Button from "@material-ui/core/Button"; | |
| import TextField from "@material-ui/core/TextField"; | |
| const _TextField = props => <TextField {...props} />; | |
| const FormikTextField = connect(_TextField); | |
| type Values = { | |
| name: string; | |
| }; | |
| type Props = {} & FormikProps<Values>; | |
| const WithFormikCustomPropsInnerForm = ({ | |
| handleChange, | |
| isValid, | |
| isSubmitting, | |
| handleSubmit, | |
| ...props | |
| }: Props) => { | |
| const isButtonDisabled = !isValid || isSubmitting; | |
| return ( | |
| <> | |
| <FormikTextField | |
| name={"name"} | |
| placeholder={"Type your name"} | |
| label={"Edit name"} | |
| onChange={handleChange} | |
| /> | |
| <Button disabled={isButtonDisabled} onClick={handleSubmit}> | |
| Submit | |
| </Button> | |
| </> | |
| ); | |
| }; | |
| const WithFormikCustomProps = withFormik<Props, Values>({ | |
| mapPropsToValues: () => {}, | |
| validationSchema: yup.object({ | |
| name: yup.string().required("Name is required") | |
| }), | |
| handleSubmit: (values: Values, formikBag: Props) => {}, | |
| customProps: props => { | |
| // do whatever you want | |
| // inject at props, just like handleSubmit | |
| } | |
| })(WithFormikCustomPropsInnerForm); | |
| export default WithFormikCustomProps; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment