diff --git a/ui/packages/shared/profile/src/ProfileView/components/ActionButtons/GroupByDropdown.tsx b/ui/packages/shared/profile/src/ProfileView/components/ActionButtons/GroupByDropdown.tsx index 11a329d32d2..70cced94e94 100644 --- a/ui/packages/shared/profile/src/ProfileView/components/ActionButtons/GroupByDropdown.tsx +++ b/ui/packages/shared/profile/src/ProfileView/components/ActionButtons/GroupByDropdown.tsx @@ -167,6 +167,25 @@ const GroupByDropdown: React.FC = ({ labelsButtonRef, }) => { const [isDropdownOpen, setIsDropdownOpen] = useState(false); + const dropdownRef = useRef(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' @@ -179,7 +198,7 @@ const GroupByDropdown: React.FC = ({ .map(l => l.slice(FIELD_LABELS.length + 1)); return ( -
+