// Copyright 2024, Command Line Inc. // SPDX-License-Identifier: Apache-2.0 import clsx from "clsx"; import { memo, useRef, useState } from "react"; import { Button } from "./button"; import { ContextMenu, MenuItem } from "./contextmenu"; import "./dropdown.less"; interface DropdownProps { label: string; items: MenuItem[]; scopeRef: React.RefObject; className?: string; } const Dropdown = memo(({ label, className, items, scopeRef }: DropdownProps) => { const anchorRef = useRef(null); const [isMenuVisible, setIsMenuVisible] = useState(false); const handleAnchorClick = () => { setIsMenuVisible((prev) => !prev); }; const mapItemsWithClick = (items: any[]) => { return items.map((item) => ({ ...item, onClick: () => { if (item.onClick) { item.onClick(); setIsMenuVisible(false); } }, subItems: item.subItems ? mapItemsWithClick(item.subItems) : undefined, })); }; return (
{isMenuVisible && ( setIsMenuVisible(visible)} anchorRef={anchorRef} scopeRef={scopeRef} /> )}
); }); Dropdown.displayName = "Dropdown"; export { Dropdown };