Skip to content

Commit

Permalink
fix(module:empty): show no empty when use pass null (#2768)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wendell authored and vthinkxie committed Jan 17, 2019
1 parent c6d2706 commit 48d5333
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions components/empty/nz-embed-empty.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<ng-container *ngIf="!content; else userDefineTpl" [ngSwitch]="size">
<ng-container *ngIf="!content && specificContent !== null" [ngSwitch]="size">
<nz-empty *ngSwitchCase="'normal'" class="ant-empty-normal" [nzNotFoundImage]="defaultSvg"></nz-empty>
<nz-empty *ngSwitchCase="'small'" class="ant-empty-small" [nzNotFoundImage]="defaultSvg"></nz-empty>
<nz-empty *ngSwitchDefault></nz-empty>
</ng-container>
<ng-template #userDefineTpl>
<ng-container *ngIf="content">
<ng-template *ngIf="contentType !== 'string'" [cdkPortalOutlet]="contentPortal"></ng-template>
<ng-container *ngIf="contentType === 'string'">
{{ content }}
</ng-container>
</ng-template>
</ng-container>
11 changes: 5 additions & 6 deletions components/empty/nz-embed-empty.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
import { DomSanitizer } from '@angular/platform-browser';
import { Subscription } from 'rxjs';

import { simpleEmptyImage, NzEmptyCustomContent, NzEmptySize, NZ_EMPTY_COMPONENT_NAME } from './nz-empty.config';
import { NzEmptyContentTypeError } from './nz-empty.error';
import { simpleEmptyImage, NzEmptyCustomContent, NzEmptySize, NZ_EMPTY_COMPONENT_NAME } from './nz-empty-config';
import { getEmptyContentTypeError } from './nz-empty-error';
import { NzEmptyService } from './nz-empty.service';

@Component({
Expand Down Expand Up @@ -88,9 +88,7 @@ export class NzEmbedEmptyComponent implements OnChanges, OnInit, OnDestroy {
private renderEmpty(): void {
const content = this.content;

if (content === undefined || content === null) {
// Do nothing.
} else if (typeof content === 'string') {
if (typeof content === 'string') {
this.contentType = 'string';
} else if (content instanceof TemplateRef) {
const context = { $implicit: this.nzComponentName } as any; // tslint:disable-line:no-any
Expand All @@ -102,7 +100,8 @@ export class NzEmbedEmptyComponent implements OnChanges, OnInit, OnDestroy {
this.contentType = 'component';
this.contentPortal = new ComponentPortal(content, this.viewContainerRef, injector);
} else {
throw NzEmptyContentTypeError(content);
this.contentType = 'string';
this.contentPortal = undefined;
}

this.cdr.markForCheck();
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function NzEmptyContentTypeError(content: any): Error { // tslint:disable-line:no-any
export function getEmptyContentTypeError(content: any): Error { // tslint:disable-line:no-any
return TypeError(`[NG-ZORRO]: useDefaultContent expect 'string', 'templateRef' or 'component' but get ${content}`);
}
2 changes: 1 addition & 1 deletion components/empty/nz-empty.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

import { emptyImage } from './nz-empty.config';
import { emptyImage } from './nz-empty-config';

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
Expand Down
16 changes: 13 additions & 3 deletions components/empty/nz-empty.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Inject, Injectable, Optional, Type } from '@angular/core';
import { Inject, Injectable, Optional, TemplateRef, Type } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { NzEmptyCustomContent, NZ_DEFAULT_EMPTY_CONTENT } from './nz-empty.config';
import { NzEmptyCustomContent, NZ_DEFAULT_EMPTY_CONTENT } from './nz-empty-config';
import { getEmptyContentTypeError } from './nz-empty-error';

@Injectable({
providedIn: 'root'
Expand All @@ -15,7 +16,16 @@ export class NzEmptyService<T = any> { // tslint:disable-line:no-any
}

setDefaultContent(content?: NzEmptyCustomContent): void {
this.userDefaultContent$.next(content);
if (typeof content === 'string'
|| content === undefined
|| content === null
|| content instanceof TemplateRef
|| content instanceof Type
) {
this.userDefaultContent$.next(content);
} else {
throw getEmptyContentTypeError(content);
}
}

resetDefault(): void {
Expand Down
13 changes: 10 additions & 3 deletions components/empty/nz-empty.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CommonModule } from '@angular/common';
import { Component, Inject, NgModule, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { Component, Inject, NgModule, TemplateRef, ViewChild } from '@angular/core';
import { fakeAsync, tick, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NzListModule } from '../list';
import { NzEmbedEmptyComponent } from './nz-embed-empty.component';
import { NZ_DEFAULT_EMPTY_CONTENT, NZ_EMPTY_COMPONENT_NAME } from './nz-empty-config';
import { NzEmptyComponent } from './nz-empty.component';
import { NZ_DEFAULT_EMPTY_CONTENT, NZ_EMPTY_COMPONENT_NAME } from './nz-empty.config';
import { NzEmptyModule } from './nz-empty.module';
import { NzEmptyService } from './nz-empty.service';

Expand Down Expand Up @@ -145,6 +145,13 @@ describe('nz-empty', () => {
expect(embedComponent).toBeTruthy();
expect(emptyComponent).toBeFalsy();
expect(embedComponent.nativeElement.innerText).toBe('list');

// Null.
testComponent.noResult = null;
refresh();
expect(embedComponent).toBeTruthy();
expect(emptyComponent).toBeFalsy();
expect(embedComponent.nativeElement.innerText).toBe('');
}));

it('should support string, template and component', fakeAsync(() => {
Expand Down Expand Up @@ -279,7 +286,7 @@ export class NzEmptyTestBasicComponent {
export class NzEmptyTestServiceComponent {
@ViewChild('tpl') template: TemplateRef<void>;

noResult = null;
noResult = undefined;

constructor(private emptyService: NzEmptyService) {
}
Expand Down
2 changes: 1 addition & 1 deletion components/empty/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './nz-embed-empty.component';
export * from './nz-empty.component';
export * from './nz-empty.module';
export * from './nz-empty.service';
export * from './nz-empty.config';
export * from './nz-empty-config';

0 comments on commit 48d5333

Please sign in to comment.