Skip to content

Commit

Permalink
Fixed issue version0chiro#140
Browse files Browse the repository at this point in the history
  • Loading branch information
Arian authored and Arian committed Oct 15, 2024
1 parent 0ece223 commit 28fb07d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
18 changes: 15 additions & 3 deletions src/components/Buttons/Filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from "react";
import React, { useContext, useEffect, useRef } from "react";

import "./Filter.css";
import { ThemeContext } from "../../Context/themeContext";
Expand All @@ -7,7 +7,7 @@ const Filter = ({ reducedState, setReducedState }) => {
const [show, setShow] = React.useState(false); // Controls Popover
const [filters, setFilters] = React.useState(reducedState);
const { theme } = useContext(ThemeContext);

const dropdownRef = useRef(null);
function updateFilters(prop, val) {
setFilters((prev) => ({
...prev,
Expand All @@ -29,10 +29,22 @@ const Filter = ({ reducedState, setReducedState }) => {
setShow(false);
setReducedState({ ...filters });
}
useEffect(() => {
const handleClickOutside = (event) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
setShow(false);
}
};

document.addEventListener("mousedown", handleClickOutside);

return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, []);
return (
<>
<div className="relative">
<div className="relative" ref={dropdownRef}>
<div
onClick={() => {
setShow(!show);
Expand Down
19 changes: 17 additions & 2 deletions src/components/Buttons/Sort.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useState } from "react";
import React, { useContext, useEffect, useRef, useState } from "react";
import { ThemeContext } from "../../Context/themeContext";

const Sort = ({ setSortByForks, setSortByStars }) => {
Expand All @@ -7,6 +7,7 @@ const Sort = ({ setSortByForks, setSortByStars }) => {
// forksdesc, forksasc, starsdesc, starsasc, default
let [sort, setSort] = useState("default");
const [show, setShow] = useState(false);
const dropdownRef = useRef(null);
useEffect(() => {
if (!setSortByForks || !setSortByStars) return;
switch (sort) {
Expand All @@ -33,9 +34,23 @@ const Sort = ({ setSortByForks, setSortByStars }) => {
}
}, [sort, setSortByForks, setSortByStars]);

// Close Sort Filter when clicking outside
useEffect(() => {
const handleClickOutside = (event) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
setShow(false);
}
};

document.addEventListener("mousedown", handleClickOutside);

return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, []);
return (
<>
<div className="relative">
<div className="relative" ref={dropdownRef}>
<div
onClick={() => {
setShow(!show);
Expand Down

0 comments on commit 28fb07d

Please sign in to comment.