-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodoApp.jsx
74 lines (66 loc) · 1.67 KB
/
todoApp.jsx
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var React = require('react');
var Immutable = require('immutable');
var TodoAdder = require('./todoAdder.jsx');
var TodoList = require('./todoList.jsx');
var AutoCounter = require("./autoCounter.jsx");
var ItemRecord = Immutable.Record({
text: "",
done: false
});
var TodoApp = React.createClass({
getInitialState: function () {
var initialItems = this.props.items.map(item => {
return new ItemRecord(item);
});
return {
items: Immutable.List(initialItems)
};
},
addItem: function (todoText) {
var newItems = this.state.items.push(
new ItemRecord({
text: todoText
})
);
this.setState({
items: newItems
});
},
updateItem: function (todoItem) {
var index = this.state.items.findIndex(item => item.text == todoItem.text);
var newItems = this.state.items.set(index, todoItem);
this.setState({
items: newItems
});
},
deleteItem: function (todoItem) {
var index = this.state.items.findIndex(item => item.text == todoItem.text);
var newItems = this.state.items.delete(index);
this.setState({
items: newItems
});
},
render: function() {
return (
<div>
<TodoAdder onAdd={this.addItem} />
<TodoList items={this.state.items} onDelete={this.deleteItem} onUpdate={this.updateItem} />
</div>
);
}
});
// mount the todo app under body with some default todos
var initialItems = [
{ done: true, text: "Prepare a presentation"},
{ done: false, text: "Hold a presentation about React.js" },
{ done: false, text: "Do some live coding" },
{ done: false, text: "Add a timer" }
];
var renderThis = (
<div>
<AutoCounter />
<TodoApp items={initialItems} />
<TodoApp items={[]} />
</div>
);
React.render(renderThis, document.body);