Skip to content

Commit d4fcb97

Browse files
Alternatively obtain componentDisplayName from props object (#229)
* Obtain componentDisplayName also from props object * docs(changeset): Permalink generation should also work now when `extract-react-types` is used. Co-authored-by: Klaus Paiva <[email protected]>
1 parent b5c1a5d commit d4fcb97

File tree

5 files changed

+45
-12
lines changed

5 files changed

+45
-12
lines changed

.changeset/tall-yaks-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pretty-proptypes': patch
3+
---
4+
5+
Permalink generation should also work now when `extract-react-types` is used.

packages/pretty-proptypes/src/LayoutRenderer/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { Components } from '../components';
1010
import getPropTypes from '../getPropTypes';
1111
import renderPropType from '../renderPropType';
1212
import PrettyPropType from '../PrettyConvert';
13+
import { getComponentDisplayName } from '../utils';
1314

1415
type Obj = {
1516
kind: 'object',
@@ -91,12 +92,11 @@ const LayoutRenderer: FC<LayoutRendererProps> = ({
9192
}
9293

9394
let renderProps = rest;
94-
// ensure displayName passes through, assuming it has been captured
95-
// $FlowFixMe as mentioned above, the typing for `component` is a bit off here...
96-
if (component && component.___displayName) {
95+
const componentDisplayName = getComponentDisplayName(component || (props || {}).component);
96+
if (typeof componentDisplayName === 'string') {
9797
renderProps = {
9898
...rest,
99-
componentDisplayName: String(component.___displayName)
99+
componentDisplayName
100100
};
101101
}
102102

packages/pretty-proptypes/src/Props/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { CommonProps } from '../types';
99
import PropsWrapper from './Wrapper';
1010
import getPropTypes from '../getPropTypes';
1111
import renderPropType from '../renderPropType';
12+
import { getComponentDisplayName } from '../utils';
1213

1314
import Prop from '../Prop';
1415

@@ -69,12 +70,12 @@ export default class Props extends Component<DynamicPropsProps> {
6970
if (!propTypes) return null;
7071

7172
let renderProps = rest;
72-
// ensure displayName passes through, assuming it has been captured
73-
// $FlowFixMe as mentioned above, the typing for `component` is a bit off here...
74-
if (component && component.___displayName) {
73+
// $FlowFixMe types are not exactly correct here ... sadly :/
74+
const componentDisplayName = getComponentDisplayName(component || (props || {}).component);
75+
if (typeof componentDisplayName === 'string') {
7576
renderProps = {
7677
...rest,
77-
componentDisplayName: String(component.___displayName)
78+
componentDisplayName
7879
};
7980
}
8081

packages/pretty-proptypes/src/PropsTable/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { CommonProps } from '../types';
1111
import PropsWrapper from '../Props/Wrapper';
1212
import getPropTypes from '../getPropTypes';
1313
import renderPropType from '../renderPropType';
14+
import { getComponentDisplayName } from '../utils';
1415
import PropRow from './PropRow';
1516

1617
type Obj = {
@@ -70,12 +71,12 @@ export default class PropsTable extends Component<DynamicPropsProps> {
7071
if (!propTypes) return null;
7172

7273
let renderProps = rest;
73-
// ensure displayName passes through, assuming it has been captured
74-
// $FlowFixMe as mentioned above, the typing for `component` is a bit off here...
75-
if (component && component.___displayName) {
74+
// $FlowFixMe types are not exactly correct here ... sadly :/
75+
const componentDisplayName = getComponentDisplayName(component || (props || {}).component);
76+
if (typeof componentDisplayName === 'string') {
7677
renderProps = {
7778
...rest,
78-
componentDisplayName: String(component.___displayName)
79+
componentDisplayName
7980
};
8081
}
8182

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-disable no-underscore-dangle */
2+
// @flow strict-local
3+
4+
// eslint-disable-next-line import/prefer-default-export
5+
export const getComponentDisplayName = (
6+
data:
7+
| {
8+
___displayName?: string
9+
}
10+
| {
11+
name?: {
12+
name?: string
13+
}
14+
}
15+
| void
16+
): string | void => {
17+
// ensure displayName passes through, assuming it has been captured by Babel
18+
if (data && data.___displayName) {
19+
return String(data.___displayName);
20+
// or it might be obtainable from the converter logic in `packages/extract-react-types/src/converter`
21+
} else if (data && data.name && data.name.name) {
22+
return String(data.name.name);
23+
}
24+
25+
return undefined;
26+
};

0 commit comments

Comments
 (0)