aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaster4385 <blaster4385@tablaster.dev>2024-05-15 12:53:04 +0530
committerBlaster4385 <blaster4385@tablaster.dev>2024-05-15 12:53:04 +0530
commit4c1b757403b676287eab222248fd38cc6e18adba (patch)
tree442ad8e3291c62a419c097c0f0115ef4ebd99872
parentf6d70cf9ab622a58739d2dc939b8b1043b4791c5 (diff)
feat: close language dropdown on clicking outside it
-rw-r--r--client/src/components/CustomSelect/CustomSelect.jsx18
1 files changed, 16 insertions, 2 deletions
diff --git a/client/src/components/CustomSelect/CustomSelect.jsx b/client/src/components/CustomSelect/CustomSelect.jsx
index f588d38..3155700 100644
--- a/client/src/components/CustomSelect/CustomSelect.jsx
+++ b/client/src/components/CustomSelect/CustomSelect.jsx
@@ -1,4 +1,4 @@
-import React, { useState } from "react";
+import React, { useEffect, useState, useRef } from "react";
import styles from "./CustomSelect.module.css";
const CustomSelect = ({ options, onSelect }) => {
@@ -6,6 +6,7 @@ const CustomSelect = ({ options, onSelect }) => {
const [selectedOption, setSelectedOption] = useState(
options.length > 0 ? options[0] : null,
);
+ const selectRef = useRef(null);
const toggleDropdown = () => {
setIsOpen(!isOpen);
@@ -17,8 +18,21 @@ const CustomSelect = ({ options, onSelect }) => {
setIsOpen(false);
};
+ const handleClickOutside = (event) => {
+ if (selectRef.current && !selectRef.current.contains(event.target)) {
+ setIsOpen(false);
+ }
+ };
+
+ useEffect(() => {
+ document.addEventListener("mousedown", handleClickOutside);
+ return () => {
+ document.removeEventListener("mousedown", handleClickOutside);
+ };
+ }, []);
+
return (
- <div className={styles.select}>
+ <div className={styles.select} ref={selectRef}>
<div className={styles.selected__option} onClick={toggleDropdown}>
{selectedOption ? (
<>