import { useEffect } from 'react'; import { Controller, useFormContext, useWatch } from 'react-hook-form'; import { FhirValueSetSearch } from '../../components'; import { useValueSetServiceContext } from '../../services'; import { Input } from '../ui/input.tsx'; import { Label } from '../ui/label.tsx'; import { Textarea } from '../ui/textarea.tsx'; import { SEVERITY_OPTIONS, severityKeyForCode } from './severityOptions.ts'; import './SimpleConditionForm.css'; export interface SimpleConditionFormProps { isNew: boolean; disabled?: boolean; } // Presentation only - the server (Condition handlers) authorizes what a patient may write. const SimpleConditionForm = ({ isNew, disabled }: SimpleConditionFormProps) => { const { control, setValue, getValues } = useFormContext(); const { valueSetUrls } = useValueSetServiceContext(); // transformToFhirCondition only emits onsetDateTime when onsetType === 'dateTime'. useEffect(() => { // Default onset to a date only when creating, or when the loaded onset is empty/already a date. // Never clobber a non-dateTime onset (e.g. a practitioner-recorded onsetAge). const currentOnsetType = getValues('onsetType'); if (isNew || !currentOnsetType || currentOnsetType === 'dateTime') { setValue('onsetType', 'dateTime'); } }, [isNew, getValues, setValue]); const clinicalStatus = useWatch({ control, name: 'clinicalStatus' }); const verificationStatus = useWatch({ control, name: 'verificationStatus' }); const recordedDate = useWatch({ control, name: 'recordedDate' }); const category = useWatch({ control, name: 'category' }); const readonlyRows: Array<[string, string]> = [ ['Clinical status', (clinicalStatus as { display?: string })?.display ?? '—'], ['Verification', (verificationStatus as { display?: string })?.display ?? '—'], ['Recorded', (recordedDate as string) || '—'], ['Category', (Array.isArray(category) ? category[0]?.display : (category as { display?: string })?.display) ?? '—'], ]; return (
{/* FhirValueSetSearch registers its own Controller (name={context}) into the shared form context. */}
{ const activeKey = severityKeyForCode((field.value as { code?: string })?.code); return (
{SEVERITY_OPTIONS.map((opt) => ( ))}
); }} />
( { field.onChange(e); setValue('onsetType', 'dateTime'); }} disabled={disabled} /> )} />
(