加入直播:美国东部时间周四上午 11 点,Storybook 9 发布和 AMA

Knobs

用于编辑 props 的 Storybook 插件

在 Github 上查看

Storybook Addon Knobs(已弃用)

我们正在弃用 Knobs,转而使用 @storybook/addon-controls。

讨论:https://github.com/storybookjs/storybook/discussions/15060

Storybook Addon Knobs 允许您使用 Storybook UI 动态编辑 props。您还可以将 Knobs 作为 Storybook 中故事内的动态变量。

框架支持.

这就是 Knobs 的样子

Storybook Knobs Demo

查看上面的 Live Storybook观看此视频

入门指南

首先,您需要将 Knobs 作为开发依赖项安装到您的项目中。

yarn add @storybook/addon-knobs --dev

此插件的最新版本支持 Storybook v7。如果您使用的是 Storybook 的早期版本,则需要安装此插件的匹配版本,例如 @storybook/addon-knobs@6.4.0

.storybook/main.js 内部

module.exports = {
  addons: ['@storybook/addon-knobs'],
};

现在,使用 Knobs 编写您的故事。

使用 React

import React from 'react';
import { withKnobs, text, boolean, number } from '@storybook/addon-knobs';

export default {
  title: 'Storybook Knobs',
  decorators: [withKnobs],
};
// Add the `withKnobs` decorator to add knobs support to your stories.
// You can also configure `withKnobs` as a global decorator.

// Knobs for React props
export const withAButton = () => (
  <button disabled={boolean('Disabled', false)}>{text('Label', 'Hello Storybook')}</button>
);

// Knobs as dynamic variables.
export const asDynamicVariables = () => {
  const name = text('Name', 'James');
  const age = number('Age', 35);
  const content = `I am ${name} and I'm ${age} years old.`;

  return <div>{content}</div>;
};

使用 Vue.js

MyButton.story.js

import { storiesOf } from '@storybook/vue';
import { withKnobs, text, boolean } from '@storybook/addon-knobs';

import MyButton from './MyButton.vue';

export default {
  title: 'Storybook Knobs',
  decorators: [withKnobs],
};

// Assign `props` to the story's component, calling
// knob methods within the `default` property of each prop,
// then pass the story's prop data to the component’s prop in
// the template with `v-bind:` or by placing the prop within
// the component’s slot.
export const exampleWithKnobs = () => ({
  components: { MyButton },
  props: {
    isDisabled: {
      default: boolean('Disabled', false),
    },
    text: {
      default: text('Text', 'Hello Storybook'),
    },
  },
  template: `<MyButton :isDisabled="isDisabled">{{ text }}</MyButton>`,
});

MyButton.vue

<template>
  <button :disabled="isDisabled">
    <slot></slot>
  </button>
</template>

<script>
export default {
  props: {
    isDisabled: {
      type: Boolean,
      default: false,
    },
  },
};
</script>

使用 Angular

import { storiesOf } from '@storybook/angular';
import { boolean, number, text, withKnobs } from '@storybook/addon-knobs';

import { Button } from '@storybook/angular/demo';

export default {
  title: 'Storybook Knobs',
  decorators: [withKnobs],
};

export const withKnobs = () => ({
  component: Button,
  props: {
    text: text('text', 'Hello Storybook'), // The first param of the knob function has to be exactly the same as the component input.
  },
});

使用 Ember

import { withKnobs, text, boolean } from '@storybook/addon-knobs';
import { hbs } from 'ember-cli-htmlbars';

export default {
  title: 'StoryBook with Knobs',
  decorators: [withKnobs],
};

export const button = () => ({
  template: hbs`
    <button disabled={{disabled}}>{{label}}</button>
  `,
  context: {
    label: text('label', 'Hello Storybook'),
    disabled: boolean('disabled', false),
  },
});

分类

通过为您的 Knobs 分配一个 groupId 来对其进行分类。当 groupId 存在时,Storybook Knobs 面板中将出现选项卡,用于在组之间进行过滤。没有 groupId 的 Knobs 会被自动归类到 ALL 组中。

