Skip to content
This repository was archived by the owner on Nov 6, 2019. It is now read-only.

Commit 69226ef

Browse files
committed
Support React-like interface for functional components/elements
This allows JSX to render custom elements defined as `const CustomElement = props = > <button onclick={props.onclick}>{props.label}</button>`
1 parent 770808e commit 69226ef

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

packages/virtualdom/src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,11 @@ type ElementSpecialAttrs = {
649649
* The inline style for the rendered DOM element.
650650
*/
651651
readonly style?: ElementInlineStyle;
652+
653+
/**
654+
* The children attribute for functional custom elements.
655+
*/
656+
children?: ReadonlyArray<VirtualNode>;
652657
};
653658

654659

@@ -1020,6 +1025,12 @@ namespace Private {
10201025
if (node.type === 'text') {
10211026
return document.createTextNode(node.content);
10221027
}
1028+
1029+
// If node is a function, call if with its attrs to return a VirtualNode.
1030+
if (typeof node.tag === 'function') {
1031+
node.attrs.children = node.children;
1032+
node = node.tag(node.attrs) as VirtualElement;
1033+
}
10231034

10241035
// Create the HTML element with the specified tag.
10251036
let element = document.createElement(node.tag);

0 commit comments

Comments
 (0)