mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +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:
@@ -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