export const inGroups = () => {
  const personalGroupId = 'personal info';
  const generalGroupId = 'general info';

  const name = text('Name', 'James', personalGroupId);
  const age = number('Age', 35, { min: 0, max: 99 }, personalGroupId);
  const message = text('Hello!', 35, generalGroupId);
  const content = `
    I am ${name} and I'm ${age} years old.
    ${message}
  `;

  return <div>{content}</div>;
};

您可以在下面的 Storybook 面板中看到您的 Knobs。

可用 Knobs

这些是您可以使用的可用 Knobs。您可以从 @storybook/addon-knobs 模块导入这些 Knobs。以下是导入 文本 Knob 的方法。

import { text } from '@storybook/addon-knobs';

就像这样,您可以导入任何其他以下 Knobs

文本

允许您从用户处获取一些文本。

import { text } from '@storybook/addon-knobs';

const label = 'Your Name';
const defaultValue = 'James';
const groupId = 'GROUP-ID1';

const value = text(label, defaultValue, groupId);

布尔值

允许您从用户处获取布尔值。

import { boolean } from '@storybook/addon-knobs';

const label = 'Agree?';
const defaultValue = false;
const groupId = 'GROUP-ID1';

const value = boolean(label, defaultValue, groupId);

数字

允许您从用户处获取数字。

import { number } from '@storybook/addon-knobs';

const label = 'Age';
const defaultValue = 78;
const groupId = 'GROUP-ID1';

const value = number(label, defaultValue);

groupId 一起使用时,将默认 options 作为第三个参数传递。

const value = number(label, defaultValue, {}, groupId);

带范围限制的数字

允许您使用范围滑块从用户处获取数字。

import { number } from '@storybook/addon-knobs';

const label = 'Temperature';
const defaultValue = 73;
const options = {
  range: true,
  min: 60,
  max: 90,
  step: 1,
};
const groupId = 'GROUP-ID1';

const value = number(label, defaultValue, options, groupId);

颜色

允许您从用户处获取颜色。

import { color } from '@storybook/addon-knobs';

const label = 'Color';
const defaultValue = '#ff00ff';
const groupId = 'GROUP-ID1';

const value = color(label, defaultValue, groupId);

对象

允许您从用户处获取 JSON 对象或数组。

import { object } from '@storybook/addon-knobs';

const label = 'Styles';
const defaultValue = {
  backgroundColor: 'red',
};
const groupId = 'GROUP-ID1';

const value = object(label, defaultValue, groupId);

在旋钮中编辑值时,请确保输入有效的 JSON 语法。

数组

允许您从用户处获取字符串数组。

import { array } from '@storybook/addon-knobs';

const label = 'Styles';
const defaultValue = ['Red'];
const groupId = 'GROUP-ID1';

const value = array(label, defaultValue);

在旋钮中编辑值时,您需要使用分隔符。默认情况下是逗号,但这可以通过传递分隔符变量来覆盖。

import { array } from '@storybook/addon-knobs';

const label = 'Styles';
const defaultValue = ['Red'];
const separator = ':';
const value = array(label, defaultValue, separator);

groupId 一起使用时,将默认 separator 作为第三个参数传递。

const value = array(label, defaultValue, ',', groupId);

选择框

允许您从用户处获取选择框中的值。

import { select } from '@storybook/addon-knobs';

const label = 'Colors';
const options = {
  Red: 'red',
  Blue: 'blue',
  Yellow: 'yellow',
  Rainbow: ['red', 'orange', 'etc'],
  None: null,
};
const defaultValue = 'red';
const groupId = 'GROUP-ID1';

const value = select(label, options, defaultValue, groupId);

选项也可以是一个数组

import { select } from '@storybook/addon-knobs';
const label = 'Cats';
const options = ['linus', 'eleanor', 'lover'];
const defaultValue = 'eleanor';
const groupId = 'GROUP-ID2';
const value = select(label, options, defaultValue, groupId);

选项也可以是对象数组

const label = 'Dogs';
const arrayOfObjects = [
  {
    label: 'Sparky',
    dogParent: 'Matthew',
    location: 'Austin',
  },
  {
    label: 'Juniper',
    dogParent: 'Joshua',
    location: 'Austin',
  },
];
const defaultValue = arrayOfObjects[0];
const groupId = 'GROUP-ID3';
const value = select(label, arrayOfObjects, defaultValue, groupId);

