Skip to content

Commit

Permalink
Merge pull request #5381 from parca-dev/auto-dismiss-groupby
Browse files Browse the repository at this point in the history
area/ui: auto close the group by dropdown when there's an outside click
  • Loading branch information
yomete authored Dec 11, 2024
2 parents f9348e9 + 68d4a3e commit c8b98c9
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ const GroupByDropdown: React.FC<GroupByDropdownProps> = ({
labelsButtonRef,
}) => {
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const handleClickOutside = (event: MouseEvent): void => {
if (
isDropdownOpen &&
dropdownRef.current != null &&
!dropdownRef.current.contains(event.target as Node)
) {
setIsDropdownOpen(false);
}
};

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

const label =
groupBy.length === 0
? 'Nothing'
Expand All @@ -179,7 +198,7 @@ const GroupByDropdown: React.FC<GroupByDropdownProps> = ({
.map(l => l.slice(FIELD_LABELS.length + 1));

return (
<div className="relative">
<div className="relative" ref={dropdownRef}>
<label className="text-sm">Group by</label>
<div className="relative text-left" id="h-group-by-filter">
<Button
Expand Down

0 comments on commit c8b98c9

Please sign in to comment.