Skip to content

Build your personal knowledge base with Trilium Notes

fix(forms): activate focused list item with Enter/Space (closes #8330)

Elian Doranf6a5051520db
diff --git a/apps/client/src/widgets/react/FormList.spec.tsx b/apps/client/src/widgets/react/FormList.spec.tsx
new file mode 100644
index 0000000000..38f5ec5492
--- /dev/null
+++ b/apps/client/src/widgets/react/FormList.spec.tsx
@@ -0,0 +1,89 @@
+import { type ComponentChildren, render } from "preact";
+import { describe, expect, it, vi } from "vitest";
+
+// FormList instantiates a real Bootstrap dropdown in an effect; stub it so the
+// component mounts without pulling in Bootstrap's layout-dependent machinery.
+vi.mock("bootstrap", () => ({
+ Dropdown: { getOrCreateInstance: () => ({ dispose() {} }) },
+ Tooltip: class { static getInstance() { return null; } }
+}));
+
+import FormList, { FormListItem } from "./FormList";
+
+describe("FormList keyboard activation", () => {
+ it.each([ "Enter", " " ])("activates the focused item on %j like a click", (key) => {
+ const { container, onSelect } = renderList();
+ const item = getItem(container, "text");
+
+ const event = press(item, key);
+
+ expect(onSelect).toHaveBeenCalledExactlyOnceWith("text");
+ expect(event.defaultPrevented).toBe(true);
+ });
+
+ it("ignores keys other than Enter/Space", () => {
+ const { container, onSelect } = renderList();
+
+ const event = press(getItem(container, "text"), "a");
+
+ expect(onSelect).not.toHaveBeenCalled();
+ expect(event.defaultPrevented).toBe(false);
+ });
+
+ it("does not activate a disabled item", () => {
+ const { container, onSelect } = renderList();
+
+ press(getItem(container, "code"), "Enter");
+
+ expect(onSelect).not.toHaveBeenCalled();
+ });
+
+ it("leaves typing inside an embedded input alone", () => {
+ const onSelect = vi.fn();
+ const container = mount(
+ <FormList onSelect={onSelect}>
+ <input className="embedded-search" />
+ <FormListItem value="text">Text</FormListItem>
+ </FormList>
+ );
+ const input = container.querySelector<HTMLInputElement>(".embedded-search");
+ expect(input).not.toBeNull();
+
+ const event = press(input as HTMLInputElement, "Enter");
+
+ expect(onSelect).not.toHaveBeenCalled();
+ expect(event.defaultPrevented).toBe(false);
+ });
+});
+
+function renderList() {
+ const onSelect = vi.fn();
+ const container = mount(
+ <FormList onSelect={onSelect}>
+ <FormListItem value="text">Text</FormListItem>
+ <FormListItem value="code" disabled>Code</FormListItem>
+ </FormList>
+ );
+ return { container, onSelect };
+}
+
+function mount(node: ComponentChildren) {
+ const container = document.createElement("div");
+ document.body.appendChild(container);
+ render(node, container);
+ return container;
+}
+
+function getItem(container: HTMLElement, value: string) {
+ const item = container.querySelector<HTMLElement>(`.dropdown-item[data-value="${value}"]`);
+ if (!item) {
+ throw new Error(`No dropdown item with value "${value}"`);
+ }
+ return item;
+}
+
+function press(el: Element, key: string) {
+ const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true });
+ el.dispatchEvent(event);
+ return event;
+}
diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsx
index af3b9a8dcb..ae68824217 100644
--- a/apps/client/src/widgets/react/FormList.tsx
+++ b/apps/client/src/widgets/react/FormList.tsx
@@ -65,7 +65,7 @@ export default function FormList({ children, onSelect, style, fullHeight, wrappe
if (value && onSelect) {
onSelect(value);
}
- }}>
+ }} onKeyDown={onDropdownMenuKeyDown}>
{children}
</div>
</div>
@@ -73,6 +73,32 @@ export default function FormList({ children, onSelect, style, fullHeight, wrappe
);
}
+/**
+ * Activates the currently focused list item on Enter/Space, so the list is keyboard-operable
+ * and not mouse-only. The focused item is "clicked" to reuse the existing item- and list-level
+ * click handlers (e.g. {@link FormList}'s `onSelect`, toggle items).
+ */
+function onDropdownMenuKeyDown(e: KeyboardEvent) {
+ if (e.key !== "Enter" && e.key !== " ") {
+ return;
+ }
+
+ const target = e.target as HTMLElement;
+
+ // Let inputs/textareas rendered inside the list handle their own typing.
+ if (target.closest("input, textarea, select")) {
+ return;
+ }
+
+ const dropdownItem = target.closest(".dropdown-item") as HTMLElement | null;
+ if (!dropdownItem || dropdownItem.classList.contains("disabled")) {
+ return;
+ }
+
+ e.preventDefault();
+ dropdownItem.click();
+}
+
export interface FormListBadge {
className?: string;
text: string;