Skip to content

Commit 4498a57

Browse files
committed
Merge remote-tracking branch 'todo-list/main' into yoonseo_working
2 parents 803a2b6 + 775a2a4 commit 4498a57

File tree

5 files changed

+44
-63
lines changed

5 files changed

+44
-63
lines changed

src/App.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import Header from "./components/Header.js";
22
import TodoForm from "./components/TodoForm.js";
33
import TodoList from "./components/TodoList.js";
44
import TodoCount from "./components/TodoCount.js";
5-
import { setItem } from "./utils/storage.js";
5+
import { setItem, getItem } from "./utils/storage.js";
66
import { TodoCount as TodoCnt, TodoList as TodoLi } from "./types/todo.js";
7-
87
export default class App {
98
private readonly todoList: TodoList;
109
private readonly todoCount: TodoCount;
10+
private readonly initialState: TodoLi;
11+
private readonly initialCount: TodoCnt;
1112

1213
constructor(
1314
private readonly $target: HTMLElement,
14-
private readonly initialState: TodoLi,
15-
private readonly initialCount: TodoCnt
1615
) {
16+
this.initialState = getItem("todo", []);
17+
this.initialCount = getItem("count", { total: 0, done: 0 });
18+
1719
new Header($target, "Todo List");
1820

1921
new TodoForm(
@@ -29,11 +31,11 @@ export default class App {
2931

3032
this.todoList = new TodoList(
3133
$target,
32-
initialState,
34+
this.initialState,
3335
(state: TodoLi) => this.#updateCount(state)
3436
);
3537

36-
this.todoCount = new TodoCount($target, initialCount);
38+
this.todoCount = new TodoCount($target, this.initialCount);
3739
}
3840

3941
// 카운트 업데이트

src/components/TodoForm.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11

22
export default class TodoForm {
33
private readonly $form = document.createElement("form");
4-
private isInit = false;
54

65
constructor(
76
private readonly $target: HTMLElement,
87
private readonly onSubmit: (text: string) => void
98
) {
109
$target.appendChild(this.$form);
1110
this.render();
11+
this.setEvent();
1212
}
1313

1414
private render() {
1515
this.$form.innerHTML = `
16-
<input type="text" placeholder="할 일을 입력하세요" name="todo" />
16+
<input type="text" placeholder="할 일을 입력하세요" name="todo" minLength="2" autocomplete="off" />
1717
<button>추가</button>
1818
`;
19+
};
1920

20-
if (this.isInit) return;
21-
21+
private setEvent() {
2222
this.$form.addEventListener("submit", (e) => {
2323
e.preventDefault();
2424
const $todo = this.$form.querySelector<HTMLInputElement>("input[name=todo]");
2525
if (!$todo) return;
2626
const text = $todo.value;
27-
if (text.length > 1) {
28-
$todo.value = "";
29-
this.onSubmit(text);
30-
} else alert("두 글자 이상 입력해주세요");
27+
$todo.value = "";
28+
this.onSubmit(text);
3129
});
32-
};
30+
}
3331
}

src/components/TodoList.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { setItem } from "../utils/storage.js";
2-
import validation from "../utils/validation.js";
32
import { TodoList as TodoLi } from "../types/todo.js";
43

54
export default class TodoList {
@@ -12,12 +11,37 @@ export default class TodoList {
1211
private readonly updateCount: (state: TodoLi) => void
1312
) {
1413
$target.appendChild(this.$todoList);
14+
this.state = initialState;
15+
this.render();
16+
this.setEvent();
17+
}
1518

16-
if (Array.isArray(initialState)) this.state = initialState;
17-
else this.state = [];
18-
19+
setState(nextState: TodoLi) {
20+
this.state = nextState;
21+
setItem("todo", JSON.stringify(nextState));
22+
this.updateCount(nextState);
1923
this.render();
24+
};
25+
26+
private render() {
27+
this.$todoList.innerHTML = `
28+
<ul>
29+
${this.state
30+
.map(
31+
({ text, isCompleted }, index) => `
32+
<li data-index=${index} class="todoList ${isCompleted ? "completed" : ""
33+
}">
34+
${text}
35+
<button class="deleteBtn">삭제</button>
36+
</li>
37+
`
38+
)
39+
.join("")}
40+
</ul>
41+
`;
42+
};
2043

44+
private setEvent() {
2145
this.$todoList.addEventListener("click", (e) => {
2246
const target = e.target as HTMLLIElement;
2347
const $li = target.closest("li");
@@ -43,30 +67,4 @@ export default class TodoList {
4367
}
4468
});
4569
}
46-
47-
setState(nextState: TodoLi) {
48-
const newState = validation.state(nextState);
49-
this.state = newState;
50-
setItem("todo", JSON.stringify(newState));
51-
this.updateCount(newState);
52-
this.render();
53-
};
54-
55-
private render() {
56-
this.$todoList.innerHTML = `
57-
<ul>
58-
${this.state
59-
.map(
60-
({ text, isCompleted }, index) => `
61-
<li data-index=${index} class="todoList ${isCompleted ? "completed" : ""
62-
}">
63-
${text}
64-
<button class="deleteBtn">삭제</button>
65-
</li>
66-
`
67-
)
68-
.join("")}
69-
</ul>
70-
`;
71-
};
7270
}

src/main.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
import App from "./App.js";
2-
import { getItem } from "./utils/storage.js";
3-
4-
const initialState = getItem("todo", []);
5-
const initialCount = getItem("count", { total: 0, done: 0 });
62
const $app = document.querySelector<HTMLElement>("#app");
7-
8-
$app && new App($app, initialState, initialCount);
3+
$app && new App($app);

src/utils/validation.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)