Forráskód Böngészése

Listing 17: adding comments - submitting the form.

Frederic G. MARAND 8 éve
szülő
commit
f41ed410f5
1 módosított fájl, 11 hozzáadás és 1 törlés
  1. 11 1
      public/scripts/tutorial.js

+ 11 - 1
public/scripts/tutorial.js

@@ -42,13 +42,23 @@ let CommentForm = React.createClass({
   handleAuthorChange: function (e) {
     this.setState({ author: e.target.value });
   },
+  handleSubmit: function (e) {
+    e.preventDefault();
+    let author = this.state.author.trim();
+    let text = this.state.text.trim();
+    if (!text || !author) {
+      return;
+    }
+    // TODO send request to the server.
+    this.setState({ author: '', text: '' });
+  },
   handleTextChange: function (e) {
     this.setState({ text: e.target.value });
   },
   render: function () {
     return (
       <div className="commentForm">
-        <form className="commentForm">
+        <form className="commentForm" onSubmit={this.handleSubmit}>
           <input
             type="text"
             placeholder="Your name"