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

feature request: add first class support for filtering HTMLAttributes #151

Closed
cdaringe opened this issue Nov 12, 2018 · 9 comments
Closed

Comments

@cdaringe
Copy link

problem

often times, one defines an a prop interface by extending HTML attributes, e.g React.ReactNode<HTMLInputAttributes>.

e.g.

interface MyProps extends React.ReactNode<HTMLInputAttributes> {
  /** a really great prop */
  bestProp: string
}

consequently, the styleguidist prop types generates prop documentation for all of the HTML attributes, not simply bestProp.

discussion

  • first, thanks for this great lib
  • second, because this pattern is so common in react, it may be worth considering adding first class support for omitting HTML attributes from the generated definitions.

thoughts? thanks!

@swashcap
Copy link

Looking at the parserOptions.propFilter, it looks like the first arg, PropItem has a parent property which points to the source file:

// styleguide.config.js
// ...
module.exports = {
  propsParser: reactDocgenTypescript.withCustomConfig('./tsconfig.json', {
    propFilter(prop, component) {
      debugger
      return true
    }
  }).parse,
  // ...
}

When debugging, it looks like prop.parent points to the source definition file:

> prop.parent
{ fileName: 'baby-registry-components/node_modules/@types/react/index.d.ts',
  name: 'HTMLAttributes' }

One could use that to conditionally return based on the definition file's path.

@cdaringe
Copy link
Author

is it recursive? perhaps this lib could expose a composable filter function?

const { withHtmlAttributesFilter } = require(...)

// case 1
module.exports = {
  propsParser: reactDocgenTypescript.withCustomConfig('./tsconfig.json', {
    propFilter: withHtmlAttributesFilter
  }).parse,
}

// case 2
module.exports = {
  propsParser: reactDocgenTypescript.withCustomConfig('./tsconfig.json', {
    propFilter: withHtmlAttributesFilter((prop, filter) => {
        // your other filters
    })
  }).parse,
}

@swashcap
Copy link

This propsFilter function solves this problem for me:

// styleguide.config.js
// ...
module.exports = {
  propsParser: reactDocgenTypescript.withCustomConfig('./tsconfig.json', {
    propFilter(prop) {
      if (prop.parent) {
        return !prop.parent.fileName.includes('node_modules')
      }

      return true
    },
  }).parse,
  // ...
}

Only outputs the component's own props. Could we perhaps modify the Readme so other users know about this?

@pvasek
Copy link
Collaborator

pvasek commented Nov 12, 2018

Extending README.md is a great idea. This is actually duplicate of #49 which was about the same thing. This was fixed by PR #111.

@ejuo
Copy link

ejuo commented Jun 17, 2019

I encountered the problem. prop.parent always is null for typescript version > 3.5.

@shermanhui
Copy link

@ejuo I'm also experiencing this issue as our project is also using TypeScript 3.5.0 is your workaround just to downgrade the version of TS?

@drewlustro
Copy link

I encountered the problem. prop.parent always is null for typescript version > 3.5.

Is there a workaround for this?

@tkiethanom
Copy link

propFilter(prop) {
      if (prop.parent) {
        return !prop.parent.fileName.includes('node_modules')
      }

      return true
    },

This doesn't seem to work if my props are named with names like color or onClick. They seem to be conflicting with existing HTML props.

@hipstersmoothie
Copy link
Contributor

seems like this one can be closed @pvasek

@pvasek pvasek closed this as completed Apr 25, 2020
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

8 participants