Skip to content

Commit b64dcfd

Browse files
committed
Merge pull request #140 from rstudner/es6-react-classes
ES6 all React stuff.
2 parents 377fe06 + 9660157 commit b64dcfd

File tree

11 files changed

+127
-112
lines changed

11 files changed

+127
-112
lines changed

client/.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ rules:
1414
indent: [1, 2, { SwitchCase: 1, VariableDeclarator: 2 }]
1515
react/sort-comp: 0
1616
react/jsx-quotes: 1
17-
id-length: [2, {"exceptions": ["e", "i"]}]
17+
id-length: [2, {"exceptions": ["e", "i", "_"]}]

client/app/components/Comment.jsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import React, { PropTypes } from 'react';
22
import marked from 'marked';
33

4-
const Comment = React.createClass({
5-
displayName: 'Comment',
6-
7-
propTypes: {
4+
class Comment extends React.Component {
5+
static displayName = 'Comment';
6+
static propTypes = {
87
author: PropTypes.string.isRequired,
98
text: PropTypes.string.isRequired,
10-
},
9+
};
1110

1211
render() {
1312
const { author, text } = this.props;
@@ -20,7 +19,7 @@ const Comment = React.createClass({
2019
<span dangerouslySetInnerHTML={{__html: rawMarkup}}/>
2120
</div>
2221
);
23-
},
24-
});
22+
}
23+
}
2524

2625
export default Comment;

client/app/components/CommentBox.jsx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,51 @@ import React, { PropTypes } from 'react';
22

33
import CommentForm from './CommentForm';
44
import CommentList from './CommentList';
5+
import _ from 'lodash';
56

6-
const CommentBox = React.createClass({
7-
displayName: 'CommentBox',
7+
class CommentBox extends React.Component {
8+
constructor(props, context) {
9+
super(props, context);
10+
this.state = {};
811

9-
propTypes: {
12+
_.bindAll(this, '_ajaxCounter', '_isSendingAjax');
13+
}
14+
15+
static displayName = 'CommentBox';
16+
static propTypes = {
1017
pollInterval: PropTypes.number.isRequired,
1118
actions: PropTypes.object.isRequired,
1219
data: PropTypes.object.isRequired,
13-
},
20+
};
1421

1522
componentDidMount() {
1623
const { fetchComments } = this.props.actions;
1724
fetchComments();
1825
setInterval(fetchComments, this.props.pollInterval);
19-
},
26+
}
2027

21-
ajaxCounter() {
28+
_ajaxCounter() {
2229
return this.props.data.get('ajaxCounter');
23-
},
30+
}
2431

25-
isSendingAjax() {
26-
return this.ajaxCounter() > 0;
27-
},
32+
_isSendingAjax() {
33+
return this._ajaxCounter() > 0;
34+
}
2835

2936
render() {
3037
const { actions, data } = this.props;
3138

3239
return (
3340
<div className="commentBox container">
3441
<h2>
35-
Comments { this.isSendingAjax() && `SENDING AJAX REQUEST! Ajax Counter is ${this.ajaxCounter()}` }
42+
Comments { this._isSendingAjax() && `SENDING AJAX REQUEST! Ajax Counter is ${this._ajaxCounter()}` }
3643
</h2>
3744
<p>
3845
Text take Github Flavored Markdown. Comments older than 24 hours are deleted.
3946
<b>Name</b> is preserved, <b>Text</b> is reset, between submits.
4047
</p>
4148
<CommentForm
42-
ajaxSending={this.isSendingAjax()}
49+
ajaxSending={this._isSendingAjax()}
4350
error={data.get('submitCommentError')}
4451
actions={actions}
4552
/>
@@ -49,7 +56,7 @@ const CommentBox = React.createClass({
4956
/>
5057
</div>
5158
);
52-
},
53-
});
59+
}
60+
}
5461

5562
export default CommentBox;

client/app/components/CommentForm.jsx

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,37 @@ import Nav from 'react-bootstrap/lib/Nav';
66
import NavItem from 'react-bootstrap/lib/NavItem';
77
import Alert from 'react-bootstrap/lib/Alert';
88
import ReactCSSTransitionGroup from 'react/lib/ReactCSSTransitionGroup';
9+
import _ from 'lodash';
910

1011
const emptyComment = { author: '', text: '' };
1112
const textPlaceholder = 'Say something using markdown...';
1213

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';
1526

16-
propTypes: {
27+
static propTypes = {
1728
ajaxSending: PropTypes.bool.isRequired,
1829
actions: PropTypes.object.isRequired,
1930
error: PropTypes.any,
20-
},
21-
22-
getInitialState() {
23-
return {
24-
formMode: 0,
25-
comment: emptyComment,
26-
};
27-
},
31+
};
2832

29-
handleSelect(selectedKey) {
33+
_handleSelect(selectedKey) {
3034
this.setState({ formMode: selectedKey });
31-
},
35+
}
3236

