Storybook 自动生成 React 组件的文档/属性描述的问题

发布于 2025-01-18 19:52:51 字数 1725 浏览 2 评论 0原文

我的是一个较旧的存储库(想想Webpack 3.*)。我在回购中添加了故事书,并为我的.scss文件修改了一些WebPack规则以正确加载。

我可以让故事书控件正确加载。但是,文档部分中的argstable不尊重我在proptypes中给出的评论。

这是我的组件 - newpill.js

import styles from './NewPill.scss';

const NewPill = props => {
    const { text, color } = props;
    return <span className={`${styles.newPill} ${color}`}> {text} </span>;
};

NewPill.propTypes = {
    /**
     * Denotes the text of the pill
     */
    text: PropTypes.string,
    /**
     * Denotes the color of the pill
     */
    color: PropTypes.string,
};

export default NewPill;

这是我的故事 - newpill.stories.js

import NewPill from './NewPill';

export default {
    title: 'Components/NewPill',
    component: NewPill,
};

const Template = args => {
        console.log(NewPill.__docgenInfo); // Gives me undefined
    return <NewPill {...args} />;
};

export const FirstStory = Template.bind({});
FirstStory.args = {
    text: 'Pill Text',
    color: 'blue',
};

,这是输出,

“

显然,实际上是为故事本身添加argtypes

FirstStory.argTypes = {
    text: {
           control: { type: 'text' },
           description: 'Denotes the text of the pill',
        }
    color: 'blue',
};

但是,如果故事书系统从原始组件本身获取该信息,那就太好了。有没有办法查看babel-plugin-react-docgen是否有效?我按照他们的说明在这里

Mine is a older repo (think webpack 3.*). I added storybook to the repo and modified a few of the webpack rules for my .scss files to load properly.

I can get the Storybook controls to load properly. But the ArgsTable in the docs section does not respect my comments that I gave in propTypes.

Here is my component - NewPill.js

import styles from './NewPill.scss';

const NewPill = props => {
    const { text, color } = props;
    return <span className={`${styles.newPill} ${color}`}> {text} </span>;
};

NewPill.propTypes = {
    /**
     * Denotes the text of the pill
     */
    text: PropTypes.string,
    /**
     * Denotes the color of the pill
     */
    color: PropTypes.string,
};

export default NewPill;

Here is my story - NewPill.stories.js

import NewPill from './NewPill';

export default {
    title: 'Components/NewPill',
    component: NewPill,
};

const Template = args => {
        console.log(NewPill.__docgenInfo); // Gives me undefined
    return <NewPill {...args} />;
};

export const FirstStory = Template.bind({});
FirstStory.args = {
    text: 'Pill Text',
    color: 'blue',
};

And this is the output,

image

The workaround is obviously to add argTypes to the story itself.

FirstStory.argTypes = {
    text: {
           control: { type: 'text' },
           description: 'Denotes the text of the pill',
        }
    color: 'blue',
};

But it'd be nice if the storybook system grabs that info from the original component itself. Is there a way to see if the babel-plugin-react-docgen worked? I followed their instructions here with no luck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2025-01-25 19:52:51

我有完全相同的问题。问题出在您使用的版本上。
将 Storybook 降级到 v 6.3.0 解决了我的问题。

PS:必须逐一降级软件包(像这样 npx [email  ;protected] init 无法按预期工作)。

I had exact same issue. The problem is in version you use.
Downgrading Storybook to v 6.3.0 solved the issue in my case.

PS: It was mandatory to downgrade packages one by one (smth like that npx [email protected] init doesn't work as expected).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文