@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
126 lines (124 loc) • 6.2 kB
JavaScript
import { DropdownMenuCheckboxItemIndicator, DropdownMenuCheckboxItemPrimitive, DropdownMenuGroup, DropdownMenuGroupLabel, DropdownMenuItem, DropdownMenuItemContent, DropdownMenuItemExtra, DropdownMenuItemIcon, DropdownMenuItemLabel, DropdownMenuPopup, DropdownMenuPortal, DropdownMenuPositioner, DropdownMenuSeparator, DropdownMenuSubmenuArrow, DropdownMenuSubmenuRoot, DropdownMenuSubmenuTrigger, DropdownMenuSwitchItem } from "./atoms.mjs";
import { getItemKey, getItemLabel, hasAnyIcon, hasCheckboxAndIcon, renderIcon } from "../Menu/renderUtils.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
import { Check, ChevronRight } from "lucide-react";
//#region src/DropdownMenu/renderItems.tsx
const renderItemContent = (item, options, iconNode) => {
const label = getItemLabel(item);
const extra = "extra" in item ? item.extra : void 0;
const indicatorOnRight = options?.indicatorOnRight;
const hasCustomIcon = iconNode !== void 0 && !indicatorOnRight;
const hasIcon = hasCustomIcon ? Boolean(iconNode) : Boolean(item.icon);
return /* @__PURE__ */ jsxs(DropdownMenuItemContent, { children: [
(hasCustomIcon ? Boolean(options?.reserveIconSpace || iconNode) : Boolean(hasIcon || options?.reserveIconSpace)) ? /* @__PURE__ */ jsx(DropdownMenuItemIcon, {
"aria-hidden": !hasIcon,
children: hasCustomIcon ? iconNode : hasIcon ? renderIcon(item.icon) : null
}) : null,
/* @__PURE__ */ jsx(DropdownMenuItemLabel, { children: label }),
extra ? /* @__PURE__ */ jsx(DropdownMenuItemExtra, { children: extra }) : null,
indicatorOnRight && iconNode ? iconNode : null,
options?.submenu ? /* @__PURE__ */ jsx(DropdownMenuSubmenuArrow, { children: /* @__PURE__ */ jsx(ChevronRight, { size: 16 }) }) : null
] });
};
const invokeItemClick = (item, keyPath, event) => {
if (!item.onClick) return;
const key = item.key ?? keyPath.at(-1) ?? "";
const info = {
domEvent: event,
item: event.currentTarget,
key: String(key),
keyPath
};
item.onClick(info);
};
const renderDropdownMenuItems = (items, keyPath = [], options) => {
const iconSpaceMode = options?.iconSpaceMode ?? "global";
const reserveIconSpace = options?.reserveIconSpace ?? hasAnyIcon(items, iconSpaceMode === "global");
const indicatorOnRight = options?.indicatorOnRight ?? hasCheckboxAndIcon(items);
return items.map((item, index) => {
if (!item) return null;
const itemKey = getItemKey(item, `${keyPath.join("-") || "root"}-${index}`);
const nextKeyPath = [...keyPath, String(itemKey)];
if (item.type === "checkbox") {
const checkboxItem = item;
const label$1 = getItemLabel(checkboxItem);
const labelText$1 = typeof label$1 === "string" ? label$1 : void 0;
const isDanger$1 = Boolean(checkboxItem.danger);
const indicator = /* @__PURE__ */ jsx(DropdownMenuCheckboxItemIndicator, { children: renderIcon(Check) });
return /* @__PURE__ */ jsx(DropdownMenuCheckboxItemPrimitive, {
checked: checkboxItem.checked,
closeOnClick: checkboxItem.closeOnClick,
danger: isDanger$1,
defaultChecked: checkboxItem.defaultChecked,
disabled: checkboxItem.disabled,
label: labelText$1,
onCheckedChange: (checked) => checkboxItem.onCheckedChange?.(checked),
children: renderItemContent(checkboxItem, {
indicatorOnRight,
reserveIconSpace
}, indicator)
}, itemKey);
}
if (item.type === "switch") {
const switchItem = item;
const label$1 = getItemLabel(switchItem);
const labelText$1 = typeof label$1 === "string" ? label$1 : void 0;
const isDanger$1 = Boolean(switchItem.danger);
return /* @__PURE__ */ jsx(DropdownMenuSwitchItem, {
checked: switchItem.checked,
closeOnClick: switchItem.closeOnClick,
danger: isDanger$1,
defaultChecked: switchItem.defaultChecked,
disabled: switchItem.disabled,
label: labelText$1,
onCheckedChange: (checked) => switchItem.onCheckedChange?.(checked),
children: renderItemContent(switchItem, { reserveIconSpace })
}, itemKey);
}
if (item.type === "divider") return /* @__PURE__ */ jsx(DropdownMenuSeparator, {}, itemKey);
if (item.type === "group") {
const group = item;
const groupReserveIconSpace = iconSpaceMode === "group" ? group.children ? hasAnyIcon(group.children) : false : reserveIconSpace;
const groupIndicatorOnRight = group.children ? hasCheckboxAndIcon(group.children) : false;
return /* @__PURE__ */ jsxs(DropdownMenuGroup, { children: [group.label ? /* @__PURE__ */ jsx(DropdownMenuGroupLabel, { children: group.label }) : null, group.children ? renderDropdownMenuItems(group.children, nextKeyPath, {
iconSpaceMode,
indicatorOnRight: groupIndicatorOnRight,
reserveIconSpace: groupReserveIconSpace
}) : null] }, itemKey);
}
if (item.type === "submenu" || "children" in item) {
const submenu = item;
const label$1 = getItemLabel(submenu);
const labelText$1 = typeof label$1 === "string" ? label$1 : void 0;
return /* @__PURE__ */ jsxs(DropdownMenuSubmenuRoot, { children: [/* @__PURE__ */ jsx(DropdownMenuSubmenuTrigger, {
danger: "danger" in submenu && Boolean(submenu.danger),
disabled: submenu.disabled,
label: labelText$1,
children: renderItemContent(submenu, {
reserveIconSpace,
submenu: true
})
}), /* @__PURE__ */ jsx(DropdownMenuPortal, { children: /* @__PURE__ */ jsx(DropdownMenuPositioner, {
alignOffset: -4,
"data-submenu": "",
sideOffset: -1,
children: /* @__PURE__ */ jsx(DropdownMenuPopup, { children: submenu.children ? renderDropdownMenuItems(submenu.children, nextKeyPath, { iconSpaceMode }) : null })
}) })] }, itemKey);
}
const menuItem = item;
const label = getItemLabel(menuItem);
const labelText = typeof label === "string" ? label : void 0;
const isDanger = "danger" in menuItem && Boolean(menuItem.danger);
return /* @__PURE__ */ jsx(DropdownMenuItem, {
closeOnClick: menuItem.closeOnClick,
danger: isDanger,
disabled: menuItem.disabled,
label: labelText,
onClick: (event) => invokeItemClick(menuItem, nextKeyPath, event),
children: renderItemContent(menuItem, { reserveIconSpace })
}, itemKey);
});
};
//#endregion
export { renderDropdownMenuItems };
//# sourceMappingURL=renderItems.mjs.map