Skip to content

Build your personal knowledge base with Trilium Notes

fix(autocomplete): pause FormAutocomplete's lookups while an IME composes

Claude61576e56c5f7
diff --git a/apps/client/src/widgets/react/FormAutocomplete.spec.tsx b/apps/client/src/widgets/react/FormAutocomplete.spec.tsx
index 5eef401a44..85acec6dfd 100644
--- a/apps/client/src/widgets/react/FormAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/FormAutocomplete.spec.tsx
@@ -3,9 +3,12 @@
* measured from the layout, which a headless DOM has none of, so the field and the viewport are
* given the measurements a browser would have taken.
*/
-import { afterEach, describe, expect, it } from "vitest";
+import { useState } from "preact/hooks";
+import { act } from "preact/test-utils";
+import { afterEach, describe, expect, it, vi } from "vitest";
-import { computeDropdownPosition, stepOver } from "./FormAutocomplete";
+import { renderInto } from "../../test/render";
+import FormAutocomplete, { computeDropdownPosition, stepOver } from "./FormAutocomplete";
/** The room the placement keeps between the list and the edge of the screen. */
const MARGIN = 8;
@@ -100,3 +103,40 @@ describe("computeDropdownPosition", () => {
expect(computeDropdownPosition(fieldAt({ top: 150 })).top).not.toBe("180px");
});
});
+
+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();
+ const source = vi.fn(async (query: string) => [ `${query}!` ]);
+ const onCompositionStart = vi.fn();
+ const onCompositionEnd = vi.fn();
+ function Field() {
+ const [ value, setValue ] = useState("");
+ return <FormAutocomplete currentValue={value} onChange={setValue} source={source}
+ onCompositionStart={onCompositionStart} onCompositionEnd={onCompositionEnd} />;
+ }
+ const found = renderInto(<Field />).querySelector("input");
+ if (!found) throw new Error("no input rendered");
+ const input: HTMLInputElement = found;
+
+ async function type(text: string) {
+ await act(async () => {
+ input.value = text;
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });
+ }
+
+ await act(async () => { input.dispatchEvent(new Event("compositionstart", { bubbles: true })); });
+ await type("n");
+ await type("ni");
+ expect(source).not.toHaveBeenCalled();
+
+ await act(async () => { input.dispatchEvent(new Event("compositionend", { bubbles: true })); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });
+ expect(source.mock.calls).toEqual([ [ "ni" ] ]);
+ expect(onCompositionStart).toHaveBeenCalledOnce();
+ expect(onCompositionEnd).toHaveBeenCalledOnce();
+ vi.useRealTimers();
+ });
+});
diff --git a/apps/client/src/widgets/react/FormAutocomplete.tsx b/apps/client/src/widgets/react/FormAutocomplete.tsx
index efa5f78a9a..aaec57f0f2 100644
--- a/apps/client/src/widgets/react/FormAutocomplete.tsx
+++ b/apps/client/src/widgets/react/FormAutocomplete.tsx
@@ -102,7 +102,7 @@ interface FormAutocompleteProps extends Omit<FormTextBoxProps, "onChange"> {
* The dropdown is portalled to the body and positioned over everything else, so it is not clipped
* by scrolling ancestors. Selecting a suggestion reports it through `onChange`, exactly like typing.
*/
-export default function FormAutocomplete({ currentValue, onChange, source, openOnFocus, openOnEnter, onPick, keepOpenOnPick, renderItem, leading, trailing, autoActivate, isHeading, dropdownMinWidth, inputRef, onFocus, onBlur, onKeyDown, ...restProps }: FormAutocompleteProps) {
+export default function FormAutocomplete({ currentValue, onChange, source, openOnFocus, openOnEnter, onPick, keepOpenOnPick, renderItem, leading, trailing, autoActivate, isHeading, dropdownMinWidth, inputRef, onFocus, onBlur, onKeyDown, onCompositionStart, onCompositionEnd, ...restProps }: FormAutocompleteProps) {
const ownInputRef = useRef<HTMLInputElement>(null);
const inputEl = inputRef ?? ownInputRef;
const fieldRef = useRef<HTMLDivElement>(null);
@@ -172,6 +172,14 @@ export default function FormAutocomplete({ currentValue, onChange, source, openO
autocomplete.handleKeyDown(e);
onKeyDown?.(e);
}}
+ onCompositionStart={(e) => {
+ autocomplete.handleCompositionStart();
+ onCompositionStart?.(e);
+ }}
+ onCompositionEnd={(e) => {
+ autocomplete.handleCompositionEnd();
+ onCompositionEnd?.(e);
+ }}
{...autocomplete.comboboxProps}
/>
);