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

Conversation

zhyd1997
Copy link
Contributor

@zhyd1997 zhyd1997 commented Nov 9, 2022

Issue: #11554

What I did

Refactor:

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,
};

So that we will not write redundant code here:

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

How to test

  • Is this testable with Jest or Chromatic screenshots?
  • Does this need a new example in the kitchen sink apps?
  • Does this need an update to the documentation?

If your answer is yes to any of these, please make sure to include it in your PR.

@zhyd1997 zhyd1997 marked this pull request as draft November 9, 2022 01:39
(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.

@zhyd1997
Copy link
Contributor Author

zhyd1997 commented Nov 9, 2022

Hi @shilman

I need your help with why test-runner-sandboxes failed on CircleCI, I have no idea about that...

Update:

the error
Cannot read properties of undefined (reading '__docgenInfo')
is thrown out from here:

return hasDocgen(component) ? component.__docgenInfo[section] : null;

the code is React.ReactElement type, which has no __docgenInfo property, I will figure it out...

export const renderJsx = (code: React.ReactElement, options: JSXOptions) => {
if (typeof code === 'undefined') {
logger.warn('Too many skip or undefined component');
return null;
}
let renderedJSX = code;

@zhyd1997 zhyd1997 marked this pull request as ready for review November 11, 2022 02:08
@shilman shilman self-assigned this Nov 14, 2022
@ndelangen
Copy link
Member

Having difficulty understanding this ticket & PR, @IanVS @andezzat @shilman can you help? I think if any of you feel confident this is the right thing to do, this can be merged!

@IanVS IanVS self-requested a review January 13, 2023 17:49
@fizz3r
Copy link

fizz3r commented Apr 5, 2023

@IanVS, @andezzat @shilman , any update on this PR? Still seeing the issue mentioned in #11554.

@ndelangen
Copy link
Member

@shilman This PR is over a year old, what do you think of this PR / code-change?

I'm tempted to just proceed. I'll re-target the base branch, merge the base branch in, and see what happens.
I'd be real nice if we had a test / story demonstrating this though.

If CI fails, then I do not have the resources to get this merged, and will likely have to close, unless @zhyd1997 has interest in getting CI green at that point.

cc: @JReinhold

@ndelangen ndelangen changed the title refactor: inherited 'react-element-to-jsx-string'. Docs: Improve inherited 'react-element-to-jsx-string' Nov 28, 2023
@ndelangen ndelangen changed the base branch from next to release-8-0 November 28, 2023 08:57
@ndelangen ndelangen changed the base branch from release-8-0 to next November 28, 2023 12:47
@zhyd1997
Copy link
Contributor Author

zhyd1997 commented Dec 6, 2023

Hi @ndelangen @shilman @IanVS @kasperpeulen @valentinpalkovic

Anything I can do on my side to improve the code confidence? 👀

@JReinhold
Copy link
Contributor

I don't know much about this code path TBH, but I like the tests and everything is passing, so I'm okay with merging it.

@ndelangen ndelangen merged commit 389eaa9 into storybookjs:next Dec 14, 2023
49 of 50 checks passed
@github-actions github-actions bot mentioned this pull request Dec 14, 2023
22 tasks
@zhyd1997 zhyd1997 deleted the refactor/ea45781-yadong-jsx-decorator branch December 14, 2023 22:21
@JReinhold JReinhold changed the title Docs: Improve inherited 'react-element-to-jsx-string' Addon-docs: Support <StrictMode /> and <Suspense /> in source viewer Dec 19, 2023
@vanessayuenn vanessayuenn linked an issue Jan 4, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: React.StrictMode is not working
5 participants