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

Dropdown won't close on item click #567

Open
thai-recurmetrics opened this issue May 30, 2023 · 11 comments
Open

Dropdown won't close on item click #567

thai-recurmetrics opened this issue May 30, 2023 · 11 comments

Comments

@thai-recurmetrics
Copy link

thai-recurmetrics commented May 30, 2023

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.

@linpan
Copy link

linpan commented Jun 6, 2023

when use prefix fb- Dropdown component not working by click

@borhansahed
Copy link

Will I try to fix it?

@fitrahmunir
Copy link

Hi, can I work on this?

After inspecting the component, there is no handler to handle targetElement to hide after click.

@linpan
Copy link

linpan commented Jun 8, 2023

test on prefix: "tw-"

@fitrahmunir
Copy link

test on prefix: "tw-"

The dropdown didn't work if we try to use any prefix. It is because the show and hide method in dropdown only targetting block and hidden class which is tailwind's utility class. That's why if we use prefix fb- for example, the method cannot targetting fb-block and fb-hidden class.

@borhansahed
Copy link

Anyone fix it ?

@jonwaghorn
Copy link

jonwaghorn commented Jun 13, 2023

This is a known issue, see #132 for updates

@leikoilja
Copy link

still not working...
unfortunately had to build a workaround messy component to cater for it.
note the use of references, state and the hey is to call again initDropdowns after the class is changed

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;

@r4cker
Copy link

r4cker commented Aug 19, 2024

still here in 2.5.1 (no js framework)

@boosh
Copy link

boosh commented Sep 17, 2024

Found a workaround:

                dropdown.hide();
                dropdownEl.classList.add('hidden');
                initDropdowns();

Only seems to work with the click trigger (not hover).

E.g. with AlpineJS:

<button type="button" id="copyDropdownButton" data-dropdown-toggle="copyDropdown" data-dropdown-trigger="click" ...>

   <div id="copyDropdown" x-data="copyButtons">
        <a href="#" @click.prevent="closer">Close me</a>
  </div>

<script>
    document.addEventListener('alpine:init', () => {
        const dropdownEl = document.getElementById('copyDropdown');
        const triggerEl = document.getElementById('copyDropdownButton');
        const dropdown = new window.Dropdown(dropdownEl, triggerEl);

        Alpine.data('copyButtons', () => ({
            closer() {
                dropdown.hide();
                dropdownEl.classList.add('hidden');
                initDropdowns();
          })
    ...
</script>

It doesn't seem to work with the hover trigger, but now this closes on click

@r4cker
Copy link

r4cker commented Dec 6, 2024

there is a quick fix. When the click is triggered

const dropdown = FlowbiteInstances.getInstance("Dropdown","copyDropdown");
dropdown.hide();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

8 participants