Browse Source

feat(lib): implement new tag interface

pull/6497/head
KenTandrian 3 weeks ago
parent
commit
6053dbb9fe
  1. 7
      libs/ui/src/lib/tags-selector/interfaces/interfaces.ts
  2. 22
      libs/ui/src/lib/tags-selector/tags-selector.component.ts

7
libs/ui/src/lib/tags-selector/interfaces/interfaces.ts

@ -0,0 +1,7 @@
import { Tag } from '@prisma/client';
export interface NewTag extends Omit<Tag, 'id'> {
id: undefined;
}
export type SelectedTag = Tag | NewTag;

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

@ -32,6 +32,8 @@ import { addIcons } from 'ionicons';
import { addCircleOutline, closeOutline } from 'ionicons/icons'; import { addCircleOutline, closeOutline } from 'ionicons/icons';
import { BehaviorSubject, Subject } from 'rxjs'; import { BehaviorSubject, Subject } from 'rxjs';
import { SelectedTag } from './interfaces/interfaces';
@Component({ @Component({
changeDetection: ChangeDetectionStrategy.OnPush, changeDetection: ChangeDetectionStrategy.OnPush,
imports: [ imports: [
@ -61,13 +63,15 @@ export class GfTagsSelectorComponent
{ {
@Input() hasPermissionToCreateTag = false; @Input() hasPermissionToCreateTag = false;
@Input() readonly = false; @Input() readonly = false;
@Input() tags: Tag[]; @Input() tags: SelectedTag[];
@Input() tagsAvailable: Tag[]; @Input() tagsAvailable: SelectedTag[];
public readonly filteredOptions: Subject<Tag[]> = new BehaviorSubject([]); public readonly filteredOptions: Subject<SelectedTag[]> = new BehaviorSubject(
[]
);
public readonly separatorKeysCodes: number[] = [COMMA, ENTER]; public readonly separatorKeysCodes: number[] = [COMMA, ENTER];
public readonly tagInputControl = new FormControl(''); public readonly tagInputControl = new FormControl('');
public readonly tagsSelected = signal<Tag[]>([]); public readonly tagsSelected = signal<SelectedTag[]>([]);
private readonly tagInput = private readonly tagInput =
viewChild.required<ElementRef<HTMLInputElement>>('tagInput'); viewChild.required<ElementRef<HTMLInputElement>>('tagInput');
@ -99,7 +103,7 @@ export class GfTagsSelectorComponent
if (!tag && this.hasPermissionToCreateTag) { if (!tag && this.hasPermissionToCreateTag) {
tag = { tag = {
id: '', id: undefined,
name: event.option.value as string, name: event.option.value as string,
userId: null userId: null
}; };
@ -132,7 +136,7 @@ export class GfTagsSelectorComponent
this.updateFilters(); this.updateFilters();
} }
public registerOnChange(fn: (value: Tag[]) => void) { public registerOnChange(fn: (value: SelectedTag[]) => void) {
this.onChange = fn; this.onChange = fn;
} }
@ -148,12 +152,12 @@ export class GfTagsSelectorComponent
} }
} }
public writeValue(value: Tag[]) { public writeValue(value: SelectedTag[]) {
this.tagsSelected.set(value || []); this.tagsSelected.set(value || []);
this.updateFilters(); this.updateFilters();
} }
private filterTags(query: string = ''): Tag[] { private filterTags(query: string = ''): SelectedTag[] {
const tags = this.tagsSelected() ?? []; const tags = this.tagsSelected() ?? [];
const tagIds = tags.map(({ id }) => { const tagIds = tags.map(({ id }) => {
return id; return id;
@ -167,7 +171,7 @@ export class GfTagsSelectorComponent
} }
// 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: SelectedTag[]): void => {
// ControlValueAccessor onChange callback // ControlValueAccessor onChange callback
}; };

Loading…
Cancel
Save