diff --git a/src/components/observation/VertebraPicker.tsx b/src/components/observation/VertebraPicker.tsx index 5ad7b13e..de7b0307 100644 --- a/src/components/observation/VertebraPicker.tsx +++ b/src/components/observation/VertebraPicker.tsx @@ -1,3 +1,103 @@ -export default function VertebraPicker() { - return
VertebraPicker placeholder
+import { useState } from 'react' +import Dialog from '@/components/ui/Dialog' +import Button from '@/components/ui/Button' +import Chip from '@/components/ui/Chip' +import type { Vertebra, VertebralRegion } from '@/domain/schema/observation' +import type { VertebralFormula } from '@/domain/species/types' + +const REGION_LABELS: Record = { + C: 'Cervicales', + T: 'Thoraciques', + L: 'Lombaires', + S: 'Sacrales', + Cd: 'Caudales', +} + +export interface VertebraPickerProps { + open: boolean + onClose: () => void + onConfirm: (vertebrae: Vertebra[]) => void + initial: Vertebra[] + formula: VertebralFormula + title: string +} + +export default function VertebraPicker({ + open, + onClose, + onConfirm, + initial, + formula, + title, +}: VertebraPickerProps) { + const [selected, setSelected] = useState(initial) + const [activeRegion, setActiveRegion] = useState('C') + + const has = (r: VertebralRegion, i: number) => + selected.some((v) => v.region === r && v.index === i) + + const toggle = (r: VertebralRegion, i: number) => { + setSelected((cur) => + has(r, i) + ? cur.filter((v) => !(v.region === r && v.index === i)) + : [...cur, { region: r, index: i }], + ) + } + + const all = (r: VertebralRegion, count: number) => { + setSelected((cur) => { + const rest = cur.filter((v) => v.region !== r) + return [...rest, ...Array.from({ length: count }, (_, k) => ({ region: r, index: k + 1 }))] + }) + } + + const none = (r: VertebralRegion) => + setSelected((cur) => cur.filter((v) => v.region !== r)) + + const count = formula[activeRegion] + + return ( + +
+ {(Object.keys(REGION_LABELS) as VertebralRegion[]).map((r) => ( + + ))} +
+ +
+ {Array.from({ length: count }, (_, k) => k + 1).map((i) => ( + toggle(activeRegion, i)} + > + {activeRegion}{i} + + ))} +
+ +
+ + +
+ +
+ + +
+
+ ) } diff --git a/tests/unit/components.VertebraPicker.test.tsx b/tests/unit/components.VertebraPicker.test.tsx new file mode 100644 index 00000000..a3de780a --- /dev/null +++ b/tests/unit/components.VertebraPicker.test.tsx @@ -0,0 +1,40 @@ +import { describe, it, expect, vi } from 'vitest' +import { render, screen } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import VertebraPicker from '@/components/observation/VertebraPicker' +import { EQUINE_VERTEBRAL_FORMULA } from '@/domain/species/equine/vertebrae' + +describe('VertebraPicker', () => { + it('renders region tabs based on the formula', () => { + render( + {}} + onConfirm={() => {}} + initial={[]} + formula={EQUINE_VERTEBRAL_FORMULA} + title="Vertèbres cervicales" + />, + ) + expect(screen.getByRole('tab', { name: /Cervicales/ })).toBeInTheDocument() + expect(screen.getByRole('tab', { name: /Thoraciques/ })).toBeInTheDocument() + }) + + it('toggles a chip and confirms', async () => { + const onConfirm = vi.fn() + render( + {}} + onConfirm={onConfirm} + initial={[]} + formula={EQUINE_VERTEBRAL_FORMULA} + title="Vertèbres thoraciques" + />, + ) + await userEvent.click(screen.getByRole('tab', { name: /Thoraciques/ })) + await userEvent.click(screen.getByRole('button', { name: 'T12' })) + await userEvent.click(screen.getByRole('button', { name: 'Valider' })) + expect(onConfirm).toHaveBeenCalledWith([{ region: 'T', index: 12 }]) + }) +})