UNPKG

inferno-create-element

Version:

Provides methods to create Inferno VNodes

104 lines (100 loc) 3.14 kB
import { getFlagsForElementVnode, createComponentVNode, createFragment, createVNode } from 'inferno'; function isNullOrUndef(o) { return o === void 0 || o === null; } function isInvalid(o) { return o === null || o === false || o === true || o === void 0; } function isString(o) { return typeof o === 'string'; } function isUndefined(o) { return o === void 0; } function createElement(type, props, ...children) { { if (isInvalid(type)) { throw new Error('Inferno Error: createElement() name parameter cannot be undefined, null, false or true, It must be a string, class, function or forwardRef.'); } } 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 };