-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
121 lines (111 loc) · 4.26 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>React - Component</title>
<script src="scripts/react.min.js"></script>
<script src="scripts/react-dom.min.js"></script>
<script src="scripts/browser.min.js"></script>
<link href="styles/styles.css" rel="stylesheet" />
</head>
<body>
<div id="container"></div>
<script type="text/babel">
var Note = React.createClass({
getInitialState: function(){
return { editing: false };
},
edit: function() {
this.setState({ editing: true });
},
remove: function() {
this.props.removeNote(this.props.index);
},
save: function(){
this.props.updateNote(this.refs.textAreaRef.value, this.props.index);
this.setState({ editing: false })
},
renderNormal: function(){
return(
<div className = "note-container">
<div className="note-body">
{this.props.children}
</div>
<div className="note-footer">
<button onClick={this.edit} className="btn success">Edit</button>
<button onClick={this.remove} className="btn danger">Remove</button>
</div>
</div>
);
},
renderForm : function(){
return(
<div className = "note-container">
<textArea
ref = "textAreaRef"
className="note-edit-area" defaultValue={this.props.children}></textArea>
<div className="note-footer">
<button onClick={this.save} className="btn success">save</button>
</div>
</div>
);
},
render: function () {
if(this.state.editing){
return this.renderForm();
} else{
return this.renderNormal();
}
} });
var Container = React.createClass({
getInitialState: function(){
return {
comments: [
'This is my very first note.',
'Second and sweet note.',
'Third one of course!'
]
};
},
prepareNote: function(note, i){
return(
<Note key={i} index = {i} updateNote={this.updateNote} removeNote={this.removeNote}>
{note}
</Note>
);
},
removeNote: function(i){
var notes = this.state.comments
notes.splice(i, 1)
this.setState({comments: notes});
},
updateNote: function(newNote, i){
var notes = this.state.comments;
notes[i] = newNote;
this.setState({comments: notes});
},
addNote : function(text){
var notes = this.state.comments;
notes.push(text);
this.setState({comments: notes});
},
render: function (){
return(
<div className="notes-container">
<div>
<button className="btn primary"
onClick={this.addNote.bind(null, 'Initial note')}> Add note</button>
</div>
{
this.state.comments.map(this.prepareNote)
}
</div>
)}
});
ReactDOM.render(
<Container/>,
document.getElementById('container'));
</script>
</body>
</html>