@@ -6,35 +6,37 @@ import Nav from 'react-bootstrap/lib/Nav';
6
6
import NavItem from 'react-bootstrap/lib/NavItem' ;
7
7
import Alert from 'react-bootstrap/lib/Alert' ;
8
8
import ReactCSSTransitionGroup from 'react/lib/ReactCSSTransitionGroup' ;
9
+ import _ from 'lodash' ;
9
10
10
11
const emptyComment = { author : '' , text : '' } ;
11
12
const textPlaceholder = 'Say something using markdown...' ;
12
13
13
- const CommentForm = React . createClass ( {
14
- displayName : 'CommentForm' ,
14
+ class CommentForm extends React . Component {
15
+ constructor ( props , context ) {
16
+ super ( props , context ) ;
17
+ this . state = {
18
+ formMode : 0 ,
19
+ comment : emptyComment ,
20
+ } ;
21
+
22
+ _ . bindAll ( this , '_handleSelect' , '_handleChange' , '_handleSubmit' , '_resetAndFocus' ) ;
23
+ }
24
+
25
+ static displayName = 'CommentForm' ;
15
26
16
- propTypes : {
27
+ static propTypes = {
17
28
ajaxSending : PropTypes . bool . isRequired ,
18
29
actions : PropTypes . object . isRequired ,
19
30
error : PropTypes . any ,
20
- } ,
21
-
22
- getInitialState ( ) {
23
- return {
24
- formMode : 0 ,
25
- comment : emptyComment ,
26
- } ;
27
- } ,
31
+ } ;
28
32
29
- handleSelect ( selectedKey ) {
33
+ _handleSelect ( selectedKey ) {
30
34
this . setState ( { formMode : selectedKey } ) ;
31
- } ,
35
+ }
32
36
33
- handleChange ( ) {
37
+ _handleChange ( ) {
34
38
let comment ;
35
39
36
- // This could also be done using ReactLink:
37
- // http://facebook.github.io/react/docs/two-way-binding-helpers.html
38
40
if ( this . state . formMode < 2 ) {
39
41
comment = {
40
42
author : this . refs . author . getValue ( ) ,
@@ -50,17 +52,17 @@ const CommentForm = React.createClass({
50
52
}
51
53
52
54
this . setState ( { comment } ) ;
53
- } ,
55
+ }
54
56
55
- handleSubmit ( e ) {
57
+ _handleSubmit ( e ) {
56
58
e . preventDefault ( ) ;
57
59
const { actions } = this . props ;
58
60
actions
59
61
. submitComment ( this . state . comment )
60
- . then ( this . resetAndFocus ) ;
61
- } ,
62
+ . then ( this . _resetAndFocus ) ;
63
+ }
62
64
63
- resetAndFocus ( ) {
65
+ _resetAndFocus ( ) {
64
66
// Don't reset a form that didn't submit, this results in data loss
65
67
if ( this . props . error ) return ;
66
68
@@ -75,13 +77,13 @@ const CommentForm = React.createClass({
75
77
}
76
78
77
79
ref . focus ( ) ;
78
- } ,
80
+ }
79
81
80
- formHorizontal ( ) {
82
+ _formHorizontal ( ) {
81
83
return (
82
84
< div >
83
85
< hr />
84
- < form className = "commentForm form-horizontal" onSubmit = { this . handleSubmit } >
86
+ < form className = "commentForm form-horizontal" onSubmit = { this . _handleSubmit } >
85
87
< Input
86
88
type = "text"
87
89
label = "Name"
@@ -90,7 +92,7 @@ const CommentForm = React.createClass({
90
92
wrapperClassName = "col-sm-10"
91
93
ref = "author"
92
94
value = { this . state . comment . author }
93
- onChange = { this . handleChange }
95
+ onChange = { this . _handleChange }
94
96
disabled = { this . props . ajaxSending }
95
97
/>
96
98
< Input
@@ -101,7 +103,7 @@ const CommentForm = React.createClass({
101
103
wrapperClassName = "col-sm-10"
102
104
ref = "text"
103
105
value = { this . state . comment . text }
104
- onChange = { this . handleChange }
106
+ onChange = { this . _handleChange }
105
107
disabled = { this . props . ajaxSending }
106
108
/>
107
109
< div className = "form-group" >
@@ -117,20 +119,20 @@ const CommentForm = React.createClass({
117
119
</ form >
118
120
</ div >
119
121
) ;
120
- } ,
122
+ }
121
123
122
- formStacked ( ) {
124
+ _formStacked ( ) {
123
125
return (
124
126
< div >
125
127
< hr />
126
- < form className = "commentForm form" onSubmit = { this . handleSubmit } >
128
+ < form className = "commentForm form" onSubmit = { this . _handleSubmit } >
127
129
< Input
128
130
type = "text"
129
131
label = "Name"
130
132
placeholder = "Your Name"
131
133
ref = "author"
132
134
value = { this . state . comment . author }
133
- onChange = { this . handleChange }
135
+ onChange = { this . _handleChange }
134
136
disabled = { this . props . ajaxSending }
135
137
/>
136
138
< Input
@@ -139,7 +141,7 @@ const CommentForm = React.createClass({
139
141
placeholder = { textPlaceholder }
140
142
ref = "text"
141
143
value = { this . state . comment . text }
142
- onChange = { this . handleChange }
144
+ onChange = { this . _handleChange }
143
145
disabled = { this . props . ajaxSending }
144
146
/>
145
147
< input
@@ -151,13 +153,13 @@ const CommentForm = React.createClass({
151
153
</ form >
152
154
</ div >
153
155
) ;
154
- } ,
156
+ }
155
157
156
- formInline ( ) {
158
+ _formInline ( ) {
157
159
return (
158
160
< div >
159
161
< hr />
160
- < form className = "commentForm form" onSubmit = { this . handleSubmit } >
162
+ < form className = "commentForm form" onSubmit = { this . _handleSubmit } >
161
163
< Input label = "Inline Form" wrapperClassName = "wrapper" >
162
164
< Row >
163
165
< Col xs = { 3 } >
@@ -167,7 +169,7 @@ const CommentForm = React.createClass({
167
169
placeholder = "Your Name"
168
170
ref = "inlineAuthor"
169
171
value = { this . state . comment . author }
170
- onChange = { this . handleChange }
172
+ onChange = { this . _handleChange }
171
173
disabled = { this . props . ajaxSending }
172
174
/>
173
175
</ Col >
@@ -178,7 +180,7 @@ const CommentForm = React.createClass({
178
180
placeholder = { textPlaceholder }
179
181
ref = "inlineText"
180
182
value = { this . state . comment . text }
181
- onChange = { this . handleChange }
183
+ onChange = { this . _handleChange }
182
184
disabled = { this . props . ajaxSending }
183
185
/>
184
186
</ Col >
@@ -195,9 +197,9 @@ const CommentForm = React.createClass({
195
197
</ form >
196
198
</ div >
197
199
) ;
198
- } ,
200
+ }
199
201
200
- errorWarning ( ) {
202
+ _errorWarning ( ) {
201
203
// If there is no error, there is nothing to add to the DOM
202
204
if ( ! this . props . error ) return undefined ;
203
205
return (
@@ -206,19 +208,19 @@ const CommentForm = React.createClass({
206
208
A server error prevented your comment from being saved. Please try again.
207
209
</ Alert >
208
210
) ;
209
- } ,
211
+ }
210
212
211
213
render ( ) {
212
214
let inputForm ;
213
215
switch ( this . state . formMode ) {
214
216
case 0 :
215
- inputForm = this . formHorizontal ( ) ;
217
+ inputForm = this . _formHorizontal ( ) ;
216
218
break ;
217
219
case 1 :
218
- inputForm = this . formStacked ( ) ;
220
+ inputForm = this . _formStacked ( ) ;
219
221
break ;
220
222
case 2 :
221
- inputForm = this . formInline ( ) ;
223
+ inputForm = this . _formInline ( ) ;
222
224
break ;
223
225
default :
224
226
throw new Error ( `Unknown form mode: ${ this . state . formMode } .` ) ;
@@ -230,18 +232,18 @@ const CommentForm = React.createClass({
230
232
transitionEnterTimeout = { 300 }
231
233
transitionLeaveTimeout = { 300 }
232
234
>
233
- { this . errorWarning ( ) }
235
+ { this . _errorWarning ( ) }
234
236
</ ReactCSSTransitionGroup >
235
237
236
- < Nav bsStyle = "pills" activeKey = { this . state . formMode } onSelect = { this . handleSelect } >
238
+ < Nav bsStyle = "pills" activeKey = { this . state . formMode } onSelect = { this . _handleSelect } >
237
239
< NavItem eventKey = { 0 } > Horizontal Form</ NavItem >
238
240
< NavItem eventKey = { 1 } > Stacked Form</ NavItem >
239
241
< NavItem eventKey = { 2 } > Inline Form</ NavItem >
240
242
</ Nav >
241
243
{ inputForm }
242
244
</ div >
243
245
) ;
244
- } ,
245
- } ) ;
246
+ }
247
+ }
246
248
247
249
export default CommentForm ;
0 commit comments