Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module:avatar): calculate size at the right time #8754

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions components/avatar/avatar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import { PlatformModule } from '@angular/cdk/platform';
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Expand All @@ -16,6 +15,7 @@ import {
Output,
ViewChild,
ViewEncapsulation,
afterRender,
numberAttribute
} from '@angular/core';

Expand All @@ -33,11 +33,9 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'avatar';
template: `
@if (nzIcon && hasIcon) {
<span nz-icon [nzType]="nzIcon"></span>
}
@if (nzSrc && hasSrc) {
} @else if (nzSrc && hasSrc) {
<img [src]="nzSrc" [attr.srcset]="nzSrcSet" [attr.alt]="nzAlt" (error)="imgError($event)" />
}
@if (nzText && hasText) {
} @else if (nzText && hasText) {
<span class="ant-avatar-string" #textEl>{{ nzText }}</span>
}
`,
Expand All @@ -59,7 +57,7 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'avatar';
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None
})
export class NzAvatarComponent implements OnChanges, AfterViewInit {
export class NzAvatarComponent implements OnChanges {
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
@Input() @WithConfig() nzShape: NzShapeSCType = 'circle';
@Input() @WithConfig() nzSize: NzSizeLDSType | number = 'default';
Expand All @@ -85,7 +83,9 @@ export class NzAvatarComponent implements OnChanges, AfterViewInit {
public nzConfigService: NzConfigService,
private elementRef: ElementRef,
private cdr: ChangeDetectorRef
) {}
) {
afterRender(() => this.calcStringSize());
}

imgError($event: Event): void {
this.nzError.emit($event);
Expand Down Expand Up @@ -113,10 +113,6 @@ export class NzAvatarComponent implements OnChanges, AfterViewInit {
this.calcStringSize();
}

ngAfterViewInit(): void {
this.calcStringSize();
}

private calcStringSize(): void {
if (!this.hasText || !this.textEl) {
return;
Expand Down
6 changes: 3 additions & 3 deletions components/avatar/avatar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('avatar', () => {
context.nzText = 'LongUsername';
fixture.detectChanges();
tick();
context.comp.ngAfterViewInit();
context.comp['calcStringSize']();
const scale = getScaleFromCSSTransform(dl.nativeElement.querySelector('.ant-avatar-string')!.style.transform!);
expect(scale).toBeLessThan(1);
}));
Expand All @@ -150,7 +150,7 @@ describe('avatar', () => {
fixture.detectChanges();
tick();
avatarText = dl.nativeElement.querySelector('.ant-avatar-string')!;
context.comp.ngAfterViewInit();
context.comp['calcStringSize']();
firstScale = getScaleFromCSSTransform(avatarText.style.transform);
}));

Expand Down Expand Up @@ -236,7 +236,7 @@ describe('avatar', () => {
fixture.detectChanges();
flush();
const textEl = document.querySelector<HTMLElement>('.ant-avatar-string')!;
context.comp.ngAfterViewInit();
context.comp['calcStringSize']();
const scale = getScaleFromCSSTransform(textEl.style.transform);
expect(scale).toBeLessThan(1);
expect(textEl.style.lineHeight).toEqual(`${size}px`);
Expand Down