Tweak to allow the x to remove the whole badge

This commit is contained in:
shamoon committed 2026-11-21 11:30:33 -08:00
1 parent debc48551d
commit 3b36dcf096
3 files changed
+47 -9

No files matched your search

@@ -25,10 +25,10 @@
@if (isImplied(item.id)) {
<span class="implied-tag"></span>
} @else {
<button class="tag-wrap btn p-0 d-flex align-items-center mw-100" (click)="removeTag(item.id)" (mousedown)="$event.stopImmediatePropagation()" type="button" title="Remove tag" i18n-title>
<button class="tag-wrap btn p-0 d-flex align-items-center mw-100" (click)="removeBadge(item.id)" (mousedown)="$event.stopImmediatePropagation()" type="button" title="Remove tag" i18n-title>
<pngx-bs-icon name="x" style="margin-inline-end: 1px;"></pngx-bs-icon>
@if (item.id && tags) {
<pngx-tag class="mw-100 overflow-hidden" [tag]="getTag(item.id)" [split]="preserveHierarchy" (parentClick)="removeTag($event)"></pngx-tag>
<pngx-tag class="mw-100 overflow-hidden" [tag]="getTag(item.id)" [split]="preserveHierarchy" (click)="removeTag(item.id); $event.stopPropagation()" (parentClick)="removeTag($event)"></pngx-tag>
}
</button>
}
@@ -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)
@@ -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