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

Fixed issue #140 #141

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading