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
refactor(autocomplete): span the note list over the whole field
Elian Dorandea30358d2ec
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsxindex ec4fda789c..b15d491a98 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx@@ -217,6 +217,17 @@ describe("NoteAutocomplete's suggestion list", () => {expect(rows()).toHaveLength(0);});+ it("spans the whole field, the buttons included", async () => {+ const input = await mount();+ const group = input.closest(".input-group");+ if (!group) throw new Error("no input group rendered");+ group.getBoundingClientRect = () => DOMRect.fromRect({ width: 320, height: 30 });+ input.getBoundingClientRect = () => DOMRect.fromRect({ width: 200, height: 30 });+ await type(input, "a");++ expect(document.querySelector<HTMLElement>(".algolia-autocomplete")?.style.width).toBe("320px");+ });+it("picks a clicked note", async () => {const noteIdChanged = vi.fn();const input = await mount({ noteIdChanged });diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsxindex 8aca13e3e5..5446f4a23c 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -3,7 +3,7 @@ import "./NoteAutocomplete.css";import clsx from "clsx";import type { RefObject } from "preact";import type { CSSProperties } from "preact/compat";-import { useCallback, useEffect, useState } from "preact/hooks";+import { useCallback, useEffect, useRef, useState } from "preact/hooks";import froca from "../../services/froca";import { t } from "../../services/i18n";@@ -35,6 +35,7 @@ export interface NoteAutocompleteProps {export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, containerStyle, opts, onChange, onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, readOnly, tabIndex }: NoteAutocompleteProps) {const inputRef = useSyncedRef<HTMLInputElement>(externalInputRef);+ const groupRef = useRef<HTMLDivElement>(null);const [ value, setValue ] = useState("");const [ notePath, setNotePath ] = useState("");@@ -73,7 +74,7 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,const showGoToButton = showButtons && !opts?.hideGoToSelectedNoteButton;return (- <div className="input-group" style={containerStyle}>+ <div ref={groupRef} className="input-group" style={containerStyle}><inputid={id}ref={inputRef}@@ -136,14 +137,14 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,/></>}- {autocomplete.isShown && inputRef.current && (+ {autocomplete.isShown && groupRef.current && (<Popup- anchor={inputRef.current}+ anchor={groupRef.current}placement="bottom-start"capHeight={false}className="algolia-autocomplete"- // The list spans the input, as `autocomplete.js` sized it.- style={{ width: `${inputRef.current.getBoundingClientRect().width}px` }}+ // The list spans the whole field, buttons included.+ style={{ width: `${groupRef.current.getBoundingClientRect().width}px` }}escapeDismisses={false}onDismiss={autocomplete.close}>