Skip to content

Build your personal knowledge base with Trilium Notes

fix(autocomplete): leave Escape and Tab to the host of a contained list

Elian Doran19a80a75d3bf
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index f3ae9973de..1583ee5ad2 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -300,7 +300,7 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(document.activeElement).toBe(input);
});
- it("lists into the host's container, open past a blur until Escape", async () => {
+ it("lists into the host's container, open past a blur, Escape and Tab, until a pick", async () => {
const host = document.createElement("div");
document.body.append(host);
const input = await mount({ container: { current: host } });
@@ -318,7 +318,17 @@ describe("NoteAutocomplete's suggestion list", () => {
});
expect(host.querySelectorAll(".aa-suggestion")).toHaveLength(2);
+ // Both are left to the host, as a dialog closes on Escape.
+ const reachedHost: string[] = [];
+ const listen = (e: KeyboardEvent) => reachedHost.push(e.key);
+ document.body.addEventListener("keydown", listen);
await press(input, "Escape");
+ await press(input, "Tab");
+ document.body.removeEventListener("keydown", listen);
+ expect(host.querySelectorAll(".aa-suggestion")).toHaveLength(2);
+ expect(reachedHost).toEqual([ "Escape", "Tab" ]);
+
+ await press(input, "Enter");
expect(host.querySelector(".aa-dropdown-menu")).toBeNull();
host.remove();
});
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index 69252184d3..2838e13e49 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -145,11 +145,15 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
noteIdChanged?.(undefined as unknown as string);
}}
onKeyDown={(e) => {
- autocomplete.handleKeyDown(e);
+ // A list in the host's container stays until a pick, so the keys that close a
+ // popup are left to the host, such as a dialog closing on Escape.
+ if (!container || (e.key !== "Escape" && e.key !== "Tab")) {
+ autocomplete.handleKeyDown(e);
+ }
onKeyDown?.(e);
}}
onBlur={() => {
- // A list in the host's container stays open until Escape or a pick.
+ // A list in the host's container stays open until a pick.
if (!container) autocomplete.handleBlur();
onBlur?.(value.trim() ? lastSegment(notePath) : "");
}}