UNPKG

tdesign-vue

Version:
230 lines (226 loc) 10.4 kB
/** * tdesign v1.15.0 * (c) 2026 tdesign * @license MIT */ import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { defineComponent, ref, reactive, computed, onMounted, h, set } 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 { useTNodeJSX } from '../hooks/tnode.js'; import './dropdown-item-props.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-e5d497fb.js'; import '@babel/runtime/helpers/typeof'; import '../_chunks/dep-1618e1d6.js'; import 'dayjs'; import '../_common/js/global-config/t.js'; import '../hooks/render-tnode.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'; 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, slots = _ref.slots; var dropdownClass = usePrefixClass("dropdown"); var dropdownMenuClass = usePrefixClass("dropdown__menu"); var renderTNodeJSX = useTNodeJSX(); var menuRef = ref(); var isOverMaxHeight = ref(false); var scrollTopMap = reactive({}); var panelTopContentHeight = ref(0); var validPanelTopContent = computed(function () { return !!slots["panel-top-content"]; }); 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); requestAnimationFrame(function () { if (validPanelTopContent.value) { var _getComputedStyle, _menuRef$value$childN; var panelTopHeight = parseInt((_getComputedStyle = getComputedStyle((_menuRef$value$childN = menuRef.value.childNodes) === null || _menuRef$value$childN === void 0 ? void 0 : _menuRef$value$childN[0])) === null || _getComputedStyle === void 0 ? void 0 : _getComputedStyle.height, 10) || 0; panelTopContentHeight.value = panelTopHeight; } }); if (menuHeight >= props.maxHeight) isOverMaxHeight.value = true; } }); return { dropdownClass: dropdownClass, dropdownMenuClass: dropdownMenuClass, handleItemClick: handleItemClick, menuRef: menuRef, isOverMaxHeight: isOverMaxHeight, handleScroll: handleScroll, scrollTopMap: scrollTopMap, renderTNodeJSX: renderTNodeJSX, validPanelTopContent: validPanelTopContent, panelTopContentHeight: panelTopContentHeight }; }, 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; var shouldCalcPanelTopContent = _this.validPanelTopContent && deep > 0; 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 + (shouldCalcPanelTopContent ? 0 : _this.panelTopContentHeight), "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 _this$renderTNodeJSX, _this$renderTNodeJSX2, _this2 = this; var h = arguments[0]; var panelTopContent = (_this$renderTNodeJSX = this.renderTNodeJSX("panelTopContent")) === null || _this$renderTNodeJSX === void 0 ? void 0 : _this$renderTNodeJSX[0]; var panelBottomContent = (_this$renderTNodeJSX2 = this.renderTNodeJSX("panelBottomContent")) === null || _this$renderTNodeJSX2 === void 0 ? void 0 : _this$renderTNodeJSX2[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); } } }, [panelTopContent ? h("div", { "attrs": { "className": "".concat(this.dropdownClass, "__top-content") } }, [panelTopContent]) : null, this.renderOptions(this.options, 0), panelBottomContent ? h("div", { "attrs": { "className": "".concat(this.dropdownClass, "__bottom-content") } }, [panelBottomContent]) : null]); } }); export { _DropdownMenu as default }; //# sourceMappingURL=dropdown-menu.js.map