import React, { Fragment, useEffect, useRef, useState } from "react"; import { observer } from "mobx-react"; import { useRouter } from "next/router"; import { usePopper } from "react-popper"; import { Check, Component, Plus, Search, Tag } from "lucide-react"; import { Combobox } from "@headlessui/react"; // hooks import { IssueLabelsList } from "@/components/ui"; import { useLabel } from "@/hooks/store"; import { useDropdownKeyDown } from "@/hooks/use-dropdown-key-down"; import useOutsideClickDetector from "@/hooks/use-outside-click-detector"; // ui // icons type Props = { setIsOpen: React.Dispatch>; value: string[]; onChange: (value: string[]) => void; projectId: string; label?: JSX.Element; disabled?: boolean; tabIndex?: number; createLabelEnabled?: boolean; }; export const IssueLabelSelect: React.FC = observer((props) => { const { setIsOpen, value, onChange, projectId, label, disabled = false, tabIndex, createLabelEnabled = true } = props; // router const router = useRouter(); const { workspaceSlug } = router.query; // store hooks const { getProjectLabels, fetchProjectLabels } = useLabel(); // states const [query, setQuery] = useState(""); const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); const [isDropdownOpen, setIsDropdownOpen] = useState(false); // refs const dropdownRef = useRef(null); const inputRef = useRef(null); // popper const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: "bottom-start", }); const projectLabels = getProjectLabels(projectId); // derived values const filteredOptions = query === "" ? projectLabels : projectLabels?.filter((l) => l.name.toLowerCase().includes(query.toLowerCase())); const onOpen = () => { if (!projectLabels && workspaceSlug && projectId) fetchProjectLabels(workspaceSlug.toString(), projectId); if (referenceElement) referenceElement.focus(); }; const handleClose = () => { if (isDropdownOpen) setIsDropdownOpen(false); if (referenceElement) referenceElement.blur(); }; const toggleDropdown = () => { if (!isDropdownOpen) onOpen(); setIsDropdownOpen((prevIsOpen) => !prevIsOpen); }; const dropdownOnChange = (val: string[]) => { onChange(val); }; const handleKeyDown = useDropdownKeyDown(toggleDropdown, handleClose); const handleOnClick = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); toggleDropdown(); }; useOutsideClickDetector(dropdownRef, handleClose); useEffect(() => { if (isDropdownOpen && inputRef.current) { inputRef.current.focus(); } }, [isDropdownOpen]); return ( {isDropdownOpen && (
setQuery(event.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} />
{projectLabels && filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((label) => { const children = projectLabels?.filter((l) => l.parent === label.id); if (children.length === 0) { if (!label.parent) return ( `${ active ? "bg-custom-background-80" : "" } group flex w-full cursor-pointer select-none items-center gap-2 truncate rounded px-1 py-1.5 text-custom-text-200` } value={label.id} > {({ selected }) => (
{label.name}
)}
); } else return (
{label.name}
{children.map((child) => ( `${ active ? "bg-custom-background-80" : "" } group flex min-w-[14rem] cursor-pointer select-none items-center gap-2 truncate rounded px-1 py-1.5 text-custom-text-200` } value={child.id} > {({ selected }) => (
{child.name}
)}
))}
); }) ) : (

No matching results

) ) : (

Loading...

)} {createLabelEnabled && ( )}
)}
); });