Skip to content
23 changes: 23 additions & 0 deletions projects/components/src/description/description.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
:host {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this doable without :host? we use it in some places, but it's a bad practice and we should try to get rid of it where possible (since the parent is generally responsible for styling the host, and will have greater precedence here)

Copy link
Contributor Author

@DmitiryPotychkin DmitiryPotychkin Jan 29, 2021

Choose a reason for hiding this comment

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

I'm try to do it without :host but for some reason style min-width: 0 for .description not affected parent component. Originally this min-width: 0 added for fix such case as on screenshots.

Without min-width: 0 for host
Screenshot from 2021-01-29 03-04-54
Add min-width: 0
Screenshot from 2021-01-29 03-04-43

anyway, I still work on this component and will try to figure way to fix this without using :host.

Copy link
Contributor

Choose a reason for hiding this comment

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

There's usually alternatives, but always the easiest to find. Don't spend too much time on it - if we need to use :host so be it.

min-width: 0;
}

.description {
display: flex;
}

.description-text {
margin-right: 6px;
}

.description-button {
cursor: pointer;
text-decoration: underline;
white-space: nowrap;
}

.truncated-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createHostFactory, Spectator } from '@ngneat/spectator/jest';
import { DescriptionComponent } from './description.component';

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

const createHost = createHostFactory<DescriptionComponent>({
component: DescriptionComponent,
shallow: true
});

test('should render the description', () => {
spectator = createHost('<ht-description [description] = "description"> </ht-description>', {
hostProps: {
description: 'Description text'
}
});
expect(spectator.query('.description')).toExist();
expect(spectator.query('.description-text')).toHaveText('Description text');
});

test('should show full description text if pressed button show more', () => {
spectator = createHost('<ht-description [description] = "description"> </ht-description>', {
hostProps: {
description: 'Description text'
}
});

expect(spectator.component.isDescriptionTextToggled).toBeFalsy();
spectator.component.toggleDescriptionText();
expect(spectator.component.isDescriptionTextToggled).toBeTruthy();
});
});
39 changes: 39 additions & 0 deletions projects/components/src/description/description.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

@Component({
selector: 'ht-description',
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrls: ['./description.component.scss'],
template: `
<div class="description">
<div
class="description-text"
[ngClass]="{ 'truncated-text': !this.isDescriptionTextToggled }"
data-sensitive-pii
#eventDescription
>
{{ description }}
<span *ngIf="this.isDescriptionTextToggled" (click)="this.toggleDescriptionText()" class="description-button"
>show less</span
>
</div>
<div
class="description-button"
*ngIf="eventDescription.offsetWidth < eventDescription.scrollWidth && !this.isDescriptionTextToggled"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also I found this ngIf not recalculate on window resize, I tried to use @ViewChild decorator and several other ways but ref always return undefined, so it's another one issue I working on

Copy link
Contributor

Choose a reason for hiding this comment

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

One option here is to use out layout change directive, which should give you a callback when the layout changes - either from a window resize, or a shift in other components. An example of this can be seen in LabelComponent

(click)="this.toggleDescriptionText()"
>
show more
</div>
</div>
`
})
export class DescriptionComponent {
public isDescriptionTextToggled: boolean = false;

@Input()
public description!: string;

public toggleDescriptionText(): void {
this.isDescriptionTextToggled = !this.isDescriptionTextToggled;
}
}
10 changes: 10 additions & 0 deletions projects/components/src/description/description.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DescriptionComponent } from './description.component';

@NgModule({
imports: [CommonModule],
declarations: [DescriptionComponent],
exports: [DescriptionComponent]
})
export class DescriptionModule {}
4 changes: 4 additions & 0 deletions projects/components/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,7 @@ export * from './confirmation/confirmation.service';
// Notification
export * from './notification/notification.service';
export * from './notification/notification.module';

// Description
export * from './description/description.component';
export * from './description/description.module';