Created
February 19, 2018 13:04
-
-
Save iamdanthedev/c80faaa1707c8693f6aacfb7e6b5307d to your computer and use it in GitHub Desktop.
storybook formik decorator
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
const initialValues = { comment: 'hello'; } | |
storiesOf('Form Controls: Select', module) | |
.addDecorator(withFormValues(initialValues) | |
.add('default', () => ( | |
<Select name="select" label="Country..." options={selectOptions} /> | |
</Formik> | |
)) |
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 React from 'react'; | |
import { Formik } from 'formik'; | |
/** | |
* Decorates a story with Formik | |
* @param initialValues | |
* @param validationSchema - yup schema (formik validationSchema) | |
* @returns {function(*): *} | |
*/ | |
export const withFormValues = (initialValues, validationSchema) => story => ( | |
<Formik | |
initialValues={initialValues} | |
validationSchema={validationSchema} | |
onSubmit={values => console.log(values)} | |
> | |
{story()} | |
</Formik> | |
); |
You can use a storybook action for onSubmit
import { action } from "@storybook/addon-actions";
const formSubmit = action("form-submit");
...
onSubmit={formSubmit}
...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@iamdanthedev Did you manage to make this work with the formik render function? Something like: