使用 Storybook 渲染多个变体

vbo*_*tio 7 javascript reactjs 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',
}
Run Code Online (Sandbox Code Playgroud)

ETC...

这会生成:

在此输入图像描述

当我渲染每个变体时,我试图实现这样的目标:

在此输入图像描述

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

som*_*llg 10

您可以使用以下方法在故事中渲染组件的多个实例decorators

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

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} />
      </>
    );
  },
];
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

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

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