storybookjs / storybook

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation
https://storybook.js.org
MIT License
84.04k stars 9.24k forks source link

No props appearing for named function export with Addon-docs #9205

Closed LiteMorgan closed 1 year ago

LiteMorgan commented 4 years ago

Describe the bug Been trying various things to get the props to appear for my component library with no luck. This is using the 5.3.0-rc.0 version of Storybook and Addon-docs.

I've been trying with both js and mdx file types and seeing no success. A named function export, with props and comments, renders in the Docs file with "No props found for this component".

I've set up the MDX rules following the Manual configuration guide on the README.

Expected behavior Props should be populated in Docs.

Code snippets Component:

import React from 'react'
import PropTypes from 'prop-types'

export const Dummy = ({
  textProp
}) => {
  return (
    <div>
      { textProp }
    </div>
  )
}

Dummy.propTypes = {
  /**
   * Test code export
   */
  textProp: PropTypes.string.isRequired,
}

MDX:

import { Meta, Props, Preview, Story } from '@storybook/addon-docs/blocks'
import { Dummy } from './'

<Meta
  title="mdx/dummy"
  component={Dummy}
/>

# Test

<Preview>
  <Story name="testing">
    <Dummy textProp="Hello world" />
  </Story>
</Preview>

## Props

<Props of={Dummy} />

Screenshots Screenshot 2019-12-20 at 09 52 22

System:

  System:
    OS: macOS 10.15.1
    CPU: (4) x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
  Binaries:
    Node: 11.13.0 - ~/.nvm/versions/node/v11.13.0/bin/node
    Yarn: 1.21.1 - ~/.yarn/bin/yarn
    npm: 6.7.0 - ~/.nvm/versions/node/v11.13.0/bin/npm
  Browsers:
    Chrome: 79.0.3945.88
    Safari: 13.0.3
  npmPackages:
    @storybook/addon-a11y: ^5.3.0-rc.0 => 5.3.0-rc.0 
    @storybook/addon-contexts: ^5.3.0-rc.0 => 5.3.0-rc.0 
    @storybook/addon-docs: ^5.3.0-rc.0 => 5.3.0-rc.0 
    @storybook/addon-knobs: ^5.3.0-rc.0 => 5.3.0-rc.0 
    @storybook/addon-links: ^5.3.0-rc.0 => 5.3.0-rc.0 
    @storybook/addon-viewport: ^5.3.0-rc.0 => 5.3.0-rc.0 
    @storybook/addons: ^5.3.0-rc.0 => 5.3.0-rc.0 
    @storybook/preset-scss: ^1.0.2 => 1.0.2 
    @storybook/react: ^5.3.0-rc.0 => 5.3.0-rc.0 
    @storybook/theming: ^5.3.0-rc.0 => 5.3.0-rc.0
shilman commented 4 years ago

Are you using CRA? If so, have you tried the standalone preset? https://github.com/storybookjs/presets/tree/master/packages/preset-create-react-app

Is babel-plugin-react-docgen being applied when you yarn storybook --debug-webpack?

LiteMorgan commented 4 years ago

Not using CRA, as this is a Components-only library (is bundled and then exported for use in a different project).

babel-plugin-react-docgen appears under the test: /\.(mjs|jsx?)$/ section, as an option for babel-loader.

