-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathToDoList.jsx
More file actions
97 lines (88 loc) · 2.5 KB
/
Copy pathToDoList.jsx
File metadata and controls
97 lines (88 loc) · 2.5 KB
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
93
94
95
96
97
import { useState, useRef } from "react";
import uuid4 from "uuid4";
import TodoItem from "./TodoItem";
import ListTask from "./ListTask";
function ToDoList({ todoData }) {
const [copyToDo, setcopyToDo] = useState(todoData);
const [getToDo, setToDo] = useState(todoData);
const [todoTitel, setToDoTitel] = useState("");
const [search, setSearch] = useState("");
const inputTitel = useRef();
console.log("hi word");
function handleTextChange(event) {
setToDoTitel(event.target.value);
}
function handleTodoCreate() {
const newToDoObj = {
id: uuid4(),
title: todoTitel,
done: false,
};
setToDo([...getToDo, newToDoObj]);
inputTitel.current.value = "";
}
function handleDelete(todoId) {
setToDo(getToDo.filter((todo) => todo.id !== todoId));
}
function handleEdit(idTodo, newText) {
setToDo(
getToDo.map((todo) =>
todo.id === idTodo ? { ...todo, title: newText } : todo
)
);
}
//###########################################
const filterTodo = (text) => {
const filterObjects = (text, todos) => {
if (text == 1) {
return todos.filter((item) => item.done == false);
} else if (text == 2) {
return todos.filter((item) => item.done == true);
} else {
return todos.slice(); // Return a copy of the original array if value is not 1 or 2
}
};
const filteredTodos = filterObjects(text, copyToDo);
setToDo(filteredTodos);
};
//################################
return (
<div className="contenr">
<input
ref={inputTitel}
type="text"
placeholder="Enter New ToDo"
onChange={handleTextChange}
className="input-title"
// {...getToDo("title",{required:true})}
></input>
<button onClick={handleTodoCreate} className="btn-add">
Add
</button>
<div className="hadar">
<input
type="text"
className="search"
onChange={(e) => setSearch(e.target.value)}
></input>
<ListTask filterTodo={filterTodo} />
</div>
{getToDo
.filter((todo) => {
return search.toLowerCase() === ""
? todo
: todo.title.toLowerCase().includes(search);
})
.map((todo, index) => (
<TodoItem
index={index}
todo={todo}
handleDelete={handleDelete}
handleEdit={handleEdit}
setToDo={setToDo}
/>
))}
</div>
);
}
export default ToDoList;