mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
fix(collections): the add-place search reads as one field and takes the focus
The search runs from a small button inside the field, dark once there is something to look for, instead of a greyed-out button beside it, and the row keeps its distance from the address box below. A dialog can now mark the field it starts in, so this one opens in the search rather than in the name above it.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user