Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
/**
2
* Copyright (c) 2014-2015, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*/
9
10
var React = require('react');
11
var ReactPropTypes = React.PropTypes;
12
var TodoActions = require('../actions/TodoActions');
13
var TodoTextInput = require('./TodoTextInput.react');
14
15
var cx = require('react/lib/cx');
16
17
var TodoItem = React.createClass({
18
19
propTypes: {
20
todo: ReactPropTypes.object.isRequired
21
},
22
23
getInitialState: function() {
24
return {
25
isEditing: false
26
};
27
},
28
29
/**
30
* @return {object}
31
*/
32
render: function() {
33
var todo = this.props.todo;
34
35
var input;
36
if (this.state.isEditing) {
37
input =
38
<TodoTextInput
39
className="edit"
40
onSave={this._onSave}
41
value={todo.text}
42
/>;
43
}
44
45
// List items should get the class 'editing' when editing
46
// and 'completed' when marked as completed.
47
// Note that 'completed' is a classification while 'complete' is a state.
48
// This differentiation between classification and state becomes important
49
// in the naming of view actions toggleComplete() vs. destroyCompleted().
50
return (
51
<li
52
className={cx({
53
'completed': todo.complete,
54
'editing': this.state.isEditing
55
})}
56
key={todo.id}>
57
<div className="view">
58
<input
59
className="toggle"
60
type="checkbox"
61
checked={todo.complete}
62
onChange={this._onToggleComplete}
63
/>
64
<label onDoubleClick={this._onDoubleClick}>
65
{todo.text}
66
</label>
67
<button className="destroy" onClick={this._onDestroyClick} />
68
</div>
69
{input}
70
</li>
71
);
72
},
73
74
_onToggleComplete: function() {
75
TodoActions.toggleComplete(this.props.todo);
76
},
77
78
_onDoubleClick: function() {
79
this.setState({isEditing: true});
80
},
81
82
/**
83
* Event handler called within TodoTextInput.
84
* Defining this here allows TodoTextInput to be used in multiple places
85
* in different ways.
86
* @param {string} text
87
*/
88
_onSave: function(text) {
89
TodoActions.updateText(this.props.todo.id, text);
90
this.setState({isEditing: false});
91
},
92
93
_onDestroyClick: function() {
94
TodoActions.destroy(this.props.todo.id);
95
}
96
97
});
98
99
module.exports = TodoItem;
100
101