itemFoundation.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* argus-disable unPkgSensitiveInfo */
  2. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  3. export interface ItemProps {
  4. text?: any;
  5. itemKey?: any;
  6. icon?: any;
  7. toggleIcon?: string;
  8. indent?: boolean | number;
  9. isCollapsed?: boolean;
  10. isSubNav?: boolean;
  11. link?: string;
  12. linkOptions?: Record<string, any>;
  13. disabled?: boolean;
  14. children?: any;
  15. }
  16. export interface SelectedItemProps<Props = ItemProps> {
  17. itemKey: string | number;
  18. text?: any;
  19. selectedKeys?: string | number[];
  20. selectedItems?: Props[];
  21. domEvent?: any;
  22. }
  23. export interface ItemAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  24. cloneDeep(value: any, customizer?: (value: any) => void): any;
  25. updateTooltipShow(showTooltip: boolean): void;
  26. updateSelected(selected: boolean): void;
  27. updateGlobalSelectedKeys(keys: string[]): void;
  28. getSelectedKeys(): string[];
  29. getSelectedKeysIsControlled(): boolean;
  30. notifyGlobalOnSelect(item: SelectedItemProps): void;
  31. notifyGlobalOnClick(item: SelectedItemProps): void;
  32. notifyClick(item: SelectedItemProps): void;
  33. notifyMouseEnter(e: any): void;
  34. notifyMouseLeave(e: any): void;
  35. getIsCollapsed(): boolean;
  36. getSelected(): boolean;
  37. }
  38. export default class ItemFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<ItemAdapter<P, S>, P, S> {
  39. _timer: number;
  40. _mounted: boolean;
  41. constructor(adapter: ItemAdapter<P, S>) {
  42. super({ ...adapter });
  43. }
  44. init() {
  45. this._timer = null;
  46. this._mounted = true;
  47. }
  48. destroy() {
  49. this._mounted = false;
  50. }
  51. isValidKey(itemKey: string) {
  52. // eslint-disable-next-line eqeqeq
  53. return itemKey != null && (typeof itemKey === 'string' || typeof itemKey === 'number');
  54. }
  55. handleClick(e: any) {
  56. const { isSubNav, itemKey, text, disabled } = this.getProps();
  57. if (disabled) {
  58. return;
  59. }
  60. if (
  61. !isSubNav &&
  62. this.isValidKey(itemKey) &&
  63. !this._adapter.getSelectedKeysIsControlled() &&
  64. !this._adapter.getSelected()
  65. ) {
  66. this._adapter.updateSelected(true);
  67. }
  68. const selectedKeys = [itemKey];
  69. // 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
  70. if (!isSubNav) {
  71. if (!this._adapter.getSelected()) {
  72. // internal-issues:51
  73. const selectedItems = [this._adapter.cloneDeep(this.getProps())];
  74. this._adapter.notifyGlobalOnSelect({ itemKey, selectedKeys, selectedItems, domEvent: e });
  75. }
  76. this._adapter.notifyGlobalOnClick({ itemKey, text, domEvent: e });
  77. }
  78. this._adapter.notifyClick({ itemKey, text, domEvent: e });
  79. }
  80. }