Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit dba782d

Browse files
committed
Add support for JSX dynamic child expressions
1 parent 9445417 commit dba782d

File tree

5 files changed

+32
-33
lines changed

5 files changed

+32
-33
lines changed

package-lock.json

+8-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "babel-plugin-jsx-dom-expressions",
33
"description": "A JSX to DOM plugin that wraps expressions for fine grained change detection",
4-
"version": "0.8.1",
4+
"version": "0.8.2",
55
"author": "Ryan Carniato",
66
"license": "MIT",
77
"repository": {

src/index.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,21 @@ export default (babel) => {
127127
function transformComponentChildren(path, children, opts) {
128128
const filteredChildren = filterChildren(children);
129129
if (!filteredChildren.length) return;
130-
let child = filteredChildren.length === 1 ?
131-
filteredChildren[0] :
132-
t.JSXFragment(t.JSXOpeningFragment(), t.JSXClosingFragment(), children);
130+
let child, dynamic;
131+
if (filteredChildren.length === 1) {
132+
if (t.isJSXExpressionContainer(filteredChildren[0])) dynamic = checkParens(filteredChildren[0], path)
133+
child = filteredChildren[0]
134+
} else child = t.JSXFragment(t.JSXOpeningFragment(), t.JSXClosingFragment(), children);
135+
133136
child = generateHTMLNode(path, child, opts);
134137
if (child == null) return;
135138
if (child.id) {
136139
registerTemplate(path, child, filteredChildren.length !== 1);
137140
if (!child.exprs.length && child.decl.declarations.length === 1)
138-
return child.decl.declarations[0].init;
139-
else return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([child.decl, ...child.exprs, t.returnStatement(child.id)])), []);
141+
return [child.decl.declarations[0].init];
142+
else return [t.callExpression(t.arrowFunctionExpression([], t.blockStatement([child.decl, ...child.exprs, t.returnStatement(child.id)])), [])];
140143
}
141-
return child.exprs[0];
144+
return [child.exprs[0], dynamic];
142145
}
143146

144147
// reduce unnecessary refs
@@ -230,9 +233,11 @@ export default (babel) => {
230233
}
231234
});
232235

233-
const children = transformComponentChildren(path, jsx.children, opts);
234-
if (children)
235-
runningObject.push(t.objectProperty(t.identifier("children"), children));
236+
const childResult = transformComponentChildren(path, jsx.children, opts);
237+
if (childResult && childResult[0]) {
238+
childResult[1] && dynamic.push(t.stringLiteral('children'))
239+
runningObject.push(t.objectProperty(t.identifier("children"), childResult[0]));
240+
}
236241

237242
if (runningObject.length)
238243
props.push(t.objectExpression(runningObject));

test/__fixtures__/subTemplates/code.js

+4
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ const template4 = (
4141
<Child>{() =>
4242
<div />
4343
}</Child>
44+
)
45+
46+
const template5 = (
47+
<Child>{( dynamicValue )}</Child>
4448
)

test/__fixtures__/subTemplates/output.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,8 @@ const template3 = Child({
8383
});
8484
const template4 = Child({
8585
children: () => _tmpl$6.content.firstChild.cloneNode(true)
86-
});
86+
});
87+
88+
const template5 = _$createComponent(Child, {
89+
children: () => dynamicValue
90+
}, ["children"]);

0 commit comments

Comments
 (0)