From 9a0b6fd40f5381807a630d4335ad91f2617dc13b Mon Sep 17 00:00:00 2001
From: Patrizio Bekerle <patrizio@bekerle.com>
Date: Thu, 5 Nov 2020 14:25:17 +0100
Subject: [PATCH] Fix place-selector issue that causes select2 to close
 immediately after opening

---
 .../location-select/src/location-select.js    | 22 +++++++++++--------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/packages/location-select/src/location-select.js b/packages/location-select/src/location-select.js
index b0ba579e..44b49571 100644
--- a/packages/location-select/src/location-select.js
+++ b/packages/location-select/src/location-select.js
@@ -81,15 +81,11 @@ export class LocationSelect extends ScopedElementsMixin(LitElement) {
         this.updateComplete.then(()=>{
             that.$select = that.$('#' + that.selectId);
 
-            // close the selector on blur of the web component
-            $(that).blur(() => {
-                // the 500ms delay is a workaround to actually get an item selected when clicking on it,
-                // because the blur gets also fired when clicking in the selector
-                setTimeout(() => {
-                    if (this.select2IsInitialized()) {
-                        that.$select.select2('close');
-                    }
-                }, 500);
+            // Close the popup when clicking outside of select2
+            document.addEventListener('click', (ev) => {
+                if (!ev.composedPath().includes(this)) {
+                    this._closeSelect2();
+                }
             });
 
             // try an init when user-interface is loaded
@@ -97,6 +93,14 @@ export class LocationSelect extends ScopedElementsMixin(LitElement) {
         });
     }
 
+    _closeSelect2() {
+        const $select = this.$('#' + this.selectId);
+        console.assert($select.length, "select2 missing");
+        if (this.select2IsInitialized($select)) {
+            $select.select2('close');
+        }
+    }
+
     initJSONLD(ignorePreset = false) {
         const that = this;
 
-- 
GitLab