-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (55 loc) · 1.94 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
58
59
60
61
62
63
64
65
66
67
const formAddTodo = document.querySelector('.form-add-todo');
const inputSearchTodo = document.querySelector('.form-search input');
const todosContainer = document.querySelector('.todos-container');
const addTodo = (inputValue) => {
if (inputValue.length) {
todosContainer.innerHTML += `
<li class="list-group-item d-flex justify-content-between align-items-center" data-todo="${inputValue}">
<span>${inputValue}</span>
<i class="far fa-trash-alt" data-trash="${inputValue}"></i>
</li>
`;
event.target.reset();
}
};
const removeTodo = (clickedElement) => {
const trashDataValue = clickedElement.dataset.trash;
const todo = document.querySelector(`[data-todo="${trashDataValue}"]`);
if (trashDataValue) {
todo.remove();
}
};
const filterTodos = (todos, inputValue, returnMatchedTodos) =>
todos.filter((todo) => {
const matchedTodos = todo.textContent.toLowerCase().includes(inputValue);
return returnMatchedTodos ? matchedTodos : !matchedTodos;
});
const handleClasses = (todos, classToAdd, classToRemove) => {
todos.forEach((todo) => {
todo.classList.remove(classToRemove);
todo.classList.add(classToAdd);
});
};
const hideTodos = (todos, inputValue) => {
const todosToHide = filterTodos(todos, inputValue, false);
handleClasses(todosToHide, 'hidden', 'd-flex');
};
const showTodos = (todos, inputValue) => {
const todosToShow = filterTodos(todos, inputValue, true);
handleClasses(todosToShow, 'd-flex', 'hidden');
};
formAddTodo.addEventListener('submit', (event) => {
event.preventDefault();
const inputValue = event.target.add.value.trim();
addTodo(inputValue);
});
todosContainer.addEventListener('click', (event) => {
const clickedElement = event.target;
removeTodo(clickedElement);
});
inputSearchTodo.addEventListener('input', (event) => {
const inputValue = event.target.value.trim().toLowerCase();
const todos = Array.from(todosContainer.children);
hideTodos(todos, inputValue);
showTodos(todos, inputValue);
});