Skip to content

Conversation

nekitk
Copy link
Contributor

@nekitk nekitk commented Aug 23, 2019

Fixes #154

Added support for extracting default prop values from object desctructuring inside functional components.

"chai": "^4.1.2",
"husky": "^0.14.3",
"lint-staged": "^7.2.0",
"lint-staged": "^7.3.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upped version because current version was exiting with Error: Cannot find any-observable implementation nor global.Observable.

check('StatelessWithDefaultProps', expectation);
});

it('should parse referenced props', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted these 2 tests (should parse referenced props and supports spread props) because this use cases are not actually supported and default props are not properly generated. These tests were not failing on master because of incorrect check inside test code itself: see https://github.com/styleguidist/react-docgen-typescript/pull/200/files#diff-a063097dc09ae77820847611faefb99b

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explanation.

}

const statement = possibleStatements[0];
return possibleStatements.reduce((res, statement) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging multiple definitions in case of defaultProps are used together with desctructuring

@pvasek pvasek merged commit 1bd1c1d into styleguidist:master Aug 25, 2019
@pvasek
Copy link
Collaborator

pvasek commented Aug 25, 2019

The version including this code has been published to npm as v1.13.0.

@rmunch
Copy link

rmunch commented Sep 5, 2019

Thank you for adding this feature - very useful! Something I notice is that if you destructure props inside the function rather than in the declaration, default props are not picked up. Would it make sense to support this case?

import * as React from "react";

interface ButtonProps {
    children: React.ReactNode,
    /** More information about the disabled property */
    disabled?: boolean
}

# Default props detected:
export default function Button({children, disabled = false}: ButtonProps) {
    return <button disabled={disabled}>{children}</button>;
}

# Default props not detected:
export default function Button(props: ButtonProps) {
    const {children, disabled = false} = props;
    return <button disabled={disabled}>{children}</button>;
}

@nekitk
Copy link
Contributor Author

nekitk commented Sep 6, 2019

@rmunch It is certainly possible to support this case, but there's no need in it right now on my current project. If you would try to add it yourself, I think you need to find an assignment statement with 'props' on the right side, take the left side and then pass its properties to this. getPropMap. Similar to the way defaultProps are parsed.

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

Successfully merging this pull request may close these issues.

Docgen of defaulted properties in a SFC (Typescript 3)
3 participants