react-docgen icon indicating copy to clipboard operation
react-docgen copied to clipboard

Missing support for Flow Unions

Open millermedeiros opened this issue 7 years ago • 5 comments

react-docgen doesn't recognize the props if it's an union type:

type BaseProps = {|
  label: string,
|};

type LinkButtonProps = {|
  ...BaseProps,
  href: string,
  target?: string,
  rel?: string,
|};

type ButtonProps = {|
  ...BaseProps,
  type?: 'button' | 'submit',
|};

type Props = ButtonProps | LinkButtonProps;

class MyButton extends React.Component<Props> {
  render() {
     // conditionally render `<a>` or `<button>` based on props...
  }
}

I don't want users to pass props that are specific to <a> at the same time as some props that are specific to <button> - I know this kind of pattern of having a single component render to <a> or <button> is "weird", but some of our legacy components use it...

For now I need to define a single type that includes all the props and mark them all as optional - which is "fine", I guess..

More info about unions here: https://flow.org/en/docs/types/unions/

tested on v3.0.0-beta10

millermedeiros avatar Feb 28 '18 04:02 millermedeiros

Can you please be more specific what "doesn't recognize" means? You get a parser error? If so can you please add the error message. Or do you receive an output that you would not expect? Then please add the output of react-docgen that you think is wrong.

danez avatar Feb 28 '18 10:02 danez

I'd assume OP is talking about missing descriptions.

With

type: PropTypes.oneOf([
			'animal',
			'bacon',
			'beard',
			'bear',
			'cat',
			'food',
			'city',
			'nature',
			'people',

I automatically receive a description containing all possible enum values.

With FlowType and its unions (i e type?: "animal" | "bacon", no such description is generated.

I'm using react styleguidist, btw.

Happy-Ferret avatar Mar 16 '18 11:03 Happy-Ferret

I'm considering using react-docgen in a Language Server Protocol instance, but the lack of this feature would be a blocker.

emilyhorsman avatar Jun 13 '18 19:06 emilyhorsman

If this is about to level unions (which I think the original post is):

We have talked about this internally, but it's not clear how exactly we would represent this in the output. Should props be an array instead then? What if flow type annotation and PropTypes are used (the latter not being able to describe unions).

If this is about prop-level unions: We should support this if we don't.

fkling avatar Jun 14 '18 06:06 fkling

Like @Happy-Ferret said, I think the PropTypes.oneOf([ ... ]) -equivalent for Flow would be closest to ideal here.

neutraali avatar Mar 27 '20 08:03 neutraali