mention-dropdown2.mjs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { defineComponent, ref, computed, watch, openBlock, createElementBlock, normalizeClass, unref, renderSlot, createCommentVNode, withDirectives, createVNode, withCtx, Fragment, renderList, withModifiers, createElementVNode, toDisplayString, vShow, createTextVNode, nextTick } from 'vue';
  2. import { ElScrollbar } from '../../scrollbar/index.mjs';
  3. import { mentionDropdownProps, mentionDropdownEmits } from './mention-dropdown.mjs';
  4. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  5. import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
  6. import { useLocale } from '../../../hooks/use-locale/index.mjs';
  7. import { scrollIntoView } from '../../../utils/dom/scroll.mjs';
  8. const __default__ = defineComponent({
  9. name: "ElMentionDropdown"
  10. });
  11. const _sfc_main = /* @__PURE__ */ defineComponent({
  12. ...__default__,
  13. props: mentionDropdownProps,
  14. emits: mentionDropdownEmits,
  15. setup(__props, { expose, emit }) {
  16. const props = __props;
  17. const ns = useNamespace("mention");
  18. const { t } = useLocale();
  19. const hoveringIndex = ref(-1);
  20. const scrollbarRef = ref();
  21. const optionRefs = ref();
  22. const dropdownRef = ref();
  23. const optionkls = (item, index) => [
  24. ns.be("dropdown", "item"),
  25. ns.is("hovering", hoveringIndex.value === index),
  26. ns.is("disabled", item.disabled || props.disabled)
  27. ];
  28. const handleSelect = (item) => {
  29. if (item.disabled || props.disabled)
  30. return;
  31. emit("select", item);
  32. };
  33. const handleMouseEnter = (index) => {
  34. hoveringIndex.value = index;
  35. };
  36. const filteredAllDisabled = computed(() => props.disabled || props.options.every((item) => item.disabled));
  37. const hoverOption = computed(() => props.options[hoveringIndex.value]);
  38. const selectHoverOption = () => {
  39. if (!hoverOption.value)
  40. return;
  41. emit("select", hoverOption.value);
  42. };
  43. const navigateOptions = (direction) => {
  44. const { options } = props;
  45. if (options.length === 0 || filteredAllDisabled.value)
  46. return;
  47. if (direction === "next") {
  48. hoveringIndex.value++;
  49. if (hoveringIndex.value === options.length) {
  50. hoveringIndex.value = 0;
  51. }
  52. } else if (direction === "prev") {
  53. hoveringIndex.value--;
  54. if (hoveringIndex.value < 0) {
  55. hoveringIndex.value = options.length - 1;
  56. }
  57. }
  58. const option = options[hoveringIndex.value];
  59. if (option.disabled) {
  60. navigateOptions(direction);
  61. return;
  62. }
  63. nextTick(() => scrollToOption(option));
  64. };
  65. const scrollToOption = (option) => {
  66. var _a, _b, _c, _d;
  67. const { options } = props;
  68. const index = options.findIndex((item) => item.value === option.value);
  69. const target = (_a = optionRefs.value) == null ? void 0 : _a[index];
  70. if (target) {
  71. const menu = (_c = (_b = dropdownRef.value) == null ? void 0 : _b.querySelector) == null ? void 0 : _c.call(_b, `.${ns.be("dropdown", "wrap")}`);
  72. if (menu) {
  73. scrollIntoView(menu, target);
  74. }
  75. }
  76. (_d = scrollbarRef.value) == null ? void 0 : _d.handleScroll();
  77. };
  78. const resetHoveringIndex = () => {
  79. if (filteredAllDisabled.value || props.options.length === 0) {
  80. hoveringIndex.value = -1;
  81. } else {
  82. hoveringIndex.value = 0;
  83. }
  84. };
  85. watch(() => props.options, resetHoveringIndex, {
  86. immediate: true
  87. });
  88. expose({
  89. hoveringIndex,
  90. navigateOptions,
  91. selectHoverOption,
  92. hoverOption
  93. });
  94. return (_ctx, _cache) => {
  95. return openBlock(), createElementBlock("div", {
  96. ref_key: "dropdownRef",
  97. ref: dropdownRef,
  98. class: normalizeClass(unref(ns).b("dropdown"))
  99. }, [
  100. _ctx.$slots.header ? (openBlock(), createElementBlock("div", {
  101. key: 0,
  102. class: normalizeClass(unref(ns).be("dropdown", "header"))
  103. }, [
  104. renderSlot(_ctx.$slots, "header")
  105. ], 2)) : createCommentVNode("v-if", true),
  106. withDirectives(createVNode(unref(ElScrollbar), {
  107. id: _ctx.contentId,
  108. ref_key: "scrollbarRef",
  109. ref: scrollbarRef,
  110. tag: "ul",
  111. "wrap-class": unref(ns).be("dropdown", "wrap"),
  112. "view-class": unref(ns).be("dropdown", "list"),
  113. role: "listbox",
  114. "aria-label": _ctx.ariaLabel,
  115. "aria-orientation": "vertical"
  116. }, {
  117. default: withCtx(() => [
  118. (openBlock(true), createElementBlock(Fragment, null, renderList(_ctx.options, (item, index) => {
  119. return openBlock(), createElementBlock("li", {
  120. id: `${_ctx.contentId}-${index}`,
  121. ref_for: true,
  122. ref_key: "optionRefs",
  123. ref: optionRefs,
  124. key: index,
  125. class: normalizeClass(optionkls(item, index)),
  126. role: "option",
  127. "aria-disabled": item.disabled || _ctx.disabled || void 0,
  128. "aria-selected": hoveringIndex.value === index,
  129. onMousemove: ($event) => handleMouseEnter(index),
  130. onClick: withModifiers(($event) => handleSelect(item), ["stop"])
  131. }, [
  132. renderSlot(_ctx.$slots, "label", {
  133. item,
  134. index
  135. }, () => {
  136. var _a;
  137. return [
  138. createElementVNode("span", null, toDisplayString((_a = item.label) != null ? _a : item.value), 1)
  139. ];
  140. })
  141. ], 42, ["id", "aria-disabled", "aria-selected", "onMousemove", "onClick"]);
  142. }), 128))
  143. ]),
  144. _: 3
  145. }, 8, ["id", "wrap-class", "view-class", "aria-label"]), [
  146. [vShow, _ctx.options.length > 0 && !_ctx.loading]
  147. ]),
  148. _ctx.loading ? (openBlock(), createElementBlock("div", {
  149. key: 1,
  150. class: normalizeClass(unref(ns).be("dropdown", "loading"))
  151. }, [
  152. renderSlot(_ctx.$slots, "loading", {}, () => [
  153. createTextVNode(toDisplayString(unref(t)("el.mention.loading")), 1)
  154. ])
  155. ], 2)) : createCommentVNode("v-if", true),
  156. _ctx.$slots.footer ? (openBlock(), createElementBlock("div", {
  157. key: 2,
  158. class: normalizeClass(unref(ns).be("dropdown", "footer"))
  159. }, [
  160. renderSlot(_ctx.$slots, "footer")
  161. ], 2)) : createCommentVNode("v-if", true)
  162. ], 2);
  163. };
  164. }
  165. });
  166. var ElMentionDropdown = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "mention-dropdown.vue"]]);
  167. export { ElMentionDropdown as default };
  168. //# sourceMappingURL=mention-dropdown2.mjs.map