itemFoundation.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 interface SelectedItemProps<Props = ItemProps> {
  19. itemKey: string | number;
  20. text?: any;
  21. selectedKeys?: string | number[];
  22. selectedItems?: Props[];
  23. domEvent?: any
  24. }
  25. export interface ItemAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  26. cloneDeep(value: any, customizer?: (value: any) => void): any;
  27. updateTooltipShow(showTooltip: boolean): void;
  28. updateSelected(selected: boolean): void;
  29. updateGlobalSelectedKeys(keys: string[]): void;
  30. getSelectedKeys(): string[];
  31. getSelectedKeysIsControlled(): boolean;
  32. notifyGlobalOnSelect(item: SelectedItemProps): void;
  33. notifyGlobalOnClick(item: SelectedItemProps): void;
  34. notifyClick(item: SelectedItemProps): void;
  35. notifyMouseEnter(e: any): void;
  36. notifyMouseLeave(e: any): void;
  37. getIsCollapsed(): boolean;
  38. getSelected(): boolean;
  39. getIsOpen(): boolean
  40. }
  41. export default class ItemFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<ItemAdapter<P, S>, P, S> {
  42. _timer: number;
  43. _mounted: boolean;
  44. constructor(adapter: ItemAdapter<P, S>) {
  45. super({ ...adapter });
  46. }
  47. init() {
  48. this._timer = null;
  49. this._mounted = true;
  50. }
  51. destroy() {
  52. this._mounted = false;
  53. }
  54. isValidKey(itemKey: string) {
  55. // eslint-disable-next-line eqeqeq
  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. }