question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Addon Docs - build prop table from dynamic propTypes?

See original GitHub issue

Describe the bug

Using the Addon-Docs, I am not being able to get a prop table from a component that has dynamic propTypes. By dynamic propTypes I mean: propTypes built by destructuring objects into the propTypes object rather than defining static properties, which is what we normally do.

This approach is used by styled-systems and similar libraries.

To Reproduce

Create a component with dynamic propTypes (like the one below) in a React application with storybook and Addon-Docs and try to get the prop table for that component with the dynamic/destructured propTypes in it.

What happened when I tried was:

  • static properties added in the propTypes are displayed as expected;
  • dynamic/destructured propTypes properties are not displayed in the props table;
  • also, in my tests, props destructured in the arguments show up on the prop table if they have default values. So, in the case below, prop2 is displayed even if I omit it from the propTypes, but prop1 isn’t, apparently due to not having a default value. Adding a default value to it get it to display;

Code snippet

import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import {space, color} from 'styled-system';
import propTypes from '@styled-system/prop-types';

const StyledDiv = styled.div`
  ${space}
  ${color}
`;

const Box = ({prop1, prop2='default_value', ...otherProps}) => (
  <StyledDiv
    prop1={prop1}
    prop2={prop2}
    {...otherProps}
  />
);

Box.propTypes = {
  prop1: PropTypes.any,
  prop2: PropTypes.any,
  ...propTypes.space,
  ...propTypes.color
};

export default Box;

Expected behavior

All props in propTypes to display in the prop table, including the dynamic ones added via destructuring.

System:

Environment Info:

System: OS: Linux 4.4 Ubuntu 16.04.3 LTS (Xenial Xerus) (actually using windows WSL) CPU: (4) x64 Intel® Core™ i5-5300U CPU @ 2.30GHz Binaries: Node: 10.19.0 - ~/.nvm/versions/node/v10.19.0/bin/node Yarn: 1.17.3 - /mnt/c/Program Files (x86)/Yarn/bin/yarn npm: 6.13.4 - ~/.nvm/versions/node/v10.19.0/bin/npm npmPackages: @storybook/addon-a11y: ^5.3.18 => 5.3.18 @storybook/addon-actions: ^5.3.18 => 5.3.18 @storybook/addon-docs: ^5.3.18 => 5.3.18 @storybook/addon-knobs: ^5.3.18 => 5.3.18 @storybook/addon-links: ^5.3.18 => 5.3.18 @storybook/addon-storysource: ^5.3.18 => 5.3.18 @storybook/addon-viewport: ^5.3.18 => 5.3.18 @storybook/addons: ^5.3.18 => 5.3.18 @storybook/react: ^5.3.18 => 5.3.18

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

11reactions
yairEOcommented, Jun 2, 2021

This makes storybook automatic ArgsTable not detect any propTypes, highly likely for the majority of serious projects, where proptypes are not all statically written but imported and also dynamically-built.

For Docs to be truly beneficial, automatic propTypes detection must be bulletproof. If react-docgen does not supply the goods, maybe another package does, and if not, then I suggest writing one from scratch. Solving automatic proptypes documentation is quite an important task.

It is extremely unreliable & hassle for developers to manually maintain both the “real” propTypes (on the components themselves) and in another file, only for Storybook presentation, especially for large projects with dozens of developers at different skill levels

7reactions
lobaakcommented, Oct 29, 2020

This is still an issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Addon Docs - build prop table from dynamic propTypes ... - Issuehunt
Create a component with dynamic propTypes (like the one below) in a React application with storybook and Addon-Docs and try to get the...
Read more >
Storybook: addons/docs/docs/props-tables.md - Fossies
Storybook Docs automatically generates props tables for components in supported frameworks. This document is a consolidated summary of prop tables, ...
Read more >
ArgsTable - Storybook - JS.ORG
These tables list the arguments (args for short) of the component, and even integrate with controls to allow you to change the args...
Read more >
How To Enable Prop-Types In Production For A React Storybook For ...
How To Enable Prop-Types In Production For A React Storybook For The Docs Addon. All without needing to stand up a server or...
Read more >
How to enable prop-types in production for a React Storybook ...
Storybook has an addon called Docs that detects the prop-types for components and creates a table of the prop-types for easy to read ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found