mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
fix(dashboard): respect pointer type when focusing trip titles
Mark the title through the existing dialog focus mechanism so a mouse can type right away and touch keeps focus on the dialog. Cover creating and editing a trip in the dashboard tests.
This commit is contained in:
@@ -52,7 +52,6 @@ interface TripFormModalProps {
|
||||
onSave: (data: TripCreateRequest & { date_shift_mode?: DateShiftMode }) => Promise<{ trip?: Trip } | void> | void;
|
||||
trip: Trip | null;
|
||||
onCoverUpdate?: (tripId: number, coverUrl: string | null) => void;
|
||||
focusTitle?: boolean;
|
||||
/**
|
||||
* Drawn inside the trip settings rather than as a dialog of its own (#2811): no
|
||||
* frame and no Cancel, the settings around it close it. One column there, since
|
||||
@@ -69,14 +68,12 @@ export default function TripFormModal({
|
||||
onSave,
|
||||
trip,
|
||||
onCoverUpdate,
|
||||
focusTitle = false,
|
||||
embedded = false,
|
||||
onReviewChange,
|
||||
}: TripFormModalProps) {
|
||||
const isEditing = !!trip;
|
||||
const labelId = useId();
|
||||
const fileRef = useRef<HTMLInputElement>(null);
|
||||
const titleRef = useRef<HTMLInputElement>(null);
|
||||
const toast = useToast();
|
||||
const { t } = useTranslation();
|
||||
const currentUser = useAuthStore((s) => s.user);
|
||||
@@ -524,7 +521,7 @@ export default function TripFormModal({
|
||||
{t('dashboard.tripTitle')} <span className="text-danger">*</span>
|
||||
</label>
|
||||
<input
|
||||
ref={focusTitle && canEditTrip ? titleRef : undefined}
|
||||
data-initial-focus={(!embedded && canEditTrip) || undefined}
|
||||
type="text"
|
||||
value={formData.title}
|
||||
onChange={(e) => canEditTrip && update('title', e.target.value)}
|
||||
@@ -799,7 +796,6 @@ export default function TripFormModal({
|
||||
return (
|
||||
<DialogShell
|
||||
open={isOpen}
|
||||
initialFocusRef={focusTitle ? titleRef : undefined}
|
||||
// In the review step Escape, the close button and a click beside the
|
||||
// dialog go back to the form, like Back, instead of throwing the whole
|
||||
// edit away. Outside that step they close the dialog as before.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef, useState, type ButtonHTMLAttributes, type ClipboardEvent, type CSSProperties, type FormEvent, type KeyboardEvent as ReactKeyboardEvent, type ReactNode, type RefObject } from 'react'
|
||||
import { useEffect, useRef, useState, type ButtonHTMLAttributes, type ClipboardEvent, type CSSProperties, type FormEvent, type KeyboardEvent as ReactKeyboardEvent, type ReactNode } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { Trash2, X } from 'lucide-react'
|
||||
import { useTranslation } from '../../i18n'
|
||||
@@ -34,8 +34,6 @@ export interface DialogShellProps {
|
||||
blocked?: boolean
|
||||
/** A separate dialog is open in front of this one. */
|
||||
covered?: boolean
|
||||
/** Preferred focus on open. If the ref is empty, focus the panel. */
|
||||
initialFocusRef?: RefObject<HTMLElement | null>
|
||||
header: ReactNode
|
||||
footer?: ReactNode
|
||||
/** Makes the scrolling body a form. */
|
||||
@@ -92,7 +90,7 @@ export function DialogShell({ open = true, ...frame }: DialogShellProps) {
|
||||
return open ? <DialogFrame {...frame} /> : null
|
||||
}
|
||||
|
||||
function DialogFrame({ onClose, labelledBy, width = 'detail', fillHeight = false, align = 'center', blocked = false, covered = false, initialFocusRef, header, footer, onSubmit, children, bodyClassName, onPaste, discardGuard }: Omit<DialogShellProps, 'open'>) {
|
||||
function DialogFrame({ onClose, labelledBy, width = 'detail', fillHeight = false, align = 'center', blocked = false, covered = false, header, footer, onSubmit, children, bodyClassName, onPaste, discardGuard }: Omit<DialogShellProps, 'open'>) {
|
||||
const { t } = useTranslation()
|
||||
const panelRef = useRef<HTMLDivElement>(null)
|
||||
const pressedOn = useRef<EventTarget | null>(null)
|
||||
@@ -124,15 +122,12 @@ function DialogFrame({ onClose, labelledBy, width = 'detail', fillHeight = false
|
||||
const focusedInside = !!panel && panel.contains(document.activeElement)
|
||||
const opener = focusedInside ? focusedBefore : document.activeElement
|
||||
const release = lockBodyScroll()
|
||||
if (!focusedInside && panel) {
|
||||
if (initialFocusRef) (initialFocusRef.current ?? panel).focus()
|
||||
else focusDialog(panel)
|
||||
}
|
||||
if (!focusedInside && panel) focusDialog(panel)
|
||||
return () => {
|
||||
release()
|
||||
if (opener instanceof HTMLElement) opener.focus()
|
||||
}
|
||||
}, [focusedBefore, initialFocusRef])
|
||||
}, [focusedBefore])
|
||||
|
||||
const bodyClass = bodyClassName ?? BODY
|
||||
const body = children == null ? null : onSubmit
|
||||
|
||||
@@ -42,8 +42,17 @@ beforeEach(() => {
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
function mockFinePointer() {
|
||||
const matchMedia = window.matchMedia.bind(window);
|
||||
vi.stubGlobal('matchMedia', (query: string) => {
|
||||
const result = matchMedia(query);
|
||||
return query === '(pointer: fine)' ? { ...result, matches: true } : result;
|
||||
});
|
||||
}
|
||||
|
||||
describe('DashboardPage', () => {
|
||||
describe('FE-PAGE-DASH-001: Unauthenticated user is redirected', () => {
|
||||
it('does not render dashboard content when not authenticated', () => {
|
||||
@@ -100,7 +109,8 @@ describe('DashboardPage', () => {
|
||||
});
|
||||
|
||||
describe('FE-PAGE-DASH-005: Create Trip button opens TripFormModal', () => {
|
||||
it('clicking New Trip button opens the trip form modal', async () => {
|
||||
it.each(['mouse', 'touch'])('PR-3305: opening the trip form sets the right focus for %s', async (pointer) => {
|
||||
if (pointer === 'mouse') mockFinePointer();
|
||||
const user = userEvent.setup();
|
||||
render(<DashboardPage />);
|
||||
|
||||
@@ -110,10 +120,9 @@ describe('DashboardPage', () => {
|
||||
|
||||
await user.click(screen.getAllByRole('button', { name: /new trip/i })[0]);
|
||||
|
||||
// TripFormModal opens — "Create New Trip" appears in heading and submit button
|
||||
await waitFor(() => {
|
||||
expect(screen.getAllByText(/create new trip/i).length).toBeGreaterThan(0);
|
||||
});
|
||||
const dialog = await screen.findByRole('dialog', { name: 'Create New Trip' });
|
||||
const target = pointer === 'mouse' ? within(dialog).getByPlaceholderText(/summer in japan/i) : dialog;
|
||||
expect(target).toHaveFocus();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -253,7 +262,8 @@ describe('DashboardPage', () => {
|
||||
});
|
||||
|
||||
describe('FE-PAGE-DASH-012: Edit trip opens form with pre-filled data', () => {
|
||||
it('clicking edit on a trip card opens TripFormModal with trip title pre-filled', async () => {
|
||||
it('PR-3305: focuses the prefilled title when editing a trip', async () => {
|
||||
mockFinePointer();
|
||||
const user = userEvent.setup();
|
||||
render(<DashboardPage />);
|
||||
|
||||
@@ -266,7 +276,7 @@ describe('DashboardPage', () => {
|
||||
|
||||
await waitFor(() => {
|
||||
const titleInput = screen.getByDisplayValue('Paris Adventure');
|
||||
expect(titleInput).toBeInTheDocument();
|
||||
expect(titleInput).toHaveFocus();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -297,7 +297,6 @@ function DashboardPageDesktop(): React.ReactElement {
|
||||
{showForm && (
|
||||
<TripFormModal
|
||||
isOpen={showForm}
|
||||
focusTitle
|
||||
trip={editingTrip}
|
||||
onClose={() => { setShowForm(false); setEditingTrip(null) }}
|
||||
onSave={editingTrip ? handleUpdate : handleCreate}
|
||||
|
||||
Reference in New Issue
Block a user