Created
November 12, 2020 13:52
-
-
Save sibelius/05584e0a8f4d9118f7db96276bd870cd to your computer and use it in GitHub Desktop.
simple FormikStorybookProvider - useful to stories that depends on a FormikProvider
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
import { Meta } from '@storybook/react/types-6-0'; | |
import { Story } from '@storybook/addon-docs/blocks'; | |
import { FormikProvider, useFormik } from 'formik'; | |
import InputField, { InputFieldProps } from './InputField'; | |
import { useMemo, ReactNode } from 'react'; | |
export default { | |
title: 'Form/InputField', | |
component: InputField, | |
} as Meta; | |
type FormikStorybookProps = { | |
initialValues?: Record<string, unknown>, | |
children: ReactNode, | |
} | |
const FormikStorybook = (props: FormikStorybookProps) => { | |
const { | |
initialValues = {}, | |
children, | |
} = props; | |
const memoInitialValues = useMemo(() => initialValues, []); | |
const formikbag = useFormik({ | |
initialValues: memoInitialValues, | |
onSubmit: () => {}, | |
}); | |
return ( | |
<FormikProvider value={formikbag}> | |
{children} | |
</FormikProvider> | |
); | |
} | |
const Template: Story<InputDrawerProps> = (args) => { | |
return ( | |
<FormikStorybook> | |
<InputField {...args} /> | |
</FormikStorybook> | |
) | |
}; | |
export const Default = Template.bind({}); | |
Default.args = {}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment