-
Notifications
You must be signed in to change notification settings - Fork 82
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
feat(formatting): render children as function #338
base: master
Are you sure you want to change the base?
Changes from all commits
1dde89e
677d5cf
df4a4bf
b97837c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* @flow */ | ||
|
||
import spacer from './spacer'; | ||
import formatTreeNode from './formatTreeNode'; | ||
import type { Options } from './../options'; | ||
import type { ReactFunctionTreeNode } from './../tree'; | ||
|
||
export default ( | ||
node: ReactFunctionTreeNode, | ||
inline: boolean, | ||
lvl: number, | ||
options: Options | ||
): string => { | ||
const { tabStop } = options; | ||
const { type, childrens } = node; | ||
|
||
if (type !== 'ReactFunction') { | ||
throw new Error( | ||
`The "formatReactFunctionNode" function could only format node of type "ReactFunction". Given: ${type}` | ||
); | ||
} | ||
|
||
const functionRender = Array.isArray(childrens) | ||
? childrens | ||
.map(children => formatTreeNode(children, false, lvl + 1, options)) | ||
.join('\n') | ||
: formatTreeNode(childrens, false, lvl + 1, options); | ||
|
||
const out = `{() => ( | ||
${spacer(lvl + 1, tabStop)}${functionRender} | ||
${spacer(lvl, tabStop)})}`; | ||
|
||
return `${out}`; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { | |
createStringTreeNode, | ||
createNumberTreeNode, | ||
createReactElementTreeNode, | ||
createReactFunctionTreeNode, | ||
createReactFragmentTreeNode, | ||
} from './../tree'; | ||
import type { TreeNode } from './../tree'; | ||
|
@@ -71,6 +72,14 @@ const parseReactElement = ( | |
.filter(onlyMeaningfulChildren) | ||
.map(child => parseReactElement(child, options)); | ||
|
||
if (typeof element.props.children === 'function') { | ||
const functionChildrens = parseReactElement( | ||
element.props.children(), | ||
options | ||
); | ||
childrens.push(createReactFunctionTreeNode([functionChildrens])); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One gotcha with this detection method is is case of mixed children (or multiple function as a children): // Multiple functions as child
reactElementToJSXString(
<div>
{() => <div>Hello Foo</div>}
{() => <div>Hello Bar</div>}
</div>,
{
showFunctions: true,
}
)
// Mixed content
reactElementToJSXString(
<div>
{() => <div>Hello Foo</div>}
<span>Some other children</span>
</div>,
{
showFunctions: true,
}
) I know this is should not be frequent or maybe event imaginable but the JSX allow it 🤷♂️ I do some try with you PR, it's a shame that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (supportFragment && element.type === Fragment) { | ||
return createReactFragmentTreeNode(key, childrens); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm worried about the need to call the children function to be able to format it's result. This could have unwanted side effect in the user land.
For example: