-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoList.tsx
48 lines (41 loc) · 1.4 KB
/
ToDoList.tsx
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
// Monday 5th Feb 2024
import React, { useState } from "react";
interface item {
id: number;
text: string;
completed: boolean;
}
export const ToDoList: React.FC = () => {
const [todos, setTodos] = useState<item[]>([
{ id: 1, text: "Learning TS", completed: false },
{ id: 2, text: "Learning js", completed: false },
{ id: 3, text: "Learning node", completed: false },
]);
const [input, setInput] = useState<string>("");
const handleToggle = (id: number) => {
setTodos(
todos.map((todo) => {
if (todo.id === id) {
return { ...todo, completed: !todo.completed };
}
return todo;
})
);
};
const handleClick = () => {
const newTodo: item = {id: Date.now(), text:input, completed:false }
setTodos([...todos,newTodo])
}
return (
<div className="main-container">
<h1>Todo List</h1>
<ul>
{todos.map((todo) => (
<li key={todo.id} onClick={() => handleToggle(todo.id)} style={{textDecoration:todo.completed ? "line-through" : "none"}}>{todo.text}</li>
))}
</ul>
<input type="text" placeholder="Add task" onChange={(e) => setInput(e.currentTarget.value)} />
<button onClick={handleClick}>Add</button>
</div>
);
};