简体   繁体   English

Storybook 中的旋钮和控件的区别?

[英]difference between knobs and controls in Storybook?

I am new to the storybook.我是故事书的新手。 When I go through the documentation and videos about the storybook I read about knobs Addon.当我通过有关故事书的文档和视频 go 时,我阅读了有关旋钮插件的信息。 Knobs addon and control looks similar.旋钮插件和控件看起来很相似。 What is the difference between those two things?这两件事有什么区别?

Controls were introduced with Storybook version 6. They replace knobs for most of the use cases.控件是在 Storybook 版本 6 中引入的。它们取代了大多数用例的旋钮。 However, there may be some edge cases were you still want to use knobs for dynamic values.但是,可能存在一些边缘情况,您仍希望将旋钮用于动态值。 For example, see this Github discussion on this topic: https://github.com/storybookjs/storybook/issues/11984例如,请参阅有关此主题的 Github 讨论: https://github.com/storybookjs/storybook/issues/11984

controls addon is a companion to the docs addon so it interfaces with the ArgsTable which by itself is designed to automatically extract your components' propTypes & defaultProps (although I found this not to work) controls插件是docs插件的伴侣,因此它与ArgsTable接口,其本身旨在自动提取组件的propTypesdefaultProps (尽管我发现这不起作用)

在此处输入图像描述

So, with Knobs you define each prop (which you wish to be dynamic) yourself, manually, and this requires some more manual sync when your component changes and also more work, and also Knobs variables definitions might be scattered all across your story's file, where controls are all defined in one place, though the same "order" can also be done with Knobs , it does not enforces it (for good reasons).因此,使用Knobs ,您可以自己手动定义每个道具(您希望它是动态的),这需要在您的组件更改和更多工作时进行更多的手动同步,并且Knobs变量定义可能分散在您的故事文件中, controls都定义在一个地方,虽然相同的“顺序”也可以用Knobs完成,但它不会强制执行它(有充分的理由)。

If you want to have an interactive propTypes documentation for your components, then I suggest using controls with addon-docs , and I've been using knobs for years, but that's it, it's time to upgrade.如果你想为你的组件创建一个交互式propTypes文档,那么我建议使用带有addon-docs控件,而且我多年来一直在使用knobs ,但就是这样,是时候升级了。

If, for some reason, your component's propTypes where not auto-detected (in the story) then you can define then (with controls ) like so:如果由于某种原因,您的组件的propTypes没有自动检测到(在故事中),那么您可以定义 then (使用控件),如下所示:

import Alert from './';

export default {
    title: 'General/Alert',
    component: Alert,
    parameters: {
        controls: { expanded: true }, // Show full documentation for each property
    },
    argTypes: {
        type: {
            description: 'Alert.Types',
            defaultValue: Alert.Types.WARNING,
            table: {
                type: {
                    summary: 'string',
                },
                defaultValue: {
                    summary: Alert.Types.WARNING,
                },
            },
            options: Alert.Types,
            control: {
                type: 'select', // for selecting between the array of options above
            },
        },

        title: {
            defaultValue: '',
            table: {
                type: {
                    summary: 'string',
                },
            },
            description: 'An optional title',
            control: {
                type: 'text',
            },
        },

        onClose: {
            table: {
                type: {
                    summary: 'func',
                },
            },
            description: '× button click callback',
            control: { type: null },
        },

        children: {
            description: 'The message body (mandatory)',
            type : {
                required: true,
            },
            table: {
                type: {
                    summary: 'node',
                },
            },
            control: { type: null },
        },
    },
}

//...export your story...

Notes:笔记:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM