From e7685116ece44421e8ccb265de15ea14faf72096 Mon Sep 17 00:00:00 2001
From: shamoon <4887959+shamoon@users.noreply.github.com>
Date: Wed, 12 Mar 2025 15:46:12 -0700
Subject: [PATCH] Fix: clear custom field query dropdown on close if invalid

---
 .../custom-fields-query-dropdown.component.spec.ts         | 7 +++++++
 .../custom-fields-query-dropdown.component.ts              | 2 ++
 2 files changed, 9 insertions(+)

diff --git a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.spec.ts b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.spec.ts
index 4dcbceb13..10ff280ae 100644
--- a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.spec.ts
+++ b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.spec.ts
@@ -93,6 +93,13 @@ describe('CustomFieldsQueryDropdownComponent', () => {
     expect(component.selectionModel.queries.length).toBe(1)
   })
 
+  it('should clear on close if model is invalid', () => {
+    component.selectionModel.clear()
+    component.selectionModel.addExpression()
+    component.onOpenChange(false)
+    expect(component.selectionModel.isEmpty()).toBeTruthy()
+  })
+
   it('should support reset the selection model', () => {
     component.selectionModel.addExpression()
     component.reset()
diff --git a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.ts b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.ts
index 479f072a1..4137d920e 100644
--- a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.ts
+++ b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.ts
@@ -274,6 +274,8 @@ export class CustomFieldsQueryDropdownComponent extends LoadingComponentWithPerm
           this.fieldSelects.first?.focus()
         }, 0)
       }
+    } else if (!this.selectionModel.isValid()) {
+      this.selectionModel.clear()
     }
   }