Skip to content

Commit

Permalink
feat(module:page-header): add default behavior for the back button (#…
Browse files Browse the repository at this point in the history
…3891)

close #3421
  • Loading branch information
hsuanxyz authored and Wendell committed Aug 9, 2019
1 parent b98624b commit 41bc285
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
8 changes: 4 additions & 4 deletions components/page-header/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import { NzPageHeaderModule } from 'ng-zorro-antd/page-header';
### nz-page-header
| Param | Description | Type | Default value |
| ----- | ----------- | ---- | ------------- |
| `[nzTitle]` | title string | `string \| TemplateRef<void>` | - |
| `[nzSubtitle]` | subTitle string | `string \| TemplateRef<void>` | - |
| `[nzBackIcon]` | custom back icon | `string \| TemplateRef<void>` | - |
| `(nzBack)` | back icon click event | `EventEmitter<void>` | - |
| `[nzTitle]` | Title string | `string \| TemplateRef<void>` | - |
| `[nzSubtitle]` | SubTitle string | `string \| TemplateRef<void>` | - |
| `[nzBackIcon]` | Custom back icon | `string \| TemplateRef<void>` | - |
| `(nzBack)` | Back icon click event | `EventEmitter<void>` | Call [Location[back]](https://angular.io/api/common/Location#back) when the event not subscribed |

### Page header sections
| Element | Description |
Expand Down
2 changes: 1 addition & 1 deletion components/page-header/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { NzPageHeaderModule } from 'ng-zorro-antd/page-header';
| `[nzTitle]` | title 文字 | `string \| TemplateRef<void>` | - |
| `[nzSubtitle]` | subTitle 文字 | `string \| TemplateRef<void>` | - |
| `[nzBackIcon]` | 自定义 back icon | `string \| TemplateRef<void>` | - |
| `(nzBack)` | 返回按钮的点击事件 | `EventEmitter<void>` | - |
| `(nzBack)` | 返回按钮的点击事件 | `EventEmitter<void>` | 未订阅该事件时默认调用 [Location[back]](https://angular.cn/api/common/Location#back) |

### Page header 组成部分
| 元素 | 说明 |
Expand Down
10 changes: 8 additions & 2 deletions components/page-header/nz-page-header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
TemplateRef,
ViewEncapsulation
} from '@angular/core';

import { Location } from '@angular/common';
import { NzPageHeaderFooterDirective } from './nz-page-header-cells';

@Component({
Expand Down Expand Up @@ -58,7 +60,7 @@ export class NzPageHeaderComponent implements OnInit, OnChanges {
NzPageHeaderFooterDirective
>;

constructor() {}
constructor(private location: Location) {}

ngOnInit(): void {}

Expand All @@ -70,6 +72,10 @@ export class NzPageHeaderComponent implements OnInit, OnChanges {
}

onBack(): void {
this.nzBack.emit();
if (this.nzBack.observers.length) {
this.nzBack.emit();
} else {
this.location.back();
}
}
}
18 changes: 17 additions & 1 deletion components/page-header/nz-page-header.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Location } from '@angular/common';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { async, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { RouterTestingModule } from '@angular/router/testing';
import { NzIconTestModule } from 'ng-zorro-antd/icon/testing';
import { NzDemoPageHeaderActionsComponent } from './demo/actions';
import { NzDemoPageHeaderBasicComponent } from './demo/basic';
Expand All @@ -11,9 +13,10 @@ import { NzPageHeaderComponent } from './nz-page-header.component';
import { NzPageHeaderModule } from './nz-page-header.module';

describe('NzPageHeaderComponent', () => {
let location: Location;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [NzPageHeaderModule, NzIconTestModule],
imports: [NzPageHeaderModule, NzIconTestModule, RouterTestingModule],
schemas: [NO_ERRORS_SCHEMA],
declarations: [
NzDemoPageHeaderBasicComponent,
Expand All @@ -22,6 +25,7 @@ describe('NzPageHeaderComponent', () => {
NzDemoPageHeaderActionsComponent
]
}).compileComponents();
location = TestBed.get(Location);
}));

it('should basic work', () => {
Expand All @@ -40,6 +44,18 @@ describe('NzPageHeaderComponent', () => {
expect(pageHeader.nativeElement.querySelector('nz-breadcrumb[nz-page-header-breadcrumb]')).toBeTruthy();
});

it('should default call location back when nzBack not has observers', () => {
const fixture = TestBed.createComponent(NzDemoPageHeaderBreadcrumbComponent);
const pageHeader = fixture.debugElement.query(By.directive(NzPageHeaderComponent));
spyOn(location, 'back');
fixture.detectChanges();
expect(location.back).not.toHaveBeenCalled();
const back = pageHeader.nativeElement.querySelector('.ant-page-header-back');
(back as HTMLElement).click();
fixture.detectChanges();
expect(location.back).toHaveBeenCalled();
});

it('should content work', () => {
const fixture = TestBed.createComponent(NzDemoPageHeaderContentComponent);
const pageHeader = fixture.debugElement.query(By.directive(NzPageHeaderComponent));
Expand Down

0 comments on commit 41bc285

Please sign in to comment.