Skip to content

Commit

Permalink
feat: counter, filters, todos 함수 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
2wndrhs committed Jan 10, 2024
1 parent 887427b commit a3972ca
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 02. 렌더링/02/view/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const getTodoCount = (todos) => {
const notCompleted = todos.filter((todo) => !todo.completed);

const { length } = notCompleted;
if (length === 1) {
return '1 Item left';
}

return `${length} Items left`;
};

export default (targetElement, { todos }) => {
const newCounter = targetElement.cloneNode(true);
newCounter.textContent = getTodoCount(todos);
return newCounter;
};
11 changes: 11 additions & 0 deletions 02. 렌더링/02/view/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default (targetElement, { currentFilter }) => {
const newCounter = targetElement.cloneNode(true);
Array.from(newCounter.querySelectorAll("li a")).forEach((a) => {
if (a.textContent === currentFilter) {
a.classList.add("selected");
} else {
a.classList.remove("selected");
}
});
return newCounter;
};
23 changes: 23 additions & 0 deletions 02. 렌더링/02/view/todos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const getTodoElement = (todo) => {
const { text, completed } = todo;

return `
<li ${completed ? 'class="completed"' : ""}>
<div class="view">
<input
${completed ? "checked" : ""}
class="toggle"
type="checkbox">
<label>${text}</label>
<button class="destroy"></button>
</div>
<input class="edit" value="${text}">
</li>`;
};

export default (targetElement, { todos }) => {
const newTodoList = targetElement.cloneNode(true);
const todosElements = todos.map(getTodoElement).join("");
newTodoList.innerHTML = todosElements;
return newTodoList;
};

0 comments on commit a3972ca

Please sign in to comment.