-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoList.jsx
60 lines (55 loc) · 1.99 KB
/
ToDoList.jsx
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
import React,{useState} from "react";
function ToDoList(){
const[tasks,setTasks]=useState(["Eat Breakfast","Take a shower","Go on a Stroll"]);
const[newTask,setNewTask]=useState("");
function handleInputChange(event){
setNewTask(event.target.value);
}
function addTask(){
if(newTask.trim()!=="")
setTasks(t=>[...t,newTask]);
setNewTask("");
}
function removeTask(index){
const updTasks=tasks.filter((_,i)=>i!==index);
setTasks(updTasks);
}
function moveTaskUp(index){
if(index>0){
const updTasks=[...tasks];
[updTasks[index],updTasks[index-1]]= [updTasks[index-1],updTasks[index]];
setTasks(updTasks);
}
}
function moveTaskDown(index){
if(index<tasks.length-1){
const updTasks=[...tasks];
[updTasks[index],updTasks[index+1]]= [updTasks[index+1],updTasks[index]];
setTasks(updTasks);
}
}
return(
<div className="to-do-list">
<h1>To-Do-List</h1>
<div>
<input type="text" placeholder="Enter a Task..." value={newTask} onChange={handleInputChange} />
<button className="add-button" onClick={addTask}>Add</button>
</div>
<ol>
{tasks.map((task,index)=><li key={index}>
<span className="text">{task}</span>
<button className="delete-button" onClick={()=>removeTask(index)}>
Delete
</button>
<button className="move-up-button" onClick={()=>moveTaskUp(index)}>
Move ⬆️
</button>
<button className="move-down-button" onClick={()=>moveTaskDown(index)}>
Move ⬇️
</button>
</li>)}
</ol>
</div>
)
}
export default ToDoList