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

Docs default props aren't working with ES6 default values #9626

Closed
smithambera opened this issue Jan 24, 2020 · 18 comments
Closed

Docs default props aren't working with ES6 default values #9626

smithambera opened this issue Jan 24, 2020 · 18 comments

Comments

@smithambera
Copy link

Describe the bug
I'm using ES6 default values as opposed to defaultProps because the React team is deprecating defaultProps in functional components (facebook/react#16210). The ES6 defaults are not showing up in the auto-generated Storybook docs.

This code:

const Tag = ({ title = 'Beta' }) => <TagWrapper>{title}</TagWrapper>;

results in these docs:

Screen Shot 2020-01-24 at 11 10 40 AM

While this code:

const Tag = ({ title }) => <TagWrapper>{title}</TagWrapper>;

Tag.defaultProps = {
  title: 'Beta'
};

results in these docs:

Screen Shot 2020-01-24 at 11 11 28 AM

To Reproduce
Create a functional component and use ES6 defaults to set default props. Look at the docs.

Expected behavior
See above.

Screenshots
See above.

Code snippets
See above.

System:

  System:
    OS: macOS Mojave 10.14.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 12.9.1 - /usr/local/bin/node
    Yarn: 1.17.3 - /usr/local/bin/yarn
    npm: 6.10.3 - /usr/local/bin/npm
  Browsers:
    Chrome: 79.0.3945.130
    Safari: 13.0.4
  npmPackages:
    @storybook/addon-a11y: ^5.2.8 => 5.2.8
    @storybook/addon-actions: ^5.3.2 => 5.3.2
    @storybook/addon-docs: ^5.3.0-rc.14 => 5.3.0-rc.14
    @storybook/addon-knobs: ^5.3.3 => 5.3.3
    @storybook/addon-links: ^5.3.2 => 5.3.2
    @storybook/addon-viewport: ^5.3.2 => 5.3.2
    @storybook/addons: ^5.3.2 => 5.3.2
    @storybook/preset-create-react-app: ^1.5.1 => 1.5.1
    @storybook/react: ^5.3.2 => 5.3.2

Additional context
That's all I've got!

@shilman
Copy link
Member

shilman commented Jan 25, 2020

Thanks @smithambera. We'll be doing an overhaul of the props handling code in 6.0 and hopefully can handle this case.

FYI, unrelated to this problem, but please upgrade your @storybook/* packages to be the same version, or you might see weird behaviors.

@smithambera
Copy link
Author

smithambera commented Jan 26, 2020

Thanks for responding so quickly, @shilman! I'm looking forward to 6.0 😄 and thanks for the tip, too - upgraded the packages!

shilman added a commit that referenced this issue Feb 14, 2020
@shilman
Copy link
Member

shilman commented Feb 14, 2020

This appears to be working in an isolated test: 3cfe6e1

@budalen
Copy link

budalen commented Feb 17, 2020

ES6 default values worked for me as well, but when I started using React.forwardRef the default props stopped working.

Using the same example as above - this does not work:

const Tag = React.forwardRef(({ title = 'Beta' }, ref) => <TagWrapper ref={ref}>{title}</TagWrapper>);

This works:

const Tag = React.forwardRef(({ title = 'Beta' }, ref) => <TagWrapper ref={ref}>{title}</TagWrapper>);

Tag.defaultProps = {
  title: 'Beta'
};

@shilman
Copy link
Member

shilman commented Feb 17, 2020

@Jaybud #8894 lots of different cases like this that we'll work through with react-docgen over the next couple months 🤕

@budalen
Copy link

budalen commented Feb 17, 2020

@shilman Gotcha, thanks for the quick response!

@thany
Copy link

thany commented Feb 28, 2020

@shilman

This appears to be working in an isolated test: 3cfe6e1

What if you write it like this:

export const Tag = (props) => {
  const { title = 'Beta' } = props;
  return React.createElement("div", null, title);
};
export const component = Tag;

Or even:

export const Tag = (props) => {
  return <div {...props />;
};
export const component = Tag;

It might be neccesary to maintain access to the full props object, for example for passing it on to a child component. How will storybook know which are the default values?

@stale
Copy link

stale bot commented Mar 20, 2020

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!

@stale stale bot added the inactive label Mar 20, 2020
@shilman
Copy link
Member

shilman commented Mar 21, 2020

@thany

Tag.defaultValues = {
  foo: 'bar',
  ...
}

@stale stale bot removed the inactive label Mar 21, 2020
@thany
Copy link

thany commented Mar 24, 2020

@shilman That will add runtime overhead, because at runtime, defaultValues is pointless.

@shilman
Copy link
Member

shilman commented Mar 25, 2020

@thany taking a very different approach in 6.0 that will address this issue

@stale
Copy link

stale bot commented Apr 17, 2020

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!

@stale stale bot added the inactive label Apr 17, 2020
@shilman shilman modified the milestones: 6.0, 6.0 docs May 8, 2020
@stale stale bot removed the inactive label May 8, 2020
@stale
Copy link

stale bot commented May 30, 2020

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!

@stale stale bot added the inactive label May 30, 2020
@shilman
Copy link
Member

shilman commented Jun 3, 2020

@thany new customization options documented here for 6.0. will get a few tweaks before final release, but hopefully nothing big: https://github.com/storybookjs/storybook/blob/next/addons/docs/docs/props-tables.md#customizing-argtypes

@stale stale bot removed the inactive label Jun 3, 2020
@shilman shilman removed this from the 6.0 docs milestone Jun 24, 2020
@shilman shilman added this to the 6.1 docs milestone Jun 24, 2020
@stale
Copy link

stale bot commented Jul 18, 2020

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!

@stale stale bot added the inactive label Jul 18, 2020
@stale
Copy link

stale bot commented Aug 22, 2020

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!

@jpmarra
Copy link

jpmarra commented Jul 20, 2021

@shilman Whats the solution here? I'm not seeing a way for Controls to register default props via es6 defaults.

@shilman
Copy link
Member

shilman commented Jul 21, 2021

@jpmarra starting in 6.0 the ArgsTable is completely configurable. If docgen doesn't successfully pick up your defaults, you can add them using args/argTypes. This was the big change I alluded to in my previous concept -- Args/ArgTypes didn't exist when the issue was filed. https://storybook.js.org/docs/react/essentials/controls

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

5 participants