Skip to content

Build your personal knowledge base with Trilium Notes

fix(quick-search): leave ArrowDown alone with no result to focus

BeatLink2a25930fe941
diff --git a/apps/client/src/widgets/quick_search.spec.ts b/apps/client/src/widgets/quick_search.spec.ts
index 030326d85d..0e60da5823 100644
--- a/apps/client/src/widgets/quick_search.spec.ts
+++ b/apps/client/src/widgets/quick_search.spec.ts
@@ -34,6 +34,13 @@ describe("QuickSearchWidget", () => {
expect(pressArrowDown($input[0])).toBe(false);
expect(document.activeElement).not.toBe($firstItem[0]);
});
+
+ it("leaves ArrowDown alone when the popup holds no result to focus", async () => {
+ const widget = await renderAndSearch(0);
+
+ expect(widget.$widget.find(".dropdown-menu .dropdown-item.disabled").length).toBe(1);
+ expect(pressArrowDown(widget.$widget.find(".search-string")[0])).toBe(false);
+ });
});
/** Returns whether the widget claimed the key, which is what keeps the caret from moving. */
@@ -45,8 +52,8 @@ function pressArrowDown(element: HTMLElement, modifiers: KeyboardEventInit = {})
return event.defaultPrevented;
}
-async function renderAndSearch() {
- const searchResults = Array.from({ length: 3 }, (_, index) => ({
+async function renderAndSearch(resultCount = 3) {
+ const searchResults = Array.from({ length: resultCount }, (_, index) => ({
notePath: `note${index}`,
noteTitle: `Note ${index}`,
notePathTitle: `Note ${index}`,
diff --git a/apps/client/src/widgets/quick_search.ts b/apps/client/src/widgets/quick_search.ts
index e572f1b4d6..93c8839334 100644
--- a/apps/client/src/widgets/quick_search.ts
+++ b/apps/client/src/widgets/quick_search.ts
@@ -229,9 +229,8 @@ export default class QuickSearchWidget extends BasicWidget {
this.$searchString.focus();
});
- // Steps from the search box into the results. Bootstrap moves between the items from there,
- // but it ignores key events on an input, and bindElShortcut() refuses a modifier-less
- // ArrowDown, so this listener is what opens the way in.
+ // Bootstrap moves between the results but ignores key events on an input, so the step from
+ // the search box into the list is bound here.
this.$searchString.on("keydown", (e) => {
const event = e.originalEvent as KeyboardEvent | undefined;
@@ -243,9 +242,15 @@ export default class QuickSearchWidget extends BasicWidget {
return;
}
+ const $firstResult = this.$dropdownMenu.find(".dropdown-item:not(.disabled)").first();
+
+ // Searching and no-results leave only a disabled item, and the caret keeps the key.
+ if (!$firstResult.length) {
+ return;
+ }
+
e.preventDefault();
- // first() rather than the `:first` selector, which jQuery 4 dropped.
- this.$dropdownMenu.find(".dropdown-item:not(.disabled)").first().focus();
+ $firstResult.focus();
});
shortcutService.bindElShortcut(this.$searchString, "esc", () => {