-
Notifications
You must be signed in to change notification settings - Fork 373
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 돔 조작과 이벤트 핸들링으로 메뉴 관리하기 #273
Open
jnny1017
wants to merge
5
commits into
blackcoffee-study:main
Choose a base branch
from
jnny1017:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const menuName = document.querySelector('#espresso-menu-name'); | ||
const menuList = document.querySelector('#espresso-menu-list'); | ||
const menuCountText = document.querySelector('.menu-count'); | ||
const submitButton = document.querySelector('#espresso-menu-submit-button'); | ||
|
||
const menuItemTemplate = (menuName) => { | ||
return `<li class="menu-list-item d-flex items-center py-2"> | ||
<span class="w-100 pl-2 menu-name">${menuName}</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>` | ||
}; | ||
|
||
const updateMenuCount = () => { | ||
const menuCount = menuList.querySelectorAll('li').length; | ||
menuCountText.innerText = `총 ${menuCount} 개` | ||
}; | ||
|
||
const addMenuName = () => { | ||
if (menuName.value === '') { | ||
alert("에스프레소 메뉴 이름을 입력해주세요."); | ||
return; | ||
} | ||
|
||
const espressoMenuName = menuName.value; | ||
|
||
menuList.insertAdjacentHTML("beforeend", menuItemTemplate(espressoMenuName)); | ||
updateMenuCount(); | ||
menuName.value = ""; | ||
} | ||
|
||
const updateMenuName = (event) => { | ||
const menuName = event.target.closest('li').querySelector('.menu-name'); | ||
const updateMenuName = prompt("수정 값을 입력하세요", menuName.innerText); | ||
|
||
menuName.innerText = updateMenuName; | ||
} | ||
|
||
const removeMenuName = (event) => { | ||
if (window.confirm("이 메뉴를 삭제하시겠습니까?")) { | ||
event.target.closest('li').remove(); | ||
updateMenuCount(); | ||
} | ||
} | ||
|
||
submitButton.addEventListener('click', addMenuName) | ||
|
||
menuName.addEventListener('keydown', (event) => { | ||
if (event.key !== 'Enter') { | ||
return | ||
} | ||
|
||
addMenuName(); | ||
}) | ||
|
||
menuList.addEventListener('click', (event) => { | ||
if (event.target.classList.contains('menu-edit-button')) { | ||
updateMenuName(event); | ||
} | ||
|
||
if (event.target.classList.contains('menu-remove-button')) { | ||
removeMenuName(event); | ||
} | ||
}) | ||
|
||
menuList.addEventListener('submit', (event) => { | ||
event.preventDefault(); | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
메뉴를 등록한 뒤 생성되는 수정과 삭제 버튼에는 이벤트 위임을 사용하면 좋을것 같아요!