-
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 3 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,96 @@ | ||
const menu = document.querySelector('#espresso-menu-name'); | ||
const menuCount = document.querySelector('.menu-count'); | ||
const menuList = document.querySelector('#espresso-menu-list'); | ||
const submitButton = document.querySelector('#espresso-menu-submit-button'); | ||
|
||
let list = [] | ||
|
||
function render() { | ||
const totalCount = list.length; | ||
menuCount.textContent = totalCount; | ||
|
||
const li = list.map(({ title }) => ( | ||
`<li class="menu-list-item d-flex items-center py-2"> | ||
<span class="w-100 pl-2 menu-name">${title}</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>` | ||
)); | ||
|
||
menuList.innerHTML = li.join(''); | ||
} | ||
|
||
render(); | ||
|
||
function addMenu() { | ||
if (menu.value === '') { | ||
alert("에스프레소 메뉴 이름을 입력해주세요."); | ||
return false; | ||
} | ||
|
||
list = [ | ||
...list, | ||
{ title: menu.value } | ||
] | ||
|
||
menu.value = ''; | ||
|
||
render(); | ||
} | ||
|
||
function editMenu(index) { | ||
let value = prompt("수정 값을 입력하세요", ""); | ||
|
||
list[index].title = value | ||
|
||
render(); | ||
} | ||
|
||
function removeMenu(index) { | ||
if (window.confirm("이 메뉴를 삭제하시겠습니까?")) { | ||
const filteredMenu = list.filter((_, i) => i !== index); | ||
|
||
list = filteredMenu | ||
} | ||
|
||
render(); | ||
} | ||
|
||
menu.addEventListener('keydown', (event) => { | ||
if (event.key === 'Enter') { | ||
event.preventDefault(); | ||
addMenu(); | ||
} | ||
}) | ||
|
||
submitButton.addEventListener('click', addMenu) | ||
|
||
const editButton = document.querySelectorAll('.menu-edit-button'); | ||
const removeButton = document.querySelectorAll('.menu-remove-button'); | ||
|
||
if (list.length > 0) { | ||
editButton.forEach((button, index) => { | ||
button.addEventListener('click', function () { | ||
editMenu(index) | ||
}) | ||
}) | ||
|
||
removeButton.forEach((button, index) => { | ||
button.addEventListener('click', function () { | ||
removeMenu(index); | ||
}) | ||
}) | ||
} | ||
|
||
|
||
|
||
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. 메뉴를 등록한 뒤 생성되는 수정과 삭제 버튼에는 이벤트 위임을 사용하면 좋을것 같아요! |
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.
변수명을 const li 보다는 li를 생성해준다는 느낌이 들어가면 좋을것 같아요!