feat(observation): PathologyPicker filtered by species + zones
This commit is contained in:
1 parent
b3e1a87690
commit
4c67c900b9
2 files changed
+121
-2
No files matched your search
@@ -1,3 +1,65 @@
|
|||||||
export default function PathologyPicker() {
|
import { useMemo } from 'react'
|
||||||
return <div>PathologyPicker placeholder</div>
|
import type { Pathology } from '@/domain/schema/pathology'
|
||||||
|
|
||||||
|
export interface PathologyPickerProps {
|
||||||
|
pool: Pathology[]
|
||||||
|
speciesId: string
|
||||||
|
boneIds: string[]
|
||||||
|
selected: string[]
|
||||||
|
onChange: (next: string[]) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
function matches(p: Pathology, speciesId: string, boneIds: string[]): boolean {
|
||||||
|
const speciesOk = p.speciesIds.length === 0 || p.speciesIds.includes(speciesId)
|
||||||
|
if (!speciesOk) return false
|
||||||
|
if (boneIds.length === 0) return true
|
||||||
|
if (p.boneIds.length === 0) return true
|
||||||
|
return p.boneIds.some((b) => boneIds.includes(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function PathologyPicker({
|
||||||
|
pool,
|
||||||
|
speciesId,
|
||||||
|
boneIds,
|
||||||
|
selected,
|
||||||
|
onChange,
|
||||||
|
}: PathologyPickerProps) {
|
||||||
|
const filtered = useMemo(
|
||||||
|
() => pool.filter((p) => matches(p, speciesId, boneIds)),
|
||||||
|
[pool, speciesId, boneIds],
|
||||||
|
)
|
||||||
|
|
||||||
|
function toggle(id: string) {
|
||||||
|
onChange(selected.includes(id) ? selected.filter((x) => x !== id) : [...selected, id])
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filtered.length === 0) {
|
||||||
|
return (
|
||||||
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||||
|
Aucune pathologie de référence pour ces zones. Ajoutez-en une en texte libre ci-dessous.
|
||||||
|
</p>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ul className="space-y-2 max-h-64 overflow-auto pr-1">
|
||||||
|
{filtered.map((p) => (
|
||||||
|
<li key={p.id}>
|
||||||
|
<label className="flex items-start gap-2 cursor-pointer">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={selected.includes(p.id)}
|
||||||
|
onChange={() => toggle(p.id)}
|
||||||
|
className="mt-1 accent-[var(--color-accent)]"
|
||||||
|
aria-label={p.labelFr}
|
||||||
|
/>
|
||||||
|
<div>
|
||||||
|
<div className="text-sm font-medium text-[var(--color-ink)]">{p.labelFr}</div>
|
||||||
|
<div className="text-xs text-[var(--color-ink-muted)]">{p.description}</div>
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import { describe, it, expect, vi } from 'vitest'
|
||||||
|
import { render, screen } from '@testing-library/react'
|
||||||
|
import userEvent from '@testing-library/user-event'
|
||||||
|
import PathologyPicker from '@/components/observation/PathologyPicker'
|
||||||
|
import type { Pathology } from '@/domain/schema/pathology'
|
||||||
|
|
||||||
|
const POOL: Pathology[] = [
|
||||||
|
{
|
||||||
|
id: 'a',
|
||||||
|
labelFr: 'Aïe',
|
||||||
|
description: '',
|
||||||
|
category: 'osseux',
|
||||||
|
speciesIds: ['equine'],
|
||||||
|
boneIds: ['epaule'],
|
||||||
|
sources: [{ type: 'manual', citation: 'X' }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'b',
|
||||||
|
labelFr: 'Aïe-aïe',
|
||||||
|
description: '',
|
||||||
|
category: 'osseux',
|
||||||
|
speciesIds: ['equine'],
|
||||||
|
boneIds: ['hanche'],
|
||||||
|
sources: [{ type: 'manual', citation: 'X' }],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
describe('PathologyPicker', () => {
|
||||||
|
it('shows only pathologies matching zones', () => {
|
||||||
|
render(
|
||||||
|
<PathologyPicker
|
||||||
|
pool={POOL}
|
||||||
|
speciesId="equine"
|
||||||
|
boneIds={['epaule']}
|
||||||
|
selected={[]}
|
||||||
|
onChange={() => {}}
|
||||||
|
/>,
|
||||||
|
)
|
||||||
|
expect(screen.getByLabelText('Aïe')).toBeInTheDocument()
|
||||||
|
expect(screen.queryByLabelText('Aïe-aïe')).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('toggles selection', async () => {
|
||||||
|
const onChange = vi.fn()
|
||||||
|
render(
|
||||||
|
<PathologyPicker
|
||||||
|
pool={POOL}
|
||||||
|
speciesId="equine"
|
||||||
|
boneIds={['epaule']}
|
||||||
|
selected={[]}
|
||||||
|
onChange={onChange}
|
||||||
|
/>,
|
||||||
|
)
|
||||||
|
await userEvent.click(screen.getByLabelText('Aïe'))
|
||||||
|
expect(onChange).toHaveBeenCalledWith(['a'])
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in new issue
Block a user