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(promoted attributes): keep relation fields in the tab order (closes #11447)
matt wilkie92812e615d90
diff --git a/apps/client/src/widgets/PromotedAttributes.spec.tsx b/apps/client/src/widgets/PromotedAttributes.spec.tsxindex 7241a83f0b..b356640305 100644--- a/apps/client/src/widgets/PromotedAttributes.spec.tsx+++ b/apps/client/src/widgets/PromotedAttributes.spec.tsx@@ -7,8 +7,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";// A relation's target is picked in an Algolia autocomplete bound to jQuery, which is not loaded// here; a plain input in its place keeps the cell around it assertable.vi.mock("./react/NoteAutocomplete", () => ({- default: ({ id, noteId }: { id?: string; noteId?: string }) =>- <input id={id} className="note-autocomplete-stub" value={noteId} />+ default: ({ id, noteId, tabIndex }: { id?: string; noteId?: string; tabIndex?: number }) =>+ <input id={id} tabIndex={tabIndex} className="note-autocomplete-stub" value={noteId} />}));// The chip fields have specs of their own; here they hand over their props, which is where the@@ -496,7 +496,10 @@ describe("PromotedAttributesContent rendering", () => {expect(container.querySelector(".promoted-attribute-cell")?.className).toContain("promoted-attribute-relation");- expect(container.querySelector<HTMLInputElement>(".note-autocomplete-stub")?.value).toBe("targetNoteId");+ const input = container.querySelector<HTMLInputElement>(".note-autocomplete-stub");+ expect(input?.value).toBe("targetNoteId");+ // 200 plus the definition's position, as the label fields around it have.+ expect(input?.getAttribute("tabindex")).toBe("210");});it("logs an error for an unknown attribute type and renders no input", async () => {diff --git a/apps/client/src/widgets/PromotedAttributes.tsx b/apps/client/src/widgets/PromotedAttributes.tsxindex b547281de9..7cee7cab02 100644--- a/apps/client/src/widgets/PromotedAttributes.tsx+++ b/apps/client/src/widgets/PromotedAttributes.tsx@@ -341,6 +341,7 @@ function RelationInput({ inputId, ...props }: CellProps & { inputId: string }) {return (<NoteAutocompleteid={inputId}+ tabIndex={200 + props.cell.definitionAttr.position}noteId={props.cell.valueAttr.value}opts={{ allowCreatingNotes: true }}noteIdChanged={async (value) => {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.tsxindex f7b6284f9c..8e2d491eb5 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@@ -94,8 +94,7 @@ describe("RelationValuesInput", () => {await mount({ values: [], onCommit: vi.fn(), inputId: "field-id", tabIndex: 205 });const input = container.querySelector("input");expect(input?.id).toBe("field-id");- // Set onto the box by hand: the field does not carry the attribute through to it.- expect(input?.getAttribute("tabindex")).toBe("205");+ expect(autocomplete.current?.tabIndex).toBe(205);});});diff --git a/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx b/apps/client/src/widgets/attribute_widgets/relation_values_input.tsxindex d36c91e7e7..12df1253ee 100644--- a/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx+++ b/apps/client/src/widgets/attribute_widgets/relation_values_input.tsx@@ -32,14 +32,6 @@ export default function RelationValuesInput({ values, onCommit, inputId, tabIndeconst inputRef = useRef<HTMLInputElement>(null);const notes = useNotes(values);- // Set by hand because {@link NoteAutocomplete} does not carry the attribute through to the box- // it builds, which is the element the focus actually lands on.- useEffect(() => {- if (tabIndex !== undefined) {- inputRef.current?.setAttribute("tabindex", String(tabIndex));- }- }, [ tabIndex ]);-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.@@ -72,6 +64,7 @@ export default function RelationValuesInput({ values, onCommit, inputId, tabInde<NoteAutocompleteid={inputId}inputRef={inputRef}+ 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.tsxnew file mode 100644index 0000000000..e4b302f310--- /dev/null+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx@@ -0,0 +1,40 @@+import { act } from "preact/test-utils";+import { describe, expect, it, vi } from "vitest";++vi.mock("../../services/note_autocomplete", () => ({+ default: { initNoteAutocomplete: vi.fn(), setText: vi.fn() }+}));++import $ from "jquery";++import { renderInto } from "../../test/render";+import NoteAutocomplete from "./NoteAutocomplete";++// The autocomplete plugin is not loaded here, so the calls that clear an empty input are stubbed.+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 = vi.fn();++describe("NoteAutocomplete", () => {+ it("sets tabIndex on the input only when the host gives one", async () => {+ let container = document.createElement("div");+ await act(async () => {+ container = renderInto(<NoteAutocomplete id="ordered" tabIndex={205} />);+ });+ const ordered = container.querySelector("input.note-autocomplete");+ expect(ordered).not.toBeNull();+ expect(ordered?.id).toBe("ordered");+ expect(ordered?.getAttribute("tabindex")).toBe("205");++ await act(async () => {+ container = renderInto(<NoteAutocomplete id="unordered" />);+ });+ const unordered = container.querySelector("input.note-autocomplete");+ expect(unordered).not.toBeNull();+ expect(unordered?.id).toBe("unordered");+ expect(unordered?.hasAttribute("tabindex")).toBe(false);+ });+});diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsxindex 6fc83bd059..d1afe15462 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -21,9 +21,11 @@ interface NoteAutocompleteProps {noteId?: string;/** Shows the selected note without allowing a different one to be picked. */readOnly?: boolean;+ /** Places the input in the tab order of a host that orders its fields with `tabIndex`. */+ tabIndex?: number;}-export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, onChange, onTextChange, container, containerStyle, opts, noteId, noteIdChanged, onKeyDown, onBlur, readOnly }: NoteAutocompleteProps) {+export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, onChange, onTextChange, container, containerStyle, opts, noteId, noteIdChanged, onKeyDown, onBlur, readOnly, tabIndex }: NoteAutocompleteProps) {const ref = useSyncedRef<HTMLInputElement>(externalInputRef);useEffect(() => {@@ -106,6 +108,7 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,ref={ref}className="note-autocomplete form-control"readOnly={readOnly}+ tabIndex={tabIndex}placeholder={placeholder ?? t("add_link.search_note")} /></div>);