Last active
August 14, 2019 16:23
-
-
Save Tevinthuku/e1959fb0b044dda7f22db05917749c2c to your computer and use it in GitHub Desktop.
our very own createElement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TEXT_ELEMENT = "TEXT"; | |
/** | |
* @param {string} type - the node type | |
* @param {?object} configObject - the props | |
* @param {?...any} args - the children array | |
* @returns {object} - to be called by tevreact.render | |
*/ | |
export function createElement(type, configObject, ...args) { | |
const props = Object.assign({}, configObject); | |
const hasChildren = args.length > 0; | |
const nodeChildren = hasChildren ? [...args] : []; | |
props.children = nodeChildren | |
.filter(Boolean) | |
.map(c => (c instanceof Object ? c : createTextElement(c))); | |
return { type, props }; | |
} | |
/** | |
* @param {string} nodeValue - the text of the node | |
* @returns {object} - a call to createElement | |
*/ | |
function createTextElement(nodeValue) { | |
return createElement(TEXT_ELEMENT, { nodeValue, children: [] }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment