-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactKopplung.js
73 lines (73 loc) · 1.72 KB
/
ReactKopplung.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
68
69
70
71
72
73
function ReactKopplung(){
this._parent = null;
this.setParent = function(obj){
this._parent = obj;
return this;
}
this.parent = function(obj){
return this._parent;
}
this._rootNode = null;
this.setRoot = function(el){
this._node = (el == '<>') ? React.Fragment: el;
this._rootNode = this;
return this;
}
this.rootNode = function(obj){
return this._rootNode;
}
this.copyRoot = function(obj){
this._rootNode = obj;
return this;
}
this._node = '';
this.setNode = function(el){
this._node = el;
return this;
}
this._props = {};
this.setAllProps = function(obj){
this._props = obj;
return this;
}
this.setProp = function(key, val){
this._props[key] = val;
return this;
}
this.removeProp = function(key){
delete this._props[key];
return this;
}
this._children = [];
this.child = function(el){
const childObj = new ReactKopplung();
childObj.setNode(el).copyRoot(this._rootNode).setParent(this);
this._children.push(childObj);
return childObj;
}
this.mapChildren = function(func){
const data = this._data;
for (let i = 0; i < data.length; i++){
func(data[i], i, this, data, func); //func arg to allow recursive calls
}
return this;
}
this._data = null;
this.data = function(val){
this._data = val;
return this;
}
this._text = '';
this.setText = function(val){
this._text = val;
return this;
}
this.render = function(){
const finalChildren = this._children.map((child, index) =>{
// Call the render function recursively
return child.render();
})
finalChildren.unshift(this._text);
return React.createElement(this._node, this._props, finalChildren);
}
}