-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[14기 정새미] step1 돔 조작과 이벤트 핸들링으로 메뉴 관리하기 #274
base: eddington524
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,79 @@ | ||||||||||||||||||||||||||||||||||||
const $ = (selector) => document.querySelector(selector); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
function App() { | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const createMenuName = () => { | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if($("#espresso-menu-name").value === ""){ | ||||||||||||||||||||||||||||||||||||
alert("값을 입력해주세요"); | ||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const espressoMenuName = $("#espresso-menu-name").value; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
$("#espresso-menu-list").insertAdjacentHTML("beforeend", menuItemTemplate(espressoMenuName)); | ||||||||||||||||||||||||||||||||||||
updateMenuCount(); | ||||||||||||||||||||||||||||||||||||
$("#espresso-menu-name").value = ""; | ||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const updateMenuCount = () => { | ||||||||||||||||||||||||||||||||||||
const menuCount = $("#espresso-menu-list").querySelectorAll('li').length; | ||||||||||||||||||||||||||||||||||||
$(".menu-count").innerText = `총 ${menuCount} 개` | ||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const menuItemTemplate = (espressoMenuName) => { | ||||||||||||||||||||||||||||||||||||
return `<li class="menu-list-item d-flex items-center py-2"> | ||||||||||||||||||||||||||||||||||||
<span class="w-100 pl-2 menu-name">${espressoMenuName}</span> | ||||||||||||||||||||||||||||||||||||
<button | ||||||||||||||||||||||||||||||||||||
type="button" | ||||||||||||||||||||||||||||||||||||
class="bg-gray-50 text-gray-500 text-sm mr-1 menu-edit-button" | ||||||||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||||||||
수정 | ||||||||||||||||||||||||||||||||||||
</button> | ||||||||||||||||||||||||||||||||||||
<button | ||||||||||||||||||||||||||||||||||||
type="button" | ||||||||||||||||||||||||||||||||||||
class="bg-gray-50 text-gray-500 text-sm menu-remove-button" | ||||||||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||||||||
삭제 | ||||||||||||||||||||||||||||||||||||
</button> | ||||||||||||||||||||||||||||||||||||
</li>` | ||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제안드립니다 // espressMenuName 문자열을 받아서 템플릿리터럴로 쓰여진 html코드 문자열을 반환하는 함수
type menuItemTemplate = (espressMenuName : string ) => string 이런 식으로 볼 수 있을 거같아요. 그럼 get- 접두사prefix 붙이는게 더 좋은 네이밍일거 같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네네! 말씀해주신대로 prefix를 붙이는게 더 좋은 것 같아요~ 감사합니다! |
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const removeMenuName = (e) => { | ||||||||||||||||||||||||||||||||||||
e.target.closest("li").remove(); | ||||||||||||||||||||||||||||||||||||
updateMenuCount(); | ||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const editMenuName = (e) =>{ | ||||||||||||||||||||||||||||||||||||
const $menuName = e.target.closest("li").querySelector(".menu-name"); | ||||||||||||||||||||||||||||||||||||
const editedName = prompt("메뉴명을 수정하세요", $menuName.innerText); | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. innerText 보다는 가급적 textContent 를 이용하시는게 미래에 있을 예상하지 못할 버그나 성능문제를 막는데에 조금 더 도움이 될 거에요. |
||||||||||||||||||||||||||||||||||||
$menuName.innerText = editedName; | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하 null값 체크를 못했네요! 감사합니다! if(!editedName) return; 이렇게 체크해보겠습니다~ |
||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
$("#espresso-menu-list").addEventListener("click", (e) => { | ||||||||||||||||||||||||||||||||||||
if (e.target.classList.contains("menu-edit-button")){ | ||||||||||||||||||||||||||||||||||||
editMenuName(e); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if(e.target.classList.contains("menu-remove-button")){ | ||||||||||||||||||||||||||||||||||||
if(confirm("정말 삭제하시겠습니까?")){ | ||||||||||||||||||||||||||||||||||||
removeMenuName(e); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// form 태그가 자동으로 전송되는 걸 막기 | ||||||||||||||||||||||||||||||||||||
$("#espresso-menu-form").addEventListener("submit", (e) => { | ||||||||||||||||||||||||||||||||||||
e.preventDefault(); | ||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HTMLFormElement 의 |
||||||||||||||||||||||||||||||||||||
// 메뉴의 이름을 입력 받기 | ||||||||||||||||||||||||||||||||||||
$("#espresso-menu-name").addEventListener("keydown", (e) => { | ||||||||||||||||||||||||||||||||||||
if(e.key !== "Enter") return; | ||||||||||||||||||||||||||||||||||||
createMenuName(); | ||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
$("#espresso-menu-submit-button").addEventListener("click", createMenuName); | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. submit이벤트에서 createMenuName를 호출하는것을 말씀하시는 것일까요? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
아아 동작은 동일하지만 아마 이렇게 줄이면 조금 더 간결하지 않을까 하는 의미였습니다 ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. form 요소 안의 input에 Enter keydown이벤트를 받는 방식을, 위에 106님이 하신 것처럼 할 수 있습니다 는 이미 달아주셧군요 |
||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
App(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
input
의.value
를 활용하여 값을 비워주는 방식도 좋지만, 우리가 사용하는 HTMLFormElement 스펙에서는reset
이라는 메서드를 지원하여 form 요소를 기본 값으로 초기화하기도 합니다. HTML 의 기본 스펙은 생각보다 많은 기능을 지원하여 알아두면 도움이 될 것 같아요.