Skip to content

Commit ec5b624

Browse files
committed
refactor: early return으로 변경
1 parent 2802b9f commit ec5b624

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/components/TodoForm.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ export default class TodoForm {
1717
<button>추가</button>
1818
`;
1919

20-
if (!this.isInit) {
21-
this.$form.addEventListener("submit", (e) => {
22-
e.preventDefault();
23-
const $todo = this.$form.querySelector<HTMLInputElement>("input[name=todo]");
24-
if (!$todo) return;
25-
const text = $todo.value;
26-
if (text.length > 1) {
27-
$todo.value = "";
28-
this.onSubmit(text);
29-
} else alert("두 글자 이상 입력해주세요");
30-
});
31-
}
20+
if (this.isInit) return;
21+
22+
this.$form.addEventListener("submit", (e) => {
23+
e.preventDefault();
24+
const $todo = this.$form.querySelector<HTMLInputElement>("input[name=todo]");
25+
if (!$todo) return;
26+
const text = $todo.value;
27+
if (text.length > 1) {
28+
$todo.value = "";
29+
this.onSubmit(text);
30+
} else alert("두 글자 이상 입력해주세요");
31+
});
3232
};
3333
}

src/components/TodoList.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ export default class TodoList {
2222
const target = e.target as HTMLLIElement;
2323
const $li = target.closest("li");
2424

25-
if ($li) {
26-
const newState = [...this.state];
27-
if (typeof $li.dataset.index !== "string") return;
28-
const index = +$li.dataset.index;
25+
if (!$li) return;
2926

30-
if (target.className === "deleteBtn") {
31-
newState.splice(index, 1);
32-
this.setState(newState);
33-
} else if (target.className.includes("todoList")) {
34-
const isCompleted = target.className.includes("completed");
35-
if (isCompleted) target.classList.remove("completed");
36-
else target.classList.add("completed");
37-
newState[index] = {
38-
...newState[index],
39-
isCompleted: !isCompleted,
40-
};
41-
this.setState(newState);
42-
}
27+
const newState = [...this.state];
28+
if (typeof $li.dataset.index !== "string") return;
29+
const index = +$li.dataset.index;
30+
31+
if (target.className === "deleteBtn") {
32+
newState.splice(index, 1);
33+
this.setState(newState);
34+
} else if (target.className.includes("todoList")) {
35+
const isCompleted = target.className.includes("completed");
36+
if (isCompleted) target.classList.remove("completed");
37+
else target.classList.add("completed");
38+
newState[index] = {
39+
...newState[index],
40+
isCompleted: !isCompleted,
41+
};
42+
this.setState(newState);
4343
}
4444
});
4545
}

0 commit comments

Comments
 (0)