Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created November 12, 2020 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibelius/05584e0a8f4d9118f7db96276bd870cd to your computer and use it in GitHub Desktop.
Save sibelius/05584e0a8f4d9118f7db96276bd870cd to your computer and use it in GitHub Desktop.
simple FormikStorybookProvider - useful to stories that depends on a FormikProvider
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