1- /** @jsx React.DOM */
2-
31var $ = require ( 'jquery' ) ;
4- module React from 'react/addons' ;
2+ var React = require ( 'react/addons' ) ;
53
64// Next line is necessary for exposing React to browser for
75// the React Developer Tools: http://facebook.github.io/react/blog/2014/01/02/react-chrome-developer-tools.html
@@ -34,6 +32,7 @@ var CommentBox = React.createClass({
3432 logError : function ( xhr , status , err ) {
3533 console . error ( `Error loading comments from server!\nURL is ${ this . props . url } \nstatus is ${ status } \nerr is ${ err . toString ( ) } ` ) ;
3634 } ,
35+
3736 loadCommentsFromServer : function ( ) {
3837 $ . ajax ( {
3938 url : this . props . url ,
@@ -42,6 +41,7 @@ var CommentBox = React.createClass({
4241 } , this . logError ) ;
4342 } ,
4443 emptyFormData : { author : "" , text : "" } ,
44+
4545 handleCommentSubmit : function ( ) {
4646 // `setState` accepts a callback. To avoid (improbable) race condition,
4747 // `we'll send the ajax request right after we optimistically set the new
@@ -61,13 +61,15 @@ var CommentBox = React.createClass({
6161 this . setState ( { ajaxSending : false } ) ;
6262 } ) ;
6363 } ,
64+
6465 getInitialState : function ( ) {
6566 return {
6667 data : [ ] ,
6768 formData : this . emptyFormData ,
6869 ajaxSending : false
6970 } ;
7071 } ,
72+
7173 componentDidMount : function ( ) {
7274 this . loadCommentsFromServer ( ) ;
7375 setInterval ( this . loadCommentsFromServer , this . props . pollInterval ) ;
@@ -78,6 +80,7 @@ var CommentBox = React.createClass({
7880 formData : obj
7981 } )
8082 } ,
83+
8184 render : function ( ) {
8285 return (
8386 < div className = "commentBox container" >
@@ -116,31 +119,36 @@ var CommentForm = React.createClass({
116119 formMode : 0
117120 } ;
118121 } ,
122+
119123 handleSubmit : function ( e ) {
120124 e . preventDefault ( ) ;
121125 this . props . onCommentSubmit ( ) ;
122126 return ;
123127 } ,
128+
124129 handleSelect : function ( selectedKey ) {
125130 this . setState ( { formMode : selectedKey } ) ;
126131 } ,
132+
127133 handleChange : function ( ) {
128134 // This could also be done using ReactLink:
129135 // http://facebook.github.io/react/docs/two-way-binding-helpers.html
130- var props ;
131- if ( this . props . formMode == 2 ) {
132- props = {
133- author : this . refs . author . getDOMNode ( ) . value ,
134- text : this . refs . text . getDOMNode ( ) . value
135- }
136- } else {
137- props = {
136+ var obj ;
137+ if ( this . state . formMode < 2 ) {
138+ obj = {
138139 author : this . refs . author . getValue ( ) ,
139140 text : this . refs . text . getValue ( )
140141 }
142+ } else {
143+ // This is different because the input is a native HTML element rather than a React element
144+ obj = {
145+ author : this . refs . inlineAuthor . getDOMNode ( ) . value ,
146+ text : this . refs . inlineText . getDOMNode ( ) . value
147+ }
141148 }
142- this . props . onChange ( props ) ;
149+ this . props . onChange ( obj ) ;
143150 } ,
151+
144152 formHorizontal : function ( ) {
145153 return (
146154 < div > < hr />
@@ -151,6 +159,7 @@ var CommentForm = React.createClass({
151159 </ form > </ div >
152160 ) ;
153161 } ,
162+
154163 formStacked : function ( ) {
155164 return (
156165 < div > < hr />
@@ -168,10 +177,10 @@ var CommentForm = React.createClass({
168177 < Input label = "Inline Form" wrapperClassName = "wrapper" >
169178 < Row >
170179 < Col xs = { 3 } >
171- < input type = "text" className = "form-control" placeholder = "Your Name" ref = "author " value = { this . props . formData . author } onChange = { this . handleChange } disabled = { this . props . ajaxSending } />
180+ < input type = "text" className = "form-control" placeholder = "Your Name" ref = "inlineAuthor " value = { this . props . formData . author } onChange = { this . handleChange } disabled = { this . props . ajaxSending } />
172181 </ Col >
173182 < Col xs = { 8 } >
174- < input type = "text" className = "form-control" placeholder = "Say something..." ref = "text " value = { this . props . formData . text } onChange = { this . handleChange } disabled = { this . props . ajaxSending } />
183+ < input type = "text" className = "form-control" placeholder = "Say something..." ref = "inlineText " value = { this . props . formData . text } onChange = { this . handleChange } disabled = { this . props . ajaxSending } />
175184 </ Col >
176185 < Col xs = { 1 } >
177186 < input type = "submit" className = "btn btn-primary" value = "Post" disabled = { this . props . ajaxSending } />
@@ -196,9 +205,9 @@ var CommentForm = React.createClass({
196205 return (
197206 < div >
198207 < Nav bsStyle = "pills" activeKey = { this . state . formMode } onSelect = { this . handleSelect } >
199- < NavItem key = { 0 } > Horizontal Form</ NavItem >
200- < NavItem key = { 1 } > Stacked Form</ NavItem >
201- < NavItem key = { 2 } > Inline Form</ NavItem >
208+ < NavItem eventKey = { 0 } > Horizontal Form</ NavItem >
209+ < NavItem eventKey = { 1 } > Stacked Form</ NavItem >
210+ < NavItem eventKey = { 2 } > Inline Form</ NavItem >
202211 </ Nav >
203212 { inputForm }
204213 </ div >
0 commit comments