mirror of
https://github.com/zhigang1992/reactfire.git
synced 2026-04-11 11:29:59 +08:00
89 lines
2.3 KiB
JavaScript
89 lines
2.3 KiB
JavaScript
/** @jsx React.DOM */
|
|
|
|
// IMPORTANT: Replace below with your Firebase app URL
|
|
var firebaseUrl = "https://my-firebase-app.firebaseio.com/";
|
|
|
|
var converter = new Showdown.converter();
|
|
|
|
|
|
var Comment = React.createClass({
|
|
render: function() {
|
|
var rawMarkup = converter.makeHtml(this.props.children.toString());
|
|
return (
|
|
<div className="comment">
|
|
<h2 className="commentAuthor">{this.props.author}</h2>
|
|
<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
|
|
</div>
|
|
);
|
|
}
|
|
});
|
|
|
|
|
|
var CommentList = React.createClass({
|
|
render: function() {
|
|
var commentNodes = this.props.data.map(function (comment, index) {
|
|
return <Comment key={index} author={comment.author}>{comment.text}</Comment>;
|
|
});
|
|
return <div className="commentList">{commentNodes}</div>;
|
|
}
|
|
});
|
|
|
|
|
|
var CommentForm = React.createClass({
|
|
handleSubmit: function() {
|
|
var author = this.refs.author.getDOMNode().value.trim();
|
|
var text = this.refs.text.getDOMNode().value.trim();
|
|
this.props.onCommentSubmit({author: author, text: text});
|
|
this.refs.author.getDOMNode().value = '';
|
|
this.refs.text.getDOMNode().value = '';
|
|
return false;
|
|
},
|
|
|
|
render: function() {
|
|
return (
|
|
<form className="commentForm" onSubmit={this.handleSubmit}>
|
|
<input type="text" placeholder="Your name" ref="author" />
|
|
<input type="text" placeholder="Say something..." ref="text" />
|
|
<input type="submit" value="Post" />
|
|
</form>
|
|
);
|
|
}
|
|
});
|
|
|
|
|
|
var CommentBox = React.createClass({
|
|
mixins: [ReactFireMixin],
|
|
|
|
handleCommentSubmit: function(comment) {
|
|
// Here we push the update out to Firebase and let ReactFire update this.state.data
|
|
this.firebaseRefs["data"].push(comment);
|
|
},
|
|
|
|
getInitialState: function() {
|
|
return {
|
|
data: []
|
|
};
|
|
},
|
|
|
|
componentWillMount: function() {
|
|
// Here we bind the component to Firebase and it handles all data updates,
|
|
// no need to poll as in the React example.
|
|
this.bindAsArray(new Firebase(firebaseUrl + "commentBox"), "data");
|
|
},
|
|
|
|
render: function() {
|
|
return (
|
|
<div className="commentBox">
|
|
<h1>Comments</h1>
|
|
<CommentList data={this.state.data} />
|
|
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
|
|
</div>
|
|
);
|
|
}
|
|
});
|
|
|
|
React.render(
|
|
<CommentBox />,
|
|
document.getElementById('content')
|
|
);
|