|
|
@ -14,7 +14,13 @@ import { |
|
|
signal, |
|
|
signal, |
|
|
ViewChild |
|
|
ViewChild |
|
|
} from '@angular/core'; |
|
|
} from '@angular/core'; |
|
|
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms'; |
|
|
import { |
|
|
|
|
|
ControlValueAccessor, |
|
|
|
|
|
FormControl, |
|
|
|
|
|
FormsModule, |
|
|
|
|
|
NG_VALUE_ACCESSOR, |
|
|
|
|
|
ReactiveFormsModule |
|
|
|
|
|
} from '@angular/forms'; |
|
|
import { |
|
|
import { |
|
|
MatAutocompleteModule, |
|
|
MatAutocompleteModule, |
|
|
MatAutocompleteSelectedEvent |
|
|
MatAutocompleteSelectedEvent |
|
|
@ -40,12 +46,21 @@ import { BehaviorSubject, Subject, takeUntil } from 'rxjs'; |
|
|
MatInputModule, |
|
|
MatInputModule, |
|
|
ReactiveFormsModule |
|
|
ReactiveFormsModule |
|
|
], |
|
|
], |
|
|
|
|
|
providers: [ |
|
|
|
|
|
{ |
|
|
|
|
|
provide: NG_VALUE_ACCESSOR, |
|
|
|
|
|
useExisting: GfTagsSelectorComponent, |
|
|
|
|
|
multi: true |
|
|
|
|
|
} |
|
|
|
|
|
], |
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA], |
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA], |
|
|
selector: 'gf-tags-selector', |
|
|
selector: 'gf-tags-selector', |
|
|
styleUrls: ['./tags-selector.component.scss'], |
|
|
styleUrls: ['./tags-selector.component.scss'], |
|
|
templateUrl: 'tags-selector.component.html' |
|
|
templateUrl: 'tags-selector.component.html' |
|
|
}) |
|
|
}) |
|
|
export class GfTagsSelectorComponent implements OnInit, OnChanges, OnDestroy { |
|
|
export class GfTagsSelectorComponent |
|
|
|
|
|
implements OnInit, OnChanges, OnDestroy, ControlValueAccessor |
|
|
|
|
|
{ |
|
|
@Input() hasPermissionToCreateTag = false; |
|
|
@Input() hasPermissionToCreateTag = false; |
|
|
@Input() readonly = false; |
|
|
@Input() readonly = false; |
|
|
@Input() tags: Tag[]; |
|
|
@Input() tags: Tag[]; |
|
|
@ -61,6 +76,13 @@ export class GfTagsSelectorComponent implements OnInit, OnChanges, OnDestroy { |
|
|
public readonly tagsSelected = signal<Tag[]>([]); |
|
|
public readonly tagsSelected = signal<Tag[]>([]); |
|
|
|
|
|
|
|
|
private unsubscribeSubject = new Subject<void>(); |
|
|
private unsubscribeSubject = new Subject<void>(); |
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
|
|
private onChange = (_value: Tag[]): void => { |
|
|
|
|
|
// ControlValueAccessor onChange callback
|
|
|
|
|
|
}; |
|
|
|
|
|
private onTouched = (): void => { |
|
|
|
|
|
// ControlValueAccessor onTouched callback
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
public constructor() { |
|
|
public constructor() { |
|
|
this.tagInputControl.valueChanges |
|
|
this.tagInputControl.valueChanges |
|
|
@ -99,7 +121,10 @@ export class GfTagsSelectorComponent implements OnInit, OnChanges, OnDestroy { |
|
|
return [...(tags ?? []), tag]; |
|
|
return [...(tags ?? []), tag]; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
this.tagsChanged.emit(this.tagsSelected()); |
|
|
const newTags = this.tagsSelected(); |
|
|
|
|
|
this.tagsChanged.emit(newTags); |
|
|
|
|
|
this.onChange(newTags); |
|
|
|
|
|
this.onTouched(); |
|
|
this.tagInput.nativeElement.value = ''; |
|
|
this.tagInput.nativeElement.value = ''; |
|
|
this.tagInputControl.setValue(undefined); |
|
|
this.tagInputControl.setValue(undefined); |
|
|
} |
|
|
} |
|
|
@ -111,7 +136,10 @@ export class GfTagsSelectorComponent implements OnInit, OnChanges, OnDestroy { |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
this.tagsChanged.emit(this.tagsSelected()); |
|
|
const newTags = this.tagsSelected(); |
|
|
|
|
|
this.tagsChanged.emit(newTags); |
|
|
|
|
|
this.onChange(newTags); |
|
|
|
|
|
this.onTouched(); |
|
|
this.updateFilters(); |
|
|
this.updateFilters(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -120,6 +148,28 @@ export class GfTagsSelectorComponent implements OnInit, OnChanges, OnDestroy { |
|
|
this.unsubscribeSubject.complete(); |
|
|
this.unsubscribeSubject.complete(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ControlValueAccessor implementation
|
|
|
|
|
|
writeValue(value: Tag[]): void { |
|
|
|
|
|
this.tagsSelected.set(value || []); |
|
|
|
|
|
this.updateFilters(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: (value: Tag[]) => void): void { |
|
|
|
|
|
this.onChange = fn; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: () => void): void { |
|
|
|
|
|
this.onTouched = fn; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setDisabledState?(isDisabled: boolean): void { |
|
|
|
|
|
if (isDisabled) { |
|
|
|
|
|
this.tagInputControl.disable(); |
|
|
|
|
|
} else { |
|
|
|
|
|
this.tagInputControl.enable(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private filterTags(query: string = ''): Tag[] { |
|
|
private filterTags(query: string = ''): Tag[] { |
|
|
const tags = this.tagsSelected() ?? []; |
|
|
const tags = this.tagsSelected() ?? []; |
|
|
const tagIds = tags.map(({ id }) => { |
|
|
const tagIds = tags.map(({ id }) => { |
|
|
|