useKeyboard.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  2. import { computed, ref, watchEffect } from 'vue';
  3. import { useBaseProps } from '../../vc-select';
  4. import KeyCode from '../../_util/KeyCode';
  5. import { SEARCH_MARK } from '../hooks/useSearchOptions';
  6. export default (function (context, options, fieldNames, activeValueCells, setActiveValueCells,
  7. // containerRef: Ref<HTMLElement>,
  8. onKeyBoardSelect) {
  9. var baseProps = useBaseProps();
  10. var rtl = computed(function () {
  11. return baseProps.direction === 'rtl';
  12. });
  13. var _ref = [ref([]), ref(), ref([])],
  14. validActiveValueCells = _ref[0],
  15. lastActiveIndex = _ref[1],
  16. lastActiveOptions = _ref[2];
  17. watchEffect(function () {
  18. var activeIndex = -1;
  19. var currentOptions = options.value;
  20. var mergedActiveIndexes = [];
  21. var mergedActiveValueCells = [];
  22. var len = activeValueCells.value.length;
  23. // Fill validate active value cells and index
  24. var _loop = function _loop(i) {
  25. // Mark the active index for current options
  26. var nextActiveIndex = currentOptions.findIndex(function (option) {
  27. return option[fieldNames.value.value] === activeValueCells.value[i];
  28. });
  29. if (nextActiveIndex === -1) {
  30. return "break";
  31. }
  32. activeIndex = nextActiveIndex;
  33. mergedActiveIndexes.push(activeIndex);
  34. mergedActiveValueCells.push(activeValueCells.value[i]);
  35. currentOptions = currentOptions[activeIndex][fieldNames.value.children];
  36. };
  37. for (var i = 0; i < len && currentOptions; i += 1) {
  38. var _ret = _loop(i);
  39. if (_ret === "break") break;
  40. }
  41. // Fill last active options
  42. var activeOptions = options.value;
  43. for (var _i = 0; _i < mergedActiveIndexes.length - 1; _i += 1) {
  44. activeOptions = activeOptions[mergedActiveIndexes[_i]][fieldNames.value.children];
  45. }
  46. var _ref2 = [mergedActiveValueCells, activeIndex, activeOptions];
  47. validActiveValueCells.value = _ref2[0];
  48. lastActiveIndex.value = _ref2[1];
  49. lastActiveOptions.value = _ref2[2];
  50. });
  51. // Update active value cells and scroll to target element
  52. var internalSetActiveValueCells = function internalSetActiveValueCells(next) {
  53. setActiveValueCells(next);
  54. };
  55. // Same options offset
  56. var offsetActiveOption = function offsetActiveOption(offset) {
  57. var len = lastActiveOptions.value.length;
  58. var currentIndex = lastActiveIndex.value;
  59. if (currentIndex === -1 && offset < 0) {
  60. currentIndex = len;
  61. }
  62. for (var i = 0; i < len; i += 1) {
  63. currentIndex = (currentIndex + offset + len) % len;
  64. var option = lastActiveOptions.value[currentIndex];
  65. if (option && !option.disabled) {
  66. var value = option[fieldNames.value.value];
  67. var nextActiveCells = validActiveValueCells.value.slice(0, -1).concat(value);
  68. internalSetActiveValueCells(nextActiveCells);
  69. return;
  70. }
  71. }
  72. };
  73. // Different options offset
  74. var prevColumn = function prevColumn() {
  75. if (validActiveValueCells.value.length > 1) {
  76. var nextActiveCells = validActiveValueCells.value.slice(0, -1);
  77. internalSetActiveValueCells(nextActiveCells);
  78. } else {
  79. baseProps.toggleOpen(false);
  80. }
  81. };
  82. var nextColumn = function nextColumn() {
  83. var _lastActiveOptions$va;
  84. var nextOptions = ((_lastActiveOptions$va = lastActiveOptions.value[lastActiveIndex.value]) === null || _lastActiveOptions$va === void 0 ? void 0 : _lastActiveOptions$va[fieldNames.value.children]) || [];
  85. var nextOption = nextOptions.find(function (option) {
  86. return !option.disabled;
  87. });
  88. if (nextOption) {
  89. var nextActiveCells = [].concat(_toConsumableArray(validActiveValueCells.value), [nextOption[fieldNames.value.value]]);
  90. internalSetActiveValueCells(nextActiveCells);
  91. }
  92. };
  93. context.expose({
  94. // scrollTo: treeRef.current?.scrollTo,
  95. onKeydown: function onKeydown(event) {
  96. var which = event.which;
  97. switch (which) {
  98. // >>> Arrow keys
  99. case KeyCode.UP:
  100. case KeyCode.DOWN:
  101. {
  102. var offset = 0;
  103. if (which === KeyCode.UP) {
  104. offset = -1;
  105. } else if (which === KeyCode.DOWN) {
  106. offset = 1;
  107. }
  108. if (offset !== 0) {
  109. offsetActiveOption(offset);
  110. }
  111. break;
  112. }
  113. case KeyCode.LEFT:
  114. {
  115. if (rtl.value) {
  116. nextColumn();
  117. } else {
  118. prevColumn();
  119. }
  120. break;
  121. }
  122. case KeyCode.RIGHT:
  123. {
  124. if (rtl.value) {
  125. prevColumn();
  126. } else {
  127. nextColumn();
  128. }
  129. break;
  130. }
  131. case KeyCode.BACKSPACE:
  132. {
  133. if (!baseProps.searchValue) {
  134. prevColumn();
  135. }
  136. break;
  137. }
  138. // >>> Select
  139. case KeyCode.ENTER:
  140. {
  141. if (validActiveValueCells.value.length) {
  142. var option = lastActiveOptions.value[lastActiveIndex.value];
  143. // Search option should revert back of origin options
  144. var originOptions = (option === null || option === void 0 ? void 0 : option[SEARCH_MARK]) || [];
  145. if (originOptions.length) {
  146. onKeyBoardSelect(originOptions.map(function (opt) {
  147. return opt[fieldNames.value.value];
  148. }), originOptions[originOptions.length - 1]);
  149. } else {
  150. onKeyBoardSelect(validActiveValueCells.value, option);
  151. }
  152. }
  153. break;
  154. }
  155. // >>> Close
  156. case KeyCode.ESC:
  157. {
  158. baseProps.toggleOpen(false);
  159. if (open) {
  160. event.stopPropagation();
  161. }
  162. }
  163. }
  164. },
  165. onKeyup: function onKeyup() {}
  166. });
  167. });