-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElement.js
40 lines (34 loc) · 958 Bytes
/
Element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Element {
constructor(tag, props, children) {
this.tag = tag;
this.props = props;
this.children = children;
}
render() {
let ele = document.createElement(this.tag);
for (let prop in this.props) {
ele.setAttribute(prop, this.props[prop]);
}
let children = this.children || [];
let childEle;
if(children instanceof Array) {
children.map(child => {
if (child instanceof Element) {
childEle = child.render();
ele.appendChild(childEle);
} else if (child instanceof HTMLDivElement) {
ele.appendChild(child);
} else {
childEle = document.createTextNode(child);
ele.appendChild(childEle);
}
});
} else {
childEle = document.createTextNode(children);
ele.appendChild(childEle);
}
return ele;
}
}
const el = (tag, props, children) => new Element(tag, props, children);
export default el;