inferno-create-element
Version:
Provides methods to create Inferno VNodes
96 lines (92 loc) • 2.84 kB
JavaScript
import { getFlagsForElementVnode, createComponentVNode, createFragment, createVNode } from 'inferno';
function isNullOrUndef(o) {
return o === void 0 || o === null;
}
function isString(o) {
return typeof o === 'string';
}
function isUndefined(o) {
return o === void 0;
}
function createElement(type, props, ...children) {
let definedChildren;
let ref = null;
let key = null;
let className = null;
let flags;
let newProps;
const childLen = children.length;
if (childLen === 1) {
definedChildren = children[0];
} else if (childLen > 1) {
definedChildren = [];
for (let i = 0; i < childLen; i++) {
definedChildren.push(children[i]);
}
}
if (isString(type)) {
flags = getFlagsForElementVnode(type);
if (!isNullOrUndef(props)) {
newProps = {};
for (const prop in props) {
if (prop === 'className' || prop === 'class') {
className = props[prop];
} else if (prop === 'key') {
key = props.key;
} else if (prop === 'children' && isUndefined(definedChildren)) {
definedChildren = props.children; // always favour children args over props
} else if (prop === 'ref') {
ref = props.ref;
} else {
if (prop === 'contenteditable') {
flags |= 4096 /* VNodeFlags.ContentEditable */;
}
newProps[prop] = props[prop];
}
}
}
} else {
flags = 2 /* VNodeFlags.ComponentUnknown */;
if (!isUndefined(definedChildren)) {
if (!props) {
props = {};
}
props.children = definedChildren;
}
if (!isNullOrUndef(props)) {
newProps = {};
for (const prop in props) {
if (prop === 'key') {
key = props.key;
} else if (prop === 'ref') {
ref = props.ref;
} else {
switch (prop) {
case 'onComponentDidAppear':
case 'onComponentDidMount':
case 'onComponentDidUpdate':
case 'onComponentShouldUpdate':
case 'onComponentWillDisappear':
case 'onComponentWillMount':
case 'onComponentWillUnmount':
case 'onComponentWillUpdate':
if (!ref) {
ref = {};
}
ref[prop] = props[prop];
break;
default:
newProps[prop] = props[prop];
break;
}
}
}
}
return createComponentVNode(flags, type, newProps, key, ref);
}
if (flags & 8192 /* VNodeFlags.Fragment */) {
return createFragment(childLen === 1 ? [definedChildren] : definedChildren, 0 /* ChildFlags.UnknownChildren */, key);
}
return createVNode(flags, type, className, definedChildren, 0 /* ChildFlags.UnknownChildren */, newProps, key, ref);
}
export { createElement };