select.mjs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import { defineComponent, computed, reactive, toRefs, provide, resolveComponent, resolveDirective, withDirectives, openBlock, createElementBlock, normalizeClass, createVNode, withCtx, createElementVNode, withModifiers, renderSlot, createCommentVNode, Fragment, renderList, normalizeStyle, createTextVNode, toDisplayString, createBlock, withKeys, vModelText, resolveDynamicComponent, vShow, createSlots, normalizeProps, guardReactiveProps } from 'vue';
  2. import { ElTooltip } from '../../tooltip/index.mjs';
  3. import { ElTag } from '../../tag/index.mjs';
  4. import { ElIcon } from '../../icon/index.mjs';
  5. import ElSelectMenu from './select-dropdown.mjs';
  6. import useSelect from './useSelect.mjs';
  7. import { selectV2Props, selectV2Emits } from './defaults.mjs';
  8. import { selectV2InjectionKey } from './token.mjs';
  9. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  10. import ClickOutside from '../../../directives/click-outside/index.mjs';
  11. import { isArray } from '@vue/shared';
  12. import { useCalcInputWidth } from '../../../hooks/use-calc-input-width/index.mjs';
  13. import { useId } from '../../../hooks/use-id/index.mjs';
  14. import { BORDER_HORIZONTAL_WIDTH } from '../../../constants/form.mjs';
  15. const _sfc_main = defineComponent({
  16. name: "ElSelectV2",
  17. components: {
  18. ElSelectMenu,
  19. ElTag,
  20. ElTooltip,
  21. ElIcon
  22. },
  23. directives: { ClickOutside },
  24. props: selectV2Props,
  25. emits: selectV2Emits,
  26. setup(props, { emit }) {
  27. const modelValue = computed(() => {
  28. const { modelValue: rawModelValue, multiple } = props;
  29. const fallback = multiple ? [] : void 0;
  30. if (isArray(rawModelValue)) {
  31. return multiple ? rawModelValue : fallback;
  32. }
  33. return multiple ? fallback : rawModelValue;
  34. });
  35. const API = useSelect(reactive({
  36. ...toRefs(props),
  37. modelValue
  38. }), emit);
  39. const { calculatorRef, inputStyle } = useCalcInputWidth();
  40. const contentId = useId();
  41. provide(selectV2InjectionKey, {
  42. props: reactive({
  43. ...toRefs(props),
  44. height: API.popupHeight,
  45. modelValue
  46. }),
  47. expanded: API.expanded,
  48. tooltipRef: API.tooltipRef,
  49. contentId,
  50. onSelect: API.onSelect,
  51. onHover: API.onHover,
  52. onKeyboardNavigate: API.onKeyboardNavigate,
  53. onKeyboardSelect: API.onKeyboardSelect
  54. });
  55. const selectedLabel = computed(() => {
  56. if (!props.multiple) {
  57. return API.states.selectedLabel;
  58. }
  59. return API.states.cachedOptions.map((i) => API.getLabel(i));
  60. });
  61. return {
  62. ...API,
  63. modelValue,
  64. selectedLabel,
  65. calculatorRef,
  66. inputStyle,
  67. contentId,
  68. BORDER_HORIZONTAL_WIDTH
  69. };
  70. }
  71. });
  72. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  73. const _component_el_tag = resolveComponent("el-tag");
  74. const _component_el_tooltip = resolveComponent("el-tooltip");
  75. const _component_el_icon = resolveComponent("el-icon");
  76. const _component_el_select_menu = resolveComponent("el-select-menu");
  77. const _directive_click_outside = resolveDirective("click-outside");
  78. return withDirectives((openBlock(), createElementBlock("div", {
  79. ref: "selectRef",
  80. class: normalizeClass([_ctx.nsSelect.b(), _ctx.nsSelect.m(_ctx.selectSize)]),
  81. onMouseenter: ($event) => _ctx.states.inputHovering = true,
  82. onMouseleave: ($event) => _ctx.states.inputHovering = false
  83. }, [
  84. createVNode(_component_el_tooltip, {
  85. ref: "tooltipRef",
  86. visible: _ctx.dropdownMenuVisible,
  87. teleported: _ctx.teleported,
  88. "popper-class": [_ctx.nsSelect.e("popper"), _ctx.popperClass],
  89. "popper-style": _ctx.popperStyle,
  90. "gpu-acceleration": false,
  91. "stop-popper-mouse-event": false,
  92. "popper-options": _ctx.popperOptions,
  93. "fallback-placements": _ctx.fallbackPlacements,
  94. effect: _ctx.effect,
  95. placement: _ctx.placement,
  96. pure: "",
  97. transition: `${_ctx.nsSelect.namespace.value}-zoom-in-top`,
  98. trigger: "click",
  99. persistent: _ctx.persistent,
  100. "append-to": _ctx.appendTo,
  101. "show-arrow": _ctx.showArrow,
  102. offset: _ctx.offset,
  103. onBeforeShow: _ctx.handleMenuEnter,
  104. onHide: ($event) => _ctx.states.isBeforeHide = false
  105. }, {
  106. default: withCtx(() => {
  107. var _a, _b;
  108. return [
  109. createElementVNode("div", {
  110. ref: "wrapperRef",
  111. class: normalizeClass([
  112. _ctx.nsSelect.e("wrapper"),
  113. _ctx.nsSelect.is("focused", _ctx.isFocused),
  114. _ctx.nsSelect.is("hovering", _ctx.states.inputHovering),
  115. _ctx.nsSelect.is("filterable", _ctx.filterable),
  116. _ctx.nsSelect.is("disabled", _ctx.selectDisabled)
  117. ]),
  118. onClick: withModifiers(_ctx.toggleMenu, ["prevent"])
  119. }, [
  120. _ctx.$slots.prefix ? (openBlock(), createElementBlock("div", {
  121. key: 0,
  122. ref: "prefixRef",
  123. class: normalizeClass(_ctx.nsSelect.e("prefix"))
  124. }, [
  125. renderSlot(_ctx.$slots, "prefix")
  126. ], 2)) : createCommentVNode("v-if", true),
  127. createElementVNode("div", {
  128. ref: "selectionRef",
  129. class: normalizeClass([
  130. _ctx.nsSelect.e("selection"),
  131. _ctx.nsSelect.is("near", _ctx.multiple && !_ctx.$slots.prefix && !!_ctx.modelValue.length)
  132. ])
  133. }, [
  134. _ctx.multiple ? renderSlot(_ctx.$slots, "tag", {
  135. key: 0,
  136. data: _ctx.states.cachedOptions,
  137. deleteTag: _ctx.deleteTag,
  138. selectDisabled: _ctx.selectDisabled
  139. }, () => [
  140. (openBlock(true), createElementBlock(Fragment, null, renderList(_ctx.showTagList, (item) => {
  141. return openBlock(), createElementBlock("div", {
  142. key: _ctx.getValueKey(_ctx.getValue(item)),
  143. class: normalizeClass(_ctx.nsSelect.e("selected-item"))
  144. }, [
  145. createVNode(_component_el_tag, {
  146. closable: !_ctx.selectDisabled && !_ctx.getDisabled(item),
  147. size: _ctx.collapseTagSize,
  148. type: _ctx.tagType,
  149. effect: _ctx.tagEffect,
  150. "disable-transitions": "",
  151. style: normalizeStyle(_ctx.tagStyle),
  152. onClose: ($event) => _ctx.deleteTag($event, item)
  153. }, {
  154. default: withCtx(() => [
  155. createElementVNode("span", {
  156. class: normalizeClass(_ctx.nsSelect.e("tags-text"))
  157. }, [
  158. renderSlot(_ctx.$slots, "label", {
  159. index: _ctx.getIndex(item),
  160. label: _ctx.getLabel(item),
  161. value: _ctx.getValue(item)
  162. }, () => [
  163. createTextVNode(toDisplayString(_ctx.getLabel(item)), 1)
  164. ])
  165. ], 2)
  166. ]),
  167. _: 2
  168. }, 1032, ["closable", "size", "type", "effect", "style", "onClose"])
  169. ], 2);
  170. }), 128)),
  171. _ctx.collapseTags && _ctx.modelValue.length > _ctx.maxCollapseTags ? (openBlock(), createBlock(_component_el_tooltip, {
  172. key: 0,
  173. ref: "tagTooltipRef",
  174. disabled: _ctx.dropdownMenuVisible || !_ctx.collapseTagsTooltip,
  175. "fallback-placements": ["bottom", "top", "right", "left"],
  176. effect: _ctx.effect,
  177. placement: "bottom",
  178. "popper-class": _ctx.popperClass,
  179. "popper-style": _ctx.popperStyle,
  180. teleported: _ctx.teleported
  181. }, {
  182. default: withCtx(() => [
  183. createElementVNode("div", {
  184. ref: "collapseItemRef",
  185. class: normalizeClass(_ctx.nsSelect.e("selected-item"))
  186. }, [
  187. createVNode(_component_el_tag, {
  188. closable: false,
  189. size: _ctx.collapseTagSize,
  190. type: _ctx.tagType,
  191. effect: _ctx.tagEffect,
  192. style: normalizeStyle(_ctx.collapseTagStyle),
  193. "disable-transitions": ""
  194. }, {
  195. default: withCtx(() => [
  196. createElementVNode("span", {
  197. class: normalizeClass(_ctx.nsSelect.e("tags-text"))
  198. }, " + " + toDisplayString(_ctx.modelValue.length - _ctx.maxCollapseTags), 3)
  199. ]),
  200. _: 1
  201. }, 8, ["size", "type", "effect", "style"])
  202. ], 2)
  203. ]),
  204. content: withCtx(() => [
  205. createElementVNode("div", {
  206. ref: "tagMenuRef",
  207. class: normalizeClass(_ctx.nsSelect.e("selection"))
  208. }, [
  209. (openBlock(true), createElementBlock(Fragment, null, renderList(_ctx.collapseTagList, (selected) => {
  210. return openBlock(), createElementBlock("div", {
  211. key: _ctx.getValueKey(_ctx.getValue(selected)),
  212. class: normalizeClass(_ctx.nsSelect.e("selected-item"))
  213. }, [
  214. createVNode(_component_el_tag, {
  215. class: "in-tooltip",
  216. closable: !_ctx.selectDisabled && !_ctx.getDisabled(selected),
  217. size: _ctx.collapseTagSize,
  218. type: _ctx.tagType,
  219. effect: _ctx.tagEffect,
  220. "disable-transitions": "",
  221. onClose: ($event) => _ctx.deleteTag($event, selected)
  222. }, {
  223. default: withCtx(() => [
  224. createElementVNode("span", {
  225. class: normalizeClass(_ctx.nsSelect.e("tags-text"))
  226. }, [
  227. renderSlot(_ctx.$slots, "label", {
  228. index: _ctx.getIndex(selected),
  229. label: _ctx.getLabel(selected),
  230. value: _ctx.getValue(selected)
  231. }, () => [
  232. createTextVNode(toDisplayString(_ctx.getLabel(selected)), 1)
  233. ])
  234. ], 2)
  235. ]),
  236. _: 2
  237. }, 1032, ["closable", "size", "type", "effect", "onClose"])
  238. ], 2);
  239. }), 128))
  240. ], 2)
  241. ]),
  242. _: 3
  243. }, 8, ["disabled", "effect", "popper-class", "popper-style", "teleported"])) : createCommentVNode("v-if", true)
  244. ]) : createCommentVNode("v-if", true),
  245. createElementVNode("div", {
  246. class: normalizeClass([
  247. _ctx.nsSelect.e("selected-item"),
  248. _ctx.nsSelect.e("input-wrapper"),
  249. _ctx.nsSelect.is("hidden", !_ctx.filterable)
  250. ])
  251. }, [
  252. withDirectives(createElementVNode("input", {
  253. id: _ctx.inputId,
  254. ref: "inputRef",
  255. "onUpdate:modelValue": ($event) => _ctx.states.inputValue = $event,
  256. style: normalizeStyle(_ctx.inputStyle),
  257. autocomplete: _ctx.autocomplete,
  258. tabindex: _ctx.tabindex,
  259. "aria-autocomplete": "none",
  260. "aria-haspopup": "listbox",
  261. autocapitalize: "off",
  262. "aria-expanded": _ctx.expanded,
  263. "aria-label": _ctx.ariaLabel,
  264. class: normalizeClass([_ctx.nsSelect.e("input"), _ctx.nsSelect.is(_ctx.selectSize)]),
  265. disabled: _ctx.selectDisabled,
  266. role: "combobox",
  267. "aria-controls": _ctx.contentId,
  268. "aria-activedescendant": _ctx.states.hoveringIndex >= 0 ? `${_ctx.contentId}-${_ctx.states.hoveringIndex}` : "",
  269. readonly: !_ctx.filterable,
  270. spellcheck: "false",
  271. type: "text",
  272. name: _ctx.name,
  273. onInput: _ctx.onInput,
  274. onCompositionstart: _ctx.handleCompositionStart,
  275. onCompositionupdate: _ctx.handleCompositionUpdate,
  276. onCompositionend: _ctx.handleCompositionEnd,
  277. onKeydown: [
  278. withKeys(withModifiers(($event) => _ctx.onKeyboardNavigate("backward"), ["stop", "prevent"]), ["up"]),
  279. withKeys(withModifiers(($event) => _ctx.onKeyboardNavigate("forward"), ["stop", "prevent"]), ["down"]),
  280. withKeys(withModifiers(_ctx.onKeyboardSelect, ["stop", "prevent"]), ["enter"]),
  281. withKeys(withModifiers(_ctx.handleEsc, ["stop", "prevent"]), ["esc"]),
  282. withKeys(withModifiers(_ctx.handleDel, ["stop"]), ["delete"])
  283. ],
  284. onClick: withModifiers(_ctx.toggleMenu, ["stop"])
  285. }, null, 46, ["id", "onUpdate:modelValue", "autocomplete", "tabindex", "aria-expanded", "aria-label", "disabled", "aria-controls", "aria-activedescendant", "readonly", "name", "onInput", "onCompositionstart", "onCompositionupdate", "onCompositionend", "onKeydown", "onClick"]), [
  286. [vModelText, _ctx.states.inputValue]
  287. ]),
  288. _ctx.filterable ? (openBlock(), createElementBlock("span", {
  289. key: 0,
  290. ref: "calculatorRef",
  291. "aria-hidden": "true",
  292. class: normalizeClass(_ctx.nsSelect.e("input-calculator")),
  293. textContent: toDisplayString(_ctx.states.inputValue)
  294. }, null, 10, ["textContent"])) : createCommentVNode("v-if", true)
  295. ], 2),
  296. _ctx.shouldShowPlaceholder ? (openBlock(), createElementBlock("div", {
  297. key: 1,
  298. class: normalizeClass([
  299. _ctx.nsSelect.e("selected-item"),
  300. _ctx.nsSelect.e("placeholder"),
  301. _ctx.nsSelect.is("transparent", !_ctx.hasModelValue || _ctx.expanded && !_ctx.states.inputValue)
  302. ])
  303. }, [
  304. _ctx.hasModelValue ? renderSlot(_ctx.$slots, "label", {
  305. key: 0,
  306. index: (_b = (_a = _ctx.allOptionsValueMap.get(_ctx.modelValue)) == null ? void 0 : _a.index) != null ? _b : -1,
  307. label: _ctx.currentPlaceholder,
  308. value: _ctx.modelValue
  309. }, () => [
  310. createElementVNode("span", null, toDisplayString(_ctx.currentPlaceholder), 1)
  311. ]) : (openBlock(), createElementBlock("span", { key: 1 }, toDisplayString(_ctx.currentPlaceholder), 1))
  312. ], 2)) : createCommentVNode("v-if", true)
  313. ], 2),
  314. createElementVNode("div", {
  315. ref: "suffixRef",
  316. class: normalizeClass(_ctx.nsSelect.e("suffix"))
  317. }, [
  318. _ctx.iconComponent ? withDirectives((openBlock(), createBlock(_component_el_icon, {
  319. key: 0,
  320. class: normalizeClass([_ctx.nsSelect.e("caret"), _ctx.nsInput.e("icon"), _ctx.iconReverse])
  321. }, {
  322. default: withCtx(() => [
  323. (openBlock(), createBlock(resolveDynamicComponent(_ctx.iconComponent)))
  324. ]),
  325. _: 1
  326. }, 8, ["class"])), [
  327. [vShow, !_ctx.showClearBtn]
  328. ]) : createCommentVNode("v-if", true),
  329. _ctx.showClearBtn && _ctx.clearIcon ? (openBlock(), createBlock(_component_el_icon, {
  330. key: 1,
  331. class: normalizeClass([
  332. _ctx.nsSelect.e("caret"),
  333. _ctx.nsInput.e("icon"),
  334. _ctx.nsSelect.e("clear")
  335. ]),
  336. onClick: withModifiers(_ctx.handleClear, ["prevent", "stop"])
  337. }, {
  338. default: withCtx(() => [
  339. (openBlock(), createBlock(resolveDynamicComponent(_ctx.clearIcon)))
  340. ]),
  341. _: 1
  342. }, 8, ["class", "onClick"])) : createCommentVNode("v-if", true),
  343. _ctx.validateState && _ctx.validateIcon && _ctx.needStatusIcon ? (openBlock(), createBlock(_component_el_icon, {
  344. key: 2,
  345. class: normalizeClass([
  346. _ctx.nsInput.e("icon"),
  347. _ctx.nsInput.e("validateIcon"),
  348. _ctx.nsInput.is("loading", _ctx.validateState === "validating")
  349. ])
  350. }, {
  351. default: withCtx(() => [
  352. (openBlock(), createBlock(resolveDynamicComponent(_ctx.validateIcon)))
  353. ]),
  354. _: 1
  355. }, 8, ["class"])) : createCommentVNode("v-if", true)
  356. ], 2)
  357. ], 10, ["onClick"])
  358. ];
  359. }),
  360. content: withCtx(() => [
  361. createVNode(_component_el_select_menu, {
  362. id: _ctx.contentId,
  363. ref: "menuRef",
  364. data: _ctx.filteredOptions,
  365. width: _ctx.popperSize - _ctx.BORDER_HORIZONTAL_WIDTH,
  366. "hovering-index": _ctx.states.hoveringIndex,
  367. "scrollbar-always-on": _ctx.scrollbarAlwaysOn,
  368. "aria-label": _ctx.ariaLabel
  369. }, createSlots({
  370. default: withCtx((scope) => [
  371. renderSlot(_ctx.$slots, "default", normalizeProps(guardReactiveProps(scope)))
  372. ]),
  373. _: 2
  374. }, [
  375. _ctx.$slots.header ? {
  376. name: "header",
  377. fn: withCtx(() => [
  378. createElementVNode("div", {
  379. class: normalizeClass(_ctx.nsSelect.be("dropdown", "header")),
  380. onClick: withModifiers(() => {
  381. }, ["stop"])
  382. }, [
  383. renderSlot(_ctx.$slots, "header")
  384. ], 10, ["onClick"])
  385. ])
  386. } : void 0,
  387. _ctx.$slots.loading && _ctx.loading ? {
  388. name: "loading",
  389. fn: withCtx(() => [
  390. createElementVNode("div", {
  391. class: normalizeClass(_ctx.nsSelect.be("dropdown", "loading"))
  392. }, [
  393. renderSlot(_ctx.$slots, "loading")
  394. ], 2)
  395. ])
  396. } : _ctx.loading || _ctx.filteredOptions.length === 0 ? {
  397. name: "empty",
  398. fn: withCtx(() => [
  399. createElementVNode("div", {
  400. class: normalizeClass(_ctx.nsSelect.be("dropdown", "empty"))
  401. }, [
  402. renderSlot(_ctx.$slots, "empty", {}, () => [
  403. createElementVNode("span", null, toDisplayString(_ctx.emptyText), 1)
  404. ])
  405. ], 2)
  406. ])
  407. } : void 0,
  408. _ctx.$slots.footer ? {
  409. name: "footer",
  410. fn: withCtx(() => [
  411. createElementVNode("div", {
  412. class: normalizeClass(_ctx.nsSelect.be("dropdown", "footer")),
  413. onClick: withModifiers(() => {
  414. }, ["stop"])
  415. }, [
  416. renderSlot(_ctx.$slots, "footer")
  417. ], 10, ["onClick"])
  418. ])
  419. } : void 0
  420. ]), 1032, ["id", "data", "width", "hovering-index", "scrollbar-always-on", "aria-label"])
  421. ]),
  422. _: 3
  423. }, 8, ["visible", "teleported", "popper-class", "popper-style", "popper-options", "fallback-placements", "effect", "placement", "transition", "persistent", "append-to", "show-arrow", "offset", "onBeforeShow", "onHide"])
  424. ], 42, ["onMouseenter", "onMouseleave"])), [
  425. [_directive_click_outside, _ctx.handleClickOutside, _ctx.popperRef]
  426. ]);
  427. }
  428. var Select = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "select.vue"]]);
  429. export { Select as default };
  430. //# sourceMappingURL=select.mjs.map