AW-3836 fixed tags component
Some checks failed
FarmMaps.Develop/FarmMapsLib/pipeline/head There was a failure building this commit

This commit is contained in:
Mark van der Wal 2022-06-06 12:45:23 +02:00
parent 57e0a37c78
commit 0c4259d72e
2 changed files with 133 additions and 107 deletions

View File

@ -1,3 +1,11 @@
<div class="tags"> <div class="tags">
<span class="tag rounded bg-primary text-white" *ngFor="let tag of tags;"><span>{{tag}}</span> <i (click)="handleDeleteTag(tag)" class="fal fa-times" aria-hidden="true"></i></span><input type="text" #taginput (blur)="handleAddTag($event)" (keyup)="handleCheckAddTag($event)" [(ngModel)]="tag" [ngbTypeahead]="findTag" (selectItem)="handleSelect($event)" placeholder="New tag"/> <span class="tag rounded bg-primary text-white" *ngFor="let tag of tags;"><span>{{tag}}</span> <i
(click)="handleDeleteTag(tag)" class="fal fa-times" aria-hidden="true"></i></span><input
type="text" #tagInputElement
(blur)="handleBlur($event, false)"
(keyup)="handleKeyUp($event)"
[(ngModel)]="tag"
[ngbTypeahead]="findTag"
(selectItem)="handleSelect($event)"
placeholder="New tag"/>
</div> </div>

View File

@ -1,8 +1,9 @@
import { Component, Input, forwardRef,ElementRef,ViewChild } from '@angular/core'; import {Component, ElementRef, forwardRef, Input, ViewChild} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR,NgModel } from '@angular/forms'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import { Observable,of } from 'rxjs'; import {Observable, of} from 'rxjs';
import { tap,catchError,debounceTime,distinctUntilChanged,switchMap } from 'rxjs/operators' import {catchError, debounceTime, distinctUntilChanged, switchMap, tap} from 'rxjs/operators';
import { TypeaheadService } from '../../services/typeahead.service'; import {TypeaheadService} from '@farmmaps/common';
import {NgbTypeahead} from '@ng-bootstrap/ng-bootstrap';
@Component({ @Component({
selector: 'fm-tag-input', selector: 'fm-tag-input',
@ -17,9 +18,11 @@ import { TypeaheadService } from '../../services/typeahead.service';
] ]
}) })
export class TagInputComponent implements ControlValueAccessor { export class TagInputComponent implements ControlValueAccessor {
@Input() tags: string[] @Input() tags: string[] = [];
@ViewChild('taginput', { static: true }) tagInputElement: ElementRef; @ViewChild('tagInputElement', {static: true}) tagInputElement: ElementRef;
@ViewChild(NgbTypeahead, {static: true}) typeahead: NgbTypeahead;
public tag: string; public tag: string;
searching = false; searching = false;
searchFailed = false; searchFailed = false;
@ -28,39 +31,52 @@ export class TagInputComponent implements ControlValueAccessor {
} }
tagExists(tag) { tagExists(tag) {
if (tag.length == 0) return true; if (tag.length === 0) {
for (let t of this.tags) { return true;
if (t.toLowerCase() == tag.toLowerCase()) return true; }
for (const t of this.tags) {
if (t.toLowerCase() === tag.toLowerCase()) {
return true;
}
} }
return false; return false;
} }
addTag(tag, keepFocus = true) {
if (!this.tagExists(tag)) {
this.tags.push(tag);
this.propagateChange(this.tags);
}
this.tag = '';
if (keepFocus) {
this.tagInputElement.nativeElement.focus();
}
}
handleDeleteTag(tag) { handleDeleteTag(tag) {
let tags = []; const tags = [];
for (let t of this.tags) { for (const t of this.tags) {
if (t != tag) tags.push(t); if (t !== tag) {
tags.push(t);
}
} }
this.tags = tags; this.tags = tags;
this.propagateChange(tags); this.propagateChange(tags);
} }
handleAddTag(event) { handleBlur(event, keepFocus = true) {
if (!this.tagExists(this.tag)) { if (!this.typeahead.isPopupOpen()) {
this.tags.push(this.tag); this.addTag(this.tag, keepFocus);
this.propagateChange(this.tags); } else {
this.tag = '';
} }
this.tag = "";
this.tagInputElement.nativeElement.focus();
} }
handleCheckAddTag(event: KeyboardEvent) { handleKeyUp(event: KeyboardEvent) {
if (event.keyCode == 188) { if (event.keyCode === 188) {
let tag = this.tag.substr(0, this.tag.length - 1); // strip , const tag = this.tag.substr(0, this.tag.length - 1); // strip ,
if (!this.tagExists(tag)) { this.addTag(tag);
this.tags.push(tag);
this.propagateChange(this.tags);
}
this.tag = "";
} }
} }
@ -70,35 +86,37 @@ export class TagInputComponent implements ControlValueAccessor {
this.propagateChange(this.tags); this.propagateChange(this.tags);
} }
event.preventDefault(); event.preventDefault();
this.tag = ""; this.tag = '';
} }
propagateChange = (_: any) => { }; propagateChange = (_: any) => {
}
registerOnChange(fn) { registerOnChange(fn) {
this.propagateChange = fn; this.propagateChange = fn;
} }
findTag = (text$: Observable<string>) => findTag = (text$: Observable<string>) =>
text$.pipe( text$.pipe(
debounceTime(200), distinctUntilChanged(),
distinctUntilChanged(), debounceTime(200),
tap(() => this.searching = true), tap(() => this.searching = true),
switchMap(term => term.length < 1 ? of([]) : switchMap(term => term.length < 1 ? of([]) :
this.typeaheadService.getTagTypeaheadItems(term).pipe( this.typeaheadService.getTagTypeaheadItems(term).pipe(
tap(() => this.searchFailed = false), tap(() => this.searchFailed = false),
catchError(() => { catchError(() => {
this.searchFailed = true; this.searchFailed = true;
return of([]); return of([]);
})) }))
), ),
tap(() => this.searching = false) tap(() => this.searching = false)
); )
writeValue(value: any) { writeValue(value: any) {
this.tags = value; this.tags = value;
this.tag = ""; this.tag = '';
} }
registerOnTouched() { } registerOnTouched() {
}
} }