hongluan-ui
Version:
Hongluan Component Library for Vue 3
122 lines (119 loc) • 4.11 kB
JavaScript
import { defineComponent, getCurrentInstance, inject, toRef, computed, reactive, onMounted, onBeforeUnmount, resolveComponent, openBlock, createElementBlock, normalizeStyle, normalizeClass, createBlock, withCtx, renderSlot, createElementVNode } from 'vue';
import { HlTooltip } from '../../tooltip/index.mjs';
import '../../popper/index.mjs';
import '../../../utils/index.mjs';
import '../../../hooks/index.mjs';
import useMenu from './use-menu.mjs';
import { menuItemProps } from './menu-item2.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { throwError } from '../../../utils/error.mjs';
import { Effect } from '../../popper/src/popper.mjs';
const COMPONENT_NAME = "MenuItem";
const _sfc_main = defineComponent({
name: COMPONENT_NAME,
components: {
HlTooltip
},
props: menuItemProps,
emits: ["click"],
setup(props, { emit }) {
const { namespace } = useNamespace();
const instance = getCurrentInstance();
const rootMenu = inject("rootMenu");
if (!rootMenu)
throwError(COMPONENT_NAME, "can not inject root menu");
const { parentMenu, indexPath } = useMenu(instance, toRef(props, "index"));
const subMenu = inject(`subMenu:${parentMenu.value.uid}`);
if (!subMenu)
throwError(COMPONENT_NAME, "can not inject sub menu");
const active = computed(() => props.index === rootMenu.activeIndex);
const backgroundColor = computed(() => rootMenu.props.backgroundColor || "");
const activeTextColor = computed(() => rootMenu.props.activeTextColor || "");
const textColor = computed(() => rootMenu.props.textColor || "");
const item = reactive({
index: props.index,
indexPath,
active
});
const itemStyle = computed(() => {
const style = {
color: active.value ? activeTextColor.value : textColor.value,
backgroundColor: backgroundColor.value
};
return style;
});
const handleClick = () => {
if (!props.disabled) {
rootMenu.handleMenuItemClick({
index: props.index,
indexPath: indexPath.value,
route: props.route
});
emit("click", item);
}
};
onMounted(() => {
subMenu.addSubMenu(item);
rootMenu.addMenuItem(item);
});
onBeforeUnmount(() => {
subMenu.removeSubMenu(item);
rootMenu.removeMenuItem(item);
});
return {
namespace,
itemStyle,
Effect,
parentMenu,
rootMenu,
active,
handleClick
};
}
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
const _component_hl_tooltip = resolveComponent("hl-tooltip");
return openBlock(), createElementBlock("li", {
role: "menuitem",
tabindex: "-1",
style: normalizeStyle([_ctx.itemStyle]),
class: normalizeClass({
[_ctx.namespace + "-menu-item"]: true,
"is-active": _ctx.active,
"is-disabled": _ctx.disabled
}),
onClick: _ctx.handleClick
}, [
_ctx.parentMenu.type.name === "Menu" && _ctx.rootMenu.finalCollapse && _ctx.$slots.title ? (openBlock(), createBlock(_component_hl_tooltip, {
key: 0,
trigger: "hover",
class: "no-style",
placement: "right",
"fallback-placements": ["left"],
"show-arrow": false,
offset: 0,
"hide-after": 0,
"popper-class": `${_ctx.namespace}-menu-popover`
}, {
content: withCtx(() => [
renderSlot(_ctx.$slots, "title")
]),
default: withCtx(() => [
createElementVNode("div", { class: "item-content" }, [
renderSlot(_ctx.$slots, "default")
])
]),
_: 3
}, 8, ["popper-class"])) : (openBlock(), createElementBlock("div", {
key: 1,
class: "item-content"
}, [
renderSlot(_ctx.$slots, "default"),
renderSlot(_ctx.$slots, "title")
]))
], 14, ["onClick"]);
}
var MenuItem = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export { MenuItem as default };
//# sourceMappingURL=menu-item.mjs.map