diff --git a/apps/app/src/components/editor/findings/useItemMode.ts b/apps/app/src/components/editor/findings/useItemMode.ts index e935ae67..de5a69b2 100644 --- a/apps/app/src/components/editor/findings/useItemMode.ts +++ b/apps/app/src/components/editor/findings/useItemMode.ts @@ -13,6 +13,24 @@ function useFocusEditButton(editing: boolean, buttonId: string) { } } +const FIRST_DRAFT_CONTROL = + 'form[data-draft-form] :is(input, select, textarea, button):not([disabled])' + +/** « Modifier » unmounts on the edit mode: the draft's first control takes the focus. */ +function useFocusDraft(editing: boolean, buttonId: string) { + const dialog = useRef(null) + useEffect(() => { + if (!editing || !dialog.current) return + const first = dialog.current.querySelector(FIRST_DRAFT_CONTROL) + dialog.current = null + first?.focus() + }, [editing]) + return () => { + dialog.current = + document.getElementById(buttonId)?.closest('dialog') ?? null + } +} + /** * The local mode of a dialog (the finding dialog, the references dialog): view or edit, * and whether a save was just made. No history entry. `buttonId` is its « Modifier ». @@ -20,9 +38,13 @@ function useFocusEditButton(editing: boolean, buttonId: string) { export function useItemMode(buttonId: string) { const [mode, setMode] = useState({ editing: false, saved: false }) const returnToEdit = useFocusEditButton(mode.editing, buttonId) + const focusDraft = useFocusDraft(mode.editing, buttonId) return { ...mode, - startEdit: () => setMode({ editing: true, saved: false }), + startEdit: () => { + focusDraft() + setMode({ editing: true, saved: false }) + }, done: (saved: boolean) => { returnToEdit() setMode({ editing: false, saved }) diff --git a/apps/app/src/components/editor/references/ReferenceTile.tsx b/apps/app/src/components/editor/references/ReferenceTile.tsx index bfa9d504..dc6c9d00 100644 --- a/apps/app/src/components/editor/references/ReferenceTile.tsx +++ b/apps/app/src/components/editor/references/ReferenceTile.tsx @@ -105,7 +105,8 @@ function TileHeader({ base, props }: { base: string; props: TileProps }) { function TileActions({ props }: { props: TileProps }) { const t = useT() const { position, count, citers, readOnly, movable, on } = props - if (readOnly || !on.edit) return null + const { edit } = on + if (readOnly || !edit) return null return ( <>
@@ -113,7 +114,7 @@ function TileActions({ props }: { props: TileProps }) { readOnly={false} variant="secondary" size="sm" - onClick={(event) => on.edit?.(event.currentTarget)} + onClick={(event) => edit(event.currentTarget)} > {t('editor.references.edit')} diff --git a/apps/app/tests/unit/editor-citations-edit.test.tsx b/apps/app/tests/unit/editor-citations-edit.test.tsx index a65c363b..3028cbbf 100644 --- a/apps/app/tests/unit/editor-citations-edit.test.tsx +++ b/apps/app/tests/unit/editor-citations-edit.test.tsx @@ -202,6 +202,14 @@ describe('the « Modifier » of the references dialog', () => { expect(inCitations().queryByRole('button', { name: 'Aller à' })).toBeNull() }) + it('puts the focus on the first control of the draft', async () => { + const user = await mount() + await editCitations(user) + const active = document.activeElement + expect(active).not.toBe(document.body) + expect(active?.closest('form[data-draft-form]')).not.toBeNull() + }) + it('is not offered read only (below 1 024 px)', async () => { const user = await mount(900) await openCitations(user) diff --git a/apps/app/tests/unit/editor-finding-dialog.test.tsx b/apps/app/tests/unit/editor-finding-dialog.test.tsx index 0adf479f..812135c4 100644 --- a/apps/app/tests/unit/editor-finding-dialog.test.tsx +++ b/apps/app/tests/unit/editor-finding-dialog.test.tsx @@ -415,6 +415,15 @@ describe('read-only flip and the gone rule', () => { expect(field).toHaveFocus() }) + it('« Modifier » puts the focus on the first control of the draft', async () => { + const user = await mount() + await openFinding(user, /Cat A/) + await user.click(inDialog().getByRole('button', { name: 'Modifier' })) + const active = document.activeElement + expect(active).not.toBe(document.body) + expect(active?.closest('form[data-draft-form]')).not.toBeNull() + }) + it('a flip to read-only while « Modifier » holds the focus keeps it in the dialog (the tab row is inert)', async () => { const user = await mount() await openFinding(user, /Cat A/)