12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /* argus-disable unPkgSensitiveInfo */
- import BaseFoundation, { DefaultAdapter } from '../base/foundation';
- export interface ItemProps {
- text?: any;
- itemKey?: any;
- icon?: any;
- toggleIcon?: string;
- indent?: boolean | number;
- isCollapsed?: boolean;
- isSubNav?: boolean;
- link?: string;
- linkOptions?: Record<string, any>;
- disabled?: boolean;
- children?: any;
- }
- export interface SelectedItemProps<Props = ItemProps> {
- itemKey: string | number;
- text?: any;
- selectedKeys?: string | number[];
- selectedItems?: Props[];
- domEvent?: any;
- }
- export interface ItemAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
- cloneDeep(value: any, customizer?: (value: any) => void): any;
- updateTooltipShow(showTooltip: boolean): void;
- updateSelected(selected: boolean): void;
- updateGlobalSelectedKeys(keys: string[]): void;
- getSelectedKeys(): string[];
- getSelectedKeysIsControlled(): boolean;
- notifyGlobalOnSelect(item: SelectedItemProps): void;
- notifyGlobalOnClick(item: SelectedItemProps): void;
- notifyClick(item: SelectedItemProps): void;
- notifyMouseEnter(e: any): void;
- notifyMouseLeave(e: any): void;
- getIsCollapsed(): boolean;
- getSelected(): boolean;
- }
- export default class ItemFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<ItemAdapter<P, S>, P, S> {
- _timer: number;
- _mounted: boolean;
- constructor(adapter: ItemAdapter<P, S>) {
- super({ ...adapter });
- }
- init() {
- this._timer = null;
- this._mounted = true;
- }
- destroy() {
- this._mounted = false;
- }
- isValidKey(itemKey: string) {
- // eslint-disable-next-line eqeqeq
- return itemKey != null && (typeof itemKey === 'string' || typeof itemKey === 'number');
- }
- handleClick(e: any) {
- const { isSubNav, itemKey, text, disabled } = this.getProps();
- if (disabled) {
- return;
- }
- if (
- !isSubNav &&
- this.isValidKey(itemKey) &&
- !this._adapter.getSelectedKeysIsControlled() &&
- !this._adapter.getSelected()
- ) {
- this._adapter.updateSelected(true);
- }
- const selectedKeys = [itemKey];
- // 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
- if (!isSubNav) {
- if (!this._adapter.getSelected()) {
- // internal-issues:51
- const selectedItems = [this._adapter.cloneDeep(this.getProps())];
- this._adapter.notifyGlobalOnSelect({ itemKey, selectedKeys, selectedItems, domEvent: e });
- }
- this._adapter.notifyGlobalOnClick({ itemKey, text, domEvent: e });
- }
- this._adapter.notifyClick({ itemKey, text, domEvent: e });
- }
- }
|