Skip to content

Commit adb0435

Browse files
committed
feat: TodoForm 컴포넌트 클래스 + ts 변환
1 parent 90adc4b commit adb0435

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/App.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ export default class App {
1616
) {
1717
new Header(this.$target, "Todo List");
1818

19-
// new TodoForm({
20-
// $target: this.$target,
21-
// onSubmit: (text: string) => {
22-
// const nextState = [...this.todoList.state, { text, isCompleted: false }];
23-
// this.todoList.setState(nextState);
24-
// },
25-
// });
19+
new TodoForm(
20+
this.$target,
21+
(text: string) => {
22+
const nextState = [...this.todoList.state, { text, isCompleted: false }];
23+
this.todoList.setState(nextState);
24+
},
25+
);
2626

2727
this.todoList = new TodoList(
2828
this.$target,

src/components/TodoForm.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
import validation from "../utils/validation.js";
21

3-
export default function TodoForm({ $target, onSubmit }) {
4-
validation.newTarget(new.target);
2+
export default class TodoForm {
3+
$form = document.createElement("form");
4+
isInit = false;
55

6-
const $form = document.createElement("form");
7-
$target.appendChild($form);
6+
constructor(
7+
private readonly $target: HTMLElement,
8+
private readonly onSubmit: (text: string) => void
9+
) {
10+
this.$target.appendChild(this.$form);
11+
this.render();
12+
}
813

9-
let isInit = false;
10-
11-
this.render = () => {
12-
$form.innerHTML = `
14+
render() {
15+
this.$form.innerHTML = `
1316
<input type="text" placeholder="할 일을 입력하세요" name="todo" />
1417
<button>추가</button>
1518
`;
1619

17-
if (!isInit) {
18-
$form.addEventListener("submit", (e) => {
19-
e.preventDefault(); // 태그가 갖고 있는 고유 기능 막기(form 태그의 새로고침 막기)
20-
21-
const $todo = $form.querySelector("input[name=todo]");
20+
if (!this.isInit) {
21+
this.$form.addEventListener("submit", (e) => {
22+
e.preventDefault();
23+
const $todo = this.$form.querySelector("input[name=todo]") as HTMLInputElement;
2224
const text = $todo.value;
2325
if (text.length > 1) {
2426
$todo.value = "";
25-
onSubmit(text);
27+
this.onSubmit(text);
2628
} else alert("두 글자 이상 입력해주세요");
2729
});
2830
}
2931
};
30-
31-
this.render();
3232
}

0 commit comments

Comments
 (0)