在 React Storybook 的单个页面上显示组件的所有变体,但仍然有旋钮?

Eva*_*nss 7 reactjs storybook

我有一个按钮组件,可以是不同类型例如primarysecondary等:

export const buttonTypes = [
  'primary',
  'secondary',
  'tertiary',
  'positive',
  'negative',
]

const Button = ({ text, type }) => {
    return(
        <button className={type}>{text}</button>
    )
}

Button.propTypes = {
    text: PropTypes.string,
    type: PropTypes.oneOf(buttonTypes),
}
Run Code Online (Sandbox Code Playgroud)

在我的 Storybook 文件中,我正在映射选项。这意味着您可以在单个页面上查看所有变体,并且如果将另一个字符串添加到buttonTypes数组中,它将自动添加到样式指南中:

import ButtonComponent, { buttonTypes } from './Button';

const Button = () => {
    return(
        <div>
            {
                buttonTypes.map(type=>(
                    <ButtonComponent key={type} text={type} type={type} />
                ))
            }
        </div>
    )
}

export default {
  title: 'Components',
  component: Button,
};
Run Code Online (Sandbox Code Playgroud)

问题是这不适用于许多附加组件,例如旋钮。要使旋钮正常工作,您需要Button成为实际的组件,而不是我上面所做的包装器。

import ButtonComponent, { buttonTypes } from './Button';

const Button = () => {
  return (
    <ButtonComponent
      type={select('type', buttonTypes, buttonTypes.primary)}
      text="Button"
    />
  );
};
Run Code Online (Sandbox Code Playgroud)

有没有办法使用旋钮并在单个页面上显示所有变化?理想情况下无需手动创建每个组件,因为这需要更多工作,并且如果将新字符串添加到buttonTypes.

zer*_*298 4

使用旋钮的分组功能,这样组件的每个实例都将获得自己的旋钮实例,而不是在所有组件实例之间共享所有旋钮实例。如果您希望共享某些内容而不想共享某些内容,您甚至可以将分组旋钮与非分组旋钮混合使用。

在下面的示例中,我有一个<Button/>故事,其中每个实例都有自己的typedisabled属性副本,但text在它们之间共享。

每种按钮类型都有自己的面板,您可以在其中设置其typedisabled。“其他”组包含未设置其组的任何旋钮(例如text)。

按钮故事书示例

src/Button/Button.component.jsx

import * as React from "react";

import "./Button.style.css";

export const Button = ({
    text,
    type,
    disabled,
    onClick
}) => (
    <button
        className={`button button--${type} ${disabled ? "button--disabled" : ""}`}
        disabled={disabled}
        onClick={onClick}
        children={text}
    />
);
Run Code Online (Sandbox Code Playgroud)

src/Button/Button.stories.jsx

import * as React from "react";
import {withKnobs, text, boolean, select} from "@storybook/addon-knobs";
import {action} from "@storybook/addon-actions";

import {Button, buttonTypes} from "./";

export default {
    title: "Button",
    component: Button,
    decorators: [withKnobs]
};

export const ButtonStory = () => {
    const buttontext = text("Text", "Click Me");

    return (
        <div>
            {buttonTypes.map(buttonType => (
                <div key={buttonType}>
                    <Button
                        type={select("Type", buttonTypes, buttonType, buttonType)}
                        disabled={boolean("Disabled", false, buttonType)}
                        onClick={action(`${buttonType} clicked`)}
                        text={buttontext}
                    />
                </div>
            ))}
        </div>
    );
};

ButtonStory.story = {
    name: "All"
}
Run Code Online (Sandbox Code Playgroud)

src/Button/Button.types.js

import * as React from "react";

import "./Button.style.css";

export const Button = ({
    text,
    type,
    disabled,
    onClick
}) => (
    <button
        className={`button button--${type} ${disabled ? "button--disabled" : ""}`}
        disabled={disabled}
        onClick={onClick}
        children={text}
    />
);
Run Code Online (Sandbox Code Playgroud)

src/Button/Button.style.css

import * as React from "react";
import {withKnobs, text, boolean, select} from "@storybook/addon-knobs";
import {action} from "@storybook/addon-actions";

import {Button, buttonTypes} from "./";

export default {
    title: "Button",
    component: Button,
    decorators: [withKnobs]
};

export const ButtonStory = () => {
    const buttontext = text("Text", "Click Me");

    return (
        <div>
            {buttonTypes.map(buttonType => (
                <div key={buttonType}>
                    <Button
                        type={select("Type", buttonTypes, buttonType, buttonType)}
                        disabled={boolean("Disabled", false, buttonType)}
                        onClick={action(`${buttonType} clicked`)}
                        text={buttontext}
                    />
                </div>
            ))}
        </div>
    );
};

ButtonStory.story = {
    name: "All"
}
Run Code Online (Sandbox Code Playgroud)

src/按钮/index.js

export const buttonTypes = [
    "primary",
    "secondary",
    "tertiary"
];
Run Code Online (Sandbox Code Playgroud)