Skip to content
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

fix(dropdown) : 드롭다운 외부클릭 시 닫기 기능을 추가합니다. #139

Merged
merged 5 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions frontend/src/components/common/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, MouseEvent } from 'react';
import { useState, MouseEvent } from 'react';
import { IoIosArrowDown } from 'react-icons/io';
import { BsCheck2 } from 'react-icons/bs';
import Popover from './Popover';

interface Props<T> {
defaultValue?: number;
Expand Down Expand Up @@ -34,7 +35,7 @@ function Dropdown<T extends string>({ defaultValue: value, list, handleChange, s
};

return (
<div className="relative">
<Popover onClose={() => setIsOpen(false)} className="relative">
<div
className="flex justify-between items-center px-4 border-1 border-black rounded-md h-11 text-sm"
onClick={() => setIsOpen((prev) => !prev)}
Expand All @@ -60,7 +61,7 @@ function Dropdown<T extends string>({ defaultValue: value, list, handleChange, s
</ul>
</div>
)}
</div>
</Popover>
);
}
export default Dropdown;
32 changes: 32 additions & 0 deletions frontend/src/components/common/Popover.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { useEffect } from 'react';

type TProps = {
className?: string;
children: React.ReactNode;
onClose: () => void;
};

const Popover: React.FC<TProps> = ({ className, children, onClose }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

컴포넌트 props 타이핑이 컴포넌트마다 조금씩 상이해서, 컨벤션을 정해도 좋겠네요🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아니 체다 저 멘션도 안걸었는데 작업한지 어케 아신건지 ㅋㅋㅋㅋ 칼리뷰 정말 감사합니다 🥹🙇‍♂️

props 타이핑이 React.FC<TProps> 요부분이죠?

저거 회사컨벤션이라 무의식중에 사용된것 같아요😅

심플썸네일에서 컨벤션이 따로 없지만, 심플썸네일에서 자주쓰는 방식으로 변경해두겠슴다!
추후 컨벤션을 정해봐도 좋을것같아요

const wrapperRef = React.useRef<HTMLDivElement>(null);

useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) {
onClose();
}
};

document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [wrapperRef]);

return (
<div ref={wrapperRef} className={className}>
{children}
</div>
);
};

export default Popover;
Loading