-
Notifications
You must be signed in to change notification settings - Fork 3
/
TodoList.js
71 lines (65 loc) 路 1.74 KB
/
TodoList.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
import React, { useState } from "react";
const TodoList = () => {
const [todos, setTodos] = useState([]);
const [newTodo, setNewTodo] = useState("");
const handleAddTodo = () => {
if (newTodo.trim() !== "") {
setTodos([...todos, { text: newTodo.trim(), checked: false }]);
setNewTodo("");
}
};
const handleDeleteTodo = (index) => {
const newTodos = [...todos];
newTodos.splice(index, 1);
setTodos(newTodos);
};
const handleToggleTodo = (index) => {
const newTodos = [...todos];
newTodos[index].checked = !newTodos[index].checked;
setTodos(newTodos);
};
return (
<div>
<h1>To-Do List</h1>
<input
type="text"
value={newTodo}
onChange={(e) => setNewTodo(e.target.value)}
/>
<button onClick={handleAddTodo}>Add</button>
<ul>
{todos.map((todo, index) => (
<li
key={index}
style={{
display: "flex",
}}
>
<div style={{ display: "flex", alignItems: "center" }}>
<input
type="checkbox"
checked={todo.checked}
onChange={() => handleToggleTodo(index)}
/>
<span
style={{
marginRight: "10px",
textDecoration: todo.checked ? "line-through" : "none",
}}
>
{todo.text}
</span>
</div>
<button
style={{ marginTop: "5px", marginBottom: "5px" }}
onClick={() => handleDeleteTodo(index)}
>
Delete
</button>
</li>
))}
</ul>
</div>
);
};
export default TodoList;