fix(editor): focus the first refused field with its own error
A user leave with several refused fields focused the first one while the last refusal owned the single error slot. The leave now focuses the first refused field, then re-applies it so its message shows (I1). The add flow no longer re-pins the tile finish() just ended (M3).
This commit is contained in:
1 parent
78fb2d076e
commit
94f8a8bf27
4 files changed
+134
-35
No files matched your search
@@ -131,7 +131,8 @@ function useEditLifecycle(
|
||||
/**
|
||||
* The write path of one reference's edit view. A field applies to the draft when it
|
||||
* loses the focus (`commitField`); `leave` re-applies every field whose text differs from
|
||||
* the document, shows each refusal and focuses the first refused field (P22, P22').
|
||||
* the document, focuses the first refused field and shows that field's refusal (the view
|
||||
* has one error slot; the other refused drafts stay as typed) (P22, P22', I1).
|
||||
* `base` is the DOM id prefix of the tile; `registerLeave` hands `leave` to the library.
|
||||
* `takeFocus` says whether this mount is a user's « Modifier » (it focuses Citation) and
|
||||
* `onDrop` receives the fields of the refused drafts the view is unmounted with (M1).
|
||||
@@ -146,9 +147,12 @@ export function useReferenceEdit(
|
||||
const { edit, applied, apply, pending } = writer
|
||||
const leave: LeaveGuard = () => {
|
||||
const refused = FIELDS.filter(pending).filter((field) => !apply(field))
|
||||
if (refused.length > 0) {
|
||||
document.getElementById(`${base}-${refused[0]}`)?.focus()
|
||||
}
|
||||
if (refused.length === 0) return refused
|
||||
// The focus move runs the blur commit of the field that held it, which writes the
|
||||
// one error slot; re-applying the first refusal last makes the focused field the
|
||||
// one that shows its message (I1). Refused again, it writes nothing.
|
||||
document.getElementById(`${base}-${refused[0]}`)?.focus()
|
||||
apply(refused[0])
|
||||
return refused
|
||||
}
|
||||
useEditLifecycle(base, hooks, leave, writer)
|
||||
|
||||
@@ -86,13 +86,13 @@ export function useReferenceActions(
|
||||
// P24 (and P22): leave the tile in edit first, then a clean view and the draft.
|
||||
startAdd: () => {
|
||||
if (!tiles.finish()) return
|
||||
shown.reset()
|
||||
shown.reset(null)
|
||||
setDraft((serial) => (serial ?? 0) + 1)
|
||||
},
|
||||
// P24: the new tile is shown whatever was typed meanwhile, so the view is reset.
|
||||
added: (id: string) => {
|
||||
setDraft(null)
|
||||
shown.reset()
|
||||
shown.reset(tiles.editingKey)
|
||||
tiles.reveal(`${id}#0`)
|
||||
later({ kind: 'reference', id })
|
||||
},
|
||||
|
||||
@@ -44,14 +44,14 @@ export function useReferenceView(
|
||||
const [seen, setSeen] = useState(resetSignal)
|
||||
const [pinned, setPinned] = useState<Pinned | null>(null)
|
||||
// The reset view is the sheet order: the tile in edit sits at its own position.
|
||||
const pinEdited = (): Pinned | null =>
|
||||
editingKey === null || !keys.includes(editingKey)
|
||||
const pinEdited = (key: string | null): Pinned | null =>
|
||||
key === null || !keys.includes(key)
|
||||
? null
|
||||
: { key: editingKey, index: keys.indexOf(editingKey) }
|
||||
: { key, index: keys.indexOf(key) }
|
||||
if (seen !== resetSignal) {
|
||||
setSeen(resetSignal)
|
||||
setView(INITIAL)
|
||||
setPinned(pinEdited())
|
||||
setPinned(pinEdited(editingKey))
|
||||
}
|
||||
const pinAt = pinned === null ? -1 : keys.indexOf(pinned.key)
|
||||
const pin =
|
||||
@@ -80,9 +80,13 @@ export function useReferenceView(
|
||||
setPinned(pinAfterChange())
|
||||
setView((current) => ({ ...current, ...patch }))
|
||||
},
|
||||
reset: () => {
|
||||
/**
|
||||
* The clean view; `keep` is the tile still in edit, passed by the caller because a
|
||||
* `finish()` in the same handler has not reached this render's `editingKey` (M3).
|
||||
*/
|
||||
reset: (keep: string | null) => {
|
||||
setView(INITIAL)
|
||||
setPinned(pinEdited())
|
||||
setPinned(pinEdited(keep))
|
||||
},
|
||||
/** The tile `key` enters edit: remember where it is shown. */
|
||||
markEdited: (key: string) => setPinned({ key, index: pinAtShown(key) }),
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
/** C1: a read-only flip never drops a draft silently, writes nothing, and keeps the tile. */
|
||||
/**
|
||||
* C1: a read-only flip never drops a draft silently, writes nothing, and keeps the tile.
|
||||
* I1: a user leave with several refused fields focuses the first one, with its own error.
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { act, cleanup, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
@@ -201,7 +204,12 @@ describe('a read-only flip with a refused draft (tab tile)', () => {
|
||||
await editTile(user, 'ref-b', /^Denoix/)
|
||||
await typeBadUrl(user, 'ref-b')
|
||||
cross(800)
|
||||
await flush()
|
||||
const watch = watchNotes()
|
||||
cross(1280)
|
||||
await flush()
|
||||
expect(watch.seen).toHaveLength(0)
|
||||
watch.stop()
|
||||
const item = tile('ref-b')
|
||||
expect(within(item).getByText(`${NOTE} : Adresse web (URL)`)).toBeVisible()
|
||||
expect(field('ref-b', 'url')).toHaveValue('https://exemple.org/Épaule')
|
||||
@@ -337,29 +345,30 @@ describe('a read-only flip without a refused draft', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('a read-only flip in the citations panel', () => {
|
||||
async function openPanel(user: User) {
|
||||
await user.click(screen.getByRole('tab', { name: 'Constats' }))
|
||||
await user.click(screen.getByRole('treeitem', { name: /Cat A/ }))
|
||||
await user.keyboard('{ArrowRight}')
|
||||
await user.click(screen.getByRole('treeitem', { name: /Node A/ }))
|
||||
await user.click(
|
||||
within(screen.getByRole('tabpanel', { name: 'Constats' })).getByRole(
|
||||
'button',
|
||||
{ name: '2 références' },
|
||||
),
|
||||
)
|
||||
await waitFor(() =>
|
||||
expect(document.activeElement).toBe(
|
||||
screen.getByRole('heading', { name: 'Références citées' }),
|
||||
),
|
||||
)
|
||||
const panel = screen.getByTestId('editor-citations-panel')
|
||||
await user.click(within(panel).getByRole('button', { name: /^Denoix/ }))
|
||||
await user.click(within(panel).getByRole('button', { name: 'Modifier' }))
|
||||
return panel
|
||||
}
|
||||
/** The citations panel of Node A, its Denoix tile in edit. */
|
||||
async function openPanel(user: User) {
|
||||
await user.click(screen.getByRole('tab', { name: 'Constats' }))
|
||||
await user.click(screen.getByRole('treeitem', { name: /Cat A/ }))
|
||||
await user.keyboard('{ArrowRight}')
|
||||
await user.click(screen.getByRole('treeitem', { name: /Node A/ }))
|
||||
await user.click(
|
||||
within(screen.getByRole('tabpanel', { name: 'Constats' })).getByRole(
|
||||
'button',
|
||||
{ name: '2 références' },
|
||||
),
|
||||
)
|
||||
await waitFor(() =>
|
||||
expect(document.activeElement).toBe(
|
||||
screen.getByRole('heading', { name: 'Références citées' }),
|
||||
),
|
||||
)
|
||||
const panel = screen.getByTestId('editor-citations-panel')
|
||||
await user.click(within(panel).getByRole('button', { name: /^Denoix/ }))
|
||||
await user.click(within(panel).getByRole('button', { name: 'Modifier' }))
|
||||
return panel
|
||||
}
|
||||
|
||||
describe('a read-only flip in the citations panel', () => {
|
||||
it('shows the note in the panel tile, says it once, focuses the tile header', async () => {
|
||||
const user = await mount()
|
||||
await openPanel(user)
|
||||
@@ -412,3 +421,85 @@ async function typeBadUrlIn(user: User) {
|
||||
await user.clear(url)
|
||||
await user.type(url, 'exemple.org')
|
||||
}
|
||||
|
||||
const REQUIRED = 'Ce champ est obligatoire.'
|
||||
|
||||
/** Citation cleared then a schemeless URL, both refused, in the tile `id` of `host`. */
|
||||
async function twoRefused(user: User, id: string, host = 'tab') {
|
||||
await user.clear(field(id, 'citation', host))
|
||||
await user.tab()
|
||||
const url = field(id, 'url', host)
|
||||
await user.clear(url)
|
||||
await user.type(url, 'exemple.org')
|
||||
}
|
||||
|
||||
/** I1: the field focused by a refused leave shows its own refusal; no draft is lost. */
|
||||
function expectCitationRefused(id: string, host = 'tab') {
|
||||
const citation = field(id, 'citation', host)
|
||||
expect(document.activeElement).toBe(citation)
|
||||
expect(citation).toHaveAttribute('aria-invalid', 'true')
|
||||
expect(citation).toHaveAccessibleDescription(REQUIRED)
|
||||
expect(citation).toHaveValue('')
|
||||
}
|
||||
|
||||
describe('a user leave with several refused fields (I1)', () => {
|
||||
it('keyboard « Terminer » focuses Citation with its own error', async () => {
|
||||
const user = await mount()
|
||||
await editTile(user, 'ref-b', /^Denoix/)
|
||||
await twoRefused(user, 'ref-b')
|
||||
const done = within(tile('ref-b')).getByRole('button', { name: 'Terminer' })
|
||||
for (let step = 0; step < 6 && document.activeElement !== done; step++) {
|
||||
await user.tab()
|
||||
}
|
||||
expect(document.activeElement).toBe(done)
|
||||
await user.keyboard('{Enter}')
|
||||
expect(done).toBeInTheDocument()
|
||||
expectCitationRefused('ref-b')
|
||||
expect(field('ref-b', 'url')).toHaveValue('exemple.org')
|
||||
expect(history()).toEqual({ undo: 0, redo: 0 })
|
||||
})
|
||||
|
||||
it("another tile's « Modifier » focuses Citation with its own error", async () => {
|
||||
const user = await mount()
|
||||
await editTile(user, 'ref-b', /^Denoix/)
|
||||
await twoRefused(user, 'ref-b')
|
||||
await user.click(
|
||||
within(tabPanel()).getByRole('button', { name: /^Barone/ }),
|
||||
)
|
||||
await user.click(
|
||||
within(tile('ref-a')).getByRole('button', { name: 'Modifier' }),
|
||||
)
|
||||
expect(field('ref-a', 'citation')).toBeNull()
|
||||
expectCitationRefused('ref-b')
|
||||
expect(field('ref-b', 'url')).toHaveValue('exemple.org')
|
||||
})
|
||||
|
||||
it('the panel ✕ is cancelled, Citation focused with its own error', async () => {
|
||||
const user = await mount()
|
||||
const panel = await openPanel(user)
|
||||
await twoRefused(user, 'ref-b', 'panel')
|
||||
await user.click(
|
||||
within(panel).getByRole('button', { name: 'Fermer les références' }),
|
||||
)
|
||||
expect(screen.getByTestId('editor-citations-panel')).toBe(panel)
|
||||
expectCitationRefused('ref-b', 'panel')
|
||||
expect(field('ref-b', 'url', 'panel')).toHaveValue('exemple.org')
|
||||
expect(history()).toEqual({ undo: 0, redo: 0 })
|
||||
})
|
||||
|
||||
it('a mouse « Terminer » with a valid URL focused keeps edit on Citation', async () => {
|
||||
const user = await mount()
|
||||
await editTile(user, 'ref-b', /^Denoix/)
|
||||
await user.clear(field('ref-b', 'citation'))
|
||||
await user.tab()
|
||||
const url = field('ref-b', 'url')
|
||||
await user.clear(url)
|
||||
await user.type(url, 'https://exemple.org/neuf')
|
||||
const done = within(tile('ref-b')).getByRole('button', { name: 'Terminer' })
|
||||
await user.click(done)
|
||||
expect(done).toBeInTheDocument()
|
||||
expectCitationRefused('ref-b')
|
||||
expect(doc().references[1].url).toBe('https://exemple.org/neuf')
|
||||
expect(history()).toEqual({ undo: 1, redo: 0 })
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user