diff --git a/src/assets/icons/dropdown.svg b/src/assets/icons/dropdown.svg new file mode 100644 index 00000000..3b76743d --- /dev/null +++ b/src/assets/icons/dropdown.svg @@ -0,0 +1,3 @@ + + + diff --git a/src/components/common/Dropdown.tsx b/src/components/common/Dropdown.tsx new file mode 100644 index 00000000..74c651bc --- /dev/null +++ b/src/components/common/Dropdown.tsx @@ -0,0 +1,92 @@ +import { + useEffect, + useRef, + useState, + type Dispatch, + type SetStateAction, +} from 'react'; +import Down from '@/assets/icons/dropdown.svg'; + +interface DropdownProps { + options: string[]; + selected: string; + setSelect: Dispatch>; // Dispatch>는 set함수 타입 + placeholder?: string; + variant: 'form' | 'filter'; +} + +export default function Dropdown({ + options, + selected, + setSelect, + placeholder = '선택', + variant, +}: DropdownProps) { + const [isOpen, setIsOpen] = useState(false); + const dropdownRef = useRef(null); + + const toggleDropdown = () => { + setIsOpen((prev) => !prev); + }; + + const closeDropdown = () => { + setIsOpen(false); + }; + + const handleSelect = (options: string) => { + setSelect(options); + closeDropdown(); + }; + + // 외부 클릭 시 닫힘 + useEffect(() => { + const handleClickOutside = (e: MouseEvent) => { + if (!dropdownRef.current?.contains(e.target as Node)) { + closeDropdown(); + } + }; + document.addEventListener('click', handleClickOutside); + return () => { + document.removeEventListener('click', handleClickOutside); + }; + }, []); + + return ( +
+ + + {isOpen && ( +
+
    + {options.map((option) => ( +
  • handleSelect(option)} + className={`flex items-center justify-center border-b border-gray-20 text-body2 leading-22 font-regular text-black last:border-b-0 ${variant === 'form' ? 'pt-12 pb-11' : 'pb-8'}`} + > + {option} +
  • + ))} +
+
+ )} +
+ ); +} diff --git a/src/constants/.gitkeep b/src/constants/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/constants/dropdownOptions.ts b/src/constants/dropdownOptions.ts new file mode 100644 index 00000000..d7dc1bf1 --- /dev/null +++ b/src/constants/dropdownOptions.ts @@ -0,0 +1,45 @@ +export const ADDRESS_OPTIONS = [ + '서울시 종로구', + '서울시 중구', + '서울시 용산구', + '서울시 성동구', + '서울시 광진구', + '서울시 동대문구', + '서울시 중랑구', + '서울시 성북구', + '서울시 강북구', + '서울시 도봉구', + '서울시 노원구', + '서울시 은평구', + '서울시 서대문구', + '서울시 마포구', + '서울시 양천구', + '서울시 강서구', + '서울시 구로구', + '서울시 금천구', + '서울시 영등포구', + '서울시 동작구', + '서울시 관악구', + '서울시 서초구', + '서울시 강남구', + '서울시 송파구', + '서울시 강동구', +]; + +export const CATEGORY_OPTIONS = [ + '한식', + '중식', + '일식', + '양식', + '분식', + '카페', + '편의점', + '기타', +]; + +export const SORT_OPTIONS = [ + '마감임박순', + '시급많은순', + '시간적은순', + '가나다순', +];