使用 Storybook 呈现多个变体

Rendering multiple variants with Storybook

我正在为我用 React 构建的库构建我的故事书文档,但我找不到在同一页面中呈现多个变体的简单方法。

所以,到目前为止我所拥有的是这样的

const Template = (args, { argTypes }) => <Title {...args} />

export const Primary = Template.bind({});
export const Success = Template.bind({});
export const Warning = Template.bind({});
export const Inverse = Template.bind({});
export const Default = Template.bind({});
export const Info = Template.bind({});
export const Danger = Template.bind({});
export const Disabled = Template.bind({});

Primary.args = {
  children: 'Primary',
  level: 3, // <- this can go from 1 to 5,
  as: 'h1', 
  variant: 'primary',
}

Success.args = {
  children: 'Success',
  level: 3, // <- this can go from 1 to 5,
  as: 'h1',
  variant: 'success',
}

等...

这会产生这个:

当我渲染每个变体时,我正在尝试实现类似的效果:

我怎样才能用 Storybook 做这样的事情?

您可以使用 decorators

在 Story 中呈现组件的多个实例

示例代码(应该与您的代码类似):

export const Primary = Template.bind({});
Primary.args = {
  primary: true,
  label: 'Button',
};

Primary.decorators = [
  () => {
    return (
      <>
        <Button {...Primary.args as ButtonProps} level={1} />
        <Button {...Primary.args as ButtonProps} level={2} />
        <Button {...Primary.args as ButtonProps} level={3} />
      </>
    );
  },
];

输出:

更多info,有3个级别decorators,值得一读:

  1. 故事级别
  2. 组件级别
  3. 全球水平