-
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
Changes from 1 commit
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,192 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||
| useState, | ||||||||||||||||||||||||||||||||||||||||||||
| useEffect, | ||||||||||||||||||||||||||||||||||||||||||||
| useRef, | ||||||||||||||||||||||||||||||||||||||||||||
| ReactNode, | ||||||||||||||||||||||||||||||||||||||||||||
| SelectHTMLAttributes, | ||||||||||||||||||||||||||||||||||||||||||||
| } 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; | ||||||||||||||||||||||||||||||||||||||||||||
| width?: number | string; | ||||||||||||||||||||||||||||||||||||||||||||
| 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, | ||||||||||||||||||||||||||||||||||||||||||||
| width, | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
| className, | ||||||||||||||||||||||||||||||||||||||||||||
| wrapperClassName, | ||||||||||||||||||||||||||||||||||||||||||||
| ...rest | ||||||||||||||||||||||||||||||||||||||||||||
| }: SelectProps) { | ||||||||||||||||||||||||||||||||||||||||||||
| const [open, setOpen] = useState(false); | ||||||||||||||||||||||||||||||||||||||||||||
| const [buttonWidth, setButtonWidth] = useState<number>(0); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const wrapperRef = useRef<HTMLDivElement>(null); | ||||||||||||||||||||||||||||||||||||||||||||
| const buttonRef = useRef<HTMLButtonElement>(null); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const selectedOption = options.find((option) => option.value === value); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const wrapperClassNames = cn( | ||||||||||||||||||||||||||||||||||||||||||||
| "relative", | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| "w-full": fullWidth, | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| wrapperClassName, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const buttonClassNames = cn( | ||||||||||||||||||||||||||||||||||||||||||||
| "flex items-center justify-between rounded-[0.375rem]", | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| "w-full": fullWidth, | ||||||||||||||||||||||||||||||||||||||||||||
| "bg-white placeholder:text-gray-40 border border-gray-30": size === "lg", | ||||||||||||||||||||||||||||||||||||||||||||
| "bg-gray-10 font-bold": size === "sm", | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| sizeMap[size], | ||||||||||||||||||||||||||||||||||||||||||||
| className, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
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
<button
id={id}
type="button"
ref={buttonRef}
onClick={() => setOpen((prev) => !prev)}
className={buttonClassNames}
style={width ? { width } : undefined}
{...rest}
>
{/* <span className={cn(value ? "" : "text-gray-40")}> */}
{selectedOption?.label || placeholder}
{/* </span> */}
{open ? <DropdownUp className="ml-2" /> : <DropdownDown className="ml-2" />}
</button> |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const listClassNames = cn( | ||||||||||||||||||||||||||||||||||||||||||||
| "absolute top-full left-0 mt-1 border rounded-[0.375rem] bg-white border-gray-30 shadow-lg z-10 max-h-48 overflow-y-auto", | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
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
리스트 아이템에 텍스트와 관련된 속성은 대부분 하위 자식요소까지 상속되기 때문에 리스트에 클래스명을 주면 좋을 것 같습니다! 😄 |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const handleSelect = (selectedValue: string) => { | ||||||||||||||||||||||||||||||||||||||||||||
| onValueChange?.(selectedValue); | ||||||||||||||||||||||||||||||||||||||||||||
| setOpen(false); | ||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // 드롭다운 외부 클릭 시 닫기 | ||||||||||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||||
| const handleClickOutside = (event: MouseEvent) => { | ||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||
| wrapperRef.current && | ||||||||||||||||||||||||||||||||||||||||||||
| !wrapperRef.current.contains(event.target as Node) | ||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||
| 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} ref={wrapperRef}> | ||||||||||||||||||||||||||||||||||||||||||||
| <button | ||||||||||||||||||||||||||||||||||||||||||||
| id={id} | ||||||||||||||||||||||||||||||||||||||||||||
| type="button" | ||||||||||||||||||||||||||||||||||||||||||||
| ref={buttonRef} | ||||||||||||||||||||||||||||||||||||||||||||
| onClick={() => setOpen((prev) => !prev)} | ||||||||||||||||||||||||||||||||||||||||||||
| className={buttonClassNames} | ||||||||||||||||||||||||||||||||||||||||||||
| style={width ? { width } : undefined} | ||||||||||||||||||||||||||||||||||||||||||||
| {...rest} | ||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||
| <span className={cn(value ? "" : "text-gray-40")}> | ||||||||||||||||||||||||||||||||||||||||||||
| {selectedOption?.label || placeholder} | ||||||||||||||||||||||||||||||||||||||||||||
| </span> | ||||||||||||||||||||||||||||||||||||||||||||
| {open ? ( | ||||||||||||||||||||||||||||||||||||||||||||
| <DropdownUp className="ml-2" /> | ||||||||||||||||||||||||||||||||||||||||||||
| ) : ( | ||||||||||||||||||||||||||||||||||||||||||||
| <DropdownDown className="ml-2" /> | ||||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||||
| </button> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| {open && ( | ||||||||||||||||||||||||||||||||||||||||||||
| <ul | ||||||||||||||||||||||||||||||||||||||||||||
| className={listClassNames} | ||||||||||||||||||||||||||||||||||||||||||||
| style={{ | ||||||||||||||||||||||||||||||||||||||||||||
| width: 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", | ||||||||||||||||||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||||||||||||||||||
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로 변경되면 좋을 것 같아요! 🤔