Storybook: Can't get props for addon-info when using decorator (higher order component) on my component

Created on 25 Aug 2017  路  17Comments  路  Source: storybookjs/storybook

Hello

I wanted to use addon-info to get props in stories, but I got something like this in PropTypes section:
"withTheme(withStyles(Avatar))" Component
No propTypes defined!
Well, that's obvious for me why it's like this, but I would like to get props for Avatar, not its HOC. I couldn't find how to do it in readme, neither in issues.
How can I achieve this?

info has workaround inactive question / support

Most helpful comment

Here is a workaround I used in my project

// ...
const Button = (props) => {
   return (
    <button}>Click me</button>
  );
};

Button.propTypes = {
  primary: PropTypes.bool,
  secondary: PropTypes.bool,
};

export const ButtonComponent = Button;
export default withStyles(styles)(Button);

And now my story will look like

// ...
import Button, { ButtonComponent } from '../src/Button';

storiesOf('Button', module)
  .add('Simple', withInfo({
    text: 'Simple button description',
    source: false,
    propTables: [ButtonComponent], // display propTable for Button component
    propTablesExclude: [Button], // do not display propTable for HOC
  })(() => <Button primary>Primary Button</Button>));

All 17 comments

Are those HOCs your own, or are they taken from some library like recompose? If the former, you may want to extend the output with static methods from input:

function withSomething(ComposedComponent) {
  const Component = ...
  return Object.assign(Component, ComposedComponent)
}

Unfortunately for me they are from libraries, in my case these are react-intl and material-ui (v1).

@michalg42 There're two ways to get props in your case.

  1. You can simply add everything you need into propTables option like this:
withInfo({
      text: 'doc string about my component',
      propTables: [Avatar],
    })
  1. You can separate your component from HOC. It means you can add High Order Components via addDecorator and then apply withInfo to your <Avatar> and add it as a story. I guess it's more "Storybook" way.

If you're using Material-UI library you might try to use this Storybook addon:
https://github.com/react-theming/storybook-addon-material-ui
It does exactly what I described in the second point: wraps your own components via decorator

I'm getting an issue when manually specifying propTables where the prop descriptions aren't passed through. The tables themselves are correct, just no comments are being parsed.

E: Have been doing some more investigation today and I don't think it's related to specifying propTables, have no idea why __docgenInfo isn't being generated in this one case, will keep experimenting.

@loliver I have the same issue with components that have linked a style sheet (export default withStyles(styles)(NavigationMenu)) and then manually adding to propTables.

Maybe you have some thoughts how to resolve this?

@loliver me too :(
is there any luck getting the description in a wrapped component?

I wasn't able to find anything further, we've decided to stop using this addon for any documentation going forwards as it doesn't quite do what we need unfortunately.

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 60 days. Thanks!

Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

Here is a workaround I used in my project

// ...
const Button = (props) => {
   return (
    <button}>Click me</button>
  );
};

Button.propTypes = {
  primary: PropTypes.bool,
  secondary: PropTypes.bool,
};

export const ButtonComponent = Button;
export default withStyles(styles)(Button);

And now my story will look like

// ...
import Button, { ButtonComponent } from '../src/Button';

storiesOf('Button', module)
  .add('Simple', withInfo({
    text: 'Simple button description',
    source: false,
    propTables: [ButtonComponent], // display propTable for Button component
    propTablesExclude: [Button], // do not display propTable for HOC
  })(() => <Button primary>Primary Button</Button>));

Another solution is to add the propTypes after the HOC

// ...
const Button = (props) => {
   return (
    <button}>Click me</button>
  );
};

const HOC = withStyles(styles)(Button);

HOC.propTypes = {
  primary: PropTypes.bool,
  secondary: PropTypes.bool,
};

export default HOC;

I have tried @zhukmj and as I am using Flow, it just works partially and is not able to get all the information.

screen shot 2018-08-14 at 18 48 58

I think this issue should still be opened, especially as CSSinJS becomes more popular

Yeah I think the same, using styled-compoents will result in

<Styled(styled.button) onClick={clicked}>
  Button
</Styled(styled.button)>

Hi everyone !
I encountered the same issue using the memo optimization HOC for functional component. Do we still have any news on this ?

Another solution is to add the propTypes after the HOC

// ...
const Button = (props) => {
   return (
    <button}>Click me</button>
  );
};

const HOC = withStyles(styles)(Button);

HOC.propTypes = {
  primary: PropTypes.bool,
  secondary: PropTypes.bool,
};

export default HOC;

Props end up not being passed at all :/

@zhukmj's workaround works, but how can I get rid of the unnecessary HOC propTable? I see he is using propTablesExclude to exclude the HOC propTable in his story, but when I use that, it has no effect and is still visible. It appears that the propTablesExclude parameter does not work on HOC components. Any suggestions?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

levithomason picture levithomason  路  3Comments

zvictor picture zvictor  路  3Comments

arunoda picture arunoda  路  3Comments

shilman picture shilman  路  3Comments

shilman picture shilman  路  3Comments