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

Display destructured props from separate files #548

Closed
reintroducing opened this issue Jul 21, 2017 · 5 comments
Closed

Display destructured props from separate files #548

reintroducing opened this issue Jul 21, 2017 · 5 comments
Labels
react-docgen Issues in react-docgen library

Comments

@reintroducing
Copy link
Contributor

In my UI component library I export components but I also export some PropTypes. Here is an example:

IconPropTypes.js

import PropTypes from 'prop-types';

export default {
    /** Additional class(es) to add to the component. */
    className: PropTypes.string,
    /** The name of the icon to display from the icons map. */
    name: PropTypes.string.isRequired,
    /** The ARIA role attribute. */
    role: PropTypes.string,
    /** The dimensions to set the width and height of the icon to. */
    size: PropTypes.number,
    /** Additional key/value pairs for the icon names/path definitions to add on to the common set provided. */
    iconDefinitions: PropTypes.object
};

This gets used in my Icon component.

Icon.jsx

import has from 'lodash/has';
import React, {Component} from 'react';
import classNames from 'classnames';
import IconPropTypes from './IconPropTypes';
import ICONS from './icon-defs';

export default class Icon extends Component {
    static propTypes = IconPropTypes;
    static defaultProps = {
        role: 'presentation',
        size: 20
    };

    constructor(props) {
        super(props);

        const {
            name,
            iconDefinitions
        } = props;

        this._ICONS = {
            ...ICONS,
            ...iconDefinitions
        };

        if (!has(this._ICONS, name)) {
            throw new Error(`'${name}' is not a valid icon name. Please use a defined name from the icon definitions available.`);
        }
    }

    render() {
        const {
            className,
            name,
            role,
            size
        } = this.props;
        const classes = classNames(
            'Icon',
            `Icon-${name}`,
            className
        );

        return (
            <svg
                className={classes}
                role={role}
                viewBox="0 0 1024 1024"
                width={`${size}px`}
                height={`${size}px`}
            >
                <path d={this._ICONS[name]} />
            </svg>
        );
    }
}

I export this because I can extend the icon set on a per project basis by using the same IconPropTypes (but really the explanation here is not important). The output in react-styleguidist only shows the role and size props with their default values, presumably ignoring my propTypes because they are set to an external source.

It would be nice if somehow, in a case like this, styleguidist could crawl that file and report the PropTypes that live within it. I think this is a relatively common use case, or maybe I'm mistaken, but it's pretty common in our UI kit so that we're not re-writing the same PropTypes for components that get extended on a per project basis.

Thoughts?

@tizmagik
Copy link
Collaborator

Curious why you don't export the Icon proptypes from the same file? Not sure if it would solve your issue, but might be worth a try?

@reintroducing
Copy link
Contributor Author

I guess it's a habit where we only have one export per file like this. That being said I'm not sure it would solve it anyway but I'd rather not export multiple items out of a component file.

@stepancar
Copy link
Member

Give a look at our project https://github.com/alfa-laboratory/arui-feather/blob/master/src/email-input/email-input.jsx#L20
We reuse proptypes from another component. See how it looks in react-styleguidist demo https://alfa-laboratory.github.io/arui-feather/styleguide/#emailinput
If you open props section - you can see that props equal to input component props.
But we use custom propsParser https://github.com/alfa-laboratory/arui-feather/blob/master/styleguide.config.js#L61
https://github.com/alfa-laboratory/arui-feather/blob/master/styleguide.config.js#L9
Try use this solution for your case.

@reintroducing
Copy link
Contributor Author

Looks like this is a known issue in react-docgen: reactjs/react-docgen#33

Closing this and following there.

@selrond

This comment has been minimized.

@sapegin sapegin added the react-docgen Issues in react-docgen library label Apr 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react-docgen Issues in react-docgen library
Projects
None yet
Development

No branches or pull requests

5 participants