-
Notifications
You must be signed in to change notification settings - Fork 4
[feat] Select 컴포넌트 생성 #45
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
Merged
Changes from all commits
Commits
Show all changes
2 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import { | ||
| useState, | ||
| useEffect, | ||
| useRef, | ||
| ReactNode, | ||
| SelectHTMLAttributes, | ||
| useMemo, | ||
| } from "react"; | ||
|
|
||
| import { DropdownDown, DropdownUp } from "@/assets/icon"; | ||
| import { cn } from "@/utils/cn"; | ||
|
|
||
| const sizeMap = { | ||
| lg: "py-4 px-5 text-[1rem]", | ||
| sm: "p-2.5 text-sm", | ||
| } as const; | ||
|
|
||
| interface Option { | ||
| label: string; | ||
| value: string; | ||
| } | ||
|
|
||
| interface SelectProps | ||
| extends Omit< | ||
| SelectHTMLAttributes<HTMLButtonElement>, | ||
| "size" | "onChange" | "disabled" | ||
| > { | ||
| id?: string; | ||
| label?: string; | ||
| options: Option[]; | ||
| value?: string; | ||
| onValueChange?: (value: string) => void; | ||
| placeholder?: string; | ||
| size?: keyof typeof sizeMap; | ||
| fullWidth?: boolean; | ||
| className?: string; | ||
| wrapperClassName?: string; | ||
| } | ||
|
|
||
| // 라벨과 입력 영역을 감싸는 공통 컴포넌트 | ||
| function Field({ | ||
| id, | ||
| label, | ||
| children, | ||
| }: { | ||
| id?: string; | ||
| label?: string; | ||
| children: ReactNode; | ||
| }) { | ||
| return ( | ||
| <div> | ||
| {label && ( | ||
| <label htmlFor={id} className="inline-block mb-2 leading-[1.625rem]"> | ||
| {label} | ||
| </label> | ||
| )} | ||
| {children} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function Select({ | ||
| id, | ||
| label, | ||
| options, | ||
| value, | ||
| onValueChange, | ||
| placeholder = "선택", | ||
| size = "lg", | ||
| fullWidth, | ||
| className, | ||
| wrapperClassName, | ||
| ...rest | ||
| }: SelectProps) { | ||
| const [open, setOpen] = useState(false); | ||
| const [buttonWidth, setButtonWidth] = useState<number>(0); | ||
|
|
||
| const wrapperRef = useRef<HTMLUListElement>(null); | ||
| const buttonRef = useRef<HTMLButtonElement>(null); | ||
|
|
||
| const selectedOption = useMemo(() => { | ||
| return options.find((option) => option.value === value); | ||
| }, [options, value]); | ||
|
|
||
| const wrapperClassNames = cn( | ||
| "relative", | ||
| { | ||
| "w-full": fullWidth, | ||
| }, | ||
| wrapperClassName, | ||
| ); | ||
|
|
||
| const buttonClassNames = cn( | ||
| "flex items-center justify-between rounded-[0.375rem] cursor-pointer", | ||
| { | ||
| "w-full": fullWidth, | ||
| "bg-white border border-gray-30": size === "lg", | ||
| "bg-gray-10 font-bold": size === "sm", | ||
| }, | ||
| value ? "text-black" : "text-gray-40", | ||
| sizeMap[size], | ||
| className, | ||
| ); | ||
|
|
||
| const listClassNames = cn( | ||
| "absolute top-full left-0 mt-1 border rounded-[0.375rem] bg-white border-gray-30 text-black shadow-lg z-10 max-h-48 overflow-y-auto", | ||
| ); | ||
|
|
||
| const handleSelect = (selectedValue: string) => { | ||
| onValueChange?.(selectedValue); | ||
| setOpen(false); | ||
| }; | ||
|
|
||
| // 드롭다운 외부 클릭 시 닫기 | ||
| useEffect(() => { | ||
| const handleClickOutside = (event: MouseEvent) => { | ||
| const target = event.target as Node; | ||
| if ( | ||
| !buttonRef.current?.contains(target) && | ||
| !wrapperRef.current?.contains(target) | ||
| ) { | ||
| setOpen(false); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener("mousedown", handleClickOutside); | ||
| return () => { | ||
| document.removeEventListener("mousedown", handleClickOutside); | ||
| }; | ||
| }, []); | ||
|
|
||
| // 버튼 너비 측정 (드롭다운 너비 일치시키기 위함) | ||
| useEffect(() => { | ||
| if (buttonRef.current) { | ||
| const rect = buttonRef.current.getBoundingClientRect(); | ||
| setButtonWidth(rect.width); | ||
| } | ||
| }, [open, fullWidth, size, value]); // 버튼 사이즈가 변할 수 있는 경우 | ||
|
|
||
| return ( | ||
| <Field id={id} label={label}> | ||
| <div className={wrapperClassNames}> | ||
| <button | ||
| id={id} | ||
| type="button" | ||
| ref={buttonRef} | ||
| onClick={() => setOpen((prev) => !prev)} | ||
| className={buttonClassNames} | ||
| {...rest} | ||
| > | ||
| {selectedOption?.label || placeholder} | ||
| {open ? ( | ||
| <DropdownUp className="ml-2" /> | ||
| ) : ( | ||
| <DropdownDown className="ml-2" /> | ||
| )} | ||
| </button> | ||
|
|
||
| {open && ( | ||
| <ul | ||
| ref={wrapperRef} | ||
| className={listClassNames} | ||
| style={{ | ||
| width: buttonWidth, | ||
| }} | ||
| > | ||
| {options.map((option) => ( | ||
| <li | ||
| key={option.value} | ||
| className="border-b border-gray-20 last:border-0" | ||
| > | ||
| <button | ||
| type="button" | ||
| className={cn( | ||
| "w-full text-center hover:bg-gray-10 cursor-pointer", | ||
| size === "sm" | ||
| ? "px-3 py-2 text-sm" | ||
| : "px-5 py-3 text-[1rem]", | ||
| )} | ||
| onClick={() => handleSelect(option.value)} | ||
| > | ||
| {option.label} | ||
| </button> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| )} | ||
| </div> | ||
| </Field> | ||
| ); | ||
| } | ||
|
|
||
| export default Select; |
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
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.
Select 컴포넌트의 리스트가 나타나고 난 뒤, 같은 가로 열 어딘가를 클릭했을 때 닫히지 않는 버그가 있습니다.
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.
그리고 버튼이나 리스트에 호버했을 때, 마우스 커서가
pointer로 변경되면 좋을 것 같아요! 🤔