Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Created April 15, 2019 18:46
Show Gist options
  • Select an option

  • Save jgcmarins/5e68ee1b6911a0bd09033cada97021f0 to your computer and use it in GitHub Desktop.

Select an option

Save jgcmarins/5e68ee1b6911a0bd09033cada97021f0 to your computer and use it in GitHub Desktop.
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