-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
172 additions
and
12 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useRef, useState } from 'react'; | ||
|
||
import useOuterClick from '@/hooks/useOuterClick'; | ||
|
||
export interface DropdownOption<T extends string> { | ||
value: T; | ||
label: string; | ||
} | ||
|
||
interface DropdownProps<T extends string> { | ||
options: DropdownOption<T>[]; | ||
value?: T; | ||
onChange?: (value: T) => void; | ||
bgColor?: string; | ||
textColor?: string; | ||
iconColor?: string; | ||
placeholder?: string; | ||
} | ||
|
||
/*아래는 미지정 시 기본값이며 이 컴포넌트 사용하실 때마다 디자인 맞춰서 바꿔주시면 됩니다!*/ | ||
|
||
const Dropdown = <T extends string>({ | ||
options, | ||
value, | ||
onChange, | ||
bgColor = 'bg-textGreen', | ||
textColor = 'text-black', | ||
iconColor = 'fill-black', | ||
placeholder = '전체', | ||
}: DropdownProps<T>) => { | ||
const [opened, setOpened] = useState<boolean>(false); | ||
const dropdownRef = useRef<HTMLDivElement>(null); | ||
const toggleRef = useRef<HTMLDivElement>(null); | ||
|
||
const selectedOption = options.find((option) => option.value === value); | ||
|
||
const handleToggle = () => { | ||
setOpened((prev) => !prev); | ||
}; | ||
|
||
const handleOptionClick = (newValue: T) => { | ||
onChange?.(newValue); | ||
setOpened(false); | ||
}; | ||
|
||
const handleClickOutside = () => { | ||
setOpened(false); | ||
}; | ||
|
||
useOuterClick(dropdownRef, handleClickOutside, [toggleRef]); | ||
|
||
return ( | ||
<div | ||
className="relative inline-block text-left" | ||
ref={toggleRef} | ||
> | ||
<div | ||
className={`h-[26px] px-3 py-[5px] ${bgColor} flex cursor-pointer items-center gap-1 rounded-md`} | ||
onClick={handleToggle} | ||
> | ||
<span className={`${textColor} text-sm font-medium`}> | ||
{selectedOption?.label || placeholder} | ||
</span> | ||
<div data-svg-wrapper> | ||
<svg | ||
width="12" | ||
height="7" | ||
viewBox="0 0 12 7" | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
className={`transition-transform ${opened ? 'rotate-180' : ''} ${iconColor}`} | ||
> | ||
<path d="M6 6.35803L0.803849 0.358031L11.1962 0.358032L6 6.35803Z" /> | ||
</svg> | ||
</div> | ||
</div> | ||
{opened && ( | ||
<div | ||
className="absolute left-0 z-10 mt-1 w-max min-w-[60px] overflow-hidden rounded-md bg-white text-sm shadow-md" | ||
ref={dropdownRef} | ||
> | ||
{options.map((option) => ( | ||
<div | ||
key={option.value} | ||
className="cursor-pointer px-3 py-2 text-sm text-gray-800 hover:bg-gray-200" | ||
onClick={() => handleOptionClick(option.value)} | ||
> | ||
{option.label} | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Dropdown; |
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,29 @@ | ||
import { RefObject, useEffect } from 'react'; | ||
|
||
const useOuterClick = ( | ||
ref: RefObject<HTMLElement>, | ||
callback: () => void, | ||
ignoreRefs: RefObject<HTMLElement>[] = [] | ||
) => { | ||
useEffect(() => { | ||
const handleClick = ({ target }: MouseEvent) => { | ||
if ( | ||
!(target instanceof Node) || | ||
!ref.current || | ||
ref.current.contains(target) | ||
) | ||
return; | ||
|
||
if (ignoreRefs.some((ignoreRef) => ignoreRef.current?.contains(target))) { | ||
return; | ||
} | ||
|
||
callback(); | ||
}; | ||
|
||
document.addEventListener('mouseup', handleClick); | ||
return () => document.removeEventListener('mouseup', handleClick); | ||
}, [ref, callback, ignoreRefs]); | ||
}; | ||
|
||
export default useOuterClick; |
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