Skip to content

Commit 7199c76

Browse files
committed
refactor: 이벤트 바인딩 함수 분리
1 parent 088d7f5 commit 7199c76

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/components/TodoList.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,9 @@ export default class TodoList {
1212
private readonly updateCount: (state: TodoLi) => void
1313
) {
1414
$target.appendChild(this.$todoList);
15-
1615
this.state = initialState;
1716
this.render();
18-
19-
this.$todoList.addEventListener("click", (e) => {
20-
const target = e.target as HTMLLIElement;
21-
const $li = target.closest("li");
22-
23-
if (!$li) return;
24-
25-
const newState = [...this.state];
26-
if (typeof $li.dataset.index !== "string") return;
27-
const index = +$li.dataset.index;
28-
29-
if (target.className === "deleteBtn") {
30-
newState.splice(index, 1);
31-
this.setState(newState);
32-
} else if (target.className.includes("todoList")) {
33-
const isCompleted = target.className.includes("completed");
34-
if (isCompleted) target.classList.remove("completed");
35-
else target.classList.add("completed");
36-
newState[index] = {
37-
...newState[index],
38-
isCompleted: !isCompleted,
39-
};
40-
this.setState(newState);
41-
}
42-
});
17+
this.setEvent();
4318
}
4419

4520
setState(nextState: TodoLi) {
@@ -67,4 +42,31 @@ export default class TodoList {
6742
</ul>
6843
`;
6944
};
45+
46+
private setEvent() {
47+
this.$todoList.addEventListener("click", (e) => {
48+
const target = e.target as HTMLLIElement;
49+
const $li = target.closest("li");
50+
51+
if (!$li) return;
52+
53+
const newState = [...this.state];
54+
if (typeof $li.dataset.index !== "string") return;
55+
const index = +$li.dataset.index;
56+
57+
if (target.className === "deleteBtn") {
58+
newState.splice(index, 1);
59+
this.setState(newState);
60+
} else if (target.className.includes("todoList")) {
61+
const isCompleted = target.className.includes("completed");
62+
if (isCompleted) target.classList.remove("completed");
63+
else target.classList.add("completed");
64+
newState[index] = {
65+
...newState[index],
66+
isCompleted: !isCompleted,
67+
};
68+
this.setState(newState);
69+
}
70+
});
71+
}
7072
}

0 commit comments

Comments
 (0)