feat(editor): badge selected-count on findings tree intermediate nodes

When drilling through the findings catalog, intermediate categories
and sub-categories now carry a rounded accent-coloured badge that
counts every selected leaf in their subtree. Sibling nodes with no
descendant selections stay badge-free, so the practitioner can scan
a level and see at a glance which branches already hold findings
without re-opening each one.

Implementation is a small `pathStartsWith` helper plus a `countSelectedUnder`
lookup against the current observation's findings — restricted to the
picker's `zoneId` so two parallel pickers on the same card never
cross-contaminate counts.

5 new tests cover: count of 2 visible at root, the same 2 visible one
level deeper on the right sub-branch, no badge on un-selected siblings,
singular aria label for 1 selection, and zone isolation.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Bastien ChanotandClaude committed 2026-05-20 04:45:45 +02:00
1 parent 95d746b555
commit 34acb0cc94
2 files changed
+183 -1

No files matched your search

@@ -25,6 +25,14 @@ function samePath(a: string[], b: string[]): boolean {
return true
}
function pathStartsWith(full: string[], prefix: string[]): boolean {
if (full.length < prefix.length) return false
for (let i = 0; i < prefix.length; i += 1) {
if (full[i] !== prefix[i]) return false
}
return true
}
function findNode(
categories: FindingNode[],
path: string[],
@@ -79,6 +87,16 @@ export default function FindingsTreePicker({
)
}
function countSelectedUnder(nodePath: string[]): number {
let n = 0
for (const f of selected) {
if (f.zoneId !== zoneId) continue
if (!pathStartsWith(f.path, nodePath)) continue
n += 1
}
return n
}
function toggleLeaf(leafPath: string[], leaf: FindingNode) {
if (isLeafSelected(leafPath)) {
onChange(
@@ -185,6 +203,7 @@ export default function FindingsTreePicker({
</li>
)
}
const selectedCount = countSelectedUnder(nodePath)
return (
<li key={node.id}>
<button
@@ -193,7 +212,17 @@ export default function FindingsTreePicker({
className="flex items-center justify-between w-full text-left text-sm px-2 py-1.5 rounded hover:bg-[var(--color-surface-strong)] text-[var(--color-ink)]"
aria-label={`Ouvrir ${node.name_fr}`}
>
<span>{node.name_fr}</span>
<span className="flex items-center gap-2">
<span>{node.name_fr}</span>
{selectedCount > 0 && (
<span
aria-label={`${selectedCount} constat${selectedCount > 1 ? 's' : ''} sélectionné${selectedCount > 1 ? 's' : ''}`}
className="inline-flex items-center justify-center min-w-[20px] h-[20px] px-1.5 rounded-full text-[11px] font-semibold bg-[var(--color-accent)] text-white"
>
{selectedCount}
</span>
)}
</span>
<span aria-hidden="true" className="text-[var(--color-ink-muted)]">
›
</span>
@@ -135,3 +135,156 @@ describe('FindingsTreePicker', () => {
expect(screen.getByLabelText('Ouvrir Dysfonctions')).toBeInTheDocument()
})
})
const DEEP_TREE: ZoneFindings = {
id: 'atm',
name_fr: 'ATM',
name_en: 'TMJ',
categories: [
{
id: 'dysf',
name_fr: 'Dysfonctions ostéopathiques de l\'ATM',
name_en: 'Osteopathic dysfunctions of the TMJ',
children: [
{
id: 'tensions',
name_fr: 'Tensions des muscles masticateurs',
name_en: 'Masticatory muscle tensions',
children: [
{
id: 'temporal',
name_fr: 'Tension du temporal',
name_en: 'Temporal tension',
},
{
id: 'pterygo',
name_fr: 'Tension des ptérygoïdiens',
name_en: 'Pterygoid tension',
},
{
id: 'masseter',
name_fr: 'Tension des masséters',
name_en: 'Masseter tension',
},
],
},
{
id: 'capsule',
name_fr: 'Dysfonction capsulaire',
name_en: 'Capsular dysfunction',
children: [
{ id: 'cap-ant', name_fr: 'Antérieure', name_en: 'Anterior' },
{ id: 'cap-post', name_fr: 'Postérieure', name_en: 'Posterior' },
],
},
],
},
],
}
describe('FindingsTreePicker selection counts on intermediate nodes', () => {
const selected: Finding[] = [
{
zoneId: 'atm',
path: ['dysf', 'tensions', 'temporal'],
labelFrSnapshot: 'Tension du temporal',
labelEnSnapshot: 'Temporal tension',
},
{
zoneId: 'atm',
path: ['dysf', 'tensions', 'pterygo'],
labelFrSnapshot: 'Tension des ptérygoïdiens',
labelEnSnapshot: 'Pterygoid tension',
},
]
it('shows a count badge of 2 on the top-level category when two descendant leaves are selected', () => {
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={selected}
onChange={() => {}}
/>,
)
// Root view: only the top-level "Dysfonctions..." category is visible.
expect(
screen.getByLabelText('2 constats sélectionnés'),
).toBeInTheDocument()
})
it('shows a count badge of 2 on the sub-category one level deeper', async () => {
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={selected}
onChange={() => {}}
/>,
)
await userEvent.click(
screen.getByLabelText('Ouvrir Dysfonctions ostéopathiques de l\'ATM'),
)
// Now we see "Tensions des muscles masticateurs" (2 leaves selected)
// and "Dysfonction capsulaire" (0 leaves selected — no badge).
expect(
screen.getByLabelText('2 constats sélectionnés'),
).toBeInTheDocument()
})
it('does not show a badge for siblings without selected descendants', async () => {
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={selected}
onChange={() => {}}
/>,
)
await userEvent.click(
screen.getByLabelText('Ouvrir Dysfonctions ostéopathiques de l\'ATM'),
)
// Capsular dysfunction has no selections — the button should not carry a badge label.
const capsular = screen.getByLabelText('Ouvrir Dysfonction capsulaire')
expect(capsular.textContent ?? '').not.toMatch(/\d/)
})
it('shows a singular badge label for exactly one selection', () => {
const oneSelected: Finding[] = [selected[0]]
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={oneSelected}
onChange={() => {}}
/>,
)
expect(
screen.getByLabelText('1 constat sélectionné'),
).toBeInTheDocument()
})
it('ignores selections from other zones when counting', () => {
const cross: Finding[] = [
{
zoneId: 'sphenoid',
path: ['dysf', 'tensions', 'temporal'],
labelFrSnapshot: 'Tension du temporal',
labelEnSnapshot: 'Temporal tension',
},
]
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={cross}
onChange={() => {}}
/>,
)
// No badge should appear on the ATM tree — the lone selected finding
// belongs to a different zone.
expect(
screen.queryByLabelText(/constat sélectionné/),
).not.toBeInTheDocument()
})
})