|
1 | 1 | import React from 'react'
|
2 | 2 |
|
3 | 3 | class ToDoList extends React.Component {
|
4 |
| - |
5 |
| - constructor(props) { |
6 |
| - super(props) |
7 |
| - this.state = { |
8 |
| - text: props.text || '' |
9 |
| - } |
10 |
| - } |
11 | 4 |
|
12 |
| - static defaultProps = { |
13 |
| - todos: [] |
| 5 | + constructor(props) { |
| 6 | + super(props) |
| 7 | + this.state = { |
| 8 | + text: props.text || '' |
14 | 9 | }
|
| 10 | + } |
15 | 11 |
|
16 |
| - handleKeyup = e => { |
17 |
| - if(e.key === 'Enter'){ |
18 |
| - let value = e.target.value.trim() |
19 |
| - |
20 |
| - this.props.todos.push({ |
21 |
| - name: value, |
22 |
| - id: new Date().getTime(), |
23 |
| - completed: false |
24 |
| - }) |
| 12 | + static defaultProps = { |
| 13 | + todos: [] |
| 14 | + } |
25 | 15 |
|
26 |
| - this.setState({ |
27 |
| - text: '' |
28 |
| - }) |
| 16 | + handleKeyup = e => { |
| 17 | + if (e.key === 'Enter') { |
| 18 | + let value = e.target.value.trim() |
29 | 19 |
|
30 |
| - console.log(this.props.todos) |
31 |
| - } |
32 |
| - } |
| 20 | + this.props.todos.push({ |
| 21 | + name: value, |
| 22 | + id: new Date().getTime(), |
| 23 | + completed: false |
| 24 | + }) |
33 | 25 |
|
34 |
| - handleChange = e => { |
35 | 26 | this.setState({
|
36 |
| - text: e.target.value |
| 27 | + text: '' |
37 | 28 | })
|
38 | 29 | }
|
| 30 | + } |
39 | 31 |
|
40 |
| - handleRemoveTodo = index => { |
41 |
| - this.props.todos.splice(index, 1) |
| 32 | + handleChange = e => { |
| 33 | + this.setState({ |
| 34 | + text: e.target.value |
| 35 | + }) |
| 36 | + } |
42 | 37 |
|
43 |
| - this.setState(this.state) |
44 |
| - } |
| 38 | + handleRemoveTodo = index => { |
| 39 | + this.props.todos.splice(index, 1) |
45 | 40 |
|
46 |
| - onToggle = todo => { |
47 |
| - todo.completed = !todo.completed |
48 |
| - this.setState(this.state) |
49 |
| - } |
| 41 | + this.setState(this.state) |
| 42 | + } |
50 | 43 |
|
51 |
| - onToggleAll = e => { |
52 |
| - let {checked} = e.target |
53 |
| - this.props.todos.forEach(todo => { |
54 |
| - todo.completed = checked |
55 |
| - }) |
56 |
| - this.setState(this.state) |
57 |
| - } |
| 44 | + onToggle = todo => { |
| 45 | + todo.completed = !todo.completed |
| 46 | + this.setState(this.state) |
| 47 | + } |
58 | 48 |
|
59 |
| - render() { |
60 |
| - let {todos} = this.props |
61 |
| - return ( |
62 |
| - <div> |
63 |
| - <input type="checkbox" onChange={this.onToggleAll}/> |
64 |
| - <input type="text" |
65 |
| - onKeyUp={this.handleKeyup} |
66 |
| - onChange={this.handleChange} |
67 |
| - value={this.state.text}/> |
68 |
| - <ul> |
69 |
| - {todos.map( (todo, index) => |
70 |
| - <li key={todo.id}> |
71 |
| - <label> |
72 |
| - <input type="checkbox" checked={todo.completed} |
73 |
| - onChange={()=> {this.onToggle(todo)}}/> |
74 |
| - {todo.name} |
75 |
| - </label> |
| 49 | + onToggleAll = e => { |
| 50 | + let { checked } = e.target |
| 51 | + this.props.todos.forEach(todo => { |
| 52 | + todo.completed = checked |
| 53 | + }) |
| 54 | + this.setState(this.state) |
| 55 | + } |
76 | 56 |
|
77 |
| - <span className="close" onClick={() => {this.handleRemoveTodo(index)}}>X</span> |
78 |
| - </li> |
79 |
| - )} |
80 |
| - </ul> |
81 |
| - </div> |
82 |
| - ) |
83 |
| - } |
| 57 | + render() { |
| 58 | + let { todos } = this.props |
| 59 | + return ( |
| 60 | + <div> |
| 61 | + <input type="checkbox" onChange={this.onToggleAll} /> |
| 62 | + <input type="text" |
| 63 | + onKeyUp={this.handleKeyup} |
| 64 | + onChange={this.handleChange} |
| 65 | + value={this.state.text} /> |
| 66 | + <ul> |
| 67 | + {todos.map((todo, index) => |
| 68 | + <li key={todo.id}> |
| 69 | + <label> |
| 70 | + <input type="checkbox" checked={todo.completed} |
| 71 | + onChange={() => { this.onToggle(todo) }} /> |
| 72 | + {todo.name} |
| 73 | + </label> |
| 74 | + |
| 75 | + <span className="close" onClick={() => { this.handleRemoveTodo(index) }}>X</span> |
| 76 | + </li> |
| 77 | + )} |
| 78 | + </ul> |
| 79 | + </div> |
| 80 | + ) |
| 81 | + } |
84 | 82 | }
|
85 | 83 | export default ToDoList
|
0 commit comments