Last active
October 23, 2019 09:24
-
-
Save stvkoch/3596c2785da1b13f77e9b276d1903552 to your computer and use it in GitHub Desktop.
Formik multiples submit buttons
This file contains 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
function FormContainer(props) { | |
const [formType, setFormType] = useState(DEFAULT_FORM_TYPE); | |
const handlerSubmit = (methodX, methodY) => async values => { | |
if (formType === FORM_TYPE_X) { | |
return await methodX(values); | |
} | |
return await methodY(values); | |
}; | |
const submitFormType = (formTypeSubmited, handleSumitType) => () => { | |
setFormType(formTypeSubmited, () => handlerSubmit()); | |
}; | |
return ( | |
<Formik | |
onSubmit={handlerSubmit(props.saveToApiMethodX, props.saveToApiMethodY)} | |
validate={values => props.validate(formType, values)} | |
> | |
{(values, handleSubmit, errors) => ( | |
<> | |
<button onClick={submitFormType(FORM_TYPE_X, handlerSubmit)}> | |
Method X | |
</button> | |
<button onClick={submitFormType(FORM_TYPE_Y, handlerSubmit)}> | |
Method Y | |
</button> | |
</> | |
)} | |
</Formik> | |
); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment