Skip to content

Commit e7b1fa0

Browse files
fix(module:avatar): avatar not re-scaling properly (#8365)
* fix(module:avatar): avatar not rescaling properly * fix(module:avatar): avatar not rescaling properly * fix(module:avatar): avatar not rescaling properly
1 parent e8dea7a commit e7b1fa0

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

components/avatar/avatar.component.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6-
import { Platform, PlatformModule } from '@angular/cdk/platform';
6+
import { PlatformModule } from '@angular/cdk/platform';
77
import {
8+
AfterViewInit,
89
ChangeDetectionStrategy,
910
ChangeDetectorRef,
1011
Component,
1112
ElementRef,
1213
EventEmitter,
1314
Input,
14-
NgZone,
1515
OnChanges,
1616
Output,
1717
ViewChild,
@@ -59,7 +59,7 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'avatar';
5959
changeDetection: ChangeDetectionStrategy.OnPush,
6060
encapsulation: ViewEncapsulation.None
6161
})
62-
export class NzAvatarComponent implements OnChanges {
62+
export class NzAvatarComponent implements OnChanges, AfterViewInit {
6363
static ngAcceptInputType_nzGap: NumberInput;
6464

6565
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
@@ -86,9 +86,7 @@ export class NzAvatarComponent implements OnChanges {
8686
constructor(
8787
public nzConfigService: NzConfigService,
8888
private elementRef: ElementRef,
89-
private cdr: ChangeDetectorRef,
90-
private platform: Platform,
91-
private ngZone: NgZone
89+
private cdr: ChangeDetectorRef
9290
) {}
9391

9492
imgError($event: Event): void {
@@ -104,7 +102,7 @@ export class NzAvatarComponent implements OnChanges {
104102
}
105103
this.cdr.detectChanges();
106104
this.setSizeStyle();
107-
this.notifyCalc();
105+
this.calcStringSize();
108106
}
109107
}
110108

@@ -114,15 +112,19 @@ export class NzAvatarComponent implements OnChanges {
114112
this.hasSrc = !!this.nzSrc;
115113

116114
this.setSizeStyle();
117-
this.notifyCalc();
115+
this.calcStringSize();
116+
}
117+
118+
ngAfterViewInit(): void {
119+
this.calcStringSize();
118120
}
119121

120122
private calcStringSize(): void {
121-
if (!this.hasText) {
123+
if (!this.hasText || !this.textEl) {
122124
return;
123125
}
124126

125-
const textEl = this.textEl!.nativeElement;
127+
const textEl = this.textEl.nativeElement;
126128
const childrenWidth = textEl.offsetWidth;
127129
const avatarWidth = this.el.getBoundingClientRect().width;
128130
const offset = this.nzGap * 2 < avatarWidth ? this.nzGap * 2 : 8;
@@ -132,23 +134,13 @@ export class NzAvatarComponent implements OnChanges {
132134
textEl.style.lineHeight = this.customSize || '';
133135
}
134136

135-
private notifyCalc(): void {
136-
// If use ngAfterViewChecked, always demands more computations, so......
137-
if (this.platform.isBrowser) {
138-
this.ngZone.runOutsideAngular(() => {
139-
setTimeout(() => {
140-
this.calcStringSize();
141-
});
142-
});
143-
}
144-
}
145-
146137
private setSizeStyle(): void {
147138
if (typeof this.nzSize === 'number') {
148139
this.customSize = `${this.nzSize}px`;
149140
} else {
150141
this.customSize = null;
151142
}
143+
152144
this.cdr.markForCheck();
153145
}
154146
}

components/avatar/avatar.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ describe('avatar', () => {
136136
context.nzText = 'LongUsername';
137137
fixture.detectChanges();
138138
tick();
139+
context.comp.ngAfterViewInit();
139140
const scale = getScaleFromCSSTransform(dl.nativeElement.querySelector('.ant-avatar-string')!.style.transform!);
140141
expect(scale).toBeLessThan(1);
141142
}));
@@ -149,6 +150,7 @@ describe('avatar', () => {
149150
fixture.detectChanges();
150151
tick();
151152
avatarText = dl.nativeElement.querySelector('.ant-avatar-string')!;
153+
context.comp.ngAfterViewInit();
152154
firstScale = getScaleFromCSSTransform(avatarText.style.transform);
153155
}));
154156

@@ -234,6 +236,7 @@ describe('avatar', () => {
234236
fixture.detectChanges();
235237
flush();
236238
const textEl = document.querySelector<HTMLElement>('.ant-avatar-string')!;
239+
context.comp.ngAfterViewInit();
237240
const scale = getScaleFromCSSTransform(textEl.style.transform);
238241
expect(scale).toBeLessThan(1);
239242
expect(textEl.style.lineHeight).toEqual(`${size}px`);

0 commit comments

Comments
 (0)