单选按钮

允许您从用户处获取单选按钮列表中的值。

import { radios } from '@storybook/addon-knobs';

const label = 'Fruits';
const options = {
  Kiwi: 'kiwi',
  Guava: 'guava',
  Watermelon: 'watermelon',
};
const defaultValue = 'kiwi';
const groupId = 'GROUP-ID1';

const value = radios(label, options, defaultValue, groupId);

选项

可配置的 UI,用于从一组选项中选择值。

import { optionsKnob } from '@storybook/addon-knobs';

const label = 'Fruits';
const valuesObj = {
  Kiwi: 'kiwi',
  Guava: 'guava',
  Watermelon: 'watermelon',
};
const defaultValue = 'kiwi';
const optionsObj = {
  display: 'inline-radio',
};
const groupId = 'GROUP-ID1';

const value = optionsKnob(label, valuesObj, defaultValue, optionsObj, groupId);

或者您可以使用这种导入方式

import { optionsKnob as options } from '@storybook/addon-knobs';

...

const value = options(label, valuesObj, defaultValue, optionsObj, groupId);

optionsObj 的 display 属性接受

  • radio
  • inline-radio
  • check
  • inline-check
  • 选择框
  • multi-select

文件

允许您从用户处获取文件输入的值。

import { files } from '@storybook/addon-knobs';

const label = 'Images';
const accept = '.xlsx, .pdf';
const defaultValue = [];
const groupId = 'GROUP-ID1';

const value = files(label, accept, defaultValue, groupId);

您可以选择指定 文件类型列表,文件输入应接受这些类型。可以多选文件,并将以 Data URLs 数组的形式返回。

日期

允许您从用户处获取日期(和时间)。

import { date } from '@storybook/addon-knobs';

const label = 'Event Date';
const defaultValue = new Date('Jan 20 2017');
const groupId = 'GROUP-ID1';

const value = date(label, defaultValue, groupId);

注意:默认值不得更改 - 例如,不要执行 date('Label', new Date())date('Label')

date 旋钮返回选定的日期作为字符串化的 Unix 时间戳(例如 "1510913096516" )。如果您的组件需要不同形式的日期,您可以包装 date 函数

function myDateKnob(name, defaultValue) {
  const stringTimestamp = date(name, defaultValue);
  return new Date(stringTimestamp);
}

按钮

允许您包含一个按钮和关联的处理程序。

import { button } from '@storybook/addon-knobs';

const label = 'Do Something';
const handler = () => doSomething('foobar');
const groupId = 'GROUP-ID1';

button(label, handler, groupId);

按钮旋钮会在处理程序触发后导致故事重新渲染。您可以通过让处理程序返回 false 来阻止这种情况。

withKnobs 选项

withKnobs 还接受两个可选选项作为故事参数。用法

import { withKnobs } from '@storybook/addon-knobs';

export default {
  title: 'Storybook Knobs',
  decorators: [withKnobs],
};

export const defaultView = () => <div />;
defaultView.parameters = {
  knobs: {
    // Doesn't emit events while user is typing.
    timestamps: true,

    // Escapes strings to be safe for inserting as innerHTML. This option is true by default. It's safe to set it to `false` with frameworks like React which do escaping on their side.
    // You can still set it to false, but it's strongly discouraged to set to true in cases when you host your storybook on some route of your main site or web app.
    escapeHTML: true,
  },
};

Typescript

如果您使用 Typescript,请确保已安装以下库的类型定义

  • node
  • react

您可以使用以下命令安装它们:(假设您使用的是 Typescript >2.0.)

yarn add @types/node @types/react --dev
作者
  • domyen
    domyen
  • kasperpeulen
    kasperpeulen
  • valentinpalkovic
    valentinpalkovic
  • jreinhold
    jreinhold
  • kylegach
    kylegach
  • ndelangen
    ndelangen
适用于
    Angular
    Ember
    HTML
    Marko
    Mithril
    Preact
    Rax
    React
    Riot
    Svelte
    Vue
    Web Components
标签