Skip to content

Commit

Permalink
refactor(module:avatar): refactor avatar
Browse files Browse the repository at this point in the history
ref: #2381
  • Loading branch information
hsuanxyz committed Nov 30, 2018
1 parent cc75c9a commit 365a0d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
6 changes: 3 additions & 3 deletions components/avatar/avatar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ describe('avatar', () => {
context.nzText = 'a';
fixture.detectChanges();
tick();
const scale = +dl.nativeElement.querySelector('.ant-avatar-string').style.transform.replace(/[^\.0-9]/ig, '');
expect(scale).toBe(0);
const scale = +/(\w+)\(([^)]*)\)/g.exec(dl.nativeElement.querySelector('.ant-avatar-string').style.transform)[2];
expect(scale).toBe(1);
}));
it('should be autoset font-size', fakeAsync(() => {
context.nzText = 'LongUsername';
fixture.detectChanges();
tick();
const scale = +dl.nativeElement.querySelector('.ant-avatar-string').style.transform.replace(/[^\.0-9]/ig, '');
const scale = +/(\w+)\(([^)]*)\)/g.exec(dl.nativeElement.querySelector('.ant-avatar-string').style.transform)[2];
expect(scale).toBeLessThan(1);
}));
});
Expand Down
45 changes: 19 additions & 26 deletions components/avatar/nz-avatar.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
Attribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Input,
OnChanges, Renderer2,
OnChanges,
Renderer2,
SimpleChanges,
ViewChild
ViewChild,
ViewEncapsulation
} from '@angular/core';

import { NzUpdateHostClassService } from '../core/services/update-host-class.service';
Expand All @@ -21,21 +22,18 @@ export type NzAvatarSize = NzSizeLDSType | number;
templateUrl : './nz-avatar.component.html',
providers : [ NzUpdateHostClassService ],
preserveWhitespaces: false,
changeDetection : ChangeDetectionStrategy.OnPush
changeDetection : ChangeDetectionStrategy.OnPush,
encapsulation : ViewEncapsulation.None

})
export class NzAvatarComponent implements OnChanges {

@Input()
set nzIcon(value: string) {
if (value) {
this.oldAPIIcon = value.indexOf('anticon') > -1;
}
this._icon = value;
}
@Input() nzShape: NzAvatarShape = 'circle';
@Input() nzSize: NzAvatarSize = 'default';
@Input() nzText: string;
@Input() nzSrc: string;
@Input() nzIcon: string;

get nzIcon(): string {
return this._icon;
}
oldAPIIcon = true; // Make the user defined icon compatible to old API. Should be removed in 2.0.
hasText: boolean = false;
hasSrc: boolean = true;
Expand All @@ -44,24 +42,16 @@ export class NzAvatarComponent implements OnChanges {

@ViewChild('textEl') textEl: ElementRef;

@Input() nzShape: NzAvatarShape = 'circle';

@Input() nzSize: NzAvatarSize = 'default';

@Input() nzText: string;

@Input() nzSrc: string;
private el: HTMLElement = this.elementRef.nativeElement;
private prefixCls = 'ant-avatar';
private sizeMap = { large: 'lg', small: 'sm' };

constructor(
private elementRef: ElementRef,
private cd: ChangeDetectorRef,
private updateHostClassService: NzUpdateHostClassService,
private renderer: Renderer2) {
private renderer: Renderer2) {
}
private el: HTMLElement = this.elementRef.nativeElement;
private prefixCls = 'ant-avatar';
private sizeMap = { large: 'lg', small: 'sm' };
private _icon: string;

setClass(): this {
const classMap = {
Expand Down Expand Up @@ -90,6 +80,9 @@ export class NzAvatarComponent implements OnChanges {
}

ngOnChanges(changes: SimpleChanges): void {
if (changes.hasOwnProperty('nzIcon') && changes.nzIcon.currentValue) {
this.oldAPIIcon = changes.nzIcon.currentValue.indexOf('anticon') > -1;
}
this.hasText = !this.nzSrc && !!this.nzText;
this.hasIcon = !this.nzSrc && !!this.nzIcon;
this.hasSrc = !!this.nzSrc;
Expand Down

0 comments on commit 365a0d2

Please sign in to comment.