Ver código fonte

Listing 16: adding comments - controlled components.

Frederic G. MARAND 8 anos atrás
pai
commit
1c3a801ac8
1 arquivos alterados com 24 adições e 2 exclusões
  1. 24 2
      public/scripts/tutorial.js

+ 24 - 2
public/scripts/tutorial.js

@@ -33,12 +33,34 @@ let CommentList = React.createClass({
 });
 
 let CommentForm = React.createClass({
+  getInitialState: function () {
+    return {
+      author: "",
+      text: ""
+    };
+  },
+  handleAuthorChange: function (e) {
+    this.setState({ author: e.target.value });
+  },
+  handleTextChange: function (e) {
+    this.setState({ text: e.target.value });
+  },
   render: function () {
     return (
       <div className="commentForm">
         <form className="commentForm">
-          <input type="text" placeholder="Your name" />
-          <input type="text" placeholder="Say something..." />
+          <input
+            type="text"
+            placeholder="Your name"
+            value={this.state.author}
+            onChange={this.handleAuthorChange}
+          />
+          <input
+            type="text"
+            placeholder="Say something..."
+            value={this.state.text}
+            onChange={this.handleTextChange}
+          />
           <input type="submit" value="Post" />
         </form>
       </div>