itemFoundation.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* argus-disable unPkgSensitiveInfo */
  2. import { get } from 'lodash';
  3. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  4. import isEnterPress from '../utils/isEnterPress';
  5. export interface ItemProps {
  6. text?: any;
  7. itemKey?: any;
  8. icon?: any;
  9. toggleIcon?: string;
  10. indent?: boolean | number;
  11. isCollapsed?: boolean;
  12. isSubNav?: boolean;
  13. link?: string;
  14. linkOptions?: Record<string, any>;
  15. disabled?: boolean;
  16. children?: any
  17. }
  18. export type ItemKey = string | number;
  19. export interface SelectedItemProps<Props = ItemProps> {
  20. itemKey: ItemKey;
  21. text?: any;
  22. selectedKeys?: ItemKey[];
  23. selectedItems?: Props[];
  24. domEvent?: any
  25. }
  26. export interface ItemAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  27. cloneDeep(value: any, customizer?: (value: any) => void): any;
  28. updateTooltipShow(showTooltip: boolean): void;
  29. updateSelected(selected: boolean): void;
  30. updateGlobalSelectedKeys(keys: ItemKey[]): void;
  31. getSelectedKeys(): ItemKey[];
  32. getSelectedKeysIsControlled(): boolean;
  33. notifyGlobalOnSelect(item: SelectedItemProps): void;
  34. notifyGlobalOnClick(item: SelectedItemProps): void;
  35. notifyClick(item: SelectedItemProps): void;
  36. notifyMouseEnter(e: any): void;
  37. notifyMouseLeave(e: any): void;
  38. getIsCollapsed(): boolean;
  39. getSelected(): boolean;
  40. getIsOpen(): boolean
  41. }
  42. export default class ItemFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<ItemAdapter<P, S>, P, S> {
  43. _timer: number;
  44. _mounted: boolean;
  45. constructor(adapter: ItemAdapter<P, S>) {
  46. super({ ...adapter });
  47. }
  48. init() {
  49. this._timer = null;
  50. this._mounted = true;
  51. }
  52. destroy() {
  53. this._mounted = false;
  54. }
  55. isValidKey(itemKey: string) {
  56. return itemKey != null && (typeof itemKey === 'string' || typeof itemKey === 'number');
  57. }
  58. handleClick(e: any) {
  59. const { isSubNav, itemKey, text, disabled } = this.getProps();
  60. if (disabled) {
  61. return;
  62. }
  63. if (
  64. !isSubNav &&
  65. this.isValidKey(itemKey) &&
  66. !this._adapter.getSelectedKeysIsControlled() &&
  67. !this._adapter.getSelected()
  68. ) {
  69. this._adapter.updateSelected(true);
  70. }
  71. const selectedKeys = [itemKey];
  72. // If the current item is subNav, there is no need to trigger the global onSelect/onClick event, instead, the SubNav component will trigger the click event
  73. if (!isSubNav) {
  74. if (!this._adapter.getSelected()) {
  75. // internal-issues:51
  76. const selectedItems = [this._adapter.cloneDeep(this.getProps())];
  77. this._adapter.notifyGlobalOnSelect({ itemKey, selectedKeys, selectedItems, domEvent: e });
  78. }
  79. this._adapter.notifyGlobalOnClick({ itemKey, text, domEvent: e });
  80. }
  81. this._adapter.notifyClick({ itemKey, text, domEvent: e });
  82. }
  83. /**
  84. * A11y: simulate item click
  85. */
  86. handleKeyPress(e: any) {
  87. if (isEnterPress(e)) {
  88. const { link, linkOptions } = this.getProps();
  89. const target = get(linkOptions, 'target', '_self');
  90. this.handleClick(e);
  91. if (typeof link === 'string') {
  92. target === '_blank' ? window.open(link) : window.location.href = link;
  93. }
  94. }
  95. }
  96. }