OptionList.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  2. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  3. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  4. var _excluded = ["disabled", "title", "children", "style", "class", "className"];
  5. import { resolveDirective as _resolveDirective, Fragment as _Fragment, createVNode as _createVNode } from "vue";
  6. import TransBtn from './TransBtn';
  7. import KeyCode from '../_util/KeyCode';
  8. import classNames from '../_util/classNames';
  9. import pickAttrs from '../_util/pickAttrs';
  10. import { isValidElement } from '../_util/props-util';
  11. import createRef from '../_util/createRef';
  12. import { computed, defineComponent, nextTick, reactive, toRaw, watch } from 'vue';
  13. import List from '../vc-virtual-list';
  14. import useMemo from '../_util/hooks/useMemo';
  15. import { isPlatformMac } from './utils/platformUtil';
  16. import omit from '../_util/omit';
  17. import useBaseProps from './hooks/useBaseProps';
  18. import useSelectProps from './SelectContext';
  19. function isTitleType(content) {
  20. return typeof content === 'string' || typeof content === 'number';
  21. }
  22. /**
  23. * Using virtual list of option display.
  24. * Will fallback to dom if use customize render.
  25. */
  26. var OptionList = defineComponent({
  27. compatConfig: {
  28. MODE: 3
  29. },
  30. name: 'OptionList',
  31. inheritAttrs: false,
  32. slots: ['option'],
  33. setup: function setup(_, _ref) {
  34. var expose = _ref.expose,
  35. slots = _ref.slots;
  36. var baseProps = useBaseProps();
  37. var props = useSelectProps();
  38. var itemPrefixCls = computed(function () {
  39. return "".concat(baseProps.prefixCls, "-item");
  40. });
  41. var memoFlattenOptions = useMemo(function () {
  42. return props.flattenOptions;
  43. }, [function () {
  44. return baseProps.open;
  45. }, function () {
  46. return props.flattenOptions;
  47. }], function (next) {
  48. return next[0];
  49. });
  50. // =========================== List ===========================
  51. var listRef = createRef();
  52. var onListMouseDown = function onListMouseDown(event) {
  53. event.preventDefault();
  54. };
  55. var scrollIntoView = function scrollIntoView(args) {
  56. if (listRef.current) {
  57. listRef.current.scrollTo(typeof args === 'number' ? {
  58. index: args
  59. } : args);
  60. }
  61. };
  62. // ========================== Active ==========================
  63. var getEnabledActiveIndex = function getEnabledActiveIndex(index) {
  64. var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  65. var len = memoFlattenOptions.value.length;
  66. for (var i = 0; i < len; i += 1) {
  67. var current = (index + i * offset + len) % len;
  68. var _memoFlattenOptions$v = memoFlattenOptions.value[current],
  69. group = _memoFlattenOptions$v.group,
  70. data = _memoFlattenOptions$v.data;
  71. if (!group && !data.disabled) {
  72. return current;
  73. }
  74. }
  75. return -1;
  76. };
  77. var state = reactive({
  78. activeIndex: getEnabledActiveIndex(0)
  79. });
  80. var setActive = function setActive(index) {
  81. var fromKeyboard = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  82. state.activeIndex = index;
  83. var info = {
  84. source: fromKeyboard ? 'keyboard' : 'mouse'
  85. };
  86. // Trigger active event
  87. var flattenItem = memoFlattenOptions.value[index];
  88. if (!flattenItem) {
  89. props.onActiveValue(null, -1, info);
  90. return;
  91. }
  92. props.onActiveValue(flattenItem.value, index, info);
  93. };
  94. // Auto active first item when list length or searchValue changed
  95. watch([function () {
  96. return memoFlattenOptions.value.length;
  97. }, function () {
  98. return baseProps.searchValue;
  99. }], function () {
  100. setActive(props.defaultActiveFirstOption !== false ? getEnabledActiveIndex(0) : -1);
  101. }, {
  102. immediate: true
  103. });
  104. // https://github.com/ant-design/ant-design/issues/34975
  105. var isSelected = function isSelected(value) {
  106. return props.rawValues.has(value) && baseProps.mode !== 'combobox';
  107. };
  108. // Auto scroll to item position in single mode
  109. watch([function () {
  110. return baseProps.open;
  111. }, function () {
  112. return baseProps.searchValue;
  113. }], function () {
  114. if (!baseProps.multiple && baseProps.open && props.rawValues.size === 1) {
  115. var value = Array.from(props.rawValues)[0];
  116. var index = toRaw(memoFlattenOptions.value).findIndex(function (_ref2) {
  117. var data = _ref2.data;
  118. return data[props.fieldNames.value] === value;
  119. });
  120. if (index !== -1) {
  121. setActive(index);
  122. nextTick(function () {
  123. scrollIntoView(index);
  124. });
  125. }
  126. }
  127. // Force trigger scrollbar visible when open
  128. if (baseProps.open) {
  129. nextTick(function () {
  130. var _listRef$current;
  131. (_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.scrollTo(undefined);
  132. });
  133. }
  134. }, {
  135. immediate: true,
  136. flush: 'post'
  137. });
  138. // ========================== Values ==========================
  139. var onSelectValue = function onSelectValue(value) {
  140. if (value !== undefined) {
  141. props.onSelect(value, {
  142. selected: !props.rawValues.has(value)
  143. });
  144. }
  145. // Single mode should always close by select
  146. if (!baseProps.multiple) {
  147. baseProps.toggleOpen(false);
  148. }
  149. };
  150. var getLabel = function getLabel(item) {
  151. return typeof item.label === 'function' ? item.label() : item.label;
  152. };
  153. function renderItem(index) {
  154. var item = memoFlattenOptions.value[index];
  155. if (!item) return null;
  156. var itemData = item.data || {};
  157. var value = itemData.value;
  158. var group = item.group;
  159. var attrs = pickAttrs(itemData, true);
  160. var mergedLabel = getLabel(item);
  161. return item ? _createVNode("div", _objectSpread(_objectSpread({
  162. "aria-label": typeof mergedLabel === 'string' && !group ? mergedLabel : null
  163. }, attrs), {}, {
  164. "key": index,
  165. "role": group ? 'presentation' : 'option',
  166. "id": "".concat(baseProps.id, "_list_").concat(index),
  167. "aria-selected": isSelected(value)
  168. }), [value]) : null;
  169. }
  170. var onKeydown = function onKeydown(event) {
  171. var which = event.which,
  172. ctrlKey = event.ctrlKey;
  173. switch (which) {
  174. // >>> Arrow keys & ctrl + n/p on Mac
  175. case KeyCode.N:
  176. case KeyCode.P:
  177. case KeyCode.UP:
  178. case KeyCode.DOWN:
  179. {
  180. var offset = 0;
  181. if (which === KeyCode.UP) {
  182. offset = -1;
  183. } else if (which === KeyCode.DOWN) {
  184. offset = 1;
  185. } else if (isPlatformMac() && ctrlKey) {
  186. if (which === KeyCode.N) {
  187. offset = 1;
  188. } else if (which === KeyCode.P) {
  189. offset = -1;
  190. }
  191. }
  192. if (offset !== 0) {
  193. var nextActiveIndex = getEnabledActiveIndex(state.activeIndex + offset, offset);
  194. scrollIntoView(nextActiveIndex);
  195. setActive(nextActiveIndex, true);
  196. }
  197. break;
  198. }
  199. // >>> Select
  200. case KeyCode.ENTER:
  201. {
  202. // value
  203. var item = memoFlattenOptions.value[state.activeIndex];
  204. if (item && !item.data.disabled) {
  205. onSelectValue(item.value);
  206. } else {
  207. onSelectValue(undefined);
  208. }
  209. if (baseProps.open) {
  210. event.preventDefault();
  211. }
  212. break;
  213. }
  214. // >>> Close
  215. case KeyCode.ESC:
  216. {
  217. baseProps.toggleOpen(false);
  218. if (baseProps.open) {
  219. event.stopPropagation();
  220. }
  221. }
  222. }
  223. };
  224. var onKeyup = function onKeyup() {};
  225. var scrollTo = function scrollTo(index) {
  226. scrollIntoView(index);
  227. };
  228. expose({
  229. onKeydown: onKeydown,
  230. onKeyup: onKeyup,
  231. scrollTo: scrollTo
  232. });
  233. return function () {
  234. // const {
  235. // renderItem,
  236. // listRef,
  237. // onListMouseDown,
  238. // itemPrefixCls,
  239. // setActive,
  240. // onSelectValue,
  241. // memoFlattenOptions,
  242. // $slots,
  243. // } = this as any;
  244. var id = baseProps.id,
  245. notFoundContent = baseProps.notFoundContent,
  246. onPopupScroll = baseProps.onPopupScroll;
  247. var menuItemSelectedIcon = props.menuItemSelectedIcon,
  248. fieldNames = props.fieldNames,
  249. virtual = props.virtual,
  250. listHeight = props.listHeight,
  251. listItemHeight = props.listItemHeight;
  252. var renderOption = slots.option;
  253. var activeIndex = state.activeIndex;
  254. var omitFieldNameList = Object.keys(fieldNames).map(function (key) {
  255. return fieldNames[key];
  256. });
  257. // ========================== Render ==========================
  258. if (memoFlattenOptions.value.length === 0) {
  259. return _createVNode("div", {
  260. "role": "listbox",
  261. "id": "".concat(id, "_list"),
  262. "class": "".concat(itemPrefixCls.value, "-empty"),
  263. "onMousedown": onListMouseDown
  264. }, [notFoundContent]);
  265. }
  266. return _createVNode(_Fragment, null, [_createVNode("div", {
  267. "role": "listbox",
  268. "id": "".concat(id, "_list"),
  269. "style": {
  270. height: 0,
  271. width: 0,
  272. overflow: 'hidden'
  273. }
  274. }, [renderItem(activeIndex - 1), renderItem(activeIndex), renderItem(activeIndex + 1)]), _createVNode(List, {
  275. "itemKey": "key",
  276. "ref": listRef,
  277. "data": memoFlattenOptions.value,
  278. "height": listHeight,
  279. "itemHeight": listItemHeight,
  280. "fullHeight": false,
  281. "onMousedown": onListMouseDown,
  282. "onScroll": onPopupScroll,
  283. "virtual": virtual
  284. }, {
  285. default: function _default(item, itemIndex) {
  286. var _classNames;
  287. var group = item.group,
  288. groupOption = item.groupOption,
  289. data = item.data,
  290. value = item.value;
  291. var key = data.key;
  292. var label = typeof item.label === 'function' ? item.label() : item.label;
  293. // Group
  294. if (group) {
  295. var _data$title;
  296. var groupTitle = (_data$title = data.title) !== null && _data$title !== void 0 ? _data$title : isTitleType(label) && label;
  297. return _createVNode("div", {
  298. "class": classNames(itemPrefixCls.value, "".concat(itemPrefixCls.value, "-group")),
  299. "title": groupTitle
  300. }, [renderOption ? renderOption(data) : label !== undefined ? label : key]);
  301. }
  302. var disabled = data.disabled,
  303. title = data.title,
  304. children = data.children,
  305. style = data.style,
  306. cls = data.class,
  307. className = data.className,
  308. otherProps = _objectWithoutProperties(data, _excluded);
  309. var passedProps = omit(otherProps, omitFieldNameList);
  310. // Option
  311. var selected = isSelected(value);
  312. var optionPrefixCls = "".concat(itemPrefixCls.value, "-option");
  313. var optionClassName = classNames(itemPrefixCls.value, optionPrefixCls, cls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(optionPrefixCls, "-grouped"), groupOption), _defineProperty(_classNames, "".concat(optionPrefixCls, "-active"), activeIndex === itemIndex && !disabled), _defineProperty(_classNames, "".concat(optionPrefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(optionPrefixCls, "-selected"), selected), _classNames));
  314. var mergedLabel = getLabel(item);
  315. var iconVisible = !menuItemSelectedIcon || typeof menuItemSelectedIcon === 'function' || selected;
  316. // https://github.com/ant-design/ant-design/issues/34145
  317. var content = typeof mergedLabel === 'number' ? mergedLabel : mergedLabel || value;
  318. // https://github.com/ant-design/ant-design/issues/26717
  319. var optionTitle = isTitleType(content) ? content.toString() : undefined;
  320. if (title !== undefined) {
  321. optionTitle = title;
  322. }
  323. return _createVNode("div", _objectSpread(_objectSpread({}, passedProps), {}, {
  324. "aria-selected": selected,
  325. "class": optionClassName,
  326. "title": optionTitle,
  327. "onMousemove": function onMousemove(e) {
  328. if (otherProps.onMousemove) {
  329. otherProps.onMousemove(e);
  330. }
  331. if (activeIndex === itemIndex || disabled) {
  332. return;
  333. }
  334. setActive(itemIndex);
  335. },
  336. "onClick": function onClick(e) {
  337. if (!disabled) {
  338. onSelectValue(value);
  339. }
  340. if (otherProps.onClick) {
  341. otherProps.onClick(e);
  342. }
  343. },
  344. "style": style
  345. }), [_createVNode("div", {
  346. "class": "".concat(optionPrefixCls, "-content")
  347. }, [renderOption ? renderOption(data) : content]), isValidElement(menuItemSelectedIcon) || selected, iconVisible && _createVNode(TransBtn, {
  348. "class": "".concat(itemPrefixCls.value, "-option-state"),
  349. "customizeIcon": menuItemSelectedIcon,
  350. "customizeIconProps": {
  351. isSelected: selected
  352. }
  353. }, {
  354. default: function _default() {
  355. return [selected ? '✓' : null];
  356. }
  357. })]);
  358. }
  359. })]);
  360. };
  361. }
  362. });
  363. export default OptionList;