import React, { Fragment, useRef, useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; import { Combobox } from "@headlessui/react"; import { usePopper } from "react-popper"; import { observer } from "mobx-react-lite"; // hooks import { useLabel } from "hooks/store"; import { useDropdownKeyDown } from "hooks/use-dropdown-key-down"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; // ui import { IssueLabelsList } from "components/ui"; // icons import { Check, Component, Plus, Search, Tag } from "lucide-react"; type Props = { setIsOpen: React.Dispatch>; value: string[]; onChange: (value: string[]) => void; projectId: string; label?: JSX.Element; disabled?: boolean; tabIndex?: number; }; export const IssueLabelSelect: React.FC = observer((props) => { const { setIsOpen, value, onChange, projectId, label, disabled = false, tabIndex } = 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); // 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 openDropdown = () => { setIsDropdownOpen(true); if (!projectLabels && workspaceSlug && projectId) fetchProjectLabels(workspaceSlug.toString(), projectId); if (referenceElement) referenceElement.focus(); }; const closeDropdown = () => setIsDropdownOpen(false); const handleKeyDown = useDropdownKeyDown(openDropdown, closeDropdown, isDropdownOpen); useOutsideClickDetector(dropdownRef, closeDropdown); return ( onChange(val)} className="relative flex-shrink-0 h-full" multiple disabled={disabled} onKeyDown={handleKeyDown} > {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 min-w-[14rem] 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...

)}
)}
); });