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: Support <StrictMode /> and <Suspense /> in source viewer #19785

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions code/renderers/react/src/docs/jsxDecorator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,23 @@ describe('renderJsx', () => {
`);
});

it('Suspense', () => {
function SuspenseComponent(props: any) {
return (
<React.Suspense fallback={null}>
<div>{props.children}</div>
</React.Suspense>
);
}

expect(renderJsx(createElement(SuspenseComponent, {}, 'I am Suspense'), {}))
.toMatchInlineSnapshot(`
<SuspenseComponent>
I am Suspense
</SuspenseComponent>
`);
});

it('should not add default props to string if the prop value has not changed', () => {
const Container = ({ className, children }: { className: string; children: string }) => {
return <div className={className}>{children}</div>;
Expand Down
27 changes: 10 additions & 17 deletions code/renderers/react/src/docs/jsxDecorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import { logger } from '@storybook/client-logger';

import type { ReactFramework } from '../types';

import { isMemo, isForwardRef } from './lib';

type JSXOptions = Options & {
/** How many wrappers to skip when rendering the jsx */
skip?: number;
Expand Down Expand Up @@ -74,21 +72,16 @@ export const renderJsx = (code: React.ReactElement, options: JSXOptions) => {
}
}

const displayNameDefaults =
typeof options.displayName === 'string'
? { showFunctions: true, displayName: () => options.displayName }
: {
// To get exotic component names resolving properly
displayName: (el: any): string =>
el.type.displayName ||
(el.type === Symbol.for('react.profiler') ? 'Profiler' : 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,
};
let displayNameDefaults;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Major Change: displayNameDefaults.displayName should be undefined if getDocgenSection returns null:

export function getDocgenSection(component: Component, section: string): any {
return hasDocgen(component) ? component.__docgenInfo[section] : null;
}

and then the logic will be handled by react-element-to-jsx-string:

https://github.com/algolia/react-element-to-jsx-string/blob/984f26908c17cea134fa53d17ac51f179e5195b2/src/parser/parseReactElement.js#L47-L77

We don't need to rewrite redundant code here.


if (typeof options.displayName === 'string') {
displayNameDefaults = { showFunctions: true, displayName: () => options.displayName };
} else if (getDocgenSection(code.type, 'displayName')) {
displayNameDefaults = {
// To get exotic component names resolving properly
displayName: (el: any): string => getDocgenSection(el.type, 'displayName'),
};
}

const filterDefaults = {
filterProps: (value: any, key: string): boolean => value !== undefined,
Expand Down