-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
57 lines (47 loc) · 1.47 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
import React, { useState } from 'react';
import './App.css';
function App() {
const [todo, setTodo] = useState({desc: '', date: ''});
const [todos, setTodos] = useState([]);
const inputChanged = (event) => {
setTodo({...todo, [event.target.name]: event.target.value});
}
const addTodo = (event) => {
event.preventDefault();
setTodos([...todos, todo]);
}
const deleteRow = (idx) => {
const rows = todos.filter((todo, i) => i !== idx);
setTodos(rows);
}
return (
<div className="App">
<h1>Simple Todolist</h1>
<fieldset>
<legend align="left">Add todo:</legend>
<form onSubmit={addTodo} name="Add todo">
<label>Description:</label>
<input type="text" name="desc" value={todo.desc} onChange={inputChanged} />
<label>Date:</label>
<input type="date" name="date" value={todo.date} onChange={inputChanged} />
<input type= "submit" value="Add" />
</form>
</fieldset>
<table id="todos">
<tbody>
<tr><td><b>Date</b></td><td><b>Description</b></td></tr>
{
todos.map((todo, idx) =>
<tr key={idx}>
<td>{todo.date}</td>
<td>{todo.desc}</td>
<td><button name="delete" onClick={() => deleteRow(idx)}>Delete</button></td>
</tr>
)
}
</tbody>
</table>
</div>
);
}
export default App;