fix(editor): stale cite choice reset, pending line under the add row

This commit is contained in:
bchanot committed 2026-11-15 20:00:28 +01:00
1 parent 70eb2fcbb4
commit 1d119c3684
6 files changed
+91 -29

No files matched your search

+18 -1
View File
@@ -493,7 +493,7 @@ test.describe('references dialog, edit mode', () => {
expect(nodeOf(saved).reference_ids).toEqual(['ref-a', 'ref-b', 'ref-c'])
})
test('keyboard: ArrowDown on the select only chooses, ✕ closes without asking; axe', async ({
test('ArrowDown only chooses, a refused save stays level, ✕ asks nothing; axe', async ({
page,
}) => {
test.setTimeout(150_000)
@@ -507,6 +507,23 @@ test.describe('references dialog, edit mode', () => {
await expect(cite).toBeEnabled()
await expect(draftRows(page)).toHaveCount(3)
await expectAccessible(page, 'the edit mode with a choice not cited')
// « Enregistrer » refuses the pending choice; its line leaves both buttons level.
await inDialog(page, dict['editor.findings.save']).click()
await expect(citationsDialog(page).getByRole('alert')).toHaveText(
dict['editor.citations.pending_choice'],
)
await expect(cite).toBeFocused()
const middle = async (target: Locator) => {
const box = (await target.boundingBox())!
return box.y + box.height / 2
}
const level = await middle(citeSelect(page))
for (const control of [
cite,
inDialog(page, dict['editor.citations.new_reference']),
]) {
expect(Math.abs((await middle(control)) - level)).toBeLessThanOrEqual(2)
}
await inDialog(page, dict['common.close']).click()
await expect(page.locator('dialog[open]')).toHaveCount(1)
await expect(citationsDialog(page).getByText(confirmText)).toHaveCount(0)
@@ -2,7 +2,7 @@ import { useEffect, useRef } from 'react'
import type { Reference } from '@faunosteo/core-wasm/types'
import Badge from '@/components/ui/Badge'
import Button from '@/components/ui/Button'
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
import FormField from '@/components/ui/FormField'
import Select from '@/components/ui/Select'
import type { SelectOption } from '@/components/editor/fields/SelectField'
import { useSheetDocument } from '@/editor/sheetDocument'
@@ -37,6 +37,9 @@ export interface DraftListProps {
/** The add choice: its id, so the focus can land on it once the last row is removed. */
const ADD_ID = 'citations-add-reference'
/** The line of a choice not cited at « Enregistrer » (Rev 6), described by the select. */
const PENDING_ID = 'citations-pending-choice'
/** « Citer »: its id, where a refused save puts the focus. */
export const CITE_ID = 'citations-cite-reference'
@@ -131,9 +134,6 @@ function DraftRow({ id, props, onRemove }: RowProps) {
function CiteField({ props }: { props: DraftListProps }) {
const t = useT()
const { choice } = props
const error = choice.pending
? t('editor.citations.pending_choice')
: undefined
const cite = () => {
props.onAdd(choice.chosen)
choice.onChoose('')
@@ -141,18 +141,14 @@ function CiteField({ props }: { props: DraftListProps }) {
}
return (
<div className="min-w-0 flex-1">
<FormField
id={ADD_ID}
label={t('editor.findings.reference_add')}
error={error}
>
<FormField id={ADD_ID} label={t('editor.findings.reference_add')}>
<div className="flex items-center gap-2">
<Select
id={ADD_ID}
value={choice.chosen}
onChange={(event) => choice.onChoose(event.target.value)}
aria-invalid={choice.pending}
aria-describedby={fieldDescribedBy(ADD_ID, undefined, error)}
aria-describedby={choice.pending ? PENDING_ID : undefined}
className="min-w-0 flex-1"
>
{choice.options.map((option) => (
@@ -176,20 +172,35 @@ function CiteField({ props }: { props: DraftListProps }) {
)
}
/** The way to cite an existing reference (edit tier only), and « Nouvelle référence ». */
/**
* The way to cite an existing reference (edit tier only), and « Nouvelle référence »; the
* pending line goes under the whole row, so both buttons stay level with the select.
*/
function AddRow({ props }: { props: DraftListProps }) {
const t = useT()
const pending = props.choice.pending && !props.readOnly
return (
<div className="flex flex-wrap items-end gap-2">
{!props.readOnly && <CiteField props={props} />}
<Button
type="button"
variant="secondary"
disabled={props.readOnly}
onClick={(event) => props.onNew(event.currentTarget)}
>
{t('editor.citations.new_reference')}
</Button>
<div className="space-y-1">
<div className="flex flex-wrap items-end gap-2">
{!props.readOnly && <CiteField props={props} />}
<Button
type="button"
variant="secondary"
disabled={props.readOnly}
onClick={(event) => props.onNew(event.currentTarget)}
>
{t('editor.citations.new_reference')}
</Button>
</div>
{pending && (
<p
id={PENDING_ID}
role="alert"
className="text-sm text-[var(--color-danger)]"
>
{t('editor.citations.pending_choice')}
</p>
)}
</div>
)
}
@@ -161,6 +161,9 @@ function useCiteChoice(ids: readonly string[]) {
const options = useCitableOptions(ids)
const [state, setState] = useState({ id: '', asked: false })
const chosen = options.some((o) => o.value === state.id) ? state.id : ''
// A choice no longer offered is dropped (a render-time adjustment, no effect): its
// reference coming back by a redo never brings it, or its line, back.
if (state.id !== '' && chosen === '') setState({ id: '', asked: false })
return {
options,
chosen,
@@ -30,10 +30,6 @@ export default function CitersList({
// The current family's row says « cette famille » once, without a second badge (Rev 6).
const isCurrent = (citer: CiterItem) =>
current !== undefined && sameKey(citer.key, current)
const ownLabel = elementLabel(
'editor.references.this_finding',
current ?? null,
)
// « Aller à » alone repeats per citer: the name says where each one goes (M6).
const where = (citer: CiterItem) =>
[...citer.trail, citer.name].map((name) => name[locale]).join(' › ')
@@ -53,7 +49,12 @@ export default function CitersList({
{citer.name[locale]}
{isCurrent(citer) && (
<span className="ml-1 rounded-md bg-[var(--color-surface-strong)] px-2 py-0.5 text-xs">
{t(ownLabel)}
{t(
elementLabel(
'editor.references.this_finding',
current ?? null,
),
)}
</span>
)}
{citer.isFamily && !isCurrent(citer) && (
@@ -34,8 +34,10 @@ function useFilterFinding(layer: ReferenceLayer) {
const gone = doc !== null && layer.filter !== null && finding === null
// The vanished finding is the trigger; the move and its wording are the latest ones.
const clear = useRef<() => void>(() => undefined)
const gonePhrase = elementLabel('editor.references.filter_gone', layer.filter)
clear.current = () => layer.clearFilter(t(gonePhrase))
clear.current = () =>
layer.clearFilter(
t(elementLabel('editor.references.filter_gone', layer.filter)),
)
useEffect(() => {
if (gone) clear.current()
}, [gone])
@@ -344,6 +344,8 @@ describe('« Citer » (Rev 6)', () => {
await editCitations(user)
expect(citeButton()).toBeDisabled()
await user.selectOptions(select(), 'ref-b')
// happy-dom moves nothing on a key: this covers the change event only. The real
// ArrowDown is proven in Chromium (e2e/shell/editor-citations.spec.ts, keyboard test).
fireEvent.keyDown(select(), { key: 'ArrowDown' })
fireEvent.change(select(), { target: { value: 'ref-b' } })
expect(rows()).toEqual(['ref-a', 'ref-c'])
@@ -387,6 +389,32 @@ describe('« Citer » (Rev 6)', () => {
expect(citeButton()).toBeDisabled()
})
it('a vanished choice stays reset when its reference comes back by an undo', async () => {
const user = await mount()
await editCitations(user)
await user.selectOptions(select(), 'ref-b')
await user.click(button('Enregistrer'))
expect(inCitations().getByRole('alert')).toBeInTheDocument()
act(() => {
store.getState().commit({
op: 'remove',
key: { kind: 'reference', id: 'ref-b' },
})
})
await waitFor(() => expect(select()).toHaveValue(''))
act(() => {
store.getState().undoStep()
})
await waitFor(() =>
expect(
inCitations().getByRole('option', { name: 'Denoix' }),
).toBeInTheDocument(),
)
expect(select()).toHaveValue('')
expect(citeButton()).toBeDisabled()
expect(inCitations().queryByRole('alert')).toBeNull()
})
it('« Enregistrer » with a choice not cited refuses: the line, focus on « Citer »', async () => {
const user = await mount()
await editCitations(user)