Skip to content

Commit

Permalink
fix(render): allow mapping of childNode to functional component (#2548)
Browse files Browse the repository at this point in the history
  • Loading branch information
George-Payne committed Jul 22, 2020
1 parent 781180d commit d0176c9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/runtime/vdom/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ const convertToPublic = (node: d.VNode): d.ChildNode => ({
});

const convertToPrivate = (node: d.ChildNode): d.VNode => {
if (typeof node.vtag === 'function') {
const vnodeData = { ...node.vattrs };

if (node.vkey) {
vnodeData.key = node.vkey;
}

if (node.vname) {
vnodeData.name = node.vname;
}

return h(
node.vtag,
vnodeData,
...node.vchildren || [],
);
}

const vnode = newVNode(node.vtag as any, node.vtext);
vnode.$attrs$ = node.vattrs;
vnode.$children$ = node.vchildren;
Expand Down
49 changes: 49 additions & 0 deletions src/runtime/vdom/test/h.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,5 +452,54 @@ describe('h()', () => {
},
]);
});

it('changing the vtag to a functional component should expand the component', () => {
const ReplacementCmp: d.FunctionalComponent = (nodeData, children) => {
return h('article', nodeData, h('p', null, ...children));
};
const FunctionalCmp: d.FunctionalComponent = (_nodeData, children, util) => {
return util.map(children, child => {
return {
...child,
vtag: child.vtag === 'div' ? ReplacementCmp : child.vtag,
};
});
};
const vnode = h(FunctionalCmp, null, h('div', { id: 'blue' }, 'innerText'), h('span', null));

expect(vnode).toEqual([
{
$flags$: 0,
$tag$: 'article',
$text$: null,
$elm$: null,
$children$: [
{
$flags$: 0,
$tag$: 'p',
$text$: null,
$elm$: null,
$children$: [newVNode(null, 'innerText')],
$attrs$: null,
$key$: null,
$name$: null,
},
],
$attrs$: { id: 'blue' },
$key$: null,
$name$: null,
},
{
$flags$: 0,
$tag$: 'span',
$text$: null,
$elm$: null,
$children$: null,
$attrs$: null,
$key$: null,
$name$: null,
},
]);
});
});
});

0 comments on commit d0176c9

Please sign in to comment.