UNPKG

tdesign-mobile-vue

Version:
221 lines (217 loc) 9.31 kB
/** * tdesign v1.7.0 * (c) 2024 TDesign Group * @license MIT */ import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { defineComponent, reactive, ref, watch, computed, provide, createVNode, resolveComponent } from 'vue'; import { onClickOutside } from '@vueuse/core'; import { CaretDownSmallIcon, CaretUpSmallIcon } from 'tdesign-icons-vue-next'; import camelCase from 'lodash/camelCase'; import config from '../config.js'; import { context, DropdownMenuExpandState } from './context.js'; import { findRelativeRect, findRelativeContainer } from './dom-utils.js'; import { useContent } from '../hooks/tnode.js'; import DropdownMenuProps from './props.js'; import { usePrefixClass } from '../hooks/useClass.js'; import { useExpose } from '../shared/useExpose/index.js'; import 'lodash/isFunction'; import 'lodash/kebabCase'; import '../hooks/render-tnode.js'; import 'lodash/isEmpty'; import 'lodash/isString'; import 'lodash/isObject'; import '../config-provider/useConfig.js'; import 'lodash/cloneDeep'; import '../config-provider/context.js'; import 'lodash/mergeWith'; import 'lodash/merge'; import 'lodash/isArray'; import '../_common/js/global-config/mobile/default-config.js'; import '../_common/js/global-config/mobile/locale/zh_CN.js'; import '../_chunks/dep-d5364bc4.js'; import '@babel/runtime/helpers/typeof'; import '../_chunks/dep-eb734424.js'; import 'dayjs'; 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 prefix = config.prefix; var _DropdownMenu = defineComponent({ name: "".concat(prefix, "-dropdown-menu"), components: { CaretDownSmallIcon: CaretDownSmallIcon, CaretUpSmallIcon: CaretUpSmallIcon }, props: _objectSpread({ onMenuOpened: Function, onMenuClosed: Function }, DropdownMenuProps), emits: ["menuOpened", "menuClosed"], setup: function setup(props, _ref) { var slots = _ref.slots; var dropdownMenuClass = usePrefixClass("dropdown-menu"); var renderContent = useContent(); var state = reactive({ activeId: null, barRect: {}, childCount: 0, itemsLabel: [] }); var menuItems = ref([]); var updateItems = function updateItems() { if (slots.default) { var itemName = "".concat(prefix, "-dropdown-item"); var children = slots.default(); menuItems.value = children.filter(function (child) { var _child$type; var childTypeName = child === null || child === void 0 || (_child$type = child.type) === null || _child$type === void 0 ? void 0 : _child$type.name; return (childTypeName === null || childTypeName === void 0 ? void 0 : childTypeName.includes) && childTypeName.includes(itemName); }); } }; watch(function () { var _slots$default; return slots === null || slots === void 0 || (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots); }, updateItems, { deep: true, immediate: true }); var menuTitles = computed(function () { return menuItems.value.map(function (item, index) { var _item$props = item.props, keys = _item$props.keys, label = _item$props.label, value = _item$props.value, modelValue = _item$props.modelValue, defaultValue = _item$props.defaultValue, disabled = _item$props.disabled, options = _item$props.options; var currentValue = value || modelValue || defaultValue; var target = options === null || options === void 0 ? void 0 : options.find(function (item2) { var _keys$value; return item2[(_keys$value = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _keys$value !== void 0 ? _keys$value : "value"] === currentValue; }); if (state.itemsLabel.length < index + 1) { var _keys$label; var targetLabel = target && target[(_keys$label = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _keys$label !== void 0 ? _keys$label : "label"] || ""; var computedLabel = label || targetLabel; state.itemsLabel.push(computedLabel); return { labelProps: label, label: computedLabel, disabled: disabled !== void 0 && disabled !== false }; } return { labelProps: label, label: label || target.label, disabled: disabled !== void 0 && disabled !== false }; }); }); provide("dropdownMenuProps", props); provide("dropdownMenuState", state); var classes = computed(function () { return ["".concat(dropdownMenuClass.value)]; }); var refBar = ref(); var styleBarItem = computed(function () { return function (item, idx) { return ["".concat(dropdownMenuClass.value, "__item"), _defineProperty(_defineProperty({}, "".concat(dropdownMenuClass.value, "__item--disabled"), item.disabled), "".concat(dropdownMenuClass.value, "__item--active"), idx === state.activeId)]; }; }); var styleIcon = computed(function () { return function (item, idx) { return ["".concat(dropdownMenuClass.value, "__icon"), _defineProperty({}, "".concat(dropdownMenuClass.value, "__icon--active"), idx === state.activeId)]; }; }); var expandMenu = function expandMenu(item, idx) { var _props$onMenuOpened; var disabled = item.disabled; if (disabled) return; if (state.activeId === idx) { var _props$onMenuClosed; collapseMenu(); (_props$onMenuClosed = props.onMenuClosed) === null || _props$onMenuClosed === void 0 || _props$onMenuClosed.call(props, { trigger: "menu" }); return; } (_props$onMenuOpened = props.onMenuOpened) === null || _props$onMenuOpened === void 0 || _props$onMenuOpened.call(props, "menuOpened"); state.activeId = idx; state.itemsLabel[idx] = item.label; var bar = refBar.value; var barRect = findRelativeRect(bar); state.barRect = barRect; var container = findRelativeContainer(bar) || document.body; context.recordMenuExpanded(container, control, DropdownMenuExpandState.expanded); }; var collapseMenu = function collapseMenu() { menuTitles.value.forEach(function (item, index) { item.label = state.itemsLabel[index]; }); state.activeId = null; var bar = refBar.value; var container = findRelativeContainer(bar) || document.body; context.recordMenuExpanded(container, control, DropdownMenuExpandState.collapsed); }; onClickOutside(refBar, function () { var _props$onMenuClosed2; collapseMenu(); (_props$onMenuClosed2 = props.onMenuClosed) === null || _props$onMenuClosed2 === void 0 || _props$onMenuClosed2.call(props, { trigger: "outside" }); }); var control = { expandMenu: expandMenu, collapseMenu: collapseMenu, emitEvents: function emitEvents(emit, trigger) { var eventHandler = props["on".concat(camelCase(emit))]; eventHandler === null || eventHandler === void 0 || eventHandler(trigger); } }; provide("dropdownMenuControl", control); useExpose({ toggle: function toggle(idx) { if (idx != null) { var item = menuTitles.value[idx]; expandMenu(item, idx); } else { collapseMenu(); } } }); useExpose({ expandMenu: expandMenu, collapseMenu: collapseMenu }); return function () { var defaultSlot = renderContent("default", "content"); var renderDownIcon = function renderDownIcon(item, idx) { if (props.direction === "down") { return createVNode(resolveComponent("caret-down-small-icon"), { "class": styleIcon.value(item, idx) }, null); } return createVNode(resolveComponent("caret-up-small-icon"), { "class": styleIcon.value(item, idx) }, null); }; return createVNode("div", { "ref": refBar, "class": classes.value }, [(menuTitles.value || []).map(function (item, idx) { return createVNode("div", { "class": styleBarItem.value(item, idx), "onClick": function onClick() { return expandMenu(item, idx); } }, [createVNode("div", { "class": "".concat(dropdownMenuClass.value, "__title") }, [item.labelProps || item.label]), renderDownIcon(item, idx)]); }), defaultSlot]); }; } }); export { _DropdownMenu as default }; //# sourceMappingURL=dropdown-menu.js.map