useKeyboard.js 5.1 KB

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