Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(collections): the add-place search reads as one field and takes the focus

Mauricea1f063c70399
diff --git a/client/src/components/Collections/AddPlaceToCollectionModal.tsx b/client/src/components/Collections/AddPlaceToCollectionModal.tsx
index 39d3c45a6..bbceb68e0 100644
--- a/client/src/components/Collections/AddPlaceToCollectionModal.tsx
+++ b/client/src/components/Collections/AddPlaceToCollectionModal.tsx
@@ -21,7 +21,7 @@ import { useAddPlaceToCollection } from './useAddPlaceToCollection'
* scrolled, so it looked like the button had vanished (#1921). The negative
* margins let its background cover the body's padding while it is stuck.
*/
-const STICKY_SEARCH = 'sticky top-0 z-20 -mx-6 -mt-5 bg-surface-card px-6 pb-1 pt-5'
+const STICKY_SEARCH = 'sticky top-0 z-20 -mx-6 -mt-5 bg-surface-card px-6 pb-4 pt-5'
/** A chip of the category row; the picked one wears the category's own colour. */
const CHIP = 'inline-flex items-center gap-1.5 rounded-full border px-3 py-1 font-semibold'
const CHIP_OFF = 'border-edge-faint bg-surface-card text-content-muted hover:text-content'
@@ -97,29 +97,34 @@ export default function AddPlaceToCollectionModal({ isOpen, collectionId, collec
{/* Search: picking a result fills the name and the location below. */}
<div className={STICKY_SEARCH}>
<div className="relative">
- <div className="flex items-stretch gap-2">
- <div className="relative min-w-0 flex-1">
- <Search size={14} className="pointer-events-none absolute start-3 top-1/2 -translate-y-1/2 text-content-faint" aria-hidden="true" />
- <input
- autoFocus
- ref={searchRef}
- type="text"
- value={query}
- onChange={e => { setQuery(e.target.value); setNoResults(false) }}
- onKeyDown={e => { if (e.key === 'Enter') { e.preventDefault(); void search() } }}
- aria-label={t('collections.addPlaceSearch')}
- placeholder={t('collections.addPlaceSearch')}
- className={`${INPUT} ps-8`}
- />
- </div>
- <DialogButton
- variant="primary"
+ {/* One field: the search runs from a small button inside its end, dark
+ once there is something to look for, instead of a greyed-out button
+ of its own beside the field. */}
+ <div className="relative">
+ <Search size={14} className="pointer-events-none absolute start-3 top-1/2 -translate-y-1/2 text-content-faint" aria-hidden="true" />
+ <input
+ autoFocus
+ data-initial-focus
+ ref={searchRef}
+ type="text"
+ value={query}
+ onChange={e => { setQuery(e.target.value); setNoResults(false) }}
+ onKeyDown={e => { if (e.key === 'Enter') { e.preventDefault(); void search() } }}
+ aria-label={t('collections.addPlaceSearch')}
+ placeholder={t('collections.addPlaceSearch')}
+ className={`${INPUT} pe-[104px] ps-8`}
+ />
+ <button
+ type="button"
onClick={() => void search()}
disabled={!query.trim() || searching}
- icon={searching ? <Loader2 size={14} className="animate-spin" /> : <Search size={14} strokeWidth={2.2} />}
+ className={`absolute end-1.5 top-1/2 inline-flex h-7 -translate-y-1/2 items-center gap-1.5 rounded-lg border-0 px-2.5 text-caption font-semibold transition-colors ${
+ query.trim() ? 'cursor-pointer bg-accent text-accent-text' : 'cursor-default bg-transparent text-content-faint'
+ }`}
>
+ {searching ? <Loader2 size={13} className="animate-spin" /> : <Search size={13} strokeWidth={2.2} />}
{t('common.search')}
- </DialogButton>
+ </button>
</div>
{(results.length > 0 || noResults) && (
<div
diff --git a/client/src/components/shared/dialogFocus.test.ts b/client/src/components/shared/dialogFocus.test.ts
new file mode 100644
index 000000000..ece940a41
--- /dev/null
+++ b/client/src/components/shared/dialogFocus.test.ts
@@ -0,0 +1,52 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { focusDialog } from './dialogFocus';
+
+function panelWith(html: string): HTMLElement {
+ const panel = document.createElement('div');
+ panel.tabIndex = -1;
+ panel.innerHTML = html;
+ document.body.appendChild(panel);
+ return panel;
+}
+
+describe('focusDialog', () => {
+ const realMatchMedia = window.matchMedia;
+ afterEach(() => {
+ window.matchMedia = realMatchMedia;
+ document.body.innerHTML = '';
+ });
+
+ const desktop = () => {
+ Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: 1400 });
+ window.matchMedia = vi
+ .fn()
+ .mockImplementation((q: string) => ({
+ matches: q.includes('pointer: fine'),
+ media: q,
+ addEventListener: vi.fn(),
+ removeEventListener: vi.fn(),
+ })) as unknown as typeof window.matchMedia;
+ };
+
+ it('FE-DIALOGFOCUS-001: on a desktop the first text field takes the focus', () => {
+ desktop();
+ const panel = panelWith('<input id="a" type="text"><input id="b" type="text">');
+ focusDialog(panel);
+ expect(document.activeElement?.id).toBe('a');
+ });
+
+ it('FE-DIALOGFOCUS-002: a field marked as the start wins over the first one', () => {
+ desktop();
+ const panel = panelWith('<input id="a" type="text"><input id="b" type="text" data-initial-focus>');
+ focusDialog(panel);
+ expect(document.activeElement?.id).toBe('b');
+ });
+
+ it('FE-DIALOGFOCUS-003: a focus already inside stays where it is', () => {
+ desktop();
+ const panel = panelWith('<input id="a" type="text"><input id="b" type="text" data-initial-focus>');
+ (panel.querySelector('#a') as HTMLElement).focus();
+ focusDialog(panel);
+ expect(document.activeElement?.id).toBe('a');
+ });
+});
diff --git a/client/src/components/shared/dialogFocus.ts b/client/src/components/shared/dialogFocus.ts
index af72ba56a..43c91ebad 100644
--- a/client/src/components/shared/dialogFocus.ts
+++ b/client/src/components/shared/dialogFocus.ts
@@ -50,7 +50,10 @@ function typingDevice(): boolean {
*/
export function focusDialog(panel: HTMLElement): void {
if (panel.contains(document.activeElement)) return
- const field = typingDevice() ? firstTextField(panel) : null
+ // A dialog that starts somewhere other than its first field marks that field,
+ // as the add-place dialog does with its search below the name in its head.
+ const marked = panel.querySelector<HTMLElement>('[data-initial-focus]')
+ const field = typingDevice() ? (marked ?? firstTextField(panel)) : null
;(field ?? panel).focus({ preventScroll: true })
}