-
Notifications
You must be signed in to change notification settings - Fork 2
#16 메뉴 컴포넌트 / Dropdown 컴포넌트 #35
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
Merged
The head ref may contain hidden characters: "feature/#16_Dropdown-\uCEF4\uD3EC\uB10C\uD2B8"
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5df0086
✨ 메뉴 팝업 컴포넌트 작성
solprime 569253f
Merge branch 'develop' of https://github.com/codeitFE11-part3-team7/w…
solprime b114cce
📝 메뉴 컴포넌트 주석 추가
solprime 7e7c0d5
📝 함수 선언식 변경
solprime 0ea1436
✨ 드롭다운 컴포넌트 작성
solprime 49fab53
📝 드롭다운 주석 추가
solprime e24c2b7
🐛 ESLint 설정에 따른 수정
solprime e614806
Merge branch 'develop' of https://github.com/codeitFE11-part3-team7/w…
solprime 936d8e6
💄 디자인 리뷰 반영
solprime 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 hidden or 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,73 @@ | ||
| import { useEffect, useRef, useState } from 'react'; | ||
|
|
||
| import Menu from './Menu'; | ||
|
|
||
| interface DropdownProps { | ||
| options: string[]; | ||
| dropdownSize?: string; | ||
| onSelect: (option: string) => void; | ||
| } | ||
|
|
||
| /** | ||
| * 드롭다운 컴포넌트 | ||
| * @param options 메뉴팝업에 들어가는 옵션을 담을 배열 | ||
| * @param dropdownSize 팝업의 크기 | ||
| * @param onSelect 선택한 옵션을 부모 컴포넌트로 반환 | ||
| */ | ||
|
|
||
| export default function Dropdown({ | ||
| options, | ||
| onSelect, | ||
| dropdownSize = 'w-auto', | ||
| }: DropdownProps) { | ||
| const [selectedOption, setSelectedOption] = useState(options[0]); //현재 선택된 옵션 | ||
| const [isOpen, setIsOpen] = useState(false); //드롭다운 열기/닫기 | ||
| const dropdownRef = useRef<HTMLDivElement>(null); // 드롭다운 DOM 요소 참조 | ||
|
|
||
| //선택한 옵션을 상태로 설정, 부모 컴포넌트로 전달 | ||
| const handleOptionClick = (option: string) => { | ||
| setSelectedOption(option); | ||
| setIsOpen(false); | ||
| onSelect(option); | ||
| }; | ||
|
|
||
| //외부 클릭시 드롭다운 닫기 | ||
| useEffect(() => { | ||
| const handleClickOutside = (event: MouseEvent) => { | ||
| if ( | ||
| dropdownRef.current && | ||
| !dropdownRef.current.contains(event.target as Node) | ||
| ) { | ||
| setIsOpen(false); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener('mousedown', handleClickOutside); | ||
| return () => { | ||
| document.removeEventListener('mousedown', handleClickOutside); | ||
| }; | ||
| }, []); | ||
|
|
||
| return ( | ||
| <div ref={dropdownRef}> | ||
| <button | ||
| onClick={() => setIsOpen(!isOpen)} | ||
| className={`${dropdownSize} flex items-center justify-between rounded-xl border border-gray-300 bg-background px-5 py-3.5 text-14 leading-none text-gray-400 hover:border-green-200 focus:ring-1 focus:ring-green-200`} | ||
| > | ||
| {selectedOption} | ||
| <img | ||
| src="/icon/icon-arrowdown.svg" | ||
| className={`h-4 w-4 ${isOpen ? 'rotate-180' : 'rotate-0'}`} | ||
| /> | ||
| </button> | ||
|
|
||
| {isOpen && ( | ||
| <Menu | ||
| options={options} | ||
| onSelect={handleOptionClick} | ||
| menuSize={dropdownSize} | ||
| /> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or 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,30 @@ | ||
| interface MenuProps { | ||
| options: string[]; | ||
| menuSize?: string; | ||
| onSelect: (option: string) => void; | ||
| } | ||
|
|
||
| /** | ||
| * 메뉴팝업을 띄우는 컴포넌트 | ||
| * @param options 메뉴팝업에 들어가는 옵션을 담을 배열 | ||
| * @param menuSize 팝업의 크기 | ||
| * @param onSelect 선택한 옵션을 반환 | ||
| */ | ||
|
|
||
| export default function Menu({ options, onSelect, menuSize }: MenuProps) { | ||
| return ( | ||
| <ul | ||
| className={`${menuSize} absolute z-10 mt-2 rounded-xl border border-gray-300 bg-background p-1 text-14 shadow-custom`} | ||
| > | ||
junghwaYang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {options.map((option, index) => ( | ||
| <li | ||
| key={index} | ||
| onClick={() => onSelect(option)} | ||
| className={`w-auto cursor-pointer rounded-md px-3 py-2.5 hover:bg-green-100`} | ||
| > | ||
| {option} | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| ); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.