Skip to content

Commit

Permalink
binding.component/jsx) Support partials when an array of JSX is ret…
Browse files Browse the repository at this point in the history
…urned.
  • Loading branch information
brianmhunt committed Jun 26, 2018
1 parent f82eafc commit e5ebeae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/tko.binding.component/spec/componentBindingBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,23 @@ describe('Components: Component binding', function () {
o2(undefined)
expect(testNode.children[0].innerHTML).toEqual('<div><!--[jsx placeholder]--></div>')
})

it('inserts a partial when the `template` is an array', function () {
class ViewModel extends components.ComponentABC {
static get template () {
// Passing <div attr={obs}>{o2}</div> through
// babel-plugin-transform-jsx will yield:
return [
{ elementName: 'b', attributes: { }, children: ['x'] },
{ elementName: 'i', attributes: { }, children: ['y'] },
{ elementName: 'em', attributes: { }, children: ['z'] }
]
}
}
ViewModel.register('test-component')
applyBindings(outerViewModel, testNode)
expect(testNode.children[0].innerHTML).toEqual('<b>x</b><i>y</i><em>z</em>')
})
})

describe('slots', function () {
Expand Down
5 changes: 4 additions & 1 deletion packages/tko.binding.component/src/componentBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export default class ComponentBinding extends DescendantBindingHandler {
if (!template) {
throw new Error('Component \'' + componentName + '\' has no template')
}
if (template.elementName) {
const possibleJsxPartial = Array.isArray(template) && template.length
if (possibleJsxPartial && template[0].hasOwnProperty('elementName')) {
virtualElements.setDomNodeChildren(element, template.map(jsxToNode))
} else if (template.elementName) {
virtualElements.setDomNodeChildren(element, [jsxToNode(template)])
} else {
const clonedNodesArray = cloneNodes(template)
Expand Down

0 comments on commit e5ebeae

Please sign in to comment.