Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(autocomplete): keep a held Enter to the text it was pressed on
Elian Doran88567a78dceb
diff --git a/apps/client/src/widgets/react/FormAutocomplete.spec.tsx b/apps/client/src/widgets/react/FormAutocomplete.spec.tsxindex 85acec6dfd..dd18a762da 100644--- a/apps/client/src/widgets/react/FormAutocomplete.spec.tsx+++ b/apps/client/src/widgets/react/FormAutocomplete.spec.tsx@@ -104,6 +104,47 @@ describe("computeDropdownPosition", () => {});});+describe("Enter pressed before the newer query's suggestions", () => {+ it("reaches the host where the newer list will open with nothing highlighted", async () => {+ vi.useFakeTimers();+ const onPick = vi.fn();+ const reachedDocument = vi.fn();+ function Field() {+ const [ value, setValue ] = useState("");+ return <FormAutocomplete currentValue={value} onChange={setValue} onPick={onPick}+ source={async (query) => [ `${query}!` ]} />;+ }+ const found = renderInto(<Field />).querySelector("input");+ if (!found) throw new Error("no input rendered");+ const input: HTMLInputElement = found;++ await act(async () => {+ input.value = "a";+ input.dispatchEvent(new Event("input", { bubbles: true }));+ });+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });+ await act(async () => {+ input.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }));+ });+ expect(document.querySelector(".form-autocomplete-item.active")?.textContent).toBe("a!");++ await act(async () => {+ input.value = "ab";+ input.dispatchEvent(new Event("input", { bubbles: true }));+ });+ const enter = new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true });+ document.addEventListener("keydown", reachedDocument);+ await act(async () => { input.dispatchEvent(enter); });+ document.removeEventListener("keydown", reachedDocument);+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });++ expect(enter.defaultPrevented).toBe(false);+ expect(reachedDocument).toHaveBeenCalledWith(enter);+ expect(onPick).not.toHaveBeenCalled();+ vi.useRealTimers();+ });+});+describe("an input method composing in the field", () => {it("looks up what the input method commits, not what it is composing, and passes the events on", async () => {vi.useFakeTimers();diff --git a/apps/client/src/widgets/react/FormAutocomplete.tsx b/apps/client/src/widgets/react/FormAutocomplete.tsxindex aaec57f0f2..f8a64e67d6 100644--- a/apps/client/src/widgets/react/FormAutocomplete.tsx+++ b/apps/client/src/widgets/react/FormAutocomplete.tsx@@ -398,7 +398,10 @@ export function useAutocomplete<T>({ query, source, onPick, inputRef, disabled,break;case "Enter":- if (isShown && activeIndex >= 0 && !pickedSinceRender.current) {+ // Without `autoActivate` the newer entries open with nothing highlighted, so an Enter+ // pressed before they arrive belongs to the host.+ if (isShown && activeIndex >= 0 && !pickedSinceRender.current+ && (autoActivate || !isStale.current)) {// Consume the key so it does not also reach the surrounding form or dialog.e.preventDefault();e.stopPropagation();@@ -448,6 +451,8 @@ export function useAutocomplete<T>({ query, source, onPick, inputRef, disabled,handleInput() {// The lookup effect runs after the next render, and a key can arrive before it.isStale.current = true;+ // A held Enter confirms the text it was pressed on, not text typed after it.+ enterHeld.current = false;open();},handleFocus() {diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsxindex 8e58b667ef..eb047d3380 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx@@ -347,6 +347,24 @@ describe("NoteAutocomplete's suggestion list", () => {expect(onChange).toHaveBeenCalledWith(notes[1]);});+ it("drops a held Enter once the text changes again", async () => {+ const onChange = vi.fn();+ const input = await mount({ onChange });+ await type(input, "al");++ let resolve: (suggestions: Suggestion[]) => void = () => {};+ getNoteSuggestions.mockImplementationOnce(() => new Promise((r) => { resolve = r; }));+ await type(input, "be");+ await press(input, "Enter");+ await type(input, "bet");++ await act(async () => { resolve([ notes[1] ]); });+ await settle();+ expect(onChange).not.toHaveBeenCalled();+ expect(getNoteSuggestions).toHaveBeenLastCalledWith("bet", expect.anything());+ expect(rows()[0].classList.contains("tn-menu-active")).toBe(true);+ });+it("searches on the first keystroke, and paces the rest of a burst", async () => {const input = await mount();