【发布时间】:2019-10-23 08:52:17
【问题描述】:
我正在尝试使用 react-docgen-typescript-loader 在 Storybook 中使用我的 TypeScript Props 生成我的道具文档,但它没有将任何内容填充到 withInfo 插件中。
我正在使用 typeScript 风格的 create-react-app 并且我正在使用多种不同的方法来配置 .storybook/webpack.config.js,但似乎没有任何效果。
这是我当前的配置:
.storybook/webpack.config.js
module.exports = ({ config, mode }) => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('babel-loader'),
options: {
presets: [['react-app', { flow: false, typescript: true }]],
}
},
require.resolve("react-docgen-typescript-loader"),
]
});
config.resolve.extensions.push('.ts', '.tsx');
return config;
};
.storybook/config.ts
import { configure } from '@storybook/react';
// automatically import all files ending in *.stories.js
const req = require.context('../', true, /.stories.tsx$/);
function loadStories() {
req.keys().forEach(filename => req(filename));
}
configure(loadStories, module);
故事/button.stories.tsx
import * as React from 'react';
import { storiesOf } from '@storybook/react';
import { withInfo } from '@storybook/addon-info';
import Button from '../src/components/Button';
storiesOf('Button', module)
.addDecorator(withInfo)
.add('continue', () => <Button buttonType="submit">Hello Button</Button>, { info: { inline: true } })
.add('back', () => <Button buttonType="reset">Hello Button</Button>, { info: { inline: true } });
src/components/Button.tsx
import React from 'react';
interface Props {
buttonType: Button.Type;
}
const Button: React.FunctionComponent<Props> = (props) => {
const getStyles = (buttonType: string): {color: string} => {
if (buttonType === 'reset') {
return { color: 'red' };
}
if (buttonType === 'submit') {
return { color: 'green' };
}
return { color: 'green' };
};
const { buttonType, children } = props;
return <button type={buttonType} style={getStyles(buttonType)}>{children}</button>;
};
export default Button;
【问题讨论】:
-
作为一个维护大型打字稿/反应故事书的人,react-docgen-typescript 不幸地使编译时间变得非常缓慢,因此我建议您只为您的道具编写文档,这也是允许您编写描述等。
-
@ShanonJackson 这就是我害怕我必须做的事情。感谢您的洞察力!
-
没问题,我知道不得不手动做事情听起来很痛苦(因为我们是程序员,我们喜欢自动化事情)但它确实不是那么糟糕,不值得像慢 50 倍的编译器时间那样进行交易
-
您可以使用 jsdoc 注释格式向道具添加描述。查看代码imgur.com/G4zGPFt 和输出imgur.com/nhHePXt
标签: reactjs typescript create-react-app storybook