Skip to content

Commit

Permalink
fix: Filter dropdown can only be closed by clicking on the filter but…
Browse files Browse the repository at this point in the history
…ton (#299)

Fixes #292
  • Loading branch information
OgDev-01 committed Sep 6, 2022
1 parent 9a7daa3 commit bce1f8e
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions components/molecules/SuperlativeSelector/superlative-selector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ContextFilterButton from "components/atoms/ContextFilterButton/context-filter-button";
import ContextFilterOption from "components/atoms/ContextFilterOption/context-filter-option";
import React, { useState } from "react";
import React, { useEffect, useRef, useState } from "react";
import Icon from "../../atoms/Icon/icon";
import cancelIcon from "public/x-circle.svg";
import Radio from "components/atoms/Radio/radio";
Expand All @@ -17,13 +17,31 @@ const SuperativeSelector: React.FC<SuperlativeSelectorProps> = ({
handleCancelClick,
selected
}) => {
const ref = useRef<HTMLDivElement>(null);
const [isOpen, setIsOpen] = useState(false);
const toggleFilter = () => {
setIsOpen(!isOpen);
};

useEffect(() => {
const checkIfClickedOutside = (e: any) => {
// If the menu is open and the clicked target is not within the menu,
// then close the menu
if (isOpen && ref.current && !ref.current.contains(e.target)) {
setIsOpen(false);
}

};
document.addEventListener("mousedown", checkIfClickedOutside);

return () => {
// Cleanup the event listener
document.removeEventListener("mousedown", checkIfClickedOutside);
};
}, [isOpen]);

return (
<div className="max-w-max relative">
<div className="max-w-max relative" ref={ref}>
<ContextFilterButton isSelected={selected ? true : false}>
{selected ? (
<div className="flex">
Expand Down

0 comments on commit bce1f8e

Please sign in to comment.