-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomment_box.html
84 lines (72 loc) · 2.09 KB
/
comment_box.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
<html>
<head>
<title>Comment Box</title>
<script src="react.min.js"></script>
<script src="reactor.js"></script>
</head>
<body>
<div id="content"></div>
<script type="text/javascript">
var Comment = Reactor.createClass({
jml: function() {
return ["div",["h2", this.props.author, ":"]].concat(this.props.children);
}
});
var CommentList = Reactor.createClass({
jml: function() {
var commentNodes = this.props.data.map(function(comment) {
return [Comment, {author:comment.author}, comment.text];
});
return ["div"].concat(commentNodes);
}
});
var Space = Reactor.createClass({
jml:function() {
return ["div", ["br"]];
}
});
var CommentForm = Reactor.createClass({
handleSubmit: function(e) {
e.preventDefault();
var author = React.findDOMNode(this.refs.author).value.trim();
var text = React.findDOMNode(this.refs.text).value.trim();
if (!text || !author) {
return;
}
this.props.onCommentSubmit({author: author, text: text});
React.findDOMNode(this.refs.author).value = '';
React.findDOMNode(this.refs.text).value = '';
return;
},
jml: function() {
return ["div",["br"],
["form", {onSubmit:this.handleSubmit},
["input",{ref:"author"}],
[Space],
["textarea",{ref:"text"}],
[Space],
["button","Submit"]
]
];
}
})
var CommentBox = Reactor.createClass({
getInitialState: function() {
return {data: []};
},
handleCommentSubmit: function(comment) {
var comments = this.state.data;
var newComments = comments.concat([comment]);
this.setState({data: newComments});
},
jml: function() {
return ["div",["h1", "Comments"],
[CommentList, {data: this.state.data}],
[CommentForm, {onCommentSubmit: this.handleCommentSubmit}]
];
}
});
Reactor.render([CommentBox], document.getElementById('content'));
</script>
</body>
</html>