itemFoundation.ts 3.1 KB

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