Fix: de-deduplicate children in tag list when filtering (#11229)

This commit is contained in:
shamoon
2025-10-30 07:02:00 -07:00
committed by GitHub
parent 9aee063347
commit 6b55740f56
2 changed files with 21 additions and 6 deletions

View File

@@ -73,9 +73,14 @@ describe('TagListComponent', () => {
)
})
it('should filter out child tags if name filter is empty, otherwise show all', () => {
it('should omit matching children from top level when their parent is present', () => {
const tags = [
{ id: 1, name: 'Tag1', parent: null },
{
id: 1,
name: 'Tag1',
parent: null,
children: [{ id: 2, name: 'Tag2', parent: 1 }],
},
{ id: 2, name: 'Tag2', parent: 1 },
{ id: 3, name: 'Tag3', parent: null },
]
@@ -86,7 +91,13 @@ describe('TagListComponent', () => {
component['_nameFilter'] = 'Tag2' // Simulate non-empty name filter
const filteredWithName = component.filterData(tags as any)
expect(filteredWithName.length).toBe(3)
expect(filteredWithName.length).toBe(2)
expect(filteredWithName.find((t) => t.id === 2)).toBeUndefined()
expect(
filteredWithName
.find((t) => t.id === 1)
?.children?.some((c) => c.id === 2)
).toBe(true)
})
it('should request only parent tags when no name filter is applied', () => {

View File

@@ -69,9 +69,13 @@ export class TagListComponent extends ManagementListComponent<Tag> {
}
filterData(data: Tag[]) {
return this.nameFilter?.length
? [...data]
: data.filter((tag) => !tag.parent)
if (!this.nameFilter?.length) {
return data.filter((tag) => !tag.parent)
}
// When filtering by name, exclude children if their parent is also present
const availableIds = new Set(data.map((tag) => tag.id))
return data.filter((tag) => !tag.parent || !availableIds.has(tag.parent))
}
protected override getSelectableIDs(tags: Tag[]): number[] {