Skip to content

Instantly share code, notes, and snippets.

@iamdanthedev
Created February 19, 2018 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamdanthedev/c80faaa1707c8693f6aacfb7e6b5307d to your computer and use it in GitHub Desktop.
Save iamdanthedev/c80faaa1707c8693f6aacfb7e6b5307d to your computer and use it in GitHub Desktop.
storybook formik decorator
const initialValues = { comment: 'hello'; }
storiesOf('Form Controls: Select', module)
.addDecorator(withFormValues(initialValues)
.add('default', () => (
<Select name="select" label="Country..." options={selectOptions} />
</Formik>
))
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>
);
@nass600
Copy link

nass600 commented Dec 7, 2018

@iamdanthedev Did you manage to make this work with the formik render function? Something like:

const withFormValues = (initialValues, validationSchema) => story => (
    <Formik
        initialValues={initialValues}
        validationSchema={validationSchema}
        validateOnBlur={false}
        validateOnChange={false}
        onSubmit={values => console.log(values)}
        render={({ values, touched, errors }) => story(values, touched, errors)}
    />
);

@arturopie
Copy link

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