src/app/shared/components/tag-list/tag-list.component.ts
A list of removable tags
changeDetection | ChangeDetectionStrategy.OnPush |
selector | ccf-tag-list |
styleUrls | ./tag-list.component.scss |
templateUrl | ./tag-list.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
HostBindings |
constructor(ga: GoogleAnalyticsService)
|
||||||||
Creates an instance of tag list component.
Parameters :
|
tags | |
Type : Tag[]
|
|
The tags |
tagRemoved | |
Type : EventEmitter
|
|
Emits when a tag is removed |
tagsChange | |
Type : EventEmitter
|
|
Emits the new array of tags when a tag has been removed |
class |
Type : "ccf-tag-list"
|
Default value : 'ccf-tag-list'
|
HTML class name |
removeTag | ||||||||
removeTag(tag: Tag)
|
||||||||
Removes a tag from the list
Parameters :
Returns :
void
|
tagClasses | ||||||
tagClasses(tag: Tag)
|
||||||
Parameters :
Returns :
string[]
|
tagId |
tagId(_index: number, tag: Tag)
|
Gets the unique identifier for a tag |
Readonly clsName |
Type : string
|
Default value : 'ccf-tag-list'
|
Decorators :
@HostBinding('class')
|
HTML class name |
import { ChangeDetectionStrategy, Component, EventEmitter, HostBinding, Input, Output } from '@angular/core';
import { GoogleAnalyticsService } from 'ngx-google-analytics';
import { Tag } from '../../../core/models/anatomical-structure-tag';
/**
* A list of removable tags
*/
@Component({
selector: 'ccf-tag-list',
templateUrl: './tag-list.component.html',
styleUrls: ['./tag-list.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TagListComponent {
/** HTML class name */
@HostBinding('class') readonly clsName = 'ccf-tag-list';
/**
* The tags
*/
@Input() tags!: Tag[];
/**
* Emits when a tag is removed
*/
@Output() readonly tagRemoved = new EventEmitter<Tag>();
/**
* Emits the new array of tags when a tag has been removed
*/
@Output() readonly tagsChange = new EventEmitter<Tag[]>();
/**
* Creates an instance of tag list component.
*
* @param ga Analytics service
*/
constructor(private readonly ga: GoogleAnalyticsService) {}
/**
* Gets the unique identifier for a tag
*
* @param _index Unused
* @param tag A tag
* @returns An identifier
*/
tagId(_index: number, tag: Tag): unknown {
return tag.id;
}
tagClasses(tag: Tag): string[] {
return tag.type === 'added' ? ['added'] : ['assigned'];
}
/**
* Removes a tag from the list
*
* @param tag Tag to remove
*/
removeTag(tag: Tag): void {
this.tags = this.tags.filter((obj) => obj !== tag);
this.ga.event('tag_removed', 'tag_list', tag.label);
this.tagRemoved.emit(tag);
this.tagsChange.emit(this.tags);
}
}
<mat-chip-listbox selectable="false">
<mat-chip
*ngFor="let tag of tags; trackBy: tagId"
removable
disableRipple
[class]="tagClasses(tag)"
(removed)="removeTag(tag)"
>
{{ tag.label }}
<mat-icon matChipRemove class="icon remove">cancel</mat-icon>
</mat-chip>
</mat-chip-listbox>
./tag-list.component.scss
:host {
display: block;
overflow-x: hidden;
overflow-y: auto;
::ng-deep .mdc-evolution-chip-set__chips {
margin: 0 !important;
.mdc-evolution-chip__text-label {
font-size: 14px;
}
}
}