-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoy-react.js
67 lines (61 loc) · 1.35 KB
/
toy-react.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class ElementWrapper {
constructor(type) {
this.root = document.createElement(type);
}
setAttribute(name, value) {
this.root.setAttribute(name, value);
}
appendChild(component) {
this.root.appendChild(component.root);
}
}
class TextWrapper {
constructor(type) {
this.root = document.createTextNode(type);
}
}
class Component {
constructor() {
this.props = Object.create(null);
this.children = [];
this._root = null;
}
setAttribute(name, value) {
this.props[name] = value;
}
appendChild(component) {
this.children.push(component);
}
get root() {
if (!this._root) {
this._root = this.render().root;
}
return this._root;
}
}
function createElement(type, attributes, ...children) {
let e;
if (typeof type === 'string') {
e = document.createElement(type);
} else {
e = new type;
}
for (const p in attributes) {
e.setAttribute(p, attributes[p]);
}
for (const child of children) {
if (typeof child === 'string') {
e.appendChild(new TextWrapper(child));
} else {
e.appendChild(child);
}
}
return e;
}
function render(component, el) {
}
module.exports = {
createElement,
Component,
render
}