hongluan-ui
Version:
Hongluan Component Library for Vue 3
164 lines (159 loc) • 5.88 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
require('../../../hooks/index.js');
require('../../../utils/index.js');
var index$1 = require('../../scrollbar/index.js');
var mentionDropdown = require('./mention-dropdown2.js');
var index = require('../../../hooks/use-locale/index.js');
var scroll = require('../../../utils/dom/scroll.js');
const __default__ = vue.defineComponent({
name: "MentionDropdown"
});
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
...__default__,
props: mentionDropdown.mentionDropdownProps,
emits: mentionDropdown.mentionDropdownEmits,
setup(__props, { expose, emit }) {
const props = __props;
const { t } = index.useLocale();
const hoveringIndex = vue.ref(-1);
const scrollbarRef = vue.ref();
const optionRefs = vue.ref();
const dropdownRef = vue.ref();
const optionkls = (item, index) => [
"mention-dropdown-item",
hoveringIndex.value === index ? "hovering" : "",
item.disabled || props.disabled ? "is-disabled" : ""
];
const handleSelect = (item) => {
if (item.disabled || props.disabled)
return;
emit("select", item);
};
const handleMouseEnter = (index) => {
hoveringIndex.value = index;
};
const filteredAllDisabled = vue.computed(() => props.disabled || props.options.every((item) => item.disabled));
const hoverOption = vue.computed(() => props.options[hoveringIndex.value]);
const selectHoverOption = () => {
if (!hoverOption.value)
return;
emit("select", hoverOption.value);
};
const navigateOptions = (direction) => {
const { options } = props;
if (options.length === 0 || filteredAllDisabled.value)
return;
if (direction === "next") {
hoveringIndex.value++;
if (hoveringIndex.value === options.length) {
hoveringIndex.value = 0;
}
} else if (direction === "prev") {
hoveringIndex.value--;
if (hoveringIndex.value < 0) {
hoveringIndex.value = options.length - 1;
}
}
const option = options[hoveringIndex.value];
if (option.disabled) {
navigateOptions(direction);
return;
}
vue.nextTick(() => scrollToOption(option));
};
const scrollToOption = (option) => {
var _a, _b, _c, _d;
const { options } = props;
const index = options.findIndex((item) => item.value === option.value);
const target = (_a = optionRefs.value) == null ? void 0 : _a[index];
if (target) {
const menu = (_c = (_b = dropdownRef.value) == null ? void 0 : _b.querySelector) == null ? void 0 : _c.call(_b, ".mention-dropdown-wrap");
if (menu) {
scroll.scrollIntoView(menu, target);
}
}
(_d = scrollbarRef.value) == null ? void 0 : _d.handleScroll();
};
const resetHoveringIndex = () => {
if (filteredAllDisabled.value || props.options.length === 0) {
hoveringIndex.value = -1;
} else {
hoveringIndex.value = 0;
}
};
vue.watch(() => props.options, resetHoveringIndex, {
immediate: true
});
expose({
navigateOptions,
selectHoverOption,
hoverOption
});
return (_ctx, _cache) => {
return vue.openBlock(), vue.createElementBlock("div", {
ref_key: "dropdownRef",
ref: dropdownRef,
class: "mention-dropdown"
}, [
_ctx.$slots.header ? (vue.openBlock(), vue.createElementBlock("div", {
key: 0,
class: "mention-dropdown-header"
}, [
vue.renderSlot(_ctx.$slots, "header")
])) : vue.createCommentVNode("v-if", true),
vue.withDirectives(vue.createVNode(vue.unref(index$1.HlScrollbar), {
ref_key: "scrollbarRef",
ref: scrollbarRef,
tag: "ul",
"wrap-class": "mention-dropdown-wrap",
"view-class": "mention-dropdown-list"
}, {
default: vue.withCtx(() => [
(vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(_ctx.options, (item, index) => {
return vue.openBlock(), vue.createElementBlock("li", {
ref_for: true,
ref_key: "optionRefs",
ref: optionRefs,
key: item.value,
class: vue.normalizeClass(optionkls(item, index)),
onMouseenter: ($event) => handleMouseEnter(index),
onClick: vue.withModifiers(($event) => handleSelect(item), ["stop"])
}, [
vue.renderSlot(_ctx.$slots, "label", {
item,
index
}, () => {
var _a;
return [
vue.createElementVNode("span", null, vue.toDisplayString((_a = item.label) != null ? _a : item.value), 1)
];
})
], 42, ["onMouseenter", "onClick"]);
}), 128))
]),
_: 3
}, 512), [
[vue.vShow, _ctx.options.length > 0 && !_ctx.loading]
]),
_ctx.loading ? (vue.openBlock(), vue.createElementBlock("div", {
key: 1,
class: "mention-dropdown-loading"
}, [
vue.renderSlot(_ctx.$slots, "loading", {}, () => [
vue.createTextVNode(vue.toDisplayString(vue.unref(t)("hl.mention.loading")), 1)
])
])) : vue.createCommentVNode("v-if", true),
_ctx.$slots.footer ? (vue.openBlock(), vue.createElementBlock("div", {
key: 2,
class: "mention-dropdown-footer"
}, [
vue.renderSlot(_ctx.$slots, "footer")
])) : vue.createCommentVNode("v-if", true)
], 512);
};
}
});
exports["default"] = _sfc_main;
//# sourceMappingURL=mention-dropdown.js.map