index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  4. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  5. import { createVNode as _createVNode } from "vue";
  6. import { isLeaf, toPathKey, toPathKeys, toPathValueStr, scrollIntoParentView } from '../utils/commonUtil';
  7. import useActive from './useActive';
  8. import useKeyboard from './useKeyboard';
  9. import { toPathOptions } from '../utils/treeUtil';
  10. import { computed, defineComponent, onMounted, ref, shallowRef, watch, watchEffect } from 'vue';
  11. import { useBaseProps } from '../../vc-select';
  12. import { useInjectCascader } from '../context';
  13. import Column, { FIX_LABEL } from './Column';
  14. export default defineComponent({
  15. compatConfig: {
  16. MODE: 3
  17. },
  18. name: 'OptionList',
  19. inheritAttrs: false,
  20. setup: function setup(_props, context) {
  21. var attrs = context.attrs,
  22. slots = context.slots;
  23. var baseProps = useBaseProps();
  24. var containerRef = ref();
  25. var rtl = computed(function () {
  26. return baseProps.direction === 'rtl';
  27. });
  28. var _useInjectCascader = useInjectCascader(),
  29. options = _useInjectCascader.options,
  30. values = _useInjectCascader.values,
  31. halfValues = _useInjectCascader.halfValues,
  32. fieldNames = _useInjectCascader.fieldNames,
  33. changeOnSelect = _useInjectCascader.changeOnSelect,
  34. onSelect = _useInjectCascader.onSelect,
  35. searchOptions = _useInjectCascader.searchOptions,
  36. dropdownPrefixCls = _useInjectCascader.dropdownPrefixCls,
  37. loadData = _useInjectCascader.loadData,
  38. expandTrigger = _useInjectCascader.expandTrigger,
  39. customSlots = _useInjectCascader.customSlots;
  40. var mergedPrefixCls = computed(function () {
  41. return dropdownPrefixCls.value || baseProps.prefixCls;
  42. });
  43. // ========================= loadData =========================
  44. var loadingKeys = shallowRef([]);
  45. var internalLoadData = function internalLoadData(valueCells) {
  46. // Do not load when search
  47. if (!loadData.value || baseProps.searchValue) {
  48. return;
  49. }
  50. var optionList = toPathOptions(valueCells, options.value, fieldNames.value);
  51. var rawOptions = optionList.map(function (_ref) {
  52. var option = _ref.option;
  53. return option;
  54. });
  55. var lastOption = rawOptions[rawOptions.length - 1];
  56. if (lastOption && !isLeaf(lastOption, fieldNames.value)) {
  57. var pathKey = toPathKey(valueCells);
  58. loadingKeys.value = [].concat(_toConsumableArray(loadingKeys.value), [pathKey]);
  59. loadData.value(rawOptions);
  60. }
  61. };
  62. watchEffect(function () {
  63. if (loadingKeys.value.length) {
  64. loadingKeys.value.forEach(function (loadingKey) {
  65. var valueStrCells = toPathValueStr(loadingKey);
  66. var optionList = toPathOptions(valueStrCells, options.value, fieldNames.value, true).map(function (_ref2) {
  67. var option = _ref2.option;
  68. return option;
  69. });
  70. var lastOption = optionList[optionList.length - 1];
  71. if (!lastOption || lastOption[fieldNames.value.children] || isLeaf(lastOption, fieldNames.value)) {
  72. loadingKeys.value = loadingKeys.value.filter(function (key) {
  73. return key !== loadingKey;
  74. });
  75. }
  76. });
  77. }
  78. });
  79. // ========================== Values ==========================
  80. var checkedSet = computed(function () {
  81. return new Set(toPathKeys(values.value));
  82. });
  83. var halfCheckedSet = computed(function () {
  84. return new Set(toPathKeys(halfValues.value));
  85. });
  86. // ====================== Accessibility =======================
  87. var _useActive = useActive(),
  88. _useActive2 = _slicedToArray(_useActive, 2),
  89. activeValueCells = _useActive2[0],
  90. setActiveValueCells = _useActive2[1];
  91. // =========================== Path ===========================
  92. var onPathOpen = function onPathOpen(nextValueCells) {
  93. setActiveValueCells(nextValueCells);
  94. // Trigger loadData
  95. internalLoadData(nextValueCells);
  96. };
  97. var isSelectable = function isSelectable(option) {
  98. var disabled = option.disabled;
  99. var isMergedLeaf = isLeaf(option, fieldNames.value);
  100. return !disabled && (isMergedLeaf || changeOnSelect.value || baseProps.multiple);
  101. };
  102. var onPathSelect = function onPathSelect(valuePath, leaf) {
  103. var fromKeyboard = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  104. onSelect(valuePath);
  105. if (!baseProps.multiple && (leaf || changeOnSelect.value && (expandTrigger.value === 'hover' || fromKeyboard))) {
  106. baseProps.toggleOpen(false);
  107. }
  108. };
  109. // ========================== Option ==========================
  110. var mergedOptions = computed(function () {
  111. if (baseProps.searchValue) {
  112. return searchOptions.value;
  113. }
  114. return options.value;
  115. });
  116. // ========================== Column ==========================
  117. var optionColumns = computed(function () {
  118. var optionList = [{
  119. options: mergedOptions.value
  120. }];
  121. var currentList = mergedOptions.value;
  122. var _loop = function _loop(i) {
  123. var activeValueCell = activeValueCells.value[i];
  124. var currentOption = currentList.find(function (option) {
  125. return option[fieldNames.value.value] === activeValueCell;
  126. });
  127. var subOptions = currentOption === null || currentOption === void 0 ? void 0 : currentOption[fieldNames.value.children];
  128. if (!(subOptions !== null && subOptions !== void 0 && subOptions.length)) {
  129. return "break";
  130. }
  131. currentList = subOptions;
  132. optionList.push({
  133. options: subOptions
  134. });
  135. };
  136. for (var i = 0; i < activeValueCells.value.length; i += 1) {
  137. var _ret = _loop(i);
  138. if (_ret === "break") break;
  139. }
  140. return optionList;
  141. });
  142. // ========================= Keyboard =========================
  143. var onKeyboardSelect = function onKeyboardSelect(selectValueCells, option) {
  144. if (isSelectable(option)) {
  145. onPathSelect(selectValueCells, isLeaf(option, fieldNames.value), true);
  146. }
  147. };
  148. useKeyboard(context, mergedOptions, fieldNames, activeValueCells, onPathOpen, onKeyboardSelect);
  149. var onListMouseDown = function onListMouseDown(event) {
  150. event.preventDefault();
  151. };
  152. onMounted(function () {
  153. watch(activeValueCells, function (cells) {
  154. for (var i = 0; i < cells.length; i += 1) {
  155. var _containerRef$value;
  156. var cellPath = cells.slice(0, i + 1);
  157. var cellKeyPath = toPathKey(cellPath);
  158. var ele = (_containerRef$value = containerRef.value) === null || _containerRef$value === void 0 ? void 0 : _containerRef$value.querySelector("li[data-path-key=\"".concat(cellKeyPath.replace(/\\{0,2}"/g, '\\"'), "\"]"));
  159. if (ele) {
  160. scrollIntoParentView(ele);
  161. }
  162. }
  163. }, {
  164. flush: 'post',
  165. immediate: true
  166. });
  167. });
  168. return function () {
  169. var _slots$notFoundConten, _customSlots$value$no, _customSlots$value, _optionColumns$value$, _optionColumns$value$2, _ref3, _ref4;
  170. // ========================== Render ==========================
  171. var _baseProps$notFoundCo = baseProps.notFoundContent,
  172. notFoundContent = _baseProps$notFoundCo === void 0 ? ((_slots$notFoundConten = slots.notFoundContent) === null || _slots$notFoundConten === void 0 ? void 0 : _slots$notFoundConten.call(slots)) || ((_customSlots$value$no = (_customSlots$value = customSlots.value).notFoundContent) === null || _customSlots$value$no === void 0 ? void 0 : _customSlots$value$no.call(_customSlots$value)) : _baseProps$notFoundCo,
  173. multiple = baseProps.multiple,
  174. toggleOpen = baseProps.toggleOpen;
  175. // >>>>> Empty
  176. var isEmpty = !((_optionColumns$value$ = optionColumns.value[0]) !== null && _optionColumns$value$ !== void 0 && (_optionColumns$value$2 = _optionColumns$value$.options) !== null && _optionColumns$value$2 !== void 0 && _optionColumns$value$2.length);
  177. var emptyList = [(_ref3 = {}, _defineProperty(_ref3, fieldNames.value.value, '__EMPTY__'), _defineProperty(_ref3, FIX_LABEL, notFoundContent), _defineProperty(_ref3, "disabled", true), _ref3)];
  178. var columnProps = _objectSpread(_objectSpread({}, attrs), {}, {
  179. multiple: !isEmpty && multiple,
  180. onSelect: onPathSelect,
  181. onActive: onPathOpen,
  182. onToggleOpen: toggleOpen,
  183. checkedSet: checkedSet.value,
  184. halfCheckedSet: halfCheckedSet.value,
  185. loadingKeys: loadingKeys.value,
  186. isSelectable: isSelectable
  187. });
  188. // >>>>> Columns
  189. var mergedOptionColumns = isEmpty ? [{
  190. options: emptyList
  191. }] : optionColumns.value;
  192. var columnNodes = mergedOptionColumns.map(function (col, index) {
  193. var prevValuePath = activeValueCells.value.slice(0, index);
  194. var activeValue = activeValueCells.value[index];
  195. return _createVNode(Column, _objectSpread(_objectSpread({
  196. "key": index
  197. }, columnProps), {}, {
  198. "prefixCls": mergedPrefixCls.value,
  199. "options": col.options,
  200. "prevValuePath": prevValuePath,
  201. "activeValue": activeValue
  202. }), null);
  203. });
  204. return _createVNode("div", {
  205. "class": ["".concat(mergedPrefixCls.value, "-menus"), (_ref4 = {}, _defineProperty(_ref4, "".concat(mergedPrefixCls.value, "-menu-empty"), isEmpty), _defineProperty(_ref4, "".concat(mergedPrefixCls.value, "-rtl"), rtl.value), _ref4)],
  206. "onMousedown": onListMouseDown,
  207. "ref": containerRef
  208. }, [columnNodes]);
  209. };
  210. }
  211. });