Skip to content

Commit ab66e5f

Browse files
committed
투두리스트 삭선/삭제 로직 변경
1 parent 1d84f5d commit ab66e5f

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

src/TodoList.js

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,59 +25,45 @@ export default function TodoList({ $target, initialState, updateCount }) {
2525
<ul>
2626
${this.state
2727
.map(
28-
({ text, isCompleted }) => `
29-
<li class=${isCompleted ? "completed" : ""}>
30-
<input type="checkbox" ${isCompleted ? "checked" : ""} />
31-
<span>${text}</span>
28+
({ text, isCompleted }, index) => `
29+
<li data-index=${index} class="todoList ${
30+
isCompleted ? "completed" : ""
31+
}">
32+
${text}
3233
<button class="deleteBtn">삭제</button>
3334
</li>
3435
`
3536
)
3637
.join("")}
3738
</ul>
3839
`;
40+
};
41+
42+
$todoList.addEventListener("click", (e) => {
43+
const { target } = e;
44+
const $li = target.closest("li");
3945

40-
const deleteButtonAll = $todoList.querySelectorAll(".deleteBtn");
41-
deleteButtonAll.forEach(($deleteBtn, index) => {
42-
$deleteBtn.addEventListener("click", (e) => {
43-
// list의 li 혹은 checkbox를 클릭했을 때 이벤트 전파를 막기 위함
44-
e.stopPropagation();
46+
if ($li) {
47+
const newState = [...this.state];
48+
const { index } = $li.dataset;
4549

46-
const newState = [...this.state];
50+
if (target.className === "deleteBtn") {
4751
newState.splice(index, 1);
4852
this.setState(newState);
49-
setItem("todo", JSON.stringify(newState));
50-
updateCount();
51-
});
52-
});
53+
} else if (target.className.includes("todoList")) {
54+
const isCompleted = target.className.includes("completed");
5355

54-
const liList = $todoList.querySelectorAll("li");
55-
liList.forEach(($li, index) => {
56-
$li.addEventListener("click", (e) => {
57-
const checkbox = e.currentTarget.children[0];
58-
if (e.target.tagName !== "INPUT") {
59-
// checkbox가 아닌 부분을 클릭했을 때 checked 상태를 변경
60-
checkbox.checked = !checkbox.checked;
61-
}
56+
if (isCompleted) target.classList.remove("completed");
57+
else target.classList.add("completed");
6258

63-
const isCompleted = checkbox.checked;
64-
if (isCompleted) {
65-
e.currentTarget.classList.add("completed");
66-
} else {
67-
e.currentTarget.classList.remove("completed");
68-
}
69-
70-
const newState = [...this.state];
7159
newState[index] = {
7260
...newState[index],
73-
isCompleted,
61+
isCompleted: !isCompleted,
7462
};
7563
this.setState(newState);
76-
setItem("todo", JSON.stringify(newState));
77-
updateCount();
78-
});
79-
});
80-
};
64+
}
65+
}
66+
});
8167

8268
this.render();
8369
}

0 commit comments

Comments
 (0)