-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
106 lines (95 loc) · 2.49 KB
/
index.html
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<title>Need Of React</title>
</head>
<body>
<div class="wrapper">
</div>
<script>
const createDOMFromString = domstring => {
const div = document.createElement("div");
div.innerHTML = domstring;
return div;
};
class Component {
constructor(props) {
this.props = props;
}
setState(state) {
const oldElem = this.elem;
this.state = state;
this._renderDOM();
if (this.onStateChange) this.onStateChange(oldElem, this.elem);
}
_renderDOM() {
this.elem = createDOMFromString(this.render());
if (this.onClick) {
this.elem.addEventListener("click", this.onClick.bind(this));
}
return this.elem;
}
}
class UpVoteButton extends Component {
constructor(props) {
super(props);
this.state = { isUpVoted: false };
}
onClick() {
this.setState({
isUpVoted: !this.state.isUpVoted
});
}
render() {
return `
<button class='upvote-btn' style="background: ${this.props.bgColor}">
<span class='upvote-text'>${
this.state.isUpVoted ? "Cancel" : "UpVote"
}</span>
<span><i class="fa fa-thumbs-up" aria-hidden="true"></i></span>
</button>
`;
}
}
class DownVoteButton extends Component {
constructor(props) {
super(props);
this.state = {
isDownVoted: false
};
}
onClick() {
this.setState({
isDownVoted: !this.state.isDownVoted
});
}
render() {
return `
<button class="downvote-btn" style="background: ${this.props.bgColor}">
<span class="downvote-text">${
this.state.isDownVoted ? "Cancel" : "Downvote"
}</span>
<span><i class="fa fa-thumbs-down" aria-hidden="true"></i></span>
</button>
`;
}
}
const componentMount = (component, container) => {
container.appendChild(component._renderDOM());
component.onStateChange = (oldElem, newElem) => {
container.insertBefore(newElem, oldElem);
container.removeChild(oldElem);
};
};
const wrapper = document.querySelector(".wrapper");
const upVoteButton = new UpVoteButton({ bgColor: "green" });
const downVoteButton = new DownVoteButton({ bgColor: "red" });
componentMount(upVoteButton, wrapper);
componentMount(downVoteButton, wrapper);
</script>
</body>
</html>