104 lines
3.2 KiB
TypeScript
104 lines
3.2 KiB
TypeScript
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<VertebralRegion, string> = {
|
|
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<Vertebra[]>(initial)
|
|
const [activeRegion, setActiveRegion] = useState<VertebralRegion>('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 (
|
|
<Dialog open={open} onClose={onClose} title={title} size="lg">
|
|
<div role="tablist" className="flex flex-wrap gap-2 mb-4">
|
|
{(Object.keys(REGION_LABELS) as VertebralRegion[]).map((r) => (
|
|
<button
|
|
key={r}
|
|
role="tab"
|
|
aria-selected={activeRegion === r}
|
|
onClick={() => setActiveRegion(r)}
|
|
className={[
|
|
'rounded-full px-4 py-1.5 text-sm font-medium border',
|
|
activeRegion === r
|
|
? 'bg-[var(--color-accent)] text-white border-[var(--color-accent)]'
|
|
: 'bg-white text-[var(--color-ink)] border-[var(--color-border)]',
|
|
].join(' ')}
|
|
>
|
|
{REGION_LABELS[r]}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
<div className="flex flex-wrap gap-2 mb-4 max-h-64 overflow-auto">
|
|
{Array.from({ length: count }, (_, k) => k + 1).map((i) => (
|
|
<Chip
|
|
key={i}
|
|
active={has(activeRegion, i)}
|
|
onClick={() => toggle(activeRegion, i)}
|
|
>
|
|
{activeRegion}{i}
|
|
</Chip>
|
|
))}
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center gap-2 mb-6">
|
|
<Button variant="ghost" size="sm" onClick={() => all(activeRegion, count)}>Tout cocher</Button>
|
|
<Button variant="ghost" size="sm" onClick={() => none(activeRegion)}>Tout décocher</Button>
|
|
</div>
|
|
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="ghost" onClick={onClose}>Annuler</Button>
|
|
<Button onClick={() => onConfirm(selected)}>Valider</Button>
|
|
</div>
|
|
</Dialog>
|
|
)
|
|
}
|