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 a multi-value relation's whole field
Elian Doran536836b90ec5
diff --git a/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx b/apps/client/src/widgets/attribute_widgets/relation_values_input.tsxindex 9d8f0d5ae0..1026cb1120 100644--- a/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx+++ b/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx@@ -30,6 +30,7 @@ interface RelationValuesInputProps {*/export default function RelationValuesInput({ values, onCommit, inputId, tabIndex, disabled }: RelationValuesInputProps) {const autocompleteRef = useRef<NoteAutocompleteHandle>(null);+ const fieldRef = useRef<HTMLDivElement>(null);const notes = useNotes(values);function take(noteId: string) {@@ -45,7 +46,7 @@ export default function RelationValuesInput({ values, onCommit, inputId, tabInde}return (- <div className="tn-field relation-values-input">+ <div ref={fieldRef} className="tn-field relation-values-input">{values.map((noteId) => (<Chipkey={noteId}@@ -59,6 +60,7 @@ export default function RelationValuesInput({ values, onCommit, inputId, tabInde<NoteAutocompleteid={inputId}handleRef={autocompleteRef}+ anchorRef={fieldRef}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.tsxindex 1487f8f0e5..4800e7c57d 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx@@ -651,6 +651,15 @@ describe("NoteAutocomplete's suggestion list", () => {expect(document.querySelector<HTMLElement>(".note-autocomplete-menu")?.style.width).toBe("320px");});+ it("spans the element a host anchors it to instead", async () => {+ const frame = document.createElement("div");+ frame.getBoundingClientRect = () => DOMRect.fromRect({ width: 480, height: 30 });+ const input = await mount({ anchorRef: { current: frame } });+ await type(input, "a");++ expect(document.querySelector<HTMLElement>(".note-autocomplete-menu")?.style.width).toBe("480px");+ });+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 98cbc0b9ee..bd068676e4 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -36,6 +36,8 @@ export interface NoteAutocompleteProps {tabIndex?: number;/** Receives the functions that drive the field from outside it. */handleRef?: MutableRef<NoteAutocompleteHandle | null>;+ /** The element the list hangs from and spans, in place of the field, for a host that frames it. */+ anchorRef?: RefObject<HTMLElement>;}/** Drives a note autocomplete from outside it, for a caller that decides when. */@@ -53,7 +55,7 @@ export interface NoteAutocompleteHandle {clear(): void;}-export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, container, containerStyle, opts, onChange, onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, readOnly, tabIndex, handleRef }: NoteAutocompleteProps) {+export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, container, containerStyle, opts, onChange, onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, readOnly, tabIndex, handleRef, anchorRef }: NoteAutocompleteProps) {const inputRef = useSyncedRef<HTMLInputElement>(externalInputRef);const groupRef = useRef<HTMLDivElement>(null);const [ value, setValue ] = useState("");@@ -199,6 +201,7 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,}}, [ text, noteId ]);+ const anchor = anchorRef?.current ?? groupRef.current;const showButtons = !opts?.hideAllButtons;const showGoToButton = showButtons && !opts?.hideGoToSelectedNoteButton;@@ -303,14 +306,14 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,? container.current && createPortal(<NoteSuggestionList autocomplete={autocomplete} searchingFor={isSearchingFullText ? value : undefined} />,container.current)- : groupRef.current && (+ : anchor && (<Popup- anchor={groupRef.current}+ anchor={anchor}placement="bottom-start"// The pointer moves the highlighted row, so `:hover` marks no second one.className="dropdown-menu show tn-dropdown-menu tn-menu-keyboard note-autocomplete-menu"// The list spans the whole field, buttons included.- style={{ width: `${groupRef.current.getBoundingClientRect().width}px` }}+ style={{ width: `${anchor.getBoundingClientRect().width}px` }}escapeDismisses={false}onDismiss={autocomplete.close}>