OptionList.js 12 KB

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