Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
michalg42 opened this issue Aug 25, 2017 · 17 comments

Comments

@michalg42
Copy link

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?

@Hypnosphi
Copy link
Member

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)
}

@michalg42
Copy link
Author

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

@usulpro
Copy link
Member

usulpro commented Aug 25, 2017

@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

@loliver
Copy link

loliver commented Oct 3, 2017

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.

@yaminyaylo
Copy link

yaminyaylo commented Oct 18, 2017

@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?

@baktiaditya
Copy link
Contributor

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

@loliver
Copy link

loliver commented Nov 29, 2017

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.

@stale
Copy link

stale bot commented Jan 25, 2018

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!

@stale
Copy link

stale bot commented Feb 9, 2018

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!

@stale stale bot closed this as completed Feb 9, 2018
@zhukmj
Copy link

zhukmj commented Feb 26, 2018

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

@spencerdcarlson
Copy link

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;

@PolGuixe
Copy link

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

@eddiemonge
Copy link
Contributor

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

@Itrulia
Copy link

Itrulia commented Oct 31, 2018

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

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

@mlnor27
Copy link

mlnor27 commented Nov 17, 2018

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

@mlnor27
Copy link

mlnor27 commented Nov 18, 2018

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 :/

@typosadovsky
Copy link

@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?

steven-steven added a commit to steven-steven/melonbun-web that referenced this issue Mar 31, 2019
1. Installed Storybook addons, and loaders in webpack.config.js

2. Uses react-docgen-typescript-loader to load propTable for the 'Info' addon

3. Renamed AppBar to NavBar so not to be confused with AppBar from Material-ui

4. Allow 'Info' addon to display propTypes of HOC-wrapped component
*Resolution 1:
- workaround suggested from storybookjs/storybook#1735.
- export the original component before it was wrapped with MUI's withStyle()
*Issue 2:
- downside is 'classes' is undefined without withStyle(). Throws ts error in Storybook.
*Resolution 2:
- Added defaultProps object defined for 'classes'. This is unnecessary if we don't use Storybook's Info addon to display propTable

5. Created Stories for AppBar and Drawer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests