list-key-manager.d-BlK3jyRn.d.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { QueryList, Signal, Injector } from '@angular/core';
  2. import { Subject } from 'rxjs';
  3. /** This interface is for items that can be passed to a ListKeyManager. */
  4. interface ListKeyManagerOption {
  5. /** Whether the option is disabled. */
  6. disabled?: boolean;
  7. /** Gets the label for this option. */
  8. getLabel?(): string;
  9. }
  10. /** Modifier keys handled by the ListKeyManager. */
  11. type ListKeyManagerModifierKey = 'altKey' | 'ctrlKey' | 'metaKey' | 'shiftKey';
  12. /**
  13. * This class manages keyboard events for selectable lists. If you pass it a query list
  14. * of items, it will set the active item correctly when arrow events occur.
  15. */
  16. declare class ListKeyManager<T extends ListKeyManagerOption> {
  17. private _items;
  18. private _activeItemIndex;
  19. private _activeItem;
  20. private _wrap;
  21. private _typeaheadSubscription;
  22. private _itemChangesSubscription?;
  23. private _vertical;
  24. private _horizontal;
  25. private _allowedModifierKeys;
  26. private _homeAndEnd;
  27. private _pageUpAndDown;
  28. private _effectRef;
  29. private _typeahead?;
  30. /**
  31. * Predicate function that can be used to check whether an item should be skipped
  32. * by the key manager. By default, disabled items are skipped.
  33. */
  34. private _skipPredicateFn;
  35. constructor(items: QueryList<T> | T[] | readonly T[]);
  36. constructor(items: Signal<T[]> | Signal<readonly T[]>, injector: Injector);
  37. /**
  38. * Stream that emits any time the TAB key is pressed, so components can react
  39. * when focus is shifted off of the list.
  40. */
  41. readonly tabOut: Subject<void>;
  42. /** Stream that emits whenever the active item of the list manager changes. */
  43. readonly change: Subject<number>;
  44. /**
  45. * Sets the predicate function that determines which items should be skipped by the
  46. * list key manager.
  47. * @param predicate Function that determines whether the given item should be skipped.
  48. */
  49. skipPredicate(predicate: (item: T) => boolean): this;
  50. /**
  51. * Configures wrapping mode, which determines whether the active item will wrap to
  52. * the other end of list when there are no more items in the given direction.
  53. * @param shouldWrap Whether the list should wrap when reaching the end.
  54. */
  55. withWrap(shouldWrap?: boolean): this;
  56. /**
  57. * Configures whether the key manager should be able to move the selection vertically.
  58. * @param enabled Whether vertical selection should be enabled.
  59. */
  60. withVerticalOrientation(enabled?: boolean): this;
  61. /**
  62. * Configures the key manager to move the selection horizontally.
  63. * Passing in `null` will disable horizontal movement.
  64. * @param direction Direction in which the selection can be moved.
  65. */
  66. withHorizontalOrientation(direction: 'ltr' | 'rtl' | null): this;
  67. /**
  68. * Modifier keys which are allowed to be held down and whose default actions will be prevented
  69. * as the user is pressing the arrow keys. Defaults to not allowing any modifier keys.
  70. */
  71. withAllowedModifierKeys(keys: ListKeyManagerModifierKey[]): this;
  72. /**
  73. * Turns on typeahead mode which allows users to set the active item by typing.
  74. * @param debounceInterval Time to wait after the last keystroke before setting the active item.
  75. */
  76. withTypeAhead(debounceInterval?: number): this;
  77. /** Cancels the current typeahead sequence. */
  78. cancelTypeahead(): this;
  79. /**
  80. * Configures the key manager to activate the first and last items
  81. * respectively when the Home or End key is pressed.
  82. * @param enabled Whether pressing the Home or End key activates the first/last item.
  83. */
  84. withHomeAndEnd(enabled?: boolean): this;
  85. /**
  86. * Configures the key manager to activate every 10th, configured or first/last element in up/down direction
  87. * respectively when the Page-Up or Page-Down key is pressed.
  88. * @param enabled Whether pressing the Page-Up or Page-Down key activates the first/last item.
  89. * @param delta Whether pressing the Home or End key activates the first/last item.
  90. */
  91. withPageUpDown(enabled?: boolean, delta?: number): this;
  92. /**
  93. * Sets the active item to the item at the index specified.
  94. * @param index The index of the item to be set as active.
  95. */
  96. setActiveItem(index: number): void;
  97. /**
  98. * Sets the active item to the specified item.
  99. * @param item The item to be set as active.
  100. */
  101. setActiveItem(item: T): void;
  102. /**
  103. * Sets the active item depending on the key event passed in.
  104. * @param event Keyboard event to be used for determining which element should be active.
  105. */
  106. onKeydown(event: KeyboardEvent): void;
  107. /** Index of the currently active item. */
  108. get activeItemIndex(): number | null;
  109. /** The active item. */
  110. get activeItem(): T | null;
  111. /** Gets whether the user is currently typing into the manager using the typeahead feature. */
  112. isTyping(): boolean;
  113. /** Sets the active item to the first enabled item in the list. */
  114. setFirstItemActive(): void;
  115. /** Sets the active item to the last enabled item in the list. */
  116. setLastItemActive(): void;
  117. /** Sets the active item to the next enabled item in the list. */
  118. setNextItemActive(): void;
  119. /** Sets the active item to a previous enabled item in the list. */
  120. setPreviousItemActive(): void;
  121. /**
  122. * Allows setting the active without any other effects.
  123. * @param index Index of the item to be set as active.
  124. */
  125. updateActiveItem(index: number): void;
  126. /**
  127. * Allows setting the active item without any other effects.
  128. * @param item Item to be set as active.
  129. */
  130. updateActiveItem(item: T): void;
  131. /** Cleans up the key manager. */
  132. destroy(): void;
  133. /**
  134. * This method sets the active item, given a list of items and the delta between the
  135. * currently active item and the new active item. It will calculate differently
  136. * depending on whether wrap mode is turned on.
  137. */
  138. private _setActiveItemByDelta;
  139. /**
  140. * Sets the active item properly given "wrap" mode. In other words, it will continue to move
  141. * down the list until it finds an item that is not disabled, and it will wrap if it
  142. * encounters either end of the list.
  143. */
  144. private _setActiveInWrapMode;
  145. /**
  146. * Sets the active item properly given the default mode. In other words, it will
  147. * continue to move down the list until it finds an item that is not disabled. If
  148. * it encounters either end of the list, it will stop and not wrap.
  149. */
  150. private _setActiveInDefaultMode;
  151. /**
  152. * Sets the active item to the first enabled item starting at the index specified. If the
  153. * item is disabled, it will move in the fallbackDelta direction until it either
  154. * finds an enabled item or encounters the end of the list.
  155. */
  156. private _setActiveItemByIndex;
  157. /** Returns the items as an array. */
  158. private _getItemsArray;
  159. /** Callback for when the items have changed. */
  160. private _itemsChanged;
  161. }
  162. export { ListKeyManager as a };
  163. export type { ListKeyManagerOption as L, ListKeyManagerModifierKey as b };