-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
92 lines (88 loc) · 2.13 KB
/
App.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
90
91
92
import React from 'react';
import logo from './logo.svg';
import './App.css';
import ListItems from './ListItems';
import { library } from '@fortawesome/fontawesome-svg-core';
import { faTrash } from '@fortawesome/free-solid-svg-icons';
library.add(faTrash);
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [],
currentItem: {
text: '',
key: ''
}
};
this.addItem = this.addItem.bind(this);
this.handleInput = this.handleInput.bind(this);
this.deleteItem = this.deleteItem.bind(this);
this.setUpdate = this.setUpdate.bind(this);
}
addItem(e) {
e.preventDefault();
const newItem = this.state.currentItem;
if (newItem.text !== '') {
const items = [...this.state.items, newItem];
this.setState({
items: items,
currentItem: {
text: '',
key: ''
}
});
}
}
handleInput(e) {
this.setState({
currentItem: {
text: e.target.value,
key: Date.now()
}
});
}
deleteItem(key) {
const filteredItems = this.state.items.filter(item => item.key !== key);
this.setState({
items: filteredItems
});
}
setUpdate(text, key) {
console.log('items:' + this.state.items);
const items = this.state.items;
items.map(item => {
if (item.key === key) {
console.log(item.key + ' ' + key);
item.text = text;
}
});
this.setState({
items: items
});
}
render() {
return (
<div className='App'>
<header>
<form id='to-do-form' onSubmit={this.addItem}>
<input
type='text'
placeholder='Enter task'
value={this.state.currentItem.text}
onChange={this.handleInput}
></input>
<button type='submit'>Add</button>
</form>
<p>{this.state.items.text}</p>
<ListItems
items={this.state.items}
deleteItem={this.deleteItem}
setUpdate={this.setUpdate}
/>
</header>
</div>
);
}
}
export default App;