-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew Text Document.txt
91 lines (80 loc) · 2.12 KB
/
New Text Document.txt
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
90
91
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import TodoList from './TodoList'
import TodoItems from './TodoItems'
class App extends Component {
constructor(props) {
super(props)
this.state = {
items: [],
currentItem: {text:'', key:''},
}
}
//Handle user Input
handleInput = e => {
const itemText = e.target.value
const currentItem = {text:itemText, key:Date.now()}
this.setState({
currentItem,
})
}
//Add item to list
addItem = e => {
e.preventDefault();
const newItem = this.state.currentItem
if(newItem.text !== ''){
console.log(newItem)
const items = [...this.state.items, newItem]
this.setState = ({
items: items,
currentItem: {text:'', key:''},
})
}
}
inputElement = React.createRef()
//delete item from list
deleteItem = key => {
const filteredItems = this.state.items.filter(item => {
return item.key !== key
})
this.setState({
items: filteredItems
})
}
render() {
return (
<div className="App">
<TodoList
addItem={this.addItem}
inputElement={this.inputElement}
handleInput={this.handleInput}
currentItem={this.state.currentItem}/>
<TodoItems entries={this.state.items} deleteItem={this.deleteItem} />
</div>
);
}
}
export default App;
import React, {Component} from 'react'
class Header extends Component {
componentDidUpdate = () => {
this.props.inputElement.current.focus()
}
render() {
return(
<div className="todoListMain">
<div className="Header">
<form onSubmit={this.props.addItem}>
<input id="inputBox" placeholder="Task"
ref={this.props.inputElement}
value={this.props.currentItem.text}
onChange={this.props.handleInput}/>
<button id="addTask" type="submit">Add Task</button>
</form>
</div>
</div>
)
}
}
export default Header;