|
| 1 | +<!-- index.html --> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Hello React</title> |
| 5 | + <script src="http://fb.me/react-0.12.2.js"></script> |
| 6 | + <script src="http://fb.me/JSXTransformer-0.12.2.js"></script> |
| 7 | + <script src="http://code.jquery.com/jquery-1.10.0.min.js"></script> |
| 8 | + <script src="http://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script> |
| 9 | +</head> |
| 10 | +<body> |
| 11 | +<div id="content"></div> |
| 12 | +<script type="text/jsx"> |
| 13 | + var converter = new Showdown.converter(); |
| 14 | + |
| 15 | + var CommentList = React.createClass({ |
| 16 | + render: function() { |
| 17 | + var commentNodes = this.props.data.map(function (comment) { |
| 18 | + return ( |
| 19 | + <Comment author={comment.author}> |
| 20 | + {comment.text} |
| 21 | + </Comment> |
| 22 | + ); |
| 23 | + }); |
| 24 | + return ( |
| 25 | + <div className="commentList"> |
| 26 | + {commentNodes} |
| 27 | + </div> |
| 28 | + ); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + var CommentForm = React.createClass({ |
| 33 | + handleSubmit: function(e) { |
| 34 | + e.preventDefault(); |
| 35 | + var author = this.refs.author.getDOMNode().value.trim(); |
| 36 | + var text = this.refs.text.getDOMNode().value.trim(); |
| 37 | + if (!text || !author) { |
| 38 | + return; |
| 39 | + } |
| 40 | + // TODO: send request to the server |
| 41 | + this.refs.author.getDOMNode().value = ''; |
| 42 | + this.refs.text.getDOMNode().value = ''; |
| 43 | + }, |
| 44 | + render: function() { |
| 45 | + return ( |
| 46 | + <form className="commentForm" onSubmit={this.handleSubmit}> |
| 47 | + <input type="text" placeholder="Your name" ref="author" /> |
| 48 | + <input type="text" placeholder="Say something..." ref="text" /> |
| 49 | + <input type="submit" value="Post" /> |
| 50 | + </form> |
| 51 | + ); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + var CommentBox = React.createClass({ |
| 56 | + loadCommentsFromServer: function() { |
| 57 | + $.ajax({ |
| 58 | + url: this.props.url, |
| 59 | + dataType: 'json', |
| 60 | + success: function(data) { |
| 61 | + this.setState({data: data}); |
| 62 | + }.bind(this), |
| 63 | + error: function(xhr, status, err) { |
| 64 | + console.error(this.props.url, status, err.toString()); |
| 65 | + }.bind(this) |
| 66 | + }); |
| 67 | + }, |
| 68 | + handleCommentSubmit: function(comment) { |
| 69 | + // TODO: submit to the server and refresh the list |
| 70 | + }, |
| 71 | + getInitialState: function() { |
| 72 | + return {data: []}; |
| 73 | + }, |
| 74 | + componentDidMount: function() { |
| 75 | + this.loadCommentsFromServer(); |
| 76 | + setInterval(this.loadCommentsFromServer, this.props.pollInterval); |
| 77 | + }, |
| 78 | + render: function() { |
| 79 | + return ( |
| 80 | + <div className="commentBox"> |
| 81 | + <h1>Comments</h1> |
| 82 | + <CommentList data={this.state.data} /> |
| 83 | + <CommentForm onCommentSubmit={this.handleCommentSubmit} /> |
| 84 | + </div> |
| 85 | + ); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + var Comment = React.createClass({ |
| 90 | + render: function() { |
| 91 | + var rawMarkup = converter.makeHtml(this.props.children.toString()); |
| 92 | + return ( |
| 93 | + <div className="comment"> |
| 94 | + <h2 className="commentAuthor"> |
| 95 | + {this.props.author} |
| 96 | + </h2> |
| 97 | + <span dangerouslySetInnerHTML={{__html: rawMarkup}} /> |
| 98 | + </div> |
| 99 | + ); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + var data = [ |
| 104 | + {author: "Pete Hunt", text: "This is one comment"}, |
| 105 | + {author: "Jordan Walke", text: "This is *another* comment"} |
| 106 | + ]; |
| 107 | + |
| 108 | + React.render( |
| 109 | + <CommentBox url="comments.json" pollInterval={2000} />, |
| 110 | + document.getElementById('content') |
| 111 | + ); |
| 112 | +</script> |
| 113 | +</body> |
| 114 | +</html> |
0 commit comments