rahisi
Version:
UI library for prototyping ideas for reactive programming.
78 lines • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
class React {
}
React.createElement = (tagName, attributes, ...children) => {
if (typeof tagName === "function") {
return tagName(attributes, children);
}
const attribs = React.getAttributes(attributes);
const kids = React.getChildren(children);
return new index_1.BaseElement(tagName, attribs, kids);
};
React.getAttributes = (attributes) => {
const attribs = new Array();
if (attributes) {
for (const k of Object.keys(attributes)) {
const key = k.toLowerCase().replace("doubleclick", "dblclick");
const attributeValue = attributes[k];
if (key.startsWith("on")) {
const event = key.substring(2);
attribs.push(new index_1.OnHandlerA(event, attributeValue));
continue;
}
switch (key) {
case "classname":
attribs.push(new index_1.NativeAttribute("class", attributeValue));
break;
case "htmlfor":
attribs.push(new index_1.NativeAttribute("for", attributeValue));
break;
case "focus":
attribs.push(new index_1.FocusA(attributeValue));
break;
default:
attribs.push(new index_1.NativeAttribute(key, attributeValue));
break;
}
}
}
return attribs;
};
React.getChildren = (children) => {
const kids = new Array();
for (const child of children) {
React.appendChild(kids, child);
}
return kids;
};
React.appendChild = (kids, child) => {
// <>{condition && <a>Display when condition is true</a>}</>
// if condition is false, the child is a boolean, but we don't want to display anything
if (typeof child === "undefined" || typeof child === "boolean" || child === null) {
return;
}
if (Array.isArray(child)) {
for (const value of child) {
React.appendChild(kids, value);
}
}
else if (typeof child === "string" || typeof child === "number") {
kids.push(new index_1.TextElement(child.toString()));
}
else if (child instanceof index_1.BaseElement
|| child instanceof index_1.TextElement
|| child instanceof index_1.ConditionalRenderElement
|| child instanceof index_1.TemplateElement) {
kids.push(child);
}
else if (typeof child === "function") {
kids.push(new index_1.TextElement(child));
}
else {
kids.push(new index_1.TextElement(String(child)));
}
};
exports.React = React;
//# sourceMappingURL=factory.js.map