-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathToDo.js
89 lines (75 loc) · 2.29 KB
/
ToDo.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React, {Component} from 'react';
import './ToDo.css';
import ToDoItem from './components/ToDoItem';
import Logo from './assets/logo.png';
class ToDo extends Component {
constructor(props) {
super(props);
this.state = {
// this is where the data goes
list: [
{
'todo': 'clean the house'
},
{
'todo': 'buy milk'
}
],
todo: ''
};
};
createNewToDoItem = () => {
this.setState(({ list, todo }) => ({
list: [
...list,
{
todo
}
],
todo: ''
}));
};
handleKeyPress = e => {
if (e.target.value !== '') {
if (e.key === 'Enter') {
this.createNewToDoItem();
}
}
};
handleInput = e => {
this.setState({
todo: e.target.value
});
};
// this is now being emitted back to the parent from the child component
deleteItem = indexToDelete => {
this.setState(({ list }) => ({
list: list.filter((toDo, index) => index !== indexToDelete)
}));
};
render() {
return (
<div className="ToDo">
<img className="Logo" src={Logo} alt="React logo"/>
<h1 className="ToDo-Header">React To Do</h1>
<div className="ToDo-Container">
<div className="ToDo-Content">
{this.state.list.map((item, key) => {
return <ToDoItem
key={key}
item={item.todo}
deleteItem={this.deleteItem.bind(this, key)}
/>
}
)}
</div>
<div>
<input type="text" value={this.state.todo} onChange={this.handleInput} onKeyPress={this.handleKeyPress}/>
<button className="ToDo-Add" onClick={this.createNewToDoItem}>+</button>
</div>
</div>
</div>
);
}
}
export default ToDo;