Browse Source

Reorder functions. Add public modifier according #5561 comments

pull/5561/head
Germán Martín 1 month ago
committed by Thomas Kaul
parent
commit
3ab0603193
  1. 52
      libs/ui/src/lib/tags-selector/tags-selector.component.ts

52
libs/ui/src/lib/tags-selector/tags-selector.component.ts

@ -48,9 +48,9 @@ import { BehaviorSubject, Subject, takeUntil } from 'rxjs';
], ],
providers: [ providers: [
{ {
multi: true,
provide: NG_VALUE_ACCESSOR, provide: NG_VALUE_ACCESSOR,
useExisting: GfTagsSelectorComponent, useExisting: GfTagsSelectorComponent
multi: true
} }
], ],
schemas: [CUSTOM_ELEMENTS_SCHEMA], schemas: [CUSTOM_ELEMENTS_SCHEMA],
@ -59,7 +59,7 @@ import { BehaviorSubject, Subject, takeUntil } from 'rxjs';
templateUrl: 'tags-selector.component.html' templateUrl: 'tags-selector.component.html'
}) })
export class GfTagsSelectorComponent export class GfTagsSelectorComponent
implements OnInit, OnChanges, OnDestroy, ControlValueAccessor implements ControlValueAccessor, OnChanges, OnDestroy, OnInit
{ {
@Input() hasPermissionToCreateTag = false; @Input() hasPermissionToCreateTag = false;
@Input() readonly = false; @Input() readonly = false;
@ -75,7 +75,19 @@ export class GfTagsSelectorComponent
public readonly tagInputControl = new FormControl(''); public readonly tagInputControl = new FormControl('');
public readonly tagsSelected = signal<Tag[]>([]); public readonly tagsSelected = signal<Tag[]>([]);
private unsubscribeSubject = new Subject<void>(); private filterTags(query: string = ''): Tag[] {
const tags = this.tagsSelected() ?? [];
const tagIds = tags.map(({ id }) => {
return id;
});
return this.tagsAvailable.filter(({ id, name }) => {
return (
!tagIds.includes(id) && name.toLowerCase().includes(query.toLowerCase())
);
});
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
private onChange = (_value: Tag[]): void => { private onChange = (_value: Tag[]): void => {
// ControlValueAccessor onChange callback // ControlValueAccessor onChange callback
@ -148,21 +160,15 @@ export class GfTagsSelectorComponent
this.unsubscribeSubject.complete(); this.unsubscribeSubject.complete();
} }
// ControlValueAccessor implementation public registerOnChange(fn: (value: Tag[]) => void): void {
writeValue(value: Tag[]): void {
this.tagsSelected.set(value || []);
this.updateFilters();
}
registerOnChange(fn: (value: Tag[]) => void): void {
this.onChange = fn; this.onChange = fn;
} }
registerOnTouched(fn: () => void): void { public registerOnTouched(fn: () => void): void {
this.onTouched = fn; this.onTouched = fn;
} }
setDisabledState?(isDisabled: boolean): void { public setDisabledState(isDisabled: boolean): void {
if (isDisabled) { if (isDisabled) {
this.tagInputControl.disable(); this.tagInputControl.disable();
} else { } else {
@ -170,20 +176,14 @@ export class GfTagsSelectorComponent
} }
} }
private filterTags(query: string = ''): Tag[] {
const tags = this.tagsSelected() ?? [];
const tagIds = tags.map(({ id }) => {
return id;
});
return this.tagsAvailable.filter(({ id, name }) => {
return (
!tagIds.includes(id) && name.toLowerCase().includes(query.toLowerCase())
);
});
}
private updateFilters() { private updateFilters() {
this.filteredOptions.next(this.filterTags()); this.filteredOptions.next(this.filterTags());
} }
private unsubscribeSubject = new Subject<void>();
// ControlValueAccessor implementation
public writeValue(value: Tag[]): void {
this.tagsSelected.set(value || []);
this.updateFilters();
}
} }

Loading…
Cancel
Save