-
Notifications
You must be signed in to change notification settings - Fork 85
feat: replace memoized/forwarded raw components as prop values #682
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
Open
quantizor
wants to merge
13
commits into
algolia:master
Choose a base branch
from
quantizor:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+472
−38
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
af5b9d2
feat: replace memoized/forwarded raw components as prop values
quantizor 1e9789a
support multi-wrapped components
quantizor fa6cae1
refactor utility into shared functions
quantizor 57e5063
fix types
quantizor fcd9d83
chore: add failing test for parsing react DOM portals
quantizor 16dcbf7
feat: add support for serializing ReactDOM portal nodes
quantizor ff7367c
Merge remote-tracking branch 'upstream/master'
quantizor 7ab5f83
feat: add formatter for portal nodes
quantizor f2d55eb
refactor: improve display of portalled contents
quantizor cce033b
fix: bitten by the arraylike goblin
quantizor 01c485f
fix: indentation
quantizor d4ee738
Merge remote-tracking branch 'upstream/master'
quantizor e0fc9ae
Merge branch 'master' into master
quantizor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* @flow */ | ||
|
||
import type { Key } from 'react'; | ||
import formatReactElementNode from './formatReactElementNode'; | ||
import type { Options } from './../options'; | ||
import type { | ||
ReactElementTreeNode, | ||
ReactPortalTreeNode, | ||
TreeNode, | ||
} from './../tree'; | ||
import spacer from './spacer'; | ||
|
||
const toReactElementTreeNode = ( | ||
displayName: string, | ||
key: ?Key, | ||
childrens: TreeNode[] | ||
): ReactElementTreeNode => { | ||
let props = {}; | ||
if (key) { | ||
props = { key }; | ||
} | ||
|
||
return { | ||
type: 'ReactElement', | ||
displayName, | ||
props, | ||
defaultProps: {}, | ||
childrens, | ||
}; | ||
}; | ||
|
||
export default ( | ||
node: ReactPortalTreeNode, | ||
inline: boolean, | ||
lvl: number, | ||
options: Options | ||
): string => { | ||
const { type, containerSelector, childrens } = node; | ||
|
||
if (type !== 'ReactPortal') { | ||
throw new Error( | ||
`The "formatReactPortalNode" function could only format node of type "ReactPortal". Given: ${type}` | ||
); | ||
} | ||
|
||
return ` | ||
{ReactDOM.createPortal(${ | ||
childrens.length | ||
? `\n${spacer(lvl + 1, options.tabStop)}${formatReactElementNode( | ||
toReactElementTreeNode('', undefined, childrens), | ||
inline, | ||
lvl + 1, | ||
options | ||
)}\n` | ||
: 'null' | ||
}, document.querySelector(\`${containerSelector}\`))} | ||
`.trim(); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* @flow */ | ||
|
||
import formatReactPortalNode from './formatReactPortalNode'; | ||
|
||
const defaultOptions = { | ||
filterProps: [], | ||
showDefaultProps: true, | ||
showFunctions: false, | ||
tabStop: 2, | ||
useBooleanShorthandSyntax: true, | ||
useFragmentShortSyntax: true, | ||
sortProps: true, | ||
}; | ||
|
||
describe('formatReactPortalNode', () => { | ||
it('should format a react portal with a string as children', () => { | ||
const tree = { | ||
type: 'ReactPortal', | ||
containerSelector: 'body', | ||
childrens: [ | ||
{ | ||
value: 'Hello world', | ||
type: 'string', | ||
}, | ||
], | ||
}; | ||
|
||
expect(formatReactPortalNode(tree, false, 0, defaultOptions)) | ||
.toMatchInlineSnapshot(` | ||
"{ReactDOM.createPortal( | ||
<> | ||
Hello world | ||
</> | ||
, document.querySelector(\`body\`))}" | ||
`); | ||
}); | ||
|
||
it('should format a react portal with multiple childrens', () => { | ||
const tree = { | ||
type: 'ReactPortal', | ||
containerSelector: 'body', | ||
childrens: [ | ||
{ | ||
type: 'ReactElement', | ||
displayName: 'div', | ||
props: { a: 'foo' }, | ||
childrens: [], | ||
}, | ||
{ | ||
type: 'ReactElement', | ||
displayName: 'div', | ||
props: { b: 'bar' }, | ||
childrens: [], | ||
}, | ||
], | ||
}; | ||
|
||
expect(formatReactPortalNode(tree, false, 0, defaultOptions)) | ||
.toMatchInlineSnapshot(` | ||
"{ReactDOM.createPortal( | ||
<> | ||
<div a=\\"foo\\" /> | ||
<div b=\\"bar\\" /> | ||
</> | ||
, document.querySelector(\`body\`))}" | ||
`); | ||
}); | ||
|
||
it('should format an empty react portal', () => { | ||
const tree = { | ||
type: 'ReactPortal', | ||
containerSelector: 'body', | ||
childrens: [], | ||
}; | ||
|
||
expect( | ||
formatReactPortalNode(tree, false, 0, defaultOptions) | ||
).toMatchInlineSnapshot( | ||
`"{ReactDOM.createPortal(null, document.querySelector(\`body\`))}"` | ||
); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* @flow */ | ||
|
||
const getFunctionTypeName = (functionType: Function): string => { | ||
if (!functionType.name || functionType.name === '_default') { | ||
return 'Component'; | ||
} | ||
return functionType.name; | ||
}; | ||
|
||
export default getFunctionTypeName; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
/* @flow */ | ||
|
||
import React from 'react'; | ||
import getFunctionTypeName from './getFunctionTypeName'; | ||
|
||
function NamedStatelessComponent(props: { children: React.Children }) { | ||
const { children } = props; | ||
return <div>{children}</div>; | ||
} | ||
|
||
const _default = function(props: { children: React.Children }) { | ||
const { children } = props; | ||
return <div>{children}</div>; | ||
}; | ||
|
||
const NamelessComponent = function(props: { children: React.Children }) { | ||
const { children } = props; | ||
return <div>{children}</div>; | ||
}; | ||
|
||
delete NamelessComponent.name; | ||
|
||
describe('getFunctionTypeName(Component)', () => { | ||
it('getFunctionTypeName(NamedStatelessComponent)', () => { | ||
expect(getFunctionTypeName(NamedStatelessComponent)).toEqual( | ||
'NamedStatelessComponent' | ||
); | ||
}); | ||
|
||
it('getFunctionTypeName(_default)', () => { | ||
expect(getFunctionTypeName(_default)).toEqual('Component'); | ||
}); | ||
|
||
it('getFunctionTypeName(NamelessComponent)', () => { | ||
expect(getFunctionTypeName(NamelessComponent)).toEqual('Component'); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* @flow */ | ||
|
||
import { ForwardRef, Memo } from 'react-is'; | ||
import getFunctionTypeName from './getFunctionTypeName'; | ||
|
||
const getWrappedComponentDisplayName = (Component: *): string => { | ||
switch (true) { | ||
case Boolean(Component.displayName): | ||
return Component.displayName; | ||
case Component.$$typeof === Memo: | ||
return getWrappedComponentDisplayName(Component.type); | ||
case Component.$$typeof === ForwardRef: | ||
return getWrappedComponentDisplayName(Component.render); | ||
default: | ||
return getFunctionTypeName(Component); | ||
} | ||
}; | ||
|
||
export default getWrappedComponentDisplayName; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.