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

Components with defined types not picked up #71

Closed
kyler-hyuna opened this issue Feb 1, 2018 · 6 comments
Closed

Components with defined types not picked up #71

kyler-hyuna opened this issue Feb 1, 2018 · 6 comments

Comments

@kyler-hyuna
Copy link

kyler-hyuna commented Feb 1, 2018

Components that have defined types aren't pickup in the styleguide.

const Button: React.StatelessComponent<ButtonProps> = ({ title }) => (
  <button className="button">{title}</button>
)

export default Button

The above example shows
Warning: src/components/Button/Button.tsx matches a pattern defined in “components” or “sections” options in your style guide config but doesn’t export a component.

It fails for React.Component as well:

class Button extends React.Component<ButtonProps> {
  render() {
    const { title } = this.props

    return <button className="button">{title}</button>
  }
}

export default Button

If I remove the prop definitions to

const Button = ({ title }: ButtonProps) => (
  <button className="button">{title}</button>
)

export default Button

It works again.

Versions:
"react-docgen-typescript": "^1.2.3",
"react-styleguidist": "^6.2.2",
"typescript": "^2.7.1",
"webpack": "^3.10.0"
"react": "^16.2.0"

@pvasek
Copy link
Collaborator

pvasek commented Feb 1, 2018 via email

@kyler-hyuna
Copy link
Author

Yea i just omitted the last line for brevity.

@kyler-hyuna
Copy link
Author

Alright @pvasek using the example in the repo, I tracked down the problem to the tsconfig setting allowSyntheticDefaultImports.

The only way it picks up the component is if I import * as React from 'react'. However this shouldn't be the case since in my tsconfig.json I have allowSyntheticDefaultImports: true.

Is there a way that react-docgen can read the setting from my tsconfig?

@kyler-hyuna
Copy link
Author

Alright so I fixed it. Apparently docgen doesn't pick up by default the tsconfig.json at the project root and I had to explicitly provide the path.

Final styleguide.config.js:

module.exports = {
  components: 'src/components/**/[A-Z]*.{ts,tsx}',
  webpackConfig: require('./webpack.dev.js'),
  resolver: require('react-docgen').resolver.findAllComponentDefinitions,
  propsParser: require('react-docgen-typescript').withCustomConfig(
    './tsconfig.json'
  ).parse,
}

@pvasek
Copy link
Collaborator

pvasek commented Feb 1, 2018

Yes, you are right. You need to define it explicitly.

@karolis-sh
Copy link

karolis-sh commented Feb 7, 2019

I'm still facing this issue (though a bit different approach I guess)

This works:

import cx from 'classnames';
import * as PropTypes from 'prop-types';
import * as React from 'react';

import { CLASS_PREFIX } from '../_constants';

export interface ContainerProps extends React.HTMLProps<HTMLDivElement> {
  fluid: boolean;
}

const Container: React.FC<ContainerProps> = ({
  className,
  fluid,
  ...props
}) => {
  const classes = cx(
    CLASS_PREFIX + '-container px-5',
    fluid ? 'container-fluid' : 'container',
    className,
  );
  return <div className={classes} {...props} />;
};

Container.propTypes = {
  fluid: PropTypes.bool.isRequired,
};

Container.defaultProps = {
  fluid: true,
};

export default Container;

While this doesn't:

import cx from 'classnames';
import * as PropTypes from 'prop-types';
import * as React from 'react';

import { CLASS_PREFIX } from '../_constants';

export interface ContainerProps extends React.HTMLProps<HTMLDivElement> {
  fluid: boolean;
}

const Container: React.FC<ContainerProps> & { defaultProps: Partial<ContainerProps> } = ({
  className,
  fluid,
  ...props
}) => {
  const classes = cx(
    CLASS_PREFIX + '-container px-5',
    fluid ? 'container-fluid' : 'container',
    className,
  );
  return <div className={classes} {...props} />;
};

Container.propTypes = {
  fluid: PropTypes.bool.isRequired,
};

Container.defaultProps = {
  fluid: true,
};

export default Container;

The type merging (not sure how it's called) is due to default props issue.

Though noticed simple function works with defaultProps:

import cx from 'classnames';
import * as PropTypes from 'prop-types';
import * as React from 'react';

import { CLASS_PREFIX } from '../_constants';

export interface ContainerProps extends React.HTMLProps<HTMLDivElement> {
  fluid: boolean;
}

function Container({ className, fluid, ...props }: ContainerProps) {
  const classes = cx(
    CLASS_PREFIX + '-container px-5',
    fluid ? 'container-fluid' : 'container',
    className,
  );
  return <div className={classes} {...props} />;
}

Container.propTypes = {
  fluid: PropTypes.bool.isRequired,
};

Container.defaultProps = {
  fluid: true,
};

export default Container;

Though not sure if function is the recommended way over React.FC as I just started using Typescript..

cdaringe added a commit to cdaringe/react-styleguidist that referenced this issue Jun 27, 2019
as discussed in styleguidist/react-docgen-typescript#71 (comment), some compiler options break component discovery.  the safest strategy is to always include the tsconfig from user space, which improves component detection.
cdaringe added a commit to cdaringe/react-styleguidist that referenced this issue Jun 27, 2019
as discussed in styleguidist/react-docgen-typescript#71 (comment), some compiler options break component discovery.  the safest strategy is to always include the tsconfig from user space, which improves component detection.
sapegin pushed a commit to styleguidist/react-styleguidist that referenced this issue Jun 28, 2019
As discussed in styleguidist/react-docgen-typescript#71 (comment), some compiler options break component discovery.  the safest strategy is to always include the tsconfig from user space, which improves component detection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants