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
client/ui/filterable menus: add a button to clear the filter
Adorian Doran5ef108c069cc
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex afb2550778..239bc1b0d4 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4775,7 +4775,8 @@"no_matches": "No matches","filter": "Filter","filter_placeholder": "Type to filter…",- "back": "Back"+ "back": "Back",+ "clear_filter": "Clear the filter"},"block_reference": {"copy_one": "Copy reference to this block",diff --git a/apps/client/src/widgets/dialogs/note_type_chooser.spec.tsx b/apps/client/src/widgets/dialogs/note_type_chooser.spec.tsxindex 0fffdfa6a2..0070eac933 100644--- a/apps/client/src/widgets/dialogs/note_type_chooser.spec.tsx+++ b/apps/client/src/widgets/dialogs/note_type_chooser.spec.tsx@@ -8,7 +8,10 @@ import { ParentComponent } from "../react/react_utils";vi.mock("bootstrap", () => ({Dropdown: { getOrCreateInstance: () => ({ dispose() {} }) },- Tooltip: class { static getInstance() { return null; } }+ Tooltip: class {+ static getInstance() { return null; }+ dispose() {}+ }}));vi.mock("../../services/i18n", () => ({diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsxindex 572410afb9..6eea688841 100644--- a/apps/client/src/widgets/react/Dropdown.spec.tsx+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx@@ -117,6 +117,46 @@ describe("Dropdown", () => {expect(picked).toEqual([ "JavaScript" ]);});+ it("clears the filter from a button in place of its magnifier, and stays up with focus in the input", async () => {+ const { toggle } = renderDropdown({+ filterable: true, items: [ { title: "Python" }, { title: "JavaScript" } ]+ }, null);+ const key = (name: string) => {+ const event = new KeyboardEvent("keydown", { key: name, bubbles: true, cancelable: true });+ (document.activeElement ?? document.body).dispatchEvent(event);+ };+ const input = () => popup()?.querySelector<HTMLInputElement>("input.tn-menu-filter-input") ?? null;+ const box = () => input()?.closest(".tn-menu-filter-box");+ const magnifier = () => box()?.querySelector(".bx-search") ?? null;+ const clearButton = () => box()?.querySelector<HTMLButtonElement>("button.bx-x") ?? null;+ const rowCount = () => popup()?.querySelectorAll("li.dropdown-item").length;++ click(toggle);+ await vi.waitFor(() => expect(document.activeElement).toBe(popup()));+ expect(magnifier()).not.toBeNull();+ expect(clearButton()).toBeNull();++ key("j");+ await vi.waitFor(() => expect(document.activeElement).toBe(input()));+ expect(rowCount()).toBe(1);+ expect(magnifier()).toBeNull();+ const button = clearButton();+ if (!button) throw new Error("expected the clear button");+ // A press keeps focus in the input.+ const press = new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 0 });+ button.dispatchEvent(press);+ expect(press.defaultPrevented).toBe(true);++ click(button);+ await vi.waitFor(() => expect(input()?.value).toBe(""));+ expect(rowCount()).toBe(2);+ expect(document.activeElement).toBe(input());+ expect(clearButton()).toBeNull();+ expect(magnifier()).not.toBeNull();+ await new Promise((resolve) => setTimeout(resolve));+ expect(popup()).not.toBeNull();+ });+it("filters the rows a filterable submenu of components draws from data, on a desktop and on a phone", async () => {for (const onMobile of [ false, true ]) {layout.onMobile = onMobile;diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsxindex 56e0d6a95f..7ec5b8932a 100644--- a/apps/client/src/widgets/react/FormList.tsx+++ b/apps/client/src/widgets/react/FormList.tsx@@ -10,6 +10,7 @@ import { useContext, useLayoutEffect, useMemo, useRef } from "preact/hooks";import { t } from "../../services/i18n";import { getActionSync } from "../../services/keyboard_actions";import { handleRightToLeftPlacement, isMobile, openInAppHelpFromUrl } from "../../services/utils";+import ActionButton from "./ActionButton";import FormTextBox from "./FormTextBox";import FormToggle from "./FormToggle";import { useStaticTooltip, useSyncedRef, useUniqueName } from "./hooks";@@ -770,7 +771,17 @@ function MenuFilterInput({ filter }: { filter: MenuFilter }) {return (<div className="tn-menu-filter">- <label className="tn-menu-filter-box tn-input-field">+ {/* A press anywhere in the box, the clear button's included, keeps focus in the input,+ and a click in it does not close the dropdown. */}+ <div+ className="tn-menu-filter-box tn-input-field"+ onMouseDown={(e) => {+ if (e.target === inputRef.current) return;+ e.preventDefault();+ inputRef.current?.focus({ preventScroll: true });+ }}+ onClick={(e) => e.stopPropagation()}+ ><FormTextBoxinputRef={inputRef} className="tn-menu-filter-input"data-level={filter.level + 1} currentValue={filter.text}@@ -780,8 +791,13 @@ function MenuFilterInput({ filter }: { filter: MenuFilter }) {? { rowId: filter.rowId, level: filter.level, text }: undefined)}/>- <span className="bx bx-search" aria-hidden="true" />- </label>+ {filter.text+ ? <ActionButton+ className="tn-menu-filter-clear" icon="bx bx-x" text={t("menu.clear_filter")}+ onClick={() => menu.setFilter(undefined)}+ />+ : <span className="bx bx-search" aria-hidden="true" />}+ </div></div>);}diff --git a/apps/client/src/widgets/react/Menu.css b/apps/client/src/widgets/react/Menu.cssindex 96758cc17b..cbb8ad07a7 100644--- a/apps/client/src/widgets/react/Menu.css+++ b/apps/client/src/widgets/react/Menu.css@@ -221,6 +221,11 @@ body.desktop .dropdown-item.dropdown-submenu.submenu-open,cursor: text;}+ .tn-menu-filter-clear {+ /* Takes the 1em of the magnifier it replaces, so the box keeps its size. */+ margin: calc((1em - var(--icon-button-size)) / 2);+ }+.tn-menu-filter-input,.tn-menu-filter-input:hover,.tn-menu-filter-input:focus {