-
Notifications
You must be signed in to change notification settings - Fork 11
Add ht-description component #543
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
Changes from all commits
7897d76
16dd167
6986dac
09afd65
aeb9df5
8f6040a
c84c1b0
9fd514c
58aa4fc
d0db5bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
:host { | ||
min-width: 0; | ||
} | ||
|
||
.description { | ||
display: flex; | ||
} | ||
|
||
.description-button { | ||
cursor: pointer; | ||
text-decoration: underline; | ||
white-space: nowrap; | ||
&-more { | ||
margin-left: 5px; | ||
} | ||
} | ||
|
||
.truncated-text { | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createHostFactory, Spectator } from '@ngneat/spectator/jest'; | ||
import { DescriptionComponent } from './description.component'; | ||
|
||
describe('Description 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(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
AfterViewInit, | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
ElementRef, | ||
Input, | ||
OnChanges, | ||
ViewChild | ||
} from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'ht-description', | ||
styleUrls: ['./description.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
template: ` | ||
<div class="description" (htLayoutChange)="this.remeasure()" #eventDescriptionContainer> | ||
<div | ||
class="description-text" | ||
[ngClass]="{ 'truncated-text': !isDescriptionTextToggled }" | ||
data-sensitive-pii | ||
#eventDescriptionText | ||
> | ||
{{ description }} | ||
<span | ||
*ngIf="isDescriptionTruncated && isDescriptionTextToggled" | ||
(click)="toggleDescriptionText()" | ||
class="description-button" | ||
>show less</span | ||
> | ||
</div> | ||
<div | ||
class="description-button description-button-more" | ||
*ngIf="isDescriptionTruncated && !isDescriptionTextToggled" | ||
(click)="toggleDescriptionText()" | ||
> | ||
show more | ||
</div> | ||
</div> | ||
` | ||
}) | ||
export class DescriptionComponent implements OnChanges, AfterViewInit { | ||
public isInitialized: boolean = false; | ||
public isDescriptionTextToggled: boolean = false; | ||
public isDescriptionTruncated!: boolean; | ||
|
||
@ViewChild('eventDescriptionText', { read: ElementRef }) | ||
public readonly eventDescriptionText!: ElementRef; | ||
|
||
@ViewChild('eventDescriptionContainer', { read: ElementRef }) | ||
public readonly eventDescriptionContainer!: ElementRef; | ||
|
||
@Input() | ||
public description!: string; | ||
|
||
public constructor(private readonly cdRef: ChangeDetectorRef) {} | ||
|
||
public ngOnChanges(): void { | ||
if (this.isInitialized) { | ||
this.remeasure(); | ||
} | ||
} | ||
|
||
public ngAfterViewInit(): void { | ||
this.isInitialized = true; | ||
this.remeasure(); | ||
} | ||
|
||
public toggleDescriptionText(): void { | ||
this.isDescriptionTextToggled = !this.isDescriptionTextToggled; | ||
} | ||
|
||
public remeasure(): void { | ||
this.isDescriptionTextToggled = false; | ||
this.cdRef.detectChanges(); | ||
|
||
this.isDescriptionTruncated = | ||
this.eventDescriptionContainer.nativeElement.offsetWidth < this.eventDescriptionText.nativeElement.scrollWidth; | ||
this.cdRef.detectChanges(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only run once, at the end of the remeasure method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will cause bug, value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm - that's one of the weird parts about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I try to use |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
import { LayoutChangeModule } from '../layout/layout-change.module'; | ||
import { DescriptionComponent } from './description.component'; | ||
|
||
@NgModule({ | ||
imports: [CommonModule, LayoutChangeModule], | ||
declarations: [DescriptionComponent], | ||
exports: [DescriptionComponent] | ||
}) | ||
export class DescriptionModule {} |
There was a problem hiding this comment.
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)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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


Add min-width: 0
anyway, I still work on this component and will try to figure way to fix this without using :host.
There was a problem hiding this comment.
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.