Skip to content

Commit

Permalink
Dropdown bug fixed (asyncapi#353)
Browse files Browse the repository at this point in the history
Co-authored-by: Cody's Dad <[email protected]>
  • Loading branch information
2 people authored and ashmit-coder committed Sep 9, 2024
1 parent a136c8c commit 9e76f58
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions components/Dropdown/dropdown.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import React, {useState} from 'react';
import React, {useState,useRef,useEffect} from 'react';

function Dropdown({ active, items, setOptions, setOptions2 }) {
const [show, setShow] = useState(false)
const dropdownRef = useRef(null);
useEffect(() => {
// This checks if the click event occurred outside the dropdown, if true we closes the dropdown.
function handleClickOutside(event) {
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
setShow(false);
}
}
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [dropdownRef]);
return (
<div className='relative inline-block w-full'>
<div className='w-full'>
Expand All @@ -11,7 +24,7 @@ function Dropdown({ active, items, setOptions, setOptions2 }) {
id='menu-button'
aria-expanded='true'
aria-haspopup='true'
onClick={() => setShow(true)}
onClick={() => setShow(!show)}
>
<div>{active}</div>
<svg
Expand Down

0 comments on commit 9e76f58

Please sign in to comment.