-
-
Notifications
You must be signed in to change notification settings - Fork 298
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
Add support for imported propTypes #33
Comments
I guess we could do this for simple cases, where a variable is spread into I didn't want to make react-docgen make any assumptions about how module identifiers are resolved, but I guess it's fair to assume that it will be CommonJS compatible (i.e. module identifiers refer to npm modules or local files). Then we could inspect the other file and if it returns an object literal, inspect and merge it as well. |
+1 :) |
That would be a great feature |
I've got a similar usecase, but not using spread when injecting the props. // ILink.js
import {PropTypes} from 'react';
export default PropTypes.shape({
url: PropTypes.string.isRequired,
selected: PropTypes.bool,
target: PropTypes.string,
title: PropTypes.string.isRequired
}); // MyComponent.jsx
import { Component } from 'react';
import ILink from './ILink.js';
import otherPropTypes './otherPropTypes.js';
export default class MyComponent extends Component {
static propTypes = {
link: ILink
}
// etc...
} |
The same idea would also apply to flow types, as they can be also imported. So maybe this "import something" could be abstracted in a way so it makes imported propTypes and flowTypes possible. |
Even we stuck with similar use case, any plans to add support for external imports of propTypes, below is our use case. constants.jsconst BUTTON_STYLES = ['A', 'B', 'C', 'D'];
export default BUTTON_STYLES; component.jsimport BUTTON_STYLES from 'constants';
propTypes = {
kind: PropTypes.oneOf(constants),
}
class SomeComp extends React.Component{
static propTypes = propTypes;
}
export default SomeComp; Generated AST:'kind': {
'type': {
'name': 'enum',
'computed': true,
'value': 'BUTTON_STYLES'
},
'required': false,
'description': 'some description',
'defaultValue': {
'value': 'A',
'computed': false
}
}, Expected AST:value of AST should be as below(which is working fine when BUTTON_STYLES are in same file, but not with import) 'kind': {
'type': {
'name': 'enum',
'value': [
{
'value': 'A',
'computed': false
},
{
'value': 'B',
'computed': false
},
{
'value': 'C',
'computed': false
},
{
'value': 'D',
'computed': false
}
]
},
'required': false,
'description': 'Button has a a different set of `kinds`',
'defaultValue': {
'value': 'A',
'computed': false
}
} |
What do you think about a simpler approach? In our case, it would be enough to document our components with a generic message whenever a spread is found. So, for a component like this one: MyComponent.propTypes = {
/**
* Component Foo proptypes
*/
...Foo.propTypes,
/**
* Some other proptypes
*/
...propTypes,
/**
* Unhandled spread
*/
...{
bar: Prop.bool,
}
}; We would like to get the following descriptors: {
"...Foo.propTypes": {
"description": "Component Foo proptypes"
},
"...propTypes": {
"description": "Some other proptypes"
}
} Right now, these proptypes are just ignored, resulting in incomplete docs :( We have got a proof of concept fork which adds support for Identifier or MemberExpression spreads, ignoring other spread expressions. I can create a PR if you think this approach is worth it :) |
@asis: They are not ignored: https://github.com/reactjs/react-docgen/blob/00f2d2d62d93b9243157c4cea19cf241121a7721/src/handlers/__tests__/propTypeCompositionHandler-test.js, but they are stored separately from normal props. |
Oh, sorry! I was focused on extracting docs from the props declaration, so when I saw
Thanks for the quick response! |
+1 {
"description": "Our wrapper component around semantic-ui button\r\nFor more props visit\r\n[semantic-ui-button](https://react.semantic-ui.com/elements/button#button-example-button)",
"methods": [],
"props": {
"children": {
"type": {
"name": "any"
},
"required": false,
"description": "Button label"
}
},
"composes": [
"semantic-ui-react"
]
} Code: import React from "react";
import PT from "prop-types";
import { Button as SButton } from "semantic-ui-react";
/**
* Our wrapper component around semantic-ui button
* For more props visit
* [semantic-ui-button](https://react.semantic-ui.com/elements/button#button-example-button)
*/
const Button = ({ children, ...rest }) =>
<SButton {...rest}>{children}</SButton>;
Button.propTypes = {
/** Button label */
children: PT.any,
...SButton.propTypes
};
export default Button; Will be awesome to have this with comments that propTypes of the imported component have. Since all semantic and our components are react-docgen documented. |
@RIP21 I guess in your specific case we would also have to record which export you are importing ( |
I'm running into the same thing while using either |
We just used the list of "composed props" to infer the source files where they were defined. To solve the problem in styleguidist/react-styleguidist#548, we use a custom propsParser which turns each composed props reference into a "custom" prop, with a description containg a link to the relevant section of our docs. It is brittle... but it works. |
Would you mind sharing your handler? |
@pasupuletics sorry I corrected the links now. |
@pasupuletics and @siddharthkp got me inspired with their implementations. I forked their repo and expanded the functionality, wrote a whole lot of snapshot tests, and in the end mostly tweaked some code related to I'd like to offer this up in hopes that it will help others: https://github.com/benjroy/react-docgen-imported-proptype-handler
Setup is pretty much the same as The test fixtures exercise the expanded capabilities: https://github.com/benjroy/react-docgen-imported-proptype-handler/tree/master/src/handlers/__tests__/fixtures/components And there is nothing computed in the final snapshot: https://github.com/benjroy/react-docgen-imported-proptype-handler/blob/master/src/handlers/__tests__/__snapshots__/importedPropTypeHandler-test.js.snap it works well following imports of files in your repo. following imports from |
@benjroy thank you for sharing this! I have tried setting it up in my project and I ran into some issues that are keeping me from building my docs with this setup. Do you have an example of how you've integrated the package in your own project? |
@benwiley4000 thank you for trying it out! I forgot to ship Additionally, here is a repo with basic example usage: |
@benjroy That's brilliant! If it works nicely, I don't mind deprecating my package. I'm not actively maintaining it anyways. You can add @pasupuletics and me as maintainers if you like. |
@benjroy Thanks for doing this. I'm unclear what the differences between your solution and this one is, however. Also, I think it would be good to show how this is used in the confines of react-styleguidist which is I think where all this originated, if possible. I'm also not able to see clearly how to use it there from your example. |
@pasupuletics @siddharthkp and @benjroy , thanks for the great work! I'm trying import BaseComponent from './BaseComponent';
// ...
MyComponent.propTypes = {
...BaseComponent.propTypes,
// ... more
}
MyComponent.defaultProps = {
...BaseComponent.defaultProps,
// ... more
}
export default MyComponent; Error:
Since there is not error without your handler, and all the Babel stuff works nicely, I'm a bit stumped. $ yarn list --pattern react-docgen
yarn list v1.13.0
├─ [email protected]
│ └─ [email protected]
├─ [email protected]
├─ [email protected]
└─ [email protected] |
It seems that these issues could be side-stepped if this handler were simply a pull-request agains the official |
@nemoDreamer yea, I think this handler I wrote would be better implemented as a PR into Your problem specifically is that I noticed too late when i was working on the handler that
Reworking this for |
Haven't tried your package yet @benjroy but if it fixes the problem, it'd be awesome to see it merged into |
Can this also solve the same problem with imported flow types? |
any progress here? |
I started hacking on this today and managed to get importing working for propTypes, flow, and typescript types. See devongovett/react-docgen@typescript...devongovett:importing. I'll open up a PR once the typescript PR (#348) is merged, since this builds on that. In the meantime, feel free to try out my branch. Current limitations:
Please let me know if you try it out and encounter bugs. |
PR opened: #352 |
There is an issue with |
I have the same problem, is this being worked on? |
Dropping a link to this conversation to help my future self when I inevitably end up here in a couple months. |
Just my 5 cents. Anyone who is still using prop-types should really invest in Typescript instead. Prop-types are quite dead at this point. While, I'm sure, many are still using prop-types, it is extremely dated approach and causes more issues than solves IMO. There are plenty of codemodes that can help you with migration. |
@RIP21 the linked conversation is relevant for Typescript types... |
Importing prop types is a fairly common use case which doesn't appear to be possible at this stage. Any plans to support this pattern?
Example:
Results in:
The text was updated successfully, but these errors were encountered: