@bbbtech/storybook-formik

中文文档
4.0.0发布于 2025/01/25MITTypeScriptESM + CJS近 30 天下载 4.2万

A storybook addon that allows you to use components in your stories that rely on Formik context and see internal Formik state in a panel.

原始使用文档
文档目录11 节
  1. Migration to BBB Tech
  2. Install
  3. Usage
  4. Legacy story format
  5. Arguments
  6. formik.castValues with formik.validationSchema
  7. Using with Controls
  8. Mature Examples
  9. Contributing
  10. Development
  11. Releasing a new version

storybook-formik

A Storybook Addon and Decorator to wrap Formik Fields and track their state in a Panel.

example screenshot

You can see the example stories in action here.

Thanks to @jaredpalmer for giving us the amazing formik library and making forms great again.

Migration to BBB Tech

Note, this package has been migrated to the org: BBB Tech. The only real difference this means is that the package needs to be installed with a new command, scoped by the org's slug (i.e. npm i --save-dev @bbbtech/storybook-formik) and to update the path when registering the package in your main.js.

It also means that all future versions will be released under the new package, so please migrate your dependency if you want to get future versions.

Install

npm install --save-dev @bbbtech/storybook-formik

Then register the addon in .storybook/main.js

module.exports = {
  stories: ['../stories/**/*.stories.tsx'],
  addons: ['@bbbtech/storybook-formik/register'],
};

Usage

Suppose you split your forms into smaller, re-usable components and these 'subforms' rely on formik context, each of these sub-forms may be used to build up a larger form, but you still want to test and run them independently. You can use the withFormik decorator so that we can wrap the subform in a Formik component, which will pass down the context as normal.

Given a simple subform:

import React from 'react';
import { Field } from 'formik';

const PersonalInfoSubform = () => (
  <div>
    <Field name="forename" type="input" />
    <Field name="surname" type="input" />
  </div>
);

export default PersonalInfoSubform;

You add the withFormik decorator to your stories and can pass any Formik props as a parameter to the individual story.

export const personalInfoSubform = () => (
  <>
    <p>
      The decorator can wrap Components that include Fields (or anything else
      expecting Formik context). This allows us to better componentise our
      larger forms.
    </p>
    <PersonalInfoSubForm />
  </>
);
personalInfoSubform.decorators = [withFormik];
// can use the DecoratorParams type and pass a type for type-safety of initialValues
const personalInfoParams: DecoratorParams<PersonalInfo> = {
  formik: {
    initialValues: personalInfoInitialValues,
    validationSchema: personalInfoValidationSchema,
  },
};
personalInfoSubform.parameters = personalInfoParams;

This gives you the benefit of rendering formik Fields that are expecting formik context, but also to track the key formik state within the storybook panel below.

See the example stories for further examples

Legacy story format

Example with the storiesOf syntax.

import { storiesOf } from '@storybook/react';
import withFormik from '@bbbtech/storybook-formik';

import PersonalInfoSubform from '<your_component_path>/PersonalInfoSubform';

storiesOf('Example', module)
  .addDecorator(withFormik)
  .add('default', () => <PersonalInfoSubform />, {
    formik: {
      initialValues: {
        forename: 'John',
        surname: 'Johnerson',
      },
    },
  });

Arguments

You can pass any Formik component props (initialValues, validationSchema, validateOnBlur, etc) as arguments to a story. These props must be passed under the formik parameter key.

If no initial values are supplied, { enableReinitialize: true, initialValues: StoryContext.args || {} } will be used.

export const myTextInput = () => (
  <MyTextInput
    name="formikTweet"
    label="Describe formik in 80 characters"
    placeholder="I love formik because..."
  />
);
myTextInput.parameters = {
  formik: {
    initialValues: {
      formikTweet: '',
    },
    validationSchema: someSchema,
    onSubmit: v => console.log('I want to log these... ', v),
  },
};

formik.castValues with formik.validationSchema

If a validationSchema is provided in the formik config and { castValues: true }, then validationSchema.cast(initialValues || StoryContext.args || {}) will be called and the result passed in as initialValues.

This is useful if the form fields require a certain format to their values or when automatically converting StoryContext.args into a valid set of initialValues.

Using with Controls

If using @storybook/addon-essentials and StoryContext.args, withFormik will syncronize the form values with the storybook controls.

export const myTextInput = () => (
  <MyTextInput
    name="formikTweet"
    label="Describe formik in 80 characters"
    placeholder="I love formik because..."
  />
);
myTextInput.args = {
  formikTweet: '',
};
myTextInput.parameters = {
  formik: {
    validationSchema: someSchema,
    onSubmit: v => console.log('I want to log these... ', v),
  },
};

Mature Examples

In time, I will add more mature examples that show the usefulness of the subforms pattern on a larger scale. If anyone has any good examples then please do submit them.

Contributing

Development

You can test changes by building the package locally. The flow would be something like this:

  • Make changes to addon
  • npm run build:watch - builds the dist bundle locally in watch mode
  • npm run storybook - runs the package storybook with the example forms, preview changes here
  • npm run test - run unit tests

Releasing a new version

Deployment is pretty basic/simple right now, all you need to do is: npm version, and specify a new version, this will kick off relevant lifecycle scripts defined in the package.json (including npm publish in the postversion script)

版本与发布记录

@bbbtech/storybook-formik 共发布 4 个版本,首次发布于 2022/06/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.0latest2025/01/251.8 年前
3.0.02023/09/083 年前
2.6.02023/04/063 年前
2.5.02022/06/204 年前

关于 @bbbtech/storybook-formik 的常见问题

@bbbtech/storybook-formik 是什么?

A storybook addon that allows you to use components in your stories that rely on Formik context and see internal Formik state in a panel.

如何安装 @bbbtech/storybook-formik?

在项目目录运行 npm install @bbbtech/storybook-formik,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:formik(^1.0.0||^2.0.0)、react(*)、react-dom(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bbbtech/storybook-formik 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bbbtech/storybook-formik 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bbbtech/storybook-formik 可以商用吗?

@bbbtech/storybook-formik 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bbbtech/storybook-formik 的下载量有多少?

根据 npm 官方下载统计,@bbbtech/storybook-formik 在 2026/10/08 – 2026/11/06 这 30 天内被下载 42,450 次,日均约 1,415 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bbbtech/storybook-formik 还在维护吗?

最新版本 4.0.0 发布于 2025/01/25(1.8 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。