Skip to content
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createHostFactory, Spectator } from '@ngneat/spectator/jest';
import { DescriptionComponent } from './description.component';

describe('Divider Component', () => {
describe('Description Component', () => {
let spectator: Spectator<DescriptionComponent>;

const createHost = createHostFactory<DescriptionComponent>({
Expand All @@ -15,6 +15,7 @@ describe('Divider Component', () => {
description: 'Description text'
}
});

expect(spectator.query('.description')).toExist();
expect(spectator.query('.description-text')).toHaveText('Description text');
});
Expand Down
29 changes: 16 additions & 13 deletions projects/components/src/description/description.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Input,
ViewChild,
AfterViewInit,
OnChanges,
ElementRef
ViewChild
} from '@angular/core';
import { Subject } from 'rxjs';

@Component({
selector: 'ht-description',
Expand All @@ -23,15 +23,15 @@ import { Subject } from 'rxjs';
>
{{ description }}
<span
*ngIf="(isDescriptionTruncated$ | async) && isDescriptionTextToggled"
*ngIf="isDescriptionTruncated && isDescriptionTextToggled"
(click)="toggleDescriptionText()"
class="description-button"
>show less</span
>
</div>
<div
class="description-button description-button-more"
*ngIf="(isDescriptionTruncated$ | async) && !isDescriptionTextToggled"
*ngIf="isDescriptionTruncated && !isDescriptionTextToggled"
(click)="toggleDescriptionText()"
>
show more
Expand All @@ -42,8 +42,7 @@ import { Subject } from 'rxjs';
export class DescriptionComponent implements OnChanges, AfterViewInit {
public isInitialized: boolean = false;
public isDescriptionTextToggled: boolean = false;
public readonly isDescriptionTruncated$: Subject<boolean> = new Subject();
public fullDescriptionTextWidth!: number;
public isDescriptionTruncated!: boolean;

@ViewChild('eventDescriptionText', { read: ElementRef })
public readonly eventDescriptionText!: ElementRef;
Expand All @@ -54,14 +53,15 @@ export class DescriptionComponent implements OnChanges, AfterViewInit {
@Input()
public description!: string;

public constructor(private readonly cdRef: ChangeDetectorRef) {}

public ngOnChanges(): void {
if (this.isInitialized) {
this.remeasure();
}
}

ngAfterViewInit() {
this.fullDescriptionTextWidth = this.eventDescriptionText.nativeElement.scrollWidth;
public ngAfterViewInit(): void {
this.isInitialized = true;
this.remeasure();
}
Expand All @@ -71,8 +71,11 @@ export class DescriptionComponent implements OnChanges, AfterViewInit {
}

public remeasure(): void {
this.isDescriptionTruncated$.next(
this.eventDescriptionContainer.nativeElement.offsetWidth < this.fullDescriptionTextWidth
);
this.isDescriptionTextToggled = false;
this.cdRef.detectChanges();

this.isDescriptionTruncated =
this.eventDescriptionContainer.nativeElement.offsetWidth < this.eventDescriptionText.nativeElement.scrollWidth;
this.cdRef.detectChanges();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only run once, at the end of the remeasure method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will cause bug, value of isDescriptionTruncated will be wrong as we try to get this.eventDescriptionText.nativeElement.scrollWidth before it truncated and DOM changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm - that's one of the weird parts about using detectChanges explicitly instead of markForCheck (which just invalidates this node in the change detection tree to be picked up by angular's general change detection, which should ensure that the changes settle. Changing that here may be better - could you try that out, and if it's not working we'll go with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to use markForCheck method but unfortunately it not work correctly

}
}
4 changes: 2 additions & 2 deletions projects/components/src/description/description.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DescriptionComponent } from './description.component';
import { NgModule } from '@angular/core';
import { LayoutChangeModule } from '../layout/layout-change.module';
import { DescriptionComponent } from './description.component';

@NgModule({
imports: [CommonModule, LayoutChangeModule],
Expand Down