-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcounter_component.js
51 lines (42 loc) · 1.48 KB
/
counter_component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import {Storage} from './local_storage';
import {Inject} from 'di/annotations';
// This is hack, because Traceur currently does not support annotations
// for inlined functions.
// We should use ES6 instead of `React.class()` anyway. Should be easy to do...
function __construct(storage: Storage) {
// An instance of Storage is injected based on the type annotation.
this.storage = storage;
}
var CounterComponent = React.createClass({
__construct: __construct,
getInitialState: function() {
return {
count: this.storage.get('count') || 0
};
},
setCount: function(value) {
this.setState({count: value});
this.storage.set('count', value);
},
handleIncrement: function(event) {
this.setCount(this.state.count + 1);
},
handleDecrement: function(event) {
this.setCount(this.state.count - 1);
},
render: function() {
var classes = 'counter ' + (this.state.count >= 0 ? 'positive' : 'negative');
// Not using JSX, because I wanna use ES6, transpiled with Traceur.
// JSX supports some parts of ES6, but I wanna use Traceur because it supporsts ES6 modules
// and also stuff like annotations and run-time type assertions.
// Again, this could be figured out.
return (
React.DOM.div( {className: classes},
React.DOM.button( {onClick:this.handleDecrement}, "-"),
React.DOM.div(null, this.state.count),
React.DOM.button( {onClick:this.handleIncrement}, "+")
)
);
}
});
export {CounterComponent};