tdesign-vue
Version:
407 lines (403 loc) • 16.8 kB
JavaScript
/**
* tdesign v1.14.1
* (c) 2025 tdesign
* @license MIT
*/
import { h as helper } from '../_chunks/dep-6a4dc7bb.js';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, inject, computed, ref, watch, toRefs, provide, reactive, onMounted, h, getCurrentInstance, nextTick } from '@vue/composition-api';
import { isFunction } from 'lodash-es';
import props from './submenu-props.js';
import { renderContent, renderTNodeJSX } from '../utils/render-tnode.js';
import FakeArrow from '../common-components/fake-arrow.js';
import Ripple from '../utils/ripple.js';
import { getKeepAnimationMixins } from '../config-provider/config-receiver.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { Popup } from '../popup/index.js';
import useCollapseAnimation from '../hooks/useCollapseAnimation.js';
import '@babel/runtime/helpers/readOnlyError';
import '@babel/runtime/helpers/typeof';
import 'vue';
import '../utils/mixins.js';
import '../_common/js/utils/setStyle.js';
import '../config.js';
import '../config-provider/context.js';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-ba613a02.js';
import '../_chunks/dep-fdb1b253.js';
import 'dayjs';
import '../_common/js/global-config/t.js';
import '@babel/runtime/helpers/slicedToArray';
import '../config-provider/useConfig.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../utils/dom.js';
import 'raf';
import '../utils/easing.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../popup/props.js';
import '../popup/container.js';
import '../utils/event.js';
import '../popup/utils.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import '../popup/plugin.js';
import '../_common/js/log/log.js';
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var keepAnimationMixins = getKeepAnimationMixins();
var _Submenu = defineComponent({
name: "TSubmenu",
components: {
FakeArrow: FakeArrow
},
mixins: [keepAnimationMixins],
directives: {
ripple: Ripple
},
props: props,
setup: function setup(props2) {
var menu = inject("TdMenu");
var theme = menu.theme,
activeValues = menu.activeValues,
expandValues = menu.expandValues,
mode = menu.mode,
isHead = menu.isHead,
open = menu.open;
var submenu = inject("TdSubmenu", {});
var setSubPopup = submenu.setSubPopup,
closeParentPopup = submenu.closeParentPopup;
var classPrefix = usePrefixClass();
var isActive = computed(function () {
return activeValues.value.indexOf(props2.value) > -1;
});
var popupVisible = ref(false);
var isCursorInPopup = ref(false);
var rippleColor = computed(function () {
return theme.value === "light" ? "#E7E7E7" : "#383838";
});
var isOpen = computed(function () {
if (mode.value === "popup") {
return popupVisible.value;
}
return expandValues ? expandValues.value.includes(props2.value) : false;
});
var menuItems = ref([]);
var isNested = ref(false);
var popupWrapperRef = ref();
var subPopupRef = ref();
var submenuRef = ref();
var transitionClass = usePrefixClass("slide-down");
var classes = computed(function () {
return ["".concat(classPrefix.value, "-submenu"), _defineProperty(_defineProperty({}, "".concat(classPrefix.value, "-is-disabled"), props2.disabled), "".concat(classPrefix.value, "-is-opened"), isOpen.value)];
});
var overlayInnerClassName = computed(function () {
return ["".concat(classPrefix.value, "-menu__popup"), "".concat(classPrefix.value, "-is-").concat(isHead ? "horizontal" : "vertical"), _defineProperty({}, "".concat(classPrefix.value, "-is-opened"), popupVisible.value)];
});
var overlayClassName = computed(function () {
var _props2$popupProps;
return ["".concat(classPrefix.value, "-menu--").concat(theme.value), isHead && "".concat(classPrefix.value, "-is-head-menu"), _defineProperty({}, "".concat(classPrefix.value, "-menu-is-nested"), isNested.value), (_props2$popupProps = props2.popupProps) === null || _props2$popupProps === void 0 ? void 0 : _props2$popupProps.overlayClassName];
});
var submenuClass = computed(function () {
return ["".concat(classPrefix.value, "-menu__item"), "".concat(classPrefix.value, "-menu__item-spacer"), "".concat(classPrefix.value, "-menu__item-spacer--").concat(isHead && !isNested.value ? "bottom" : "right"), _defineProperty(_defineProperty(_defineProperty({}, "".concat(classPrefix.value, "-is-disabled"), props2.disabled), "".concat(classPrefix.value, "-is-opened"), isOpen.value), "".concat(classPrefix.value, "-is-active"), isActive.value)];
});
var subClass = computed(function () {
return ["".concat(classPrefix.value, "-menu__sub"), _defineProperty({}, "".concat(classPrefix.value, "-is-opened"), isOpen.value)];
});
var arrowClass = computed(function () {
return [_defineProperty({}, "".concat(classPrefix.value, "-fake-arrow--active"), isOpen.value)];
});
var passSubPopupRefToParent = function passSubPopupRefToParent(val) {
if (isFunction(setSubPopup)) {
setSubPopup(val);
}
};
var handleMouseEnter = function handleMouseEnter() {
if (props2.disabled) return;
setTimeout(function () {
if (!popupVisible.value) {
open(props2.value);
nextTick(function () {
passSubPopupRefToParent(popupWrapperRef.value);
});
}
popupVisible.value = true;
}, 0);
};
var targetInPopup = function targetInPopup(el) {
return el === null || el === void 0 ? void 0 : el.classList.contains("".concat(classPrefix.value, "-menu__popup"));
};
var _loopInPopup = function loopInPopup(el) {
if (!el) return false;
return targetInPopup(el) || _loopInPopup(el.parentElement);
};
var handleMouseLeave = function handleMouseLeave(e) {
setTimeout(function () {
var inPopup = targetInPopup(e.relatedTarget);
if (isCursorInPopup.value || inPopup) return;
popupVisible.value = false;
}, 0);
};
var handleMouseLeavePopup = function handleMouseLeavePopup(e) {
var toElement = e.toElement,
relatedTarget = e.relatedTarget;
var target = toElement || relatedTarget;
if (target === subPopupRef.value) return;
var isSubmenu = function isSubmenu(el) {
return el === submenuRef.value;
};
while (target !== null && target !== document && !isSubmenu(target)) {
target = target.parentNode;
}
isCursorInPopup.value = false;
if (!isSubmenu(target)) {
popupVisible.value = false;
}
closeParentPopup === null || closeParentPopup === void 0 || closeParentPopup(e);
};
var handleEnterPopup = function handleEnterPopup() {
isCursorInPopup.value = true;
};
var handleSubmenuItemClick = function handleSubmenuItemClick() {
if (props2.disabled) return;
open(props2.value);
};
watch(popupVisible, function (visible) {
menu.open(props2.value, visible ? "add" : "remove");
});
var _toRefs = toRefs(props2),
value = _toRefs.value;
provide("TdSubmenu", reactive({
value: value,
addMenuItem: function addMenuItem(item) {
menuItems.value.push(item);
if (submenu) {
submenu.addMenuItem(item);
}
},
setSubPopup: function setSubPopup(ref2) {
subPopupRef.value = ref2;
},
closeParentPopup: function closeParentPopup(e) {
var related = e.relatedTarget;
if (_loopInPopup(related)) return;
handleMouseLeavePopup(e);
}
}));
watch(popupWrapperRef, function () {
passSubPopupRefToParent(popupWrapperRef.value);
});
onMounted(function () {
var _menu$vMenu;
menu === null || menu === void 0 || (_menu$vMenu = menu.vMenu) === null || _menu$vMenu === void 0 || _menu$vMenu.add({
value: props2.value,
parent: submenu === null || submenu === void 0 ? void 0 : submenu.value,
vnode: isFunction(props2.title) ? [props2.title(h)] : props2.title
});
var instance = getCurrentInstance();
var node = instance.parent;
while (node && !/^t(head)?menu/i.test((_node$vnode = node.vnode) === null || _node$vnode === void 0 ? void 0 : _node$vnode.tag)) {
var _node$vnode, _node$vnode2, _node;
if (/submenu/i.test((_node$vnode2 = node.vnode) === null || _node$vnode2 === void 0 ? void 0 : _node$vnode2.tag)) {
isNested.value = true;
break;
}
node = (_node = node) === null || _node === void 0 ? void 0 : _node.parent;
}
});
return {
theme: theme,
mode: mode,
isHead: isHead,
isNested: isNested,
popupVisible: popupVisible,
classes: classes,
subClass: subClass,
isOpen: isOpen,
transitionClass: transitionClass,
arrowClass: arrowClass,
overlayInnerClassName: overlayInnerClassName,
overlayClassName: overlayClassName,
submenuClass: submenuClass,
rippleColor: rippleColor,
popupWrapperRef: popupWrapperRef,
handleEnterPopup: handleEnterPopup,
handleMouseEnter: handleMouseEnter,
handleMouseLeave: handleMouseLeave,
handleMouseLeavePopup: handleMouseLeavePopup,
handleSubmenuItemClick: handleSubmenuItemClick,
classPrefix: classPrefix,
activeValues: activeValues
};
},
methods: {
renderPopup: function renderPopup(triggerElement) {
var _this$popupProps, _this$popupProps$plac, _this$popupProps2;
var h = this.$createElement;
var placement = "right-top";
if (!this.isNested && this.isHead) {
placement = "bottom-left";
}
var popupWrapper = h("div", {
"ref": "popupWrapperRef",
"class": ["".concat(this.classPrefix, "-menu__spacer"), "".concat(this.classPrefix, "-menu__spacer--").concat(!this.isNested && this.isHead ? "top" : "left")],
"on": {
"mouseenter": this.handleEnterPopup,
"mouseleave": this.handleMouseLeavePopup
}
}, [h("ul", {
"class": "".concat(this.classPrefix, "-menu__popup-wrapper")
}, [renderContent(this, "default", "content")])]);
var realPopup = h(Popup, helper([{}, (_this$popupProps = this.popupProps) !== null && _this$popupProps !== void 0 ? _this$popupProps : {}, {
"attrs": {
"overlayInnerClassName": _toConsumableArray(this.overlayInnerClassName),
"overlayClassName": _toConsumableArray(this.overlayClassName),
"visible": this.popupVisible,
"placement": (_this$popupProps$plac = (_this$popupProps2 = this.popupProps) === null || _this$popupProps2 === void 0 ? void 0 : _this$popupProps2.placement) !== null && _this$popupProps$plac !== void 0 ? _this$popupProps$plac : placement,
"content": function content() {
return popupWrapper;
}
}
}]), [h("div", {
"ref": "submenuRef",
"class": this.submenuClass
}, [triggerElement])]);
return realPopup;
},
renderHeadSubmenu: function renderHeadSubmenu() {
var h = this.$createElement;
var icon = renderTNodeJSX(this, "icon");
var rippleVal = this.keepAnimation.ripple ? this.rippleColor : false;
var normalSubmenu = [h("div", {
"directives": [{
name: "ripple",
value: rippleVal
}],
"class": this.submenuClass,
"on": {
"click": this.handleSubmenuItemClick
}
}, [renderTNodeJSX(this, "title")]), h("ul", {
"style": "opacity: 0; width: 0; height: 0; overflow: hidden"
}, [renderContent(this, "default", "content")])];
var needRotate = this.mode === "popup" && this.isNested;
var triggerElement = [icon, h("span", {
"class": ["".concat(this.classPrefix, "-menu__content")]
}, [renderTNodeJSX(this, "title", {
silent: true
})]), h(FakeArrow, {
"attrs": {
"overlayClassName": /menu/i.test(this.$parent.$options.name) ? this.arrowClass : null,
"overlayStyle": {
transform: "rotate(".concat(needRotate ? -90 : 0, "deg)")
}
}
})];
return this.mode === "normal" ? normalSubmenu : this.renderPopup(triggerElement);
},
renderSubmenu: function renderSubmenu() {
var _this$$parent$$vnode;
var h = this.$createElement;
var hasContent = this.$slots.content || this.$slots["default"];
var icon = renderTNodeJSX(this, "icon");
var child = renderContent(this, "default", "content");
var paddingLeft = 44;
if (/submenu/i.test((_this$$parent$$vnode = this.$parent.$vnode) === null || _this$$parent$$vnode === void 0 ? void 0 : _this$$parent$$vnode.tag)) {
paddingLeft += 16;
}
var _useCollapseAnimation = useCollapseAnimation(),
beforeEnter = _useCollapseAnimation.beforeEnter,
enter = _useCollapseAnimation.enter,
afterEnter = _useCollapseAnimation.afterEnter,
beforeLeave = _useCollapseAnimation.beforeLeave,
leave = _useCollapseAnimation.leave,
afterLeave = _useCollapseAnimation.afterLeave;
var needRotate = this.mode === "popup";
var rippleVal = this.keepAnimation.ripple ? this.rippleColor : false;
var normalSubmenu = [h("div", {
"directives": [{
name: "ripple",
value: rippleVal
}],
"class": this.submenuClass,
"on": {
"click": this.handleSubmenuItemClick
}
}, [icon, h("span", {
"class": ["".concat(this.classPrefix, "-menu__content")]
}, [renderTNodeJSX(this, "title")]), hasContent && h("fake-arrow", {
"attrs": {
"overlayClassName": this.arrowClass,
"overlayStyle": {
transform: "rotate(".concat(needRotate ? -90 : 0, "deg)")
}
}
})]), h("transition", {
"attrs": {
"name": this.transitionClass
},
"on": {
"beforeEnter": beforeEnter,
"enter": enter,
"afterEnter": afterEnter,
"beforeLeave": beforeLeave,
"leave": leave,
"afterLeave": afterLeave
}
}, [h("ul", {
"directives": [{
name: "show",
value: this.isOpen
}],
"attrs": {
"level": this.level
},
"class": this.subClass,
"style": {
"--padding-left": "".concat(paddingLeft, "px")
}
}, [child])])];
var triggerElement = [icon, h("span", {
"class": ["".concat(this.classPrefix, "-menu__content")]
}, [renderTNodeJSX(this, "title", {
silent: true
})]), h(FakeArrow, {
"attrs": {
"overlayClassName": /menu/i.test(this.$parent.$options.name) ? this.arrowClass : null,
"overlayStyle": {
transform: "rotate(".concat(needRotate ? -90 : 0, "deg)"),
"margin-left": "auto"
}
}
})];
return this.mode === "normal" ? normalSubmenu : this.renderPopup(triggerElement);
}
},
render: function render() {
var h = arguments[0];
var child = null;
var events = {};
var virtualChild;
if (this.activeValues.length < 2) virtualChild = h("div", {
"style": "display:none"
}, [renderContent(this, "default", "content")]);
if (this.mode === "popup") {
events = {
mouseenter: this.handleMouseEnter,
mouseleave: this.handleMouseLeave
};
}
if (Object.keys(this.$slots).length > 0) {
child = this.isHead ? this.renderHeadSubmenu() : this.renderSubmenu();
}
return h("li", {
"class": this.classes,
"on": _objectSpread({}, events)
}, [child, virtualChild]);
}
});
export { _Submenu as default };
//# sourceMappingURL=submenu.js.map