Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
wenqi73 committed Aug 26, 2018
1 parent d2c0eec commit 24d3320
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
25 changes: 19 additions & 6 deletions components/card/demo/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@ import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-card-loading',
template: `
<nz-card [nzLoading]="loading" nzTitle="Card title">
Whatever content
<nz-switch [(ngModel)]="loading"></nz-switch>
<nz-card
style="width: 300px;margin-top: 16px"
[nzActions]="[actionSetting,actionEdit,actionEllipsis]">
<nz-skeleton [nzActive]="true" [nzLoading]="loading" [nzAvatar]="{size: 'large'}">
<nz-card-meta [nzAvatar]="avatarTemplate" nzTitle="Card title" nzDescription="This is the description"></nz-card-meta>
</nz-skeleton>
</nz-card>
<button nz-button style="margin-top: 16px;" (click)="toggleLoading()">Toggle loading</button>
<ng-template #avatarTemplate>
<nz-avatar nzSrc="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"></nz-avatar>
</ng-template>
<ng-template #actionSetting>
<i class="anticon anticon-setting"></i>
</ng-template>
<ng-template #actionEdit>
<i class="anticon anticon-edit"></i>
</ng-template>
<ng-template #actionEllipsis>
<i class="anticon anticon-ellipsis"></i>
</ng-template>
`
})
export class NzDemoCardLoadingComponent {
loading = true;
toggleLoading(): void {
this.loading = !this.loading;
}
}
15 changes: 8 additions & 7 deletions components/card/nz-card.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ describe('card', () => {
fixture.detectChanges();
expect(card.nativeElement.classList).toContain('ant-card');
});
it('should loading work', () => {
const fixture = TestBed.createComponent(NzDemoCardLoadingComponent);
const card = fixture.debugElement.query(By.directive(NzCardComponent));
fixture.detectChanges();
expect(card.nativeElement.classList).toContain('ant-card-loading');
expect(card.nativeElement.querySelector('nz-card-loading').classList).toContain('ant-card-loading-content');
});
// this demo has changed to use nz-skeleton
// it('should loading work', () => {
// const fixture = TestBed.createComponent(NzDemoCardLoadingComponent);
// const card = fixture.debugElement.query(By.directive(NzCardComponent));
// fixture.detectChanges();
// expect(card.nativeElement.classList).toContain('ant-card-loading');
// expect(card.nativeElement.querySelector('nz-card-loading').classList).toContain('ant-card-loading-content');
// });
it('should grid work', () => {
const fixture = TestBed.createComponent(NzDemoCardGridCardComponent);
const card = fixture.debugElement.query(By.directive(NzCardComponent));
Expand Down
55 changes: 25 additions & 30 deletions components/skeleton/nz-skeleton.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
import { NzSkeletonAvatar, NzSkeletonParagraph, NzSkeletonTitle } from './nz-skeleton.type';
import { AvatarShape, NzSkeletonAvatar, NzSkeletonParagraph, NzSkeletonTitle } from './nz-skeleton.type';

@Component({
selector: 'nz-skeleton',
Expand All @@ -24,24 +24,28 @@ export class NzSkeletonComponent implements OnInit, OnChanges {
@Input() nzAvatar: NzSkeletonAvatar | boolean = false;
@Input() nzParagraph: NzSkeletonParagraph | boolean = true;

getTitleBasicProps(hasAvatar: boolean, hasParagraph: boolean): NzSkeletonTitle {
private getTitleBasicProps(): NzSkeletonTitle {
const hasAvatar: boolean = !!this.nzAvatar;
const hasParagraph: boolean = !!this.nzParagraph;
let width: string;
if (!hasAvatar && hasParagraph) {
return { width: '38%' };
}
if (hasAvatar && hasParagraph) {
return { width: '50%' };
width = '38%';
} else if (hasAvatar && hasParagraph) {
width = '50%';
} else {
width = '100%';
}
return { width: '100%' };
return { width, ...this.getProps(this.nzTitle) };
}

getAvatarBasicProps(hasTitle: boolean, hasParagraph: boolean): NzSkeletonAvatar {
if (hasTitle && !hasParagraph) {
return { shape: 'square' };
}
return { shape: 'circle' };
private getAvatarBasicProps(): NzSkeletonAvatar {
const shape: AvatarShape = (!!this.nzTitle && !this.nzParagraph) ? 'square' : 'circle';
return { shape, ...this.getProps(this.nzAvatar) };
}

getParagraphBasicProps(hasAvatar: boolean, hasTitle: boolean): NzSkeletonParagraph {
private getParagraphBasicProps(): NzSkeletonParagraph {
const hasAvatar: boolean = !!this.nzAvatar;
const hasTitle: boolean = !!this.nzTitle;
const basicProps: NzSkeletonParagraph = {};
// Width
if (hasAvatar && hasTitle) {
Expand All @@ -55,25 +59,25 @@ export class NzSkeletonComponent implements OnInit, OnChanges {
} else {
basicProps.rows = 2;
}
return basicProps;
return { ...basicProps, ...this.getProps(this.nzParagraph) };
}

getProps<T>(prop: T | boolean | undefined): T | {} {
private getProps<T>(prop: T | boolean | undefined): T | {} {
if (prop && typeof prop === 'object') {
return prop;
}
return {};
}

toCSSUnit(value: number | string = ''): string {
toCSSUnit(value: number | string = '100%'): string {
if (typeof value === 'number') {
return `${value}px`;
} else if (typeof value === 'string') {
return value;
}
}

getWidthList(): Array<number | string> {
private getWidthList(): Array<number | string> {
const { width, rows } = this.paragraph;
let widthList = [];
if (width && Array.isArray(width)) {
Expand All @@ -95,19 +99,10 @@ export class NzSkeletonComponent implements OnInit, OnChanges {
}

updateProps(): void {
this.title = {
...this.getTitleBasicProps(!!this.nzAvatar, !!this.nzParagraph),
...this.getProps(this.nzTitle)
};
this.avatar = {
...this.getAvatarBasicProps(!!this.nzTitle, !!this.nzParagraph),
...this.getProps(this.nzAvatar)
};
this.paragraph = {
...this.getParagraphBasicProps(!!this.nzAvatar, !!this.nzTitle),
...this.getProps(this.nzParagraph)
};
this.rowsList = [...Array(this.paragraph.rows)];
this.title = this.getTitleBasicProps();
this.avatar = this.getAvatarBasicProps();
this.paragraph = this.getParagraphBasicProps();
this.rowsList = [...Array(this.paragraph.rows)];
this.widthList = this.getWidthList();
}

Expand Down
10 changes: 5 additions & 5 deletions components/skeleton/nz-skeleton.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,26 @@ describe('skeleton', () => {
fixture.detectChanges();
let paragraphs = dl.nativeElement.querySelectorAll('.ant-skeleton-paragraph > li');
expect(paragraphs.length).toBe(2);
expect(paragraphs[0].style.width).toBe('');
expect(paragraphs[0].style.width).toBe('100%');
expect(paragraphs[1].style.width).toBe('100%');
testComp.nzAvatar = false;
fixture.detectChanges();
paragraphs = dl.nativeElement.querySelectorAll('.ant-skeleton-paragraph > li');
expect(paragraphs.length).toBe(3);
expect(paragraphs[1].style.width).toBe('');
expect(paragraphs[1].style.width).toBe('100%');
expect(paragraphs[2].style.width).toBe('61%');
});
it('should width type is string or number work', () => {
testComp.nzParagraph = { width: 100 };
fixture.detectChanges();
let paragraphs = dl.nativeElement.querySelectorAll('.ant-skeleton-paragraph > li');
expect(paragraphs[0].style.width).toBe('');
expect(paragraphs[0].style.width).toBe('100%');
expect(paragraphs[1].style.width).toBe('100px');
expect(paragraphs[2]).toBeFalsy();
testComp.nzParagraph = { width: 100, rows: 3 };
fixture.detectChanges();
paragraphs = dl.nativeElement.querySelectorAll('.ant-skeleton-paragraph > li');
expect(paragraphs[1].style.width).toBe('');
expect(paragraphs[1].style.width).toBe('100%');
expect(paragraphs[2].style.width).toBe('100px');
});
it('should define width type is Array work', () => {
Expand All @@ -125,7 +125,7 @@ describe('skeleton', () => {
fixture.detectChanges();
paragraphs = dl.nativeElement.querySelectorAll('.ant-skeleton-paragraph > li');
expect(paragraphs[2].style.width).toBe('90%');
expect(paragraphs[3].style.width).toBe('');
expect(paragraphs[3].style.width).toBe('100%');
});
});
});
Expand Down

0 comments on commit 24d3320

Please sign in to comment.