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 React Profiler in source snippets #19004

Merged
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
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) ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the major change here!

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, '() => {}');
Copy link
Contributor Author

@zhyd1997 zhyd1997 Aug 25, 2022

Choose a reason for hiding this comment

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

const Template: ComponentStory<typeof Button> = (args) => 
  <Profiler id="test" onRender={() => console.log('test-profiler')}>
    <Button {...args} />
  </Profiler>

export const Primary = Template.bind({});
// More on args: https://storybook.js.org/docs/react/writing-stories/args
Primary.args = {
  primary: true,
  label: 'Button',
  onClick: () => console.log('test-button')
};

Before:
Screen Shot 2022-08-25 at 12 47 42

After:
Screen Shot 2022-08-25 at 12 46 58

Copy link

@ns-kutsav ns-kutsav Oct 14, 2023

Choose a reason for hiding this comment

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

It still isn't showing the actual function. Is that the expected behavior?

};

const defaultOpts = {
Expand Down