mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
fix(dashboard): focus the title in desktop trip forms
This commit is contained in:
@@ -52,6 +52,7 @@ 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
|
||||
@@ -68,12 +69,14 @@ 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);
|
||||
@@ -521,6 +524,7 @@ export default function TripFormModal({
|
||||
{t('dashboard.tripTitle')} <span className="text-danger">*</span>
|
||||
</label>
|
||||
<input
|
||||
ref={focusTitle && canEditTrip ? titleRef : undefined}
|
||||
type="text"
|
||||
value={formData.title}
|
||||
onChange={(e) => canEditTrip && update('title', e.target.value)}
|
||||
@@ -795,6 +799,7 @@ 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 } from 'react'
|
||||
import { useEffect, useRef, useState, type ButtonHTMLAttributes, type ClipboardEvent, type CSSProperties, type FormEvent, type KeyboardEvent as ReactKeyboardEvent, type ReactNode, type RefObject } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { Trash2, X } from 'lucide-react'
|
||||
import { useTranslation } from '../../i18n'
|
||||
@@ -34,6 +34,8 @@ 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. */
|
||||
@@ -90,7 +92,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, header, footer, onSubmit, children, bodyClassName, onPaste, discardGuard }: Omit<DialogShellProps, 'open'>) {
|
||||
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'>) {
|
||||
const { t } = useTranslation()
|
||||
const panelRef = useRef<HTMLDivElement>(null)
|
||||
const pressedOn = useRef<EventTarget | null>(null)
|
||||
@@ -122,12 +124,15 @@ 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) focusDialog(panel)
|
||||
if (!focusedInside && panel) {
|
||||
if (initialFocusRef) (initialFocusRef.current ?? panel).focus()
|
||||
else focusDialog(panel)
|
||||
}
|
||||
return () => {
|
||||
release()
|
||||
if (opener instanceof HTMLElement) opener.focus()
|
||||
}
|
||||
}, [focusedBefore])
|
||||
}, [focusedBefore, initialFocusRef])
|
||||
|
||||
const bodyClass = bodyClassName ?? BODY
|
||||
const body = children == null ? null : onSubmit
|
||||
|
||||
@@ -297,6 +297,7 @@ 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