fix(dashboard): focus the title in desktop trip forms

This commit is contained in:
Luffy
2026-12-02 18:00:36 +08:00
committed by Maurice
parent 748434fc47
commit 7a66a54e66
3 changed files with 15 additions and 4 deletions
+9 -4
View File
@@ -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