125 lines
4.6 KiB
TypeScript
125 lines
4.6 KiB
TypeScript
import * as React from "react"
|
||
import { ChevronsUpDownIcon, UserSearchIcon, XIcon } from "lucide-react"
|
||
|
||
import { cn } from "../lib/utils"
|
||
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "../ui/command"
|
||
import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover"
|
||
import { GenderMark, PatientAvatar, type AvatarPerson } from "./patient-avatar"
|
||
|
||
export interface PickerPatient extends AvatarPerson {
|
||
readonly id: number
|
||
readonly middleName?: string
|
||
readonly birthDate: string
|
||
readonly phone?: string
|
||
readonly cardNumber?: string
|
||
}
|
||
|
||
/** Выбор пациента из картотеки: поиск по ФИО, телефону, номеру карты. */
|
||
export function PatientPicker({
|
||
patients,
|
||
value,
|
||
onChange,
|
||
placeholder = "Найдите пациента",
|
||
invalid,
|
||
className,
|
||
}: {
|
||
patients: readonly PickerPatient[]
|
||
value: number | null
|
||
onChange: (id: number | null) => void
|
||
placeholder?: string
|
||
invalid?: boolean
|
||
className?: string
|
||
}) {
|
||
const [open, setOpen] = React.useState(false)
|
||
const selected = patients.find((p) => p.id === value)
|
||
const name = (p: PickerPatient) => `${p.lastName} ${p.firstName} ${p.middleName ?? ""}`.trim()
|
||
|
||
return (
|
||
<Popover open={open} onOpenChange={setOpen}>
|
||
<PopoverTrigger asChild>
|
||
<button
|
||
type="button"
|
||
role="combobox"
|
||
aria-expanded={open}
|
||
aria-invalid={invalid}
|
||
className={cn(
|
||
"flex min-h-12 w-full items-center gap-3 rounded-lg border border-input bg-transparent px-3 py-2 text-left text-sm shadow-xs transition outline-none",
|
||
"focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-invalid:border-destructive",
|
||
className,
|
||
)}
|
||
>
|
||
{selected ? (
|
||
<>
|
||
<PatientAvatar patient={selected} className="size-8" />
|
||
<span className="min-w-0 flex-1">
|
||
<span className="block truncate font-semibold">
|
||
{name(selected)} <GenderMark gender={selected.gender} className="text-xs" />
|
||
</span>
|
||
<span className="block text-xs text-muted-foreground">
|
||
{selected.birthDate}
|
||
{selected.cardNumber && <> · карта {selected.cardNumber}</>}
|
||
{selected.phone && <> · {selected.phone}</>}
|
||
</span>
|
||
</span>
|
||
<span
|
||
role="button"
|
||
tabIndex={0}
|
||
aria-label="Очистить"
|
||
onClick={(e) => {
|
||
e.stopPropagation()
|
||
onChange(null)
|
||
}}
|
||
onKeyDown={(e) => {
|
||
if (e.key === "Enter") {
|
||
e.stopPropagation()
|
||
onChange(null)
|
||
}
|
||
}}
|
||
className="grid size-6 place-items-center rounded text-muted-foreground hover:bg-muted hover:text-foreground"
|
||
>
|
||
<XIcon className="size-3.5" />
|
||
</span>
|
||
</>
|
||
) : (
|
||
<>
|
||
<UserSearchIcon className="size-4 text-muted-foreground" />
|
||
<span className="flex-1 text-muted-foreground">{placeholder}</span>
|
||
<ChevronsUpDownIcon className="size-4 text-muted-foreground" />
|
||
</>
|
||
)}
|
||
</button>
|
||
</PopoverTrigger>
|
||
<PopoverContent align="start" className="w-(--radix-popover-trigger-width) min-w-80 p-0">
|
||
<Command>
|
||
<CommandInput placeholder="ФИО, телефон или номер карты…" />
|
||
<CommandList>
|
||
<CommandEmpty>Пациент не найден</CommandEmpty>
|
||
<CommandGroup>
|
||
{patients.map((p) => (
|
||
<CommandItem
|
||
key={p.id}
|
||
value={`${name(p)} ${p.phone ?? ""} ${p.cardNumber ?? ""} ${p.id}`}
|
||
onSelect={() => {
|
||
onChange(p.id)
|
||
setOpen(false)
|
||
}}
|
||
className="gap-3"
|
||
>
|
||
<PatientAvatar patient={p} className="size-7" />
|
||
<div className="min-w-0 flex-1">
|
||
<div className="truncate font-medium">{name(p)}</div>
|
||
<div className="text-xs text-muted-foreground">
|
||
{p.birthDate}
|
||
{p.cardNumber && <> · карта {p.cardNumber}</>}
|
||
</div>
|
||
</div>
|
||
</CommandItem>
|
||
))}
|
||
</CommandGroup>
|
||
</CommandList>
|
||
</Command>
|
||
</PopoverContent>
|
||
</Popover>
|
||
)
|
||
}
|