[ '/.../@storybook/react/node_modules/babel-plugin-react-docgen/lib/index.js',
   { DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES' } ]
Zunaib commented 4 years ago

Try doing this, got out of the same problem.

import React, {  FC } from 'react';
import PropTypes from 'prop-types'

export interface DummyProps extends HTMLAttributes<HTMLElement> {
textProp:string;
 };

export const Dummy: FC<DummyProps > = function ({ textProp }) {
 return (
    <div>
      { textProp }
    </div>
  );
};

Dummy.propTypes = {
  /**
   * Test code export
   */
  textProp: PropTypes.string.isRequired,
};

export default Dummy;
LiteMorgan commented 4 years ago

That seems a little excessive just to get Prop definitions working within Storybook - I was under the impression standard React could be used, rather than having to double up on type definitions for every Component.

@shilman has there been any update on this? I've updated to 5.3.0-rc.9 and I'm still facing the same issue.

shilman commented 4 years ago

@getignited Do you have a reproduction?

LiteMorgan commented 4 years ago

@shilman I'll put something together for you.

LiteMorgan commented 4 years ago

@shilman I've managed to reproduce that correctly here: https://github.com/getignited/storybook-docs-test

shilman commented 4 years ago

@getignited

I need to jump onto something else, but maybe that gets you on the right path?

LiteMorgan commented 4 years ago

@shilman I'll take a look at that, thank you. When I was setting that reproduction up, it definitely worked for a bit, until some of the Babel stuff started coming into play. Just got to figure out which one is upsetting it, I suppose!

andreasabilio commented 4 years ago

We've also run into this issue. Indeed babel-plugin-react-docgen seems not to be running - eventhough it is present in the config object outputted by --debug-webpack.

Furthermore, it works with a dummy React component but doesn't work with Semantic UI React (SUIR) components... And all SUIR components do have a propTypes property.

When a SUIR component reaches the getDocgenSection function it lacks the __docgenInfo property.

This is on Storybook v5.3.3 and v5.3.6.

Hope it helps with the debugging :-)

stale[bot] commented 4 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

AlexMold commented 4 years ago

@shilman I have the same problem with CRA .storybook/main.js

module.exports = {
    presets: [
        {
            name: 'storybook-addon-deps/preset',
            options: {
                exclude: /^@babel/,
            },
        },
    ],
    stories: ['../src/**/*.stories.(js|mdx)'],
    addons: [
        '@storybook/preset-create-react-app',
        '@storybook/addon-actions',
        '@storybook/addon-links',
        '@storybook/addon-knobs/register',
        '@storybook/addon-storysource',
        '@storybook/addon-backgrounds/register',
        '@storybook/addon-docs',
    ],
}

I have a component

import React from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import {capitalize} from 'util/capitalize'
import styles from './styles/Button.module.sass'

const baseClassName = 'Button'

export const ButtonView = React.memo(Button)

function Button({children, classes = {root: styles[baseClassName]}, className, color, ...other}) {
    return (
        <button
            className={classNames(
                classes.root,
                {
                    [styles[`${baseClassName}${capitalize(color)}`]]: color !== 'default',
                },
                className,
            )}
            {...other}
        >
            {children}
        </button>
    )
}

Button.defaultProps = {
    color: 'default',
}

Button.propTypes = {
    children: PropTypes.node.isRequired,
    color: PropTypes.oneOf(['default', 'primary']),
    classes: PropTypes.object,
}

Screenshot

Screen Shot 2020-02-11 at 08 42 15

File Structure

Screen Shot 2020-02-11 at 08 59 45

In components, I'm using named exports and reexports if it matters

soulfresh commented 4 years ago

I've noticed this same issue on a couple projects and was just able to track it down to the CRA preset. The props table works if I remove @storybook/preset-create-react-app in main.js. Removing this doesn't seem to have any other impact on my storybook (though error messages note this will break in the future).

Here's my config setup:

// main.js
var path = require('path');

module.exports = {
  stories: ['../src/**/*.stories.mdx'],
  addons: [
    // '@storybook/preset-create-react-app',
    '@storybook/addon-actions',
    '@storybook/addon-storysource',
    '@storybook/addon-knobs',
    '@storybook/addon-a11y',
    '@storybook/addon-docs',
  ],
  webpackFinal: (config) => {
    config.resolve.alias = {
      '~': path.resolve(__dirname, '../src/'),
    };

    return config;
  },
};

I also have a preview.js file that imports a global stylesheet but I doubt that has any impact here.

shilman commented 4 years ago

cc @mrmckeb

anusky commented 4 years ago

Same problem here. This is my main.js configuration. image I'm currently using version 5.3.13 of storybooks and addons.

mrmckeb commented 4 years ago

@soulfresh, are you able to supply a simple repro repo? I can then take a look in context and see what's happening.

We have this working with the preset in the examples folder in that project, so it might be something else conflicting.

stale[bot] commented 4 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

Dashue commented 4 years ago

Another one having the same issue here. It was working recently

shilman commented 4 years ago

@Dashue do you have a repro?

Dashue commented 4 years ago

@shilman Hey Michael, thanks for your time. I spent yesterday digging through bugs, issues and discussions. I was both wrapping my components in a hoc (to apply consistent styling to all my components), which involved generics, this was working until I moved the hoc to it's own file for re-use.

I've solved it by exporting one component to feed to the docs addon and another to consume and use in the story itself. I also upgraded to 6.0 but I believe this approach would work for 5x as well.

I.e

export const _heading = () => ...
export const Heading = ComponentBaseHoc<TProps>(_heading)

Story:

export default {
  title: 'UI | Type / Heading',
  component: _heading,
}

export const HeadingWithTitle = () => <Heading title='my title' />
thehulke commented 4 years ago

@getignited Hey, Have you tried import * as React from 'react' in your component?

stale[bot] commented 4 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

Dashue commented 4 years ago

I would love to help out with this. I'm currently in the 6 alpha, but still using the old 1.2.2 preset. As soon upgrade to 2 or 3 my props disappear

stale[bot] commented 4 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

tabrez96 commented 4 years ago

I had a similar issue, I had forgotten to add 'Component' attribute in the story's export.


export default {
  title: 'Playground|Components/Button',
  decorators: [/** Your decorators */],
  component: Button (/** Your component name */),
};```
stale[bot] commented 4 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

Horsty80 commented 4 years ago

Hi all, i've a similar issue, and i didn't know how to fix it ? Any suggestion ?

//main.js

module.exports = {
  stories: ["../src/**/*.stories.js"],
  addons: [
    "@storybook/preset-create-react-app",
    "@storybook/addon-actions",
    "@storybook/addon-links",
    "@storybook/addon-docs",
  ],
};
addParameters({
  docs: {
    inlineStories: true,
    container: DocsContainer,
    page: DocsPage,
  },
});
storiesOf("Test", module)
  .addParameters({ component: Test })
  .add("primary", () => <Test text={primaryButton.text} />);
import React from "react";
import PropTypes from "prop-types";

const Test = ({ text }) => {
  return <button>{text}</button>;
};
Test.defaultProps = {
  text: "Create",
};
Test.propTypes = {
  text: PropTypes.string,
};

export default Test;
shilman commented 4 years ago

We've overhauled props handling in 6.0, can somebody who's having this issue try upgrading and LMK if you're still having the issue? https://github.com/storybookjs/storybook/issues/9311

Horsty80 commented 4 years ago

Hi @shilman , i've update to v6

  "devDependencies": {
    "@storybook/addon-actions": "^6.0.0-beta.37",
    "@storybook/addon-docs": "^6.0.0-beta.37",
    "@storybook/addon-links": "^6.0.0-beta.37",
    "@storybook/addons": "^6.0.0-beta.37",
    "@storybook/preset-create-react-app": "^3.1.2",
    "@storybook/react": "^6.0.0-beta.37",

But props it's steel not working and don't appear in docs tab. =/ And i've no error message just the sentence No inputs found for this component. Read the docs >

shilman commented 4 years ago

@Horsty80 do you have a public repro i can look at?

Horsty80 commented 4 years ago

Yes sure normally it's public, let me know if it's not https://gitlab.com/Horsty/horsty-components

here it's hosted test version of the repo https://happy-wiles-0180d2.netlify.app/?path=/docs/button--primary thanks for helping me ;)

Horsty80 commented 4 years ago

Hi @shilman any news of this bug ? =/ How can i help you to investigate on this ?

shilman commented 4 years ago

Took me awhile to track down with @mrmckeb and I'm not very satisfied with where we got. Try adding the following to your babel.config.js:

  const presets = ["@babel/preset-env", "@babel/preset-react"];

I modified a bunch of other things in the project in the process of debugging, but pretty sure that's the proper fix. WDYT?

Horsty80 commented 4 years ago

Thanks @shilman it's working fine ! i've my propsTypes showing.

So fix with :

EdenTurgeman commented 4 years ago

@shilman after upgrading to 6.0.0-rc.3 We couldn't get our typescript props to display even with a simple example component No inputs found for this component. Read the docs >

with this main.ts:

const path = require('path');

interface TypescriptOptions {
  check?: boolean;
  docgen?: 'none' | 'react-docgen' | 'react-docgen-typescript';
}

export const typescript: TypescriptOptions = {
  check: true,
  docgen: 'react-docgen-typescript'
};

module.exports = {
  typescript,
  stories: ['../stories/**/*.story.@(js|jsx|ts|tsx)'],
  addons: [
    {
      name: '@storybook/addon-docs',
      options: {
        sourceLoaderOptions: {
          rule: {
            include: [path.resolve(__dirname, '../stories')],
          },
          loaderOptions: {
            prettierConfig: { printWidth: 80, singleQuote: false },
          },
        },
      },
    },
    {
      name: '@storybook/addon-storysource',
      options: {
        rule: {
          include: [path.resolve(__dirname, '../stories')],
        },
        loaderOptions: {
          prettierConfig: { printWidth: 80, singleQuote: false },
        },
      },
    },
    '@storybook/addon-knobs/register',
]
};

preview.js:

import React from 'react';
import { bootstrap } from "@ui/styleguide";
import { addDecorator ,addParameters} from '@storybook/react';
import { DocsPage, DocsContainer } from '@storybook/addon-docs/blocks';
import { StoryContainer } from "../stories/storyComponents/StoryContainer";

addDecorator(Story =>
  <StoryContainer>
    <Story />
  </StoryContainer>
);

addParameters({
  docs: {
    container: DocsContainer,
    page: DocsPage,
  },
});

bootstrap();

Our example:

import React from 'react';
import { ClickThing } from './ClickThing';

export default {
  title: 'Components|Click',
  component: ClickThing,
};

export const Basic = ({ value = 5, showZero = true }) => <ClickThing {...{ value, showZero }} />;
import React, { FunctionComponent } from 'react';

export interface IClickProps {
  value?: number;
  maxDigits?: 1 | 2 | 3;
  showZero?: boolean;
  onClick?(value: number): void;
  subject?: string;
}

export const ClickThing: FunctionComponent<IClickProps> = ({
  value,
  maxDigits,
  showZero = false,
  onClick = () => {},
  children,
  subject,
}) => (
  <div>
    {value}
    <span>{showZero}</span>
    <div>{subject}</div>
  </div>
);
shilman commented 4 years ago

@EdenTurgeman do you have a repro?

EdenTurgeman commented 4 years ago

@shilman Unfortunately Not at this time... since we're working with a private repo. I might eventually try to create a public repo since we're having quite a few issues upgrading which we can't diagnose, is there a way I could give out more info considering our current state? (webpack debug info? custom webpack config?)

shilman commented 4 years ago

@EdenTurgeman standalone minimal repro is the best way. if you have a bunch of issues around a specific feature, like prop tables, a repo that demonstrates all those issues in one place is also great

stale[bot] commented 4 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

wolfhoundjesse commented 4 years ago

Just popping in to say that I created a minimum repro for you, but everything was working fine, so … .

In my own project, the props aren't showing up for a default export. If I change it to named (but won't, because code splitting) it works, and if I export it both ways (why is this allowed?), then I get props when I import it as a named component.

At any rate, thanks for taking the time!

Edit: Jeremy W. Sherman makes a point about using both.

stale[bot] commented 3 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

ianldgs commented 3 years ago

In my case is a Stencil component using the react integration.

No propTypes on the component, but there are TypeScript Props definitions.

export default {
  title: 'My Stencil Component',
  parameters: { notes: { markdown } },
  component: MyStencilComponent
};
stale[bot] commented 3 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

keyserfaty commented 3 years ago

Same issue here. Proptypes are not shown not even in the example provided by Storybook:

import React from 'react';
import PropTypes from 'prop-types';
import './button.css';

/**
 * Primary UI component for user interaction
 */
export const Button = ({ primary, backgroundColor, size, label, ...props }) => {
  const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';
  return (
    <button
      type="button"
      className={['storybook-button', `storybook-button--${size}`, mode].join(' ')}
      style={backgroundColor && { backgroundColor }}
      {...props}
    >
      {label}
    </button>
  );
};

Button.propTypes = {
  /**
   * Is this the principal call to action on the page?
   */
  primary: PropTypes.bool,
  /**
   * What background color to use
   */
  backgroundColor: PropTypes.string,
  /**
   * How large should the button be?
   */
  size: PropTypes.oneOf(['small', 'medium', 'large']),
  /**
   * Button contents
   */
  label: PropTypes.string.isRequired,
  /**
   * Optional click handler
   */
  onClick: PropTypes.func,
};

Button.defaultProps = {
  backgroundColor: null,
  primary: false,
  size: 'medium',
  onClick: undefined,
};

Docs:

Screen Shot 2021-02-13 at 14 17 25

Versions:

    "@storybook/addon-actions": "^6.1.17",
    "@storybook/addon-essentials": "^6.1.17",
    "@storybook/addon-links": "^6.1.17",
    "@storybook/node-logger": "^6.1.17",
    "@storybook/preset-create-react-app": "^3.1.5",
    "@storybook/react": "^6.1.17",
    "react": "^16.13.1",
keyserfaty commented 3 years ago

Nevermind! Can confirm that adding and installing the following solved the issue for me:

"presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ]
srt4rulez commented 3 years ago

On Storybook 6.1.21, I had the same issue as @keyserfaty, where it only showed a few props from the default "button" component that Storybook shipped with.

The issue was because I completely overwrote the module.rules array in webackFinal function of main.js with my own webpack config.

I instead spread storybooks rules first, then did my own webpack config:

const webpackConfig = require('./../webpack.config');

module.exports = {
    // ...
    webpackFinal: async (config) => {

        const appConfig = webpackConfig(); // my webpack config exports a function

        return {
            ...config,
            module: {
                ...config.module,
                rules: [
                    ...config.module.rules,
                    ...appConfig.module.rules,
                ],
            },
        };
    },
}