diff --git a/packages/abc/sidebar-nav/index.md b/packages/abc/sidebar-nav/index.md index 76601c4e91..5e479b8d59 100644 --- a/packages/abc/sidebar-nav/index.md +++ b/packages/abc/sidebar-nav/index.md @@ -21,3 +21,4 @@ ng-alain 左边主菜单,依赖于 `@delon/theme`。 参数 | 说明 | 类型 | 默认值 ----|------|-----|------ autoCloseUnderPad | 小于Pad宽度时路由切换后自动关闭侧边栏 | `boolean` | `true` +select | 切换时回调 | `EventEmitter` | - diff --git a/packages/abc/sidebar-nav/sidebar-nav.component.html b/packages/abc/sidebar-nav/sidebar-nav.component.html index eb82fbdc60..963e490573 100644 --- a/packages/abc/sidebar-nav/sidebar-nav.component.html +++ b/packages/abc/sidebar-nav/sidebar-nav.component.html @@ -7,7 +7,7 @@
  • - + @@ -37,7 +37,7 @@
  • - {{ child2.text }} + {{ child2.text }} {{ child2.text }} @@ -53,7 +53,7 @@
  • - {{ child3.text }} + {{ child3.text }} {{ child3.text }} diff --git a/packages/abc/sidebar-nav/sidebar-nav.component.ts b/packages/abc/sidebar-nav/sidebar-nav.component.ts index 1b71373921..d259d18863 100644 --- a/packages/abc/sidebar-nav/sidebar-nav.component.ts +++ b/packages/abc/sidebar-nav/sidebar-nav.component.ts @@ -1,10 +1,10 @@ -import { Component, ElementRef, Renderer2, Inject, OnInit, OnDestroy, HostListener, ChangeDetectionStrategy, ChangeDetectorRef, Input } from '@angular/core'; +import { Component, ElementRef, Renderer2, Inject, OnInit, OnDestroy, HostListener, ChangeDetectionStrategy, ChangeDetectorRef, Input, Output, EventEmitter } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; import { DOCUMENT } from '@angular/common'; import { Subscription } from 'rxjs/Subscription'; import { debounceTime, filter } from 'rxjs/operators'; import { FromEventObservable } from 'rxjs/observable/FromEventObservable'; -import { MenuService, SettingsService } from '@delon/theme'; +import { MenuService, SettingsService, Menu } from '@delon/theme'; import { Nav } from './interface'; @@ -27,6 +27,8 @@ export class SidebarNavComponent implements OnInit, OnDestroy { @Input() autoCloseUnderPad = true; + @Output() select = new EventEmitter(); + constructor( private menuSrv: MenuService, public settings: SettingsService, @@ -61,6 +63,7 @@ export class SidebarNavComponent implements OnInit, OnDestroy { url = url.slice(1); } this.router.navigateByUrl(url); + this.onSelect(this.menuSrv.getPathByUrl(url).pop()); this.hideAll(); return false; } @@ -126,6 +129,10 @@ export class SidebarNavComponent implements OnInit, OnDestroy { this.calPos(linkNode as HTMLLinkElement, subNode); } + onSelect(item: Menu) { + this.select.emit(item); + } + toggleOpen(item: Nav) { this.menuSrv.visit((i, p) => { if (i !== item) i._open = false; diff --git a/packages/abc/sidebar-nav/sidebar-nav.spec.ts b/packages/abc/sidebar-nav/sidebar-nav.spec.ts index 191172b75a..91b48bf153 100644 --- a/packages/abc/sidebar-nav/sidebar-nav.spec.ts +++ b/packages/abc/sidebar-nav/sidebar-nav.spec.ts @@ -76,6 +76,20 @@ describe('abc: sidebar-nav', () => { return ''; } + it('should be navigate url', () => { + createComp(); + spyOn(context, 'select'); + const data = deepCopy(MOCKMENUS); + menuSrv.add(data); + expect(context.select).not.toHaveBeenCalled(); + expect(router.navigateByUrl).not.toHaveBeenCalled(); + const itemEl = getEl('.nav-depth1 a'); + itemEl.click(); + fixture.detectChanges(); + expect(context.select).toHaveBeenCalled(); + expect(router.navigateByUrl).toHaveBeenCalled(); + }); + it('should be toggle open', () => { createComp(); const data = deepCopy(MOCKMENUS); @@ -230,10 +244,11 @@ describe('abc: sidebar-nav', () => { @Component({ template: ` - + ` }) class TestComponent { @ViewChild('comp') comp: SidebarNavComponent; autoCloseUnderPad = false; + select() {} }