-
Notifications
You must be signed in to change notification settings - Fork 759
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
Dropdown won't close on item click #567
Comments
when use prefix fb- Dropdown component not working by click |
Will I try to fix it? |
Hi, can I work on this? After inspecting the component, there is no handler to handle |
test on prefix: "tw-" |
The dropdown didn't work if we try to use any prefix. It is because the |
Anyone fix it ? |
This is a known issue, see #132 for updates |
still not working... import { forwardRef, useState, useEffect, useRef } from "react";
import { initDropdowns } from "flowbite";
import { ReactComponent as ChevronDownIcon } from "@components/icons/chevron-down.svg";
interface DropdownProps {
textColor?: string;
textHover?: string;
textSize?: string;
fontSize?: string;
id?: string;
options: { code: string; nativeName: string }[];
onSelect?: (selectedOption: string) => void;
initialValue?: string;
}
const Dropdown = forwardRef(
(
{
options,
onSelect,
initialValue = options[0]?.code,
id = "default",
}: DropdownProps,
_
) => {
const [isOpen, setIsOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>();
const onOptionSelect = (selectedOption: string) => {
if (onSelect) {
onSelect(selectedOption);
}
setIsOpen(false);
};
useEffect(() => {
initDropdowns();
}, []);
useEffect(() => {
if (dropdownRef.current && isOpen === false) {
dropdownRef.current.className = dropdownRef.current.className.replace(
"block",
"hidden"
);
initDropdowns();
}
}, [isOpen]);
return (
<div>
<button
id={`buttondropdown${id}`}
data-dropdown-toggle={`dropdownTarget${id}`}
data-dropdown-placement="bottom"
className={
"bg-transparent focus:ring-0 flex items-center focus:outline-none text-center"
}
type="button"
onClick={() => setIsOpen(!isOpen)}
>
{options.find((option) => option.code === initialValue)?.nativeName}
<ChevronDownIcon className="ml-1 w-4" />
</button>
<div
id={`dropdownTarget${id}`}
className="hidden z-50 bg-white divide-y divide-gray-100 rounded-lg shadow w-44"
ref={dropdownRef}
>
<ul className="py-2 text-sm text-gray-500">
{options.map((option, index) => (
<li key={`option_${index}`}>
<span
onClick={() => onOptionSelect(option.code)}
className="block py-2 hover:bg-gray-100 cursor-pointer text-center"
>
{option.nativeName}
</span>
</li>
))}
</ul>
</div>
</div>
);
}
);
export default Dropdown; |
still here in 2.5.1 (no js framework) |
Found a workaround:
Only seems to work with the click trigger (not hover). E.g. with AlpineJS:
It doesn't seem to work with the hover trigger, but now this closes on click |
there is a quick fix. When the click is triggered
|
I am experiencing a problem with the dropdown menu, as it fails to close upon selecting an item from the dropdown list. To illustrate this issue, I have provided a video demonstration. The problem can also be reproduced on the demo site: https://www.flowbite-react.com/docs/components/dropdown. I have acquired the pro version and would appreciate assistance in finding a workaround for this issue. I attempted to create a reference and used the following code snippet: ref.current.querySelector('.menu').classList.add('hidden'), but unfortunately, it did not resolve the problem.
dropdown.mov
Definitely looking for a way to programmatically tell the dropdown to close.
The text was updated successfully, but these errors were encountered: