hongluan-ui
Version:
Hongluan Component Library for Vue 3
160 lines (157 loc) • 5.89 kB
JavaScript
import { defineComponent, ref, computed, nextTick, watch, openBlock, createElementBlock, renderSlot, createCommentVNode, withDirectives, createVNode, unref, withCtx, Fragment, renderList, normalizeClass, withModifiers, createElementVNode, toDisplayString, vShow, createTextVNode } from 'vue';
import '../../../hooks/index.mjs';
import '../../../utils/index.mjs';
import { HlScrollbar } from '../../scrollbar/index.mjs';
import { mentionDropdownProps, mentionDropdownEmits } from './mention-dropdown2.mjs';
import { useLocale } from '../../../hooks/use-locale/index.mjs';
import { scrollIntoView } from '../../../utils/dom/scroll.mjs';
const __default__ = defineComponent({
name: "MentionDropdown"
});
const _sfc_main = /* @__PURE__ */ defineComponent({
...__default__,
props: mentionDropdownProps,
emits: mentionDropdownEmits,
setup(__props, { expose, emit }) {
const props = __props;
const { t } = useLocale();
const hoveringIndex = ref(-1);
const scrollbarRef = ref();
const optionRefs = ref();
const dropdownRef = 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 = computed(() => props.disabled || props.options.every((item) => item.disabled));
const hoverOption = 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;
}
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) {
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;
}
};
watch(() => props.options, resetHoveringIndex, {
immediate: true
});
expose({
navigateOptions,
selectHoverOption,
hoverOption
});
return (_ctx, _cache) => {
return openBlock(), createElementBlock("div", {
ref_key: "dropdownRef",
ref: dropdownRef,
class: "mention-dropdown"
}, [
_ctx.$slots.header ? (openBlock(), createElementBlock("div", {
key: 0,
class: "mention-dropdown-header"
}, [
renderSlot(_ctx.$slots, "header")
])) : createCommentVNode("v-if", true),
withDirectives(createVNode(unref(HlScrollbar), {
ref_key: "scrollbarRef",
ref: scrollbarRef,
tag: "ul",
"wrap-class": "mention-dropdown-wrap",
"view-class": "mention-dropdown-list"
}, {
default: withCtx(() => [
(openBlock(true), createElementBlock(Fragment, null, renderList(_ctx.options, (item, index) => {
return openBlock(), createElementBlock("li", {
ref_for: true,
ref_key: "optionRefs",
ref: optionRefs,
key: item.value,
class: normalizeClass(optionkls(item, index)),
onMouseenter: ($event) => handleMouseEnter(index),
onClick: withModifiers(($event) => handleSelect(item), ["stop"])
}, [
renderSlot(_ctx.$slots, "label", {
item,
index
}, () => {
var _a;
return [
createElementVNode("span", null, toDisplayString((_a = item.label) != null ? _a : item.value), 1)
];
})
], 42, ["onMouseenter", "onClick"]);
}), 128))
]),
_: 3
}, 512), [
[vShow, _ctx.options.length > 0 && !_ctx.loading]
]),
_ctx.loading ? (openBlock(), createElementBlock("div", {
key: 1,
class: "mention-dropdown-loading"
}, [
renderSlot(_ctx.$slots, "loading", {}, () => [
createTextVNode(toDisplayString(unref(t)("hl.mention.loading")), 1)
])
])) : createCommentVNode("v-if", true),
_ctx.$slots.footer ? (openBlock(), createElementBlock("div", {
key: 2,
class: "mention-dropdown-footer"
}, [
renderSlot(_ctx.$slots, "footer")
])) : createCommentVNode("v-if", true)
], 512);
};
}
});
export { _sfc_main as default };
//# sourceMappingURL=mention-dropdown.mjs.map