Skip to content

Commit 3a5ba37

Browse files
fix(module:avatar): calculate size at the right time (#8754)
1 parent 5fec53e commit 3a5ba37

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

components/avatar/avatar.component.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import { PlatformModule } from '@angular/cdk/platform';
77
import {
8-
AfterViewInit,
98
ChangeDetectionStrategy,
109
ChangeDetectorRef,
1110
Component,
@@ -16,6 +15,7 @@ import {
1615
Output,
1716
ViewChild,
1817
ViewEncapsulation,
18+
afterRender,
1919
numberAttribute
2020
} from '@angular/core';
2121

@@ -33,11 +33,9 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'avatar';
3333
template: `
3434
@if (nzIcon && hasIcon) {
3535
<span nz-icon [nzType]="nzIcon"></span>
36-
}
37-
@if (nzSrc && hasSrc) {
36+
} @else if (nzSrc && hasSrc) {
3837
<img [src]="nzSrc" [attr.srcset]="nzSrcSet" [attr.alt]="nzAlt" (error)="imgError($event)" />
39-
}
40-
@if (nzText && hasText) {
38+
} @else if (nzText && hasText) {
4139
<span class="ant-avatar-string" #textEl>{{ nzText }}</span>
4240
}
4341
`,
@@ -59,7 +57,7 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'avatar';
5957
changeDetection: ChangeDetectionStrategy.OnPush,
6058
encapsulation: ViewEncapsulation.None
6159
})
62-
export class NzAvatarComponent implements OnChanges, AfterViewInit {
60+
export class NzAvatarComponent implements OnChanges {
6361
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
6462
@Input() @WithConfig() nzShape: NzShapeSCType = 'circle';
6563
@Input() @WithConfig() nzSize: NzSizeLDSType | number = 'default';
@@ -85,7 +83,9 @@ export class NzAvatarComponent implements OnChanges, AfterViewInit {
8583
public nzConfigService: NzConfigService,
8684
private elementRef: ElementRef,
8785
private cdr: ChangeDetectorRef
88-
) {}
86+
) {
87+
afterRender(() => this.calcStringSize());
88+
}
8989

9090
imgError($event: Event): void {
9191
this.nzError.emit($event);
@@ -113,10 +113,6 @@ export class NzAvatarComponent implements OnChanges, AfterViewInit {
113113
this.calcStringSize();
114114
}
115115

116-
ngAfterViewInit(): void {
117-
this.calcStringSize();
118-
}
119-
120116
private calcStringSize(): void {
121117
if (!this.hasText || !this.textEl) {
122118
return;

components/avatar/avatar.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('avatar', () => {
137137
context.nzText = 'LongUsername';
138138
fixture.detectChanges();
139139
tick();
140-
context.comp.ngAfterViewInit();
140+
context.comp['calcStringSize']();
141141
const scale = getScaleFromCSSTransform(dl.nativeElement.querySelector('.ant-avatar-string')!.style.transform!);
142142
expect(scale).toBeLessThan(1);
143143
}));
@@ -151,7 +151,7 @@ describe('avatar', () => {
151151
fixture.detectChanges();
152152
tick();
153153
avatarText = dl.nativeElement.querySelector('.ant-avatar-string')!;
154-
context.comp.ngAfterViewInit();
154+
context.comp['calcStringSize']();
155155
firstScale = getScaleFromCSSTransform(avatarText.style.transform);
156156
}));
157157

@@ -237,7 +237,7 @@ describe('avatar', () => {
237237
fixture.detectChanges();
238238
flush();
239239
const textEl = document.querySelector<HTMLElement>('.ant-avatar-string')!;
240-
context.comp.ngAfterViewInit();
240+
context.comp['calcStringSize']();
241241
const scale = getScaleFromCSSTransform(textEl.style.transform);
242242
expect(scale).toBeLessThan(1);
243243
expect(textEl.style.lineHeight).toEqual(`${size}px`);

0 commit comments

Comments
 (0)