diff --git a/src-ui/src/app/components/common/input/tags/tags.component.html b/src-ui/src/app/components/common/input/tags/tags.component.html index c9182163be..1227bb35a6 100644 --- a/src-ui/src/app/components/common/input/tags/tags.component.html +++ b/src-ui/src/app/components/common/input/tags/tags.component.html @@ -25,10 +25,10 @@ @if (isImplied(item.id)) { } @else { - } diff --git a/src-ui/src/app/components/common/input/tags/tags.component.spec.ts b/src-ui/src/app/components/common/input/tags/tags.component.spec.ts index da8bf67fa9..738a2413b0 100644 --- a/src-ui/src/app/components/common/input/tags/tags.component.spec.ts +++ b/src-ui/src/app/components/common/input/tags/tags.component.spec.ts @@ -301,6 +301,23 @@ describe('TagsComponent', () => { expect(component.isImplied(1)).toBe(false) }) + it('should remove a whole badge, keeping ancestors other tags still need', () => { + component.removeBadge(3) + expect(component.value).toEqual([4]) + + const sibling: Tag = { id: 5, name: 'sibling', parent: 1 } + component.tags = [root, mid, leaf, other, sibling] + component.value = [3, 2, 1, 5, 4] + component.removeBadge(3) + expect(component.value).toEqual([1, 5, 4]) + }) + + it('should remove only the tag itself from a badge when hierarchy is not shown', () => { + component.preserveHierarchy = false + component.removeBadge(3) + expect(component.value).toEqual([2, 1, 4]) + }) + it('should remove the last visible tag on backspace, not a hidden ancestor', () => { const backspace = new KeyboardEvent('keydown', { key: 'Backspace' }) expect(component.onKeyDown(backspace)).toBe(false) diff --git a/src-ui/src/app/components/common/input/tags/tags.component.ts b/src-ui/src/app/components/common/input/tags/tags.component.ts index 6127a831fd..710334fd7d 100644 --- a/src-ui/src/app/components/common/input/tags/tags.component.ts +++ b/src-ui/src/app/components/common/input/tags/tags.component.ts @@ -143,16 +143,17 @@ export class TagsComponent implements OnInit, ControlValueAccessor { * as segments of their descendant's badge. */ isImplied(id: number): boolean { - if ( - !this.preserveHierarchy || - !this.tags || - !this.value || - !this.settingsService.get(SETTINGS_KEYS.TAGS_SHOW_HIERARCHY) - ) - return false + if (!this.showsHierarchy || !this.tags || !this.value) return false return !collapseImpliedAncestors(this.value, this.tagsById).includes(id) } + private get showsHierarchy(): boolean { + return ( + this.preserveHierarchy && + !!this.settingsService.get(SETTINGS_KEYS.TAGS_SHOW_HIERARCHY) + ) + } + /** * Remove the last visible tag (in case its actually a hidden ancestor) */ @@ -172,6 +173,26 @@ export class TagsComponent implements OnInit, ControlValueAccessor { } } + /** + * Remove a whole badge i.e. the tag plus any ancestors that no other selected + * tag still needs. + */ + removeBadge(tagID: number) { + if (this.disabled) return + if (!this.showsHierarchy) return this.removeTag(tagID) + + const ancestors = new Set(this.getParentChain(tagID).map((t) => t.id)) + const needed = new Set( + this.value + .filter((id) => id !== tagID && !ancestors.has(id)) + .flatMap((id) => this.getParentChain(id).map((t) => t.id)) + ) + this.value = this.value.filter( + (id) => id !== tagID && (!ancestors.has(id) || needed.has(id)) + ) + this.onChange(this.value) + } + removeTag(tagID: number) { if (this.disabled) return