reactjs / react-docgen

A CLI and library to extract information from React component files for documentation generation purposes.
https://react-docgen.dev
MIT License
3.64k stars 296 forks source link

Support for compound components #775

Open mayank99 opened 1 year ago

mayank99 commented 1 year ago

I've been playing around with the latest beta, and while it works well for many cases, I could not get it to work with compound components.

A compound component generally looks like this:

<Compound foo>
  <Compound.Child />
</Compound>

I'm defining it as follows:

import React from 'react';
import type { PolymorphicComponent } from '~/utils';

const ParentComponent = React.forwardRef((props, ref) => {
  return (/* ... */)
}) as PolymorphicComponent<'div', ParentOwnProps>;
type ParentOwnProps = {
  /** foo prop */
  foo?: boolean;
};

const ChildComponent = React.forwardRef((props, ref) => {
  return (/* ... */)
}) as PolymorphicComponent<'div', ChildOwnProps>;
type ChildOwnProps = {
  /** bar prop */
  bar?: string;
};

/** description for main component */
export const Compound = Object.assign(ParentComponent, {
  /** description for subcomponent */
  Child: ChildComponent,
});
export default Compound;

I would guess that react-docgen would need to support a few different things here:

Powerplex commented 1 year ago

I have the same issue. We define our compound components the same way, using Object.assign. I can use the library to get the props from the root element of the compound only. Our components are written in Typescript.

I tried react-docgen-typescript too but it doesn't have the reactDocs.parse(src, reactDocgen.resolver.findAllComponentDefinitions)

dimitur2204 commented 2 days ago

I am running into the same problem. Did you find a workaround?