tdesign-vue
Version:
205 lines (201 loc) • 8.75 kB
JavaScript
/**
* tdesign v1.12.1
* (c) 2025 tdesign
* @license MIT
*/
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, ref, reactive, set, onMounted, h } from '@vue/composition-api';
import { ChevronRightIcon } from 'tdesign-icons-vue';
import { isFunction } from 'lodash-es';
import _DropdownItem from './dropdown-item.js';
import props from './props.js';
import { Divider } from '../divider/index.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { useGlobalIcon } from '../hooks/useGlobalIcon.js';
import './dropdown-item-props.js';
import '../hooks/tnode.js';
import '../hooks/render-tnode.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../utils/ripple.js';
import '../_common/js/utils/setStyle.js';
import '../config.js';
import '../config-provider/config-receiver.js';
import 'vue';
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-c44a474d.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-d639fbd7.js';
import 'dayjs';
import '../_chunks/dep-3c66615e.js';
import '../config-provider/type.js';
import '../_common/js/global-config/t.js';
import '../config-provider/useConfig.js';
import '../divider/divider.js';
import '../divider/props.js';
import '../utils/render-tnode.js';
import '@babel/runtime/helpers/readOnlyError';
import '../utils/mixins.js';
import '../utils/withInstall.js';
import './style/css.js';
import '../divider/type.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 _DropdownMenu = defineComponent({
name: "TDropdownMenu",
props: _objectSpread({}, props),
setup: function setup(props, _ref) {
var emit = _ref.emit;
var dropdownClass = usePrefixClass("dropdown");
var dropdownMenuClass = usePrefixClass("dropdown__menu");
var menuRef = ref();
var isOverMaxHeight = ref(false);
var scrollTopMap = reactive({});
var handleItemClick = function handleItemClick(optionItem, options) {
var _data$onClick, _props$onClick;
if (optionItem.disabled || optionItem.children) return;
var data = options.data,
context = options.context;
data === null || data === void 0 || (_data$onClick = data.onClick) === null || _data$onClick === void 0 || _data$onClick.call(data, data, context);
(_props$onClick = props.onClick) === null || _props$onClick === void 0 || _props$onClick.call(props, data, context);
emit("click", data, context);
};
var handleScroll = function handleScroll(e, deep) {
var scrollTop = e.target.scrollTop;
set(scrollTopMap, deep, scrollTop);
};
onMounted(function () {
if (menuRef.value) {
var _window;
var menuHeight = parseInt((_window = window) === null || _window === void 0 ? void 0 : _window.getComputedStyle(menuRef.value).height, 10);
if (menuHeight >= props.maxHeight) isOverMaxHeight.value = true;
}
});
return {
dropdownClass: dropdownClass,
dropdownMenuClass: dropdownMenuClass,
handleItemClick: handleItemClick,
menuRef: menuRef,
isOverMaxHeight: isOverMaxHeight,
handleScroll: handleScroll,
scrollTopMap: scrollTopMap
};
},
methods: {
renderOptionContent: function renderOptionContent(content) {
if (isFunction(content)) {
return content(h);
}
return content;
},
renderOptions: function renderOptions(data, deep) {
var _data$forEach,
_this = this;
var h = this.$createElement;
var _useGlobalIcon = useGlobalIcon({
ChevronRightIcon: ChevronRightIcon
}),
ChevronRightIcon$1 = _useGlobalIcon.ChevronRightIcon;
var arr = [];
var renderContent;
(_data$forEach = data.forEach) === null || _data$forEach === void 0 || _data$forEach.call(data, function (menu, idx) {
var optionItem = _objectSpread({}, menu);
var onViewIdx = idx - Math.ceil(_this.scrollTopMap[deep] / 30);
var renderIdx = onViewIdx >= 0 ? onViewIdx : idx;
if (optionItem.children) {
optionItem.children = _this.renderOptions(optionItem.children, deep + 1);
renderContent = h("div", {
"key": idx
}, [h(_DropdownItem, {
"class": ["".concat(_this.dropdownClass, "__item"), "".concat(_this.dropdownClass, "__item--suffix"), optionItem["class"]],
"props": _objectSpread({}, {
value: optionItem.value,
theme: optionItem.theme,
active: optionItem.active,
prefixIcon: optionItem.prefixIcon,
disabled: optionItem.disabled,
minColumnWidth: _this.minColumnWidth,
maxColumnWidth: _this.maxColumnWidth,
isSubmenu: true
})
}, [h("div", {
"class": "".concat(_this.dropdownClass, "__item-content")
}, [h("span", {
"class": "".concat(_this.dropdownClass, "__item-text")
}, [_this.renderOptionContent(optionItem.content)]), h(ChevronRightIcon$1, {
"class": "".concat(_this.dropdownClass, "__item-direction"),
"attrs": {
"size": "16"
}
})]), h("div", {
"class": ["".concat(_this.dropdownClass, "__submenu-wrapper"), _defineProperty({}, "".concat(_this.dropdownClass, "__submenu-wrapper--").concat(_this.direction), _this.direction)],
"style": {
position: "absolute",
top: "".concat(renderIdx * 30, "px")
}
}, [h("div", {
"class": ["".concat(_this.dropdownClass, "__submenu"), _defineProperty({}, "".concat(_this.dropdownClass, "__submenu--disabled"), optionItem.disabled)],
"style": {
position: "static",
maxHeight: "".concat(_this.maxHeight, "px")
},
"on": {
"scroll": function scroll(e) {
return _this.handleScroll(e, deep + 1);
}
}
}, [h("ul", [optionItem.children])])])]), optionItem.divider ? h(Divider) : null]);
} else {
renderContent = h("div", {
"key": idx
}, [h(_DropdownItem, {
"class": ["".concat(_this.dropdownClass, "__item"), optionItem["class"]],
"props": _objectSpread({}, {
value: optionItem.value,
theme: optionItem.theme,
active: optionItem.active,
prefixIcon: optionItem.prefixIcon,
disabled: optionItem.disabled,
minColumnWidth: _this.minColumnWidth,
maxColumnWidth: _this.maxColumnWidth,
onClick: function onClick(value, context) {
return _this.handleItemClick({
disabled: optionItem.disabled,
children: optionItem.children
}, {
data: optionItem,
context: context
});
}
})
}, [h("span", {
"class": "".concat(_this.dropdownClass, "__item-text")
}, [_this.renderOptionContent(optionItem.content)])]), optionItem.divider ? h(Divider) : null]);
}
arr.push(renderContent);
});
return arr;
}
},
render: function render() {
var _this2 = this;
var h = arguments[0];
return h("div", {
"class": [this.dropdownMenuClass, "".concat(this.dropdownMenuClass, "--").concat(this.direction), _defineProperty({}, "".concat(this.dropdownMenuClass, "--overflow"), this.isOverMaxHeight)],
"style": {
maxHeight: "".concat(this.maxHeight, "px")
},
"ref": "menuRef",
"on": {
"scroll": function scroll(e) {
return _this2.handleScroll(e, 0);
}
}
}, [this.renderOptions(this.options, 0)]);
}
});
export { _DropdownMenu as default };
//# sourceMappingURL=dropdown-menu.js.map