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(geomap): bring the results back when the field is come back to
Elian Doran2c33e0b2da8e
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsxindex 747e91268e..90c0b561ce 100644--- a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx@@ -421,6 +421,59 @@ describe("geo map SearchBox", () => {expect(labels()).toEqual([ "Tokyo" ]);});+ it("puts the rows back on Enter once one of them has been taken", async () => {+ mockGeocoder([ TOKYO ]);+ const container = renderSearchBox(fakeMap(), mapNotes());++ const offered = [ "Tokyo trip", "geo-map.search-online(tokyo)" ];++ await type(container, "tokyo");+ expect(labels()).toEqual(offered);++ // The first row is taken, and the list stands down rather than staying open on it.+ await press(container, "Enter");+ expect(entries()).toHaveLength(0);++ // The query was right, so looking at what it offers again should not mean retyping it.+ await press(container, "Enter");+ expect(labels()).toEqual(offered);+ });++ it("puts the rows back on Enter after Escape has sent them away", async () => {+ mockGeocoder([ TOKYO ]);+ const container = renderSearchBox(fakeMap(), mapNotes());++ await type(container, "tokyo");+ await press(container, "Escape");+ expect(entries()).toHaveLength(0);++ await press(container, "Enter");++ expect(labels()).toEqual([ "Tokyo trip", "geo-map.search-online(tokyo)" ]);+ });++ it("puts the rows back when the field is come back to, without a key being pressed", async () => {+ mockGeocoder([ TOKYO ]);+ const container = renderSearchBox(fakeMap(), mapNotes());+ const offered = [ "Tokyo trip", "geo-map.search-online(tokyo)" ];++ await act(async () => { field(container).focus(); });+ await type(container, "tokyo");+ expect(labels()).toEqual(offered);++ // Clicking away from the field, as a press on the map does, takes the list with it.+ await act(async () => { field(container).blur(); });+ await settle();+ expect(entries()).toHaveLength(0);++ // Coming back to the field is asking for what it was offering; Enter is not reaching it+ // while the map has the focus, so it cannot be what brings the list back.+ await act(async () => { field(container).focus(); });+ await settle();++ expect(labels()).toEqual(offered);+ });+it("offers to clear the field only once there is something in it to clear", async () => {mockGeocoder([ TOKYO ]);const container = renderSearchBox(fakeMap(), mapNotes());diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.tsxindex 35d1102192..1884d3c417 100644--- a/apps/client/src/widgets/collections/geomap/SearchBox.tsx+++ b/apps/client/src/widgets/collections/geomap/SearchBox.tsx@@ -1,6 +1,7 @@import "./SearchBox.css";import clsx from "clsx";+import type { TargetedKeyboardEvent } from "preact";import { useCallback, useContext, useRef, useState } from "preact/hooks";import type { Map as MapLibreGLMap } from "maplibre-gl";@@ -170,6 +171,25 @@ export default function SearchBox({ notes, onPickPlace }: SearchBoxProps) {}}, [ provider, map ]);+ /**+ * Puts the rows back on offer, so a query that was right can be looked at again without being+ * retyped: the field is come back to, or Enter is pressed on it with nothing on offer.+ *+ * Only where there is nothing on offer, since the Enter that takes a row arrives here too and+ * would otherwise undo the dismissal it has just caused.+ */+ const offerRowsAgain = useCallback(() => {+ if (!entriesByKey.current.size) {+ setDismissed(false);+ }+ }, []);++ const offerRowsOnEnter = useCallback((e: TargetedKeyboardEvent<HTMLInputElement>) => {+ if (e.key === "Enter") {+ offerRowsAgain();+ }+ }, [ offerRowsAgain ]);+/** Empties the field, which is also what takes a searched place off the map (see `changeQuery`). */const clearSearch = useCallback(() => {changeQuery("");@@ -250,7 +270,11 @@ export default function SearchBox({ notes, onPickPlace }: SearchBoxProps) {isHeading={isHeading}dropdownMinWidth={RESULT_LIST_WIDTH}autoActivate+ openOnFocus+ openOnEnterkeepOpenOnPick+ onFocus={offerRowsAgain}+ onKeyDown={offerRowsOnEnter}placeholder={t("geo-map.search-placeholder")}aria-label={t("geo-map.search")}/>diff --git a/apps/client/src/widgets/react/FormAutocomplete.tsx b/apps/client/src/widgets/react/FormAutocomplete.tsxindex 0f6fc84869..ca54ed52c0 100644--- a/apps/client/src/widgets/react/FormAutocomplete.tsx+++ b/apps/client/src/widgets/react/FormAutocomplete.tsx@@ -28,6 +28,15 @@ interface FormAutocompleteProps extends Omit<FormTextBoxProps, "onChange"> {source(query: string): Promise<string[]>;/** Opens the dropdown as soon as the field receives focus, not just on typing. */openOnFocus?: boolean;+ /**+ * Opens the list on Enter where it is closed, for a box whose entries are what the field is for.+ * A list sent away — by Escape, or by taking something from it — is otherwise only brought back+ * by typing, which means editing a query that was already right.+ *+ * The key is not consumed when it only opens the list, so a form around the field is submitted by+ * it as before.+ */+ openOnEnter?: boolean;/*** Receives a suggestion picked from the dropdown (click or Enter) instead of `onChange`, for a* host that treats a made choice differently from typing — both otherwise arrive as the same@@ -85,7 +94,7 @@ interface FormAutocompleteProps extends Omit<FormTextBoxProps, "onChange"> {* The dropdown is portalled to the body and positioned over everything else, so it is not clipped* by scrolling ancestors. Selecting a suggestion reports it through `onChange`, exactly like typing.*/-export default function FormAutocomplete({ currentValue, onChange, source, openOnFocus, onPick, keepOpenOnPick, renderItem, leading, autoActivate, isHeading, dropdownMinWidth, inputRef, onBlur, onKeyDown, ...restProps }: FormAutocompleteProps) {+export default function FormAutocomplete({ currentValue, onChange, source, openOnFocus, openOnEnter, onPick, keepOpenOnPick, renderItem, leading, autoActivate, isHeading, dropdownMinWidth, inputRef, onFocus, onBlur, onKeyDown, ...restProps }: FormAutocompleteProps) {const ownInputRef = useRef<HTMLInputElement>(null);const inputEl = inputRef ?? ownInputRef;const fieldRef = useRef<HTMLDivElement>(null);@@ -202,6 +211,8 @@ export default function FormAutocomplete({ currentValue, onChange, source, openOe.preventDefault();e.stopPropagation();selectItem(items[activeIndex]);+ } else if (openOnEnter && !isDisabled) {+ setIsOpen(true);}break;@@ -236,7 +247,12 @@ export default function FormAutocomplete({ currentValue, onChange, source, openOsetIsOpen(true);}}}- onFocus={openOnFocus && !isDisabled ? () => setIsOpen(true) : undefined}+ onFocus={(e) => {+ if (openOnFocus && !isDisabled) {+ setIsOpen(true);+ }+ onFocus?.(e);+ }}onBlur={(newValue) => {close();onBlur?.(newValue);