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

Addon-docs: Hide React default props in source block #13003

Merged
merged 1 commit into from
Nov 4, 2020

Conversation

zeckdude
Copy link
Contributor

@zeckdude zeckdude commented Nov 3, 2020

Issue: #11510

What I did

Added option to not show default props on code preview when a component isn't overriding those values. In jsxDecorator.tsx, it is using the reactElementToString method from the react-element-to-jsx-string package. If you check their options, they accept a showDefaultProps property, which when set to false omits the default props in the JSX. This sets that to false so it doesn't show the default props by default.

How to test

  • Is this testable with Jest or Chromatic screenshots?
    Yes. I added a test case, but it looks like it is failing due to a snapshot. I'm not familiar with snapshots, so I could use some help there please.
  • Does this need a new example in the kitchen sink apps?
    No
  • Does this need an update to the documentation?
    Yeah I think so, but I'm struggling determining where that would be added the best. Any advice?

If your answer is yes to any of these, please make sure to include it in your PR.

@shilman shilman changed the title Disable showing default props in code preview Addon-docs: Hide React default props in source block Nov 4, 2020
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for tracking this down @zeckdude

@shilman shilman merged commit ff57dd1 into storybookjs:next Nov 4, 2020
@Greenheart
Copy link

Thank you for fixing this @zeckdude! This has been one of the few issues my team have had with Storybook since we upgraded to v6, and thanks to your PR we'll get a really smooth DX with relevant code samples in docs.

Keep up the good work with the 6.1.0 release! ✨

@shleewhite
Copy link

@zeckdude does this work with typescript? instead of setting Component.defaultProps, we just depend on the type.

@zeckdude
Copy link
Contributor Author

zeckdude commented Feb 9, 2021

@shleewhite It currently has nothing to do with TS but I'm sure that could be included. Which type would it be looking for to determine that it is a component?

@shleewhite
Copy link

shleewhite commented Feb 9, 2021

@zeckdude our components are written like this:

export type componentProps = {
  propA?: string,
};

export const myComponent = React.functionComponent<componentProps>(
  {
       propA = 'Sample Text',
  }
) => (
   <div>{propA}</div>
)

So I'd want the source code for the story to just show:<myComponent /> if I don't pass a value.

@victor-grajski
Copy link

+1 to @shleewhite !

@shilman shilman added this to the 6.4 PRs milestone Oct 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants