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

Addon Docs: Fix [Object object] displayName in some JSX components #26566

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions code/renderers/react/src/docs/jsxDecorator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,22 @@ describe('renderJsx', () => {
}
);

expect(renderJsx(createElement(MyExoticComponentRef, {}, 'I am forwardRef!'), {}))
expect(renderJsx(<MyExoticComponentRef>I am forwardRef!</MyExoticComponentRef>))
.toMatchInlineSnapshot(`
<MyExoticComponent>
<React.ForwardRef>
I am forwardRef!
</MyExoticComponent>
</React.ForwardRef>
`);

// if docgenInfo is present, it should use the displayName from there
(MyExoticComponentRef as any).__docgenInfo = {
displayName: 'ExoticComponent',
};
expect(renderJsx(<MyExoticComponentRef>I am forwardRef!</MyExoticComponentRef>))
.toMatchInlineSnapshot(`
<ExoticComponent>
I am forwardRef!
</ExoticComponent>
`);
});

Expand All @@ -143,11 +154,20 @@ describe('renderJsx', () => {
return <div>{props.children}</div>;
});

expect(renderJsx(createElement(MyMemoComponentRef, {}, 'I am memo!'), {}))
.toMatchInlineSnapshot(`
<MyMemoComponent>
expect(renderJsx(<MyMemoComponentRef>I am memo!</MyMemoComponentRef>)).toMatchInlineSnapshot(`
<React.Memo>
I am memo!
</React.Memo>
`);

// if docgenInfo is present, it should use the displayName from there
(MyMemoComponentRef as any).__docgenInfo = {
displayName: 'MyMemoComponentRef',
};
expect(renderJsx(<MyMemoComponentRef>I am memo!</MyMemoComponentRef>)).toMatchInlineSnapshot(`
<MyMemoComponentRef>
I am memo!
</MyMemoComponent>
</MyMemoComponentRef>
`);
});

Expand Down
35 changes: 24 additions & 11 deletions code/renderers/react/src/docs/jsxDecorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const toPascalCase = (str: string) => str.charAt(0).toUpperCase() + str.slice(1)
* @returns {string | null} A displayName for the Symbol in case elementType is a Symbol; otherwise, null.
*/
export const getReactSymbolName = (elementType: any): string => {
const symbolDescription: string = elementType.toString().replace(/^Symbol\((.*)\)$/, '$1');
const elementName = elementType.$$typeof || elementType;
const symbolDescription: string = elementName.toString().replace(/^Symbol\((.*)\)$/, '$1');

const reactComponentName = symbolDescription
.split('.')
Expand Down Expand Up @@ -124,16 +125,28 @@ export const renderJsx = (code: React.ReactElement, options?: JSXOptions) => {
} else {
displayNameDefaults = {
// To get exotic component names resolving properly
displayName: (el: any): string =>
el.type.displayName || typeof el.type === 'symbol'
? getReactSymbolName(el.type)
: null ||
getDocgenSection(el.type, 'displayName') ||
(el.type.name !== '_default' ? el.type.name : null) ||
(typeof el.type === 'function' ? 'No Display Name' : null) ||
(isForwardRef(el.type) ? el.type.render.name : null) ||
(isMemo(el.type) ? el.type.type.name : null) ||
el.type,
displayName: (el: any): string => {
if (el.type.displayName) {
return el.type.displayName;
} else if (getDocgenSection(el.type, 'displayName')) {
Copy link
Member Author

@yannbf yannbf Mar 19, 2024

Choose a reason for hiding this comment

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

I moved this up because I am assuming that docgenInfo should take precedence over an element's symbol name.

Before, if you had a forwardRef element AND docgenInfo with a displayName of "Button", it would say <React.ForwardRef> instead of <Button>.

Now it will say <Button>.

return getDocgenSection(el.type, 'displayName');
} else if (
typeof el.type === 'symbol' ||
(el.type.$$typeof && typeof el.type.$$typeof === 'symbol')
) {
return getReactSymbolName(el.type);
} else if (el.type.name && el.type.name !== '_default') {
return el.type.name;
} else if (typeof el.type === 'function') {
return 'No Display Name';
} else if (isForwardRef(el.type)) {
return el.type.render.name;
} else if (isMemo(el.type)) {
return el.type.type.name;
} else {
return el.type;
}
},
};
}

Expand Down
Loading