Skip to content

Build your personal knowledge base with Trilium Notes

refactor(autocomplete): empty the relation field through the handle

Elian Doran5209eb0aeee7
diff --git a/apps/client/src/widgets/attribute_widgets/relation_values_input.spec.tsx b/apps/client/src/widgets/attribute_widgets/relation_values_input.spec.tsx
index 7b47a1d0ec..6ea38bb046 100644
--- a/apps/client/src/widgets/attribute_widgets/relation_values_input.spec.tsx
+++ b/apps/client/src/widgets/attribute_widgets/relation_values_input.spec.tsx
@@ -2,36 +2,30 @@ import { render } from "preact";
import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-// The real box is an Algolia autocomplete bound to jQuery, which is not loaded here; the mock hands
-// the test the props instead, `noteIdChanged` being the one the field's behaviour hangs off. The ref
-// is forwarded to the box it builds, as the real field forwards it to the box the plugin builds.
-const autocomplete = vi.hoisted(() => ({ current: null as Record<string, unknown> | null }));
+// The mock hands the test the props of the box, `noteIdChanged` being the one the field's behavior
+// hangs off, and the field a handle whose `clear` it can watch.
+const { autocomplete, clear } = vi.hoisted(() => ({
+ autocomplete: { current: null as Record<string, unknown> | null },
+ clear: vi.fn()
+}));
vi.mock("../react/NoteAutocomplete", async () => {
const { h } = await import("preact");
return {
default: (props: Record<string, unknown>) => {
autocomplete.current = props;
+ const handleRef = props.handleRef as { current: unknown } | undefined;
+ if (handleRef) handleRef.current = { clear };
return h("input", {
id: props.id as string | undefined,
- tabIndex: props.tabIndex as number | undefined,
- ref: props.inputRef
+ tabIndex: props.tabIndex as number | undefined
});
}
};
});
-import $ from "jquery";
-
import { buildNote } from "../../test/easy-froca";
import RelationValuesInput, { RelationValueChips } from "./relation_values_input";
-// Taking a pick clears the box through the plugin, which holds text of its own; the plugin is not
-// loaded here, so its two calls are stubbed where jQuery keeps them.
-const setSelectedNotePath = vi.fn();
-type PluggedIn = { autocomplete(...args: unknown[]): PluggedIn; setSelectedNotePath(path: string): void };
-($.fn as unknown as PluggedIn).autocomplete = function (this: PluggedIn) { return this; };
-($.fn as unknown as PluggedIn).setSelectedNotePath = setSelectedNotePath;
-
describe("RelationValuesInput", () => {
let container: HTMLElement;
@@ -82,9 +76,8 @@ describe("RelationValuesInput", () => {
await act(async () => pick(gamma.noteId));
expect(onCommit).toHaveBeenCalledWith([ alpha.noteId, gamma.noteId ]);
- // What was in the box is spent on the choice made: the plugin is told to let its text go,
- // or it would write the picked title right back over the cleared box.
- expect(setSelectedNotePath).toHaveBeenCalledWith("");
+ // What was in the box is spent on the choice made.
+ expect(clear).toHaveBeenCalled();
// A target already held would come out as a second chip there is no telling apart — and
// clearing the box reports an empty pick, which is not a target at all.
diff --git a/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx b/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx
index 12df1253ee..9d8f0d5ae0 100644
--- a/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx
+++ b/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx
@@ -5,7 +5,7 @@ import froca from "../../services/froca";
import { t } from "../../services/i18n";
import Chip from "../react/Chip";
import Icon from "../react/Icon";
-import NoteAutocomplete from "../react/NoteAutocomplete";
+import NoteAutocomplete, { type NoteAutocompleteHandle } from "../react/NoteAutocomplete";
interface RelationValuesInputProps {
/** The targets' noteIds, in the order they are shown. */
@@ -29,18 +29,13 @@ interface RelationValuesInputProps {
* already held is not taken a second time — the chips are a set.
*/
export default function RelationValuesInput({ values, onCommit, inputId, tabIndex, disabled }: RelationValuesInputProps) {
- const inputRef = useRef<HTMLInputElement>(null);
+ const autocompleteRef = useRef<NoteAutocompleteHandle>(null);
const notes = useNotes(values);
function take(noteId: string) {
// The box is emptied either way — what was in it is spent on the choice made — but a target
// already held is not taken twice, and clearing an empty pick has nothing to add.
- const picker = inputRef.current;
- if (picker) {
- // Through the plugin rather than the element: the plugin holds text of its own, and would
- // write the picked title right back over a cleared box.
- $(picker).autocomplete("val", "").setSelectedNotePath("");
- }
+ autocompleteRef.current?.clear();
if (!noteId || values.includes(noteId)) return;
onCommit([ ...values, noteId ]);
}
@@ -63,7 +58,7 @@ export default function RelationValuesInput({ values, onCommit, inputId, tabInde
))}
<NoteAutocomplete
id={inputId}
- inputRef={inputRef}
+ handleRef={autocompleteRef}
tabIndex={tabIndex}
opts={{ allowCreatingNotes: true, hideAllButtons: true }}
noteIdChanged={take}
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index a2cd0624fd..1487f8f0e5 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -617,6 +617,29 @@ describe("NoteAutocomplete's suggestion list", () => {
});
});
+ it("empties the field through the handle without reporting it", async () => {
+ const handleRef = createRef<NoteAutocompleteHandle>();
+ const onChange = vi.fn();
+ const noteIdChanged = vi.fn();
+ const onTextChange = vi.fn();
+ const input = await mount({ handleRef, onChange, noteIdChanged, onTextChange });
+ await type(input, "a");
+ await press(input, "Enter");
+ for (const callback of [ onChange, noteIdChanged, onTextChange ]) callback.mockClear();
+
+ // From the host's own `noteIdChanged`, as the relation field clears the box it took from.
+ act(() => {
+ handleRef.current?.clear();
+ expect(input.value).toBe("");
+ });
+ await settle();
+
+ expect(input.dataset.notePath).toBe("");
+ expect(onChange).not.toHaveBeenCalled();
+ expect(noteIdChanged).not.toHaveBeenCalled();
+ expect(onTextChange).not.toHaveBeenCalled();
+ });
+
it("spans the whole field, the buttons included", async () => {
const input = await mount();
const group = input.closest(".input-group");
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index ee2df1ca96..98cbc0b9ee 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -49,6 +49,8 @@ export interface NoteAutocompleteHandle {
* `selectedPath` is the note the text stands for, where it names one already.
*/
setText(text: string, selectedPath?: string): void;
+ /** Empties the field and drops its selection without reporting either, as after a pick a host spends. */
+ clear(): void;
}
export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, container, containerStyle, opts, onChange, onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, readOnly, tabIndex, handleRef }: NoteAutocompleteProps) {
@@ -173,10 +175,16 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
const showRecentNotes = () => showAndFocus("");
const showAllCommands = () => showAndFocus(">");
const setText = (newText: string, selectedPath?: string) => showSuggestionsFor(newText.trim(), selectedPath);
+ const clear = () => {
+ setFullTextSearchCount(0);
+ setNotePath("");
+ setValue("");
+ if (inputRef.current) inputRef.current.value = "";
+ };
// Refreshed on every render, so a call from outside reaches the current callbacks.
useLayoutEffect(() => {
- if (handleRef) handleRef.current = { showRecentNotes, showAllCommands, setText };
+ if (handleRef) handleRef.current = { showRecentNotes, showAllCommands, setText, clear };
});
useEffect(() => {