mention-dropdown.mjs 7.4 KB

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