File
Implements
Metadata
selector |
app-picture-tag-input |
styleUrls |
./picture-tag-input.component.less |
templateUrl |
./picture-tag-input.component.html |
Index
Properties
|
|
Methods
|
|
Inputs
|
|
Outputs
|
|
tagString
|
Default value : ''
|
|
Methods
handleTagClose
|
handleTagClose(removedTag: literal type)
|
|
Parameters :
Name |
Type |
Optional |
removedTag |
literal type
|
No
|
|
handleTagConfirm
|
handleTagConfirm(evName: string)
|
|
Parameters :
Name |
Type |
Optional |
evName |
string
|
No
|
|
showTagInput
|
showTagInput()
|
|
|
sliceTagName
|
sliceTagName(tag: string)
|
|
Parameters :
Name |
Type |
Optional |
tag |
string
|
No
|
|
Optional
inputElement
|
Type : ElementRef
|
Decorators :
@ViewChild('inputElement', {static: false})
|
|
inputTagValue
|
Type : string
|
Default value : ''
|
|
inputVisible
|
Default value : false
|
|
tags
|
Type : string[]
|
Default value : []
|
|
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
@Component({
selector: 'app-picture-tag-input',
templateUrl: './picture-tag-input.component.html',
styleUrls: ['./picture-tag-input.component.less']
})
export class PictureTagInputComponent implements OnInit {
// tags
@Input() tagString = '';
tags: string[] = [];
// changed
@Output() changeEvent = new EventEmitter();
inputVisible = false;
inputTagValue = '';
@ViewChild('inputElement', { static: false }) inputElement?: ElementRef;
constructor() { }
handleTagClose(removedTag: {}) {
this.tags = this.tags.filter(tag => tag !== removedTag);
this.changeEvent.emit(this.tags.join('#'));
return false;
}
sliceTagName(tag: string): string {
const isLongTag = tag.length > 20;
return isLongTag ? `${tag.slice(0, 20)}...` : tag;
}
showTagInput(): void {
this.inputVisible = true;
setTimeout(() => {
this.inputElement?.nativeElement.focus();
}, 10);
}
handleTagConfirm(evName: string) {
const value = this.inputTagValue.trim();
if (value && this.tags.indexOf(value) === -1) {
this.tags = [...this.tags, value];
}
this.inputTagValue = '';
this.inputVisible = false;
if (evName === 'enter') {
this.showTagInput();
}
this.changeEvent.emit(this.tags.join('#'));
return false;
}
ngOnInit(): void {
if (this.tagString) {
this.tags = this.tagString.split('#');
}
}
}
<nz-tag *ngFor="let tag of tags; let i = index" [nzMode]="'closeable'" (nzOnClose)="handleTagClose(tag)"
[nzColor]="'#2db7f5'">
{{ sliceTagName(tag) }}
</nz-tag>
<nz-tag *ngIf="!inputVisible" class="editable-tag" (click)="showTagInput()" [nzColor]="'error'">
<i nz-icon nzType="plus"></i>
关键词
</nz-tag>
<input #inputElement nz-input nzSize="small" *ngIf="inputVisible" type="text" [(ngModel)]="inputTagValue"
style="width: 78px;" (blur)="handleTagConfirm('blue')" (keydown.enter)="handleTagConfirm('enter')" />
.editable-tag {
background: rgb(255, 255, 255);
border-style: dashed;
}
Legend
Html element with directive