Date: Thu, 12 Sep 2024 18:50:40 -0600
Subject: [PATCH 2/5] fix: stop loss of input focus on click
---
.../shared/components/search-select.svelte | 89 ++++++++++---------
1 file changed, 45 insertions(+), 44 deletions(-)
diff --git a/src/svelte/shared/components/search-select.svelte b/src/svelte/shared/components/search-select.svelte
index 1460026..dc3e795 100644
--- a/src/svelte/shared/components/search-select.svelte
+++ b/src/svelte/shared/components/search-select.svelte
@@ -26,7 +26,7 @@
isOpen = true;
inputValue = "";
- await tick();
+ await tick(); //Wait for the dropdown to be rendered
if (inputRef && dropdownRef) {
const inputRect = inputRef.getBoundingClientRect();
@@ -44,10 +44,14 @@
currentFocusIndex = 0;
}
+ function handleOptionMouseDown(e: MouseEvent) {
+ e.preventDefault();
+ }
+
function handleOptionClick(option: string) {
+ closeDropdown();
inputValue = option;
value = option;
- isOpen = false;
dispatch("select", { value: option });
}
@@ -57,45 +61,39 @@
currentFocusIndex = 0;
}
- function handleInputFocus() {
- openDropdown();
+ function handleInputClick() {
+ if (isOpen) {
+ closeDropdown();
+ } else {
+ openDropdown();
+ }
}
function handleInputKeyDown(e: KeyboardEvent) {
if (e.key === "Tab") {
closeDropdown();
- } else if (e.key === "ArrowDown") {
- if (!isOpen) {
- openDropdown();
- return;
- }
+ return;
+ }
+
+ if (!isOpen) {
+ openDropdown();
+ return;
+ }
+
+ if (e.key === "ArrowDown") {
currentFocusIndex = currentFocusIndex + 1;
if (currentFocusIndex == filteredOptions.length) {
currentFocusIndex = currentFocusIndex - 1;
}
} else if (e.key === "ArrowUp") {
- if (!isOpen) {
- openDropdown();
- return;
- }
-
currentFocusIndex = currentFocusIndex - 1;
if (currentFocusIndex < 0) {
currentFocusIndex = currentFocusIndex = 0;
}
} else if (e.key === "Enter") {
- if (isOpen) {
- const option = filteredOptions[currentFocusIndex];
- if (option) {
- handleOptionClick(option);
- }
- } else {
- openDropdown();
- }
- } else {
- // open dropdown on any key press
- if (!isOpen) {
- openDropdown();
+ const option = filteredOptions[currentFocusIndex];
+ if (option) {
+ handleOptionClick(option);
}
}
}
@@ -103,9 +101,11 @@
function handleClickOutside(e: Event) {
const targetEl = e.target as HTMLElement;
const isInsideClick = targetEl.closest(
- ".vault-explorer-search-selection",
+ ".vault-explorer-search-select, .vault-explorer-search-select__dropdown-item",
);
+
if (!isInsideClick && isOpen) {
+ console.log("clicking outside");
closeDropdown();
}
}
@@ -127,23 +127,23 @@
$: filteredOptions = fuzzySearch(inputValue);
-
+
-
{#if isOpen}
@@ -152,10 +152,11 @@
tabindex="-1"
role="option"
aria-selected={option === value}
- class="vault-explorer-dropdown-item"
- class:vault-explorer-dropdown-item--selected={currentFocusIndex ===
+ class="vault-explorer-search-select__dropdown-item"
+ class:vault-explorer-search-select__dropdown-item--selected={currentFocusIndex ===
i}
- on:click={() => handleOptionClick(option)}
+ on:mousedown={handleOptionMouseDown}
+ on:click={(e) => handleOptionClick(option)}
on:keydown={() => {}}
>
{option}
@@ -163,7 +164,7 @@
{/each}
{#if filteredOptions.length === 0}
No results found
@@ -173,16 +174,16 @@
From 6dba81b3ae8a375491bb2e34a937a622f8aee46e Mon Sep 17 00:00:00 2001
From: DecafDev <40307803+decaf-dev@users.noreply.github.com>
Date: Thu, 12 Sep 2024 18:52:27 -0600
Subject: [PATCH 3/5] fix: unmount dropdown when parent is removed
---
src/svelte/shared/components/search-select.svelte | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/svelte/shared/components/search-select.svelte b/src/svelte/shared/components/search-select.svelte
index dc3e795..396a241 100644
--- a/src/svelte/shared/components/search-select.svelte
+++ b/src/svelte/shared/components/search-select.svelte
@@ -19,6 +19,7 @@
return () => {
document.removeEventListener("click", handleClickOutside);
+ dropdownRef?.remove();
};
});
From d3fd756c9d1fcf3f45e8f846f653caf53f465160 Mon Sep 17 00:00:00 2001
From: DecafDev <40307803+decaf-dev@users.noreply.github.com>
Date: Thu, 12 Sep 2024 19:04:46 -0600
Subject: [PATCH 4/5] fix: add clear button
---
.../shared/components/search-select.svelte | 33 ++++++++++++++++---
1 file changed, 28 insertions(+), 5 deletions(-)
diff --git a/src/svelte/shared/components/search-select.svelte b/src/svelte/shared/components/search-select.svelte
index 396a241..8c04da7 100644
--- a/src/svelte/shared/components/search-select.svelte
+++ b/src/svelte/shared/components/search-select.svelte
@@ -1,6 +1,7 @@