33-
handleChange() {
37+
_handleChange() {
3438
let comment;
3539

36-
// This could also be done using ReactLink:
37-
// http://facebook.github.io/react/docs/two-way-binding-helpers.html
3840
if (this.state.formMode < 2) {
3941
comment = {
4042
author: this.refs.author.getValue(),
@@ -50,17 +52,17 @@ const CommentForm = React.createClass({
5052
}
5153

5254
this.setState({ comment });
53-
},
55+
}
5456

55-
handleSubmit(e) {
57+
_handleSubmit(e) {
5658
e.preventDefault();
5759
const { actions } = this.props;
5860
actions
5961
.submitComment(this.state.comment)
60-
.then(this.resetAndFocus);
61-
},
62+
.then(this._resetAndFocus);
63+
}
6264

63-
resetAndFocus() {
65+
_resetAndFocus() {
6466
// Don't reset a form that didn't submit, this results in data loss
6567
if (this.props.error) return;
6668

@@ -75,13 +77,13 @@ const CommentForm = React.createClass({
7577
}
7678

7779
ref.focus();
78-
},
80+
}
7981

80-
formHorizontal() {
82+
_formHorizontal() {
8183
return (
8284
<div>
8385
<hr />
84-
<form className="commentForm form-horizontal" onSubmit={this.handleSubmit}>
86+
<form className="commentForm form-horizontal" onSubmit={this._handleSubmit}>
8587
<Input
8688
type="text"
8789
label="Name"
@@ -90,7 +92,7 @@ const CommentForm = React.createClass({
9092
wrapperClassName="col-sm-10"
9193
ref="author"
9294
value={this.state.comment.author}
93-
onChange={this.handleChange}
95+
onChange={this._handleChange}
9496
disabled={this.props.ajaxSending}
9597
/>
9698
<Input
@@ -101,7 +103,7 @@ const CommentForm = React.createClass({
101103
wrapperClassName="col-sm-10"
102104
ref="text"
103105
value={this.state.comment.text}
104-
onChange={this.handleChange}
106+
onChange={this._handleChange}
105107
disabled={this.props.ajaxSending}
106108
/>
107109
<div className="form-group">
@@ -117,20 +119,20 @@ const CommentForm = React.createClass({
117119
</form>
118120
</div>
119121
);
120-
},
122+
}
121123

122-
formStacked() {
124+
_formStacked() {
123125
return (
124126
<div>
125127
<hr />
126-
<form className="commentForm form" onSubmit={this.handleSubmit}>
128+
<form className="commentForm form" onSubmit={this._handleSubmit}>
127129
<Input
128130
type="text"
129131
label="Name"
130132
placeholder="Your Name"
131133
ref="author"
132134
value={this.state.comment.author}
133-
onChange={this.handleChange}
135+
onChange={this._handleChange}
134136
disabled={this.props.ajaxSending}
135137
/>
136138
<Input
@@ -139,7 +141,7 @@ const CommentForm = React.createClass({
139141
placeholder={textPlaceholder}
140142
ref="text"
141143
value={this.state.comment.text}
142-
onChange={this.handleChange}
144+
onChange={this._handleChange}
143145
disabled={this.props.ajaxSending}
144146
/>
145147
<input
@@ -151,13 +153,13 @@ const CommentForm = React.createClass({
151153
</form>
152154
</div>
153155
);
154-
},
156+
}
155157

156-
formInline() {
158+
_formInline() {
157159
return (
158160
<div>
159161
<hr />
160-
<form className="commentForm form" onSubmit={this.handleSubmit}>
162+
<form className="commentForm form" onSubmit={this._handleSubmit}>
161163
<Input label="Inline Form" wrapperClassName="wrapper">
162164
<Row>
163165
<Col xs={3}>
@@ -167,7 +169,7 @@ const CommentForm = React.createClass({
167169
placeholder="Your Name"
168170
ref="inlineAuthor"
169171
value={this.state.comment.author}
170-
onChange={this.handleChange}
172+
onChange={this._handleChange}
171173
disabled={this.props.ajaxSending}
172174
/>
173175
</Col>
@@ -178,7 +180,7 @@ const CommentForm = React.createClass({
178180
placeholder={textPlaceholder}
179181
ref="inlineText"
180182
value={this.state.comment.text}
181-
onChange={this.handleChange}
183+
onChange={this._handleChange}
182184
disabled={this.props.ajaxSending}
183185
/>
184186
</Col>
@@ -195,9 +197,9 @@ const CommentForm = React.createClass({
195197
</form>
196198
</div>
197199
);
198-
},
200+
}
199201

200-
errorWarning() {
202+
_errorWarning() {
201203
// If there is no error, there is nothing to add to the DOM
202204
if (!this.props.error) return undefined;
203205
return (
@@ -206,19 +208,19 @@ const CommentForm = React.createClass({
206208
A server error prevented your comment from being saved. Please try again.
207209
</Alert>
208210
);
209-
},
211+
}
210212

211213
render() {
212214
let inputForm;
213215
switch (this.state.formMode) {
214216
case 0:
215-
inputForm = this.formHorizontal();
217+
inputForm = this._formHorizontal();
216218
break;
217219
case 1:
218-
inputForm = this.formStacked();
220+
inputForm = this._formStacked();
219221
break;
220222
case 2:
221-
inputForm = this.formInline();
223+
inputForm = this._formInline();
222224
break;
223225
default:
224226
throw new Error(`Unknown form mode: ${this.state.formMode}.`);
@@ -230,18 +232,18 @@ const CommentForm = React.createClass({
230232
transitionEnterTimeout={300}
231233
transitionLeaveTimeout={300}
232234
>
233-
{this.errorWarning()}
235+
{this._errorWarning()}
234236
</ReactCSSTransitionGroup>
235237

236-
<Nav bsStyle="pills" activeKey={this.state.formMode} onSelect={this.handleSelect}>
238+
<Nav bsStyle="pills" activeKey={this.state.formMode} onSelect={this._handleSelect}>
237239
<NavItem eventKey={0}>Horizontal Form</NavItem>
238240
<NavItem eventKey={1}>Stacked Form</NavItem>
239241
<NavItem eventKey={2}>Inline Form</NavItem>
240242
</Nav>
241243
{inputForm}
242244
</div>
243245
);
244-
},
245-
});
246+
}
247+
}
246248

247249
export default CommentForm;

0 commit comments

Comments
 (0)