Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Storybook react-docgen-typescript-loader to use typescript props

I'm trying to use react-docgen-typescript-loader to generate my props documentation in Storybook with my TypeScript Props, but it's not populating anything into the withInfo addon.

I'm using the TypeScript flavor of create-react-app and I'm following multiple different methods of configuring the .storybook/webpack.config.js and nothing seems to work.

Here's what my current config is:

.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);

stories/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;

There are currently no issues with this configuration, but I only see this as the info output in Storybook: storybook screenshot

like image 832
fentech Avatar asked Oct 16 '22 14:10

fentech


1 Answers

There is some difference in using named imports, this worked for me:

import React, {FC} from 'react' ...

const Button: FC<Props> = (props) => {

like image 79
einaralex Avatar answered Nov 15 '22 06:11

einaralex