Skip to content

Commit

Permalink
Merge pull request #19004 from zhyd1997/fix/support-profiler-use-in-c…
Browse files Browse the repository at this point in the history
…omponent

Addon-docs: Fix React Profiler in source snippets
  • Loading branch information
shilman authored Oct 27, 2022
2 parents 7a6828e + 117fab9 commit 8009143
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 18 additions & 1 deletion code/renderers/react/src/docs/jsxDecorator.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
import React, { createElement, FC, PropsWithChildren } from 'react';
import React, { createElement, FC, Profiler, PropsWithChildren } from 'react';
import PropTypes from 'prop-types';
import { addons, useEffect } from '@storybook/addons';
import { SNIPPET_RENDERED } from '@storybook/docs-tools';
Expand Down Expand Up @@ -137,6 +137,23 @@ describe('renderJsx', () => {
`);
});

it('Profiler', () => {
function ProfilerComponent(props: any) {
return (
<Profiler id="profiler-test" onRender={() => {}}>
<div>{props.children}</div>
</Profiler>
);
}

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

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
3 changes: 2 additions & 1 deletion code/renderers/react/src/docs/jsxDecorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const renderJsx = (code: React.ReactElement, options: JSXOptions) => {
// 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) ||
Expand Down Expand Up @@ -119,7 +120,7 @@ export const renderJsx = (code: React.ReactElement, options: JSXOptions) => {
return string;
}).join('\n');

return result.replace(/function\s+noRefCheck\(\)\s+\{\}/, '() => {}');
return result.replace(/function\s+noRefCheck\(\)\s+\{\}/g, '() => {}');
};

const defaultOpts = {
Expand Down

0 comments on commit 8009143

Please sign in to comment.