flowbite icon indicating copy to clipboard operation
flowbite copied to clipboard

Dropdown won't close on item click

Open thai-recurmetrics opened this issue 1 year ago • 10 comments

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.

https://github.com/themesberg/flowbite/assets/132764857/01af793f-d4cf-41bf-b696-a7399ff4352c

Definitely looking for a way to programmatically tell the dropdown to close.

thai-recurmetrics avatar May 30 '23 00:05 thai-recurmetrics

when use prefix fb- Dropdown component not working by click

linpan avatar Jun 06 '23 03:06 linpan

Will I try to fix it?

borhansahed avatar Jun 06 '23 15:06 borhansahed

Hi, can I work on this?

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

fitrahmunir avatar Jun 08 '23 09:06 fitrahmunir

test on prefix: "tw-"

linpan avatar Jun 08 '23 13:06 linpan

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.

fitrahmunir avatar Jun 08 '23 18:06 fitrahmunir

Anyone fix it ?

borhansahed avatar Jun 10 '23 13:06 borhansahed

This is a known issue, see https://github.com/themesberg/flowbite/issues/132 for updates

jonwaghorn avatar Jun 13 '23 19:06 jonwaghorn

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;

leikoilja avatar Jul 06 '23 11:07 leikoilja

still here in 2.5.1 (no js framework)

r4cker avatar Aug 19 '24 10:08 r4cker