Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | 75x 75x 75x 75x 75x 14x 14x 14x | import { Controller, type Control, type FieldErrors, type UseFormRegister } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { AutocompleteInput } from '@/shared/components/AutocompleteInput'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Textarea } from '@/components/ui/textarea'
import { DatePickerField } from '@/shared/components/DatePickerField'
import { fieldErrorKey } from '@/shared/utils/zod-error'
import type { MedicationFormInput } from '../api/medications.schemas'
interface MedicationFieldsProps {
section?: 'medicine' | 'details'
prescribers?: string[]
register: UseFormRegister<MedicationFormInput>
control: Control<MedicationFormInput>
errors: FieldErrors<MedicationFormInput>
}
/**
* Os campos de uma receita, escritos como a receita foi escrita.
*
* Dose e frequência são texto livre e não têm máscara nem unidade fixa: gotas, ml, mg, meio
* comprimido, "de 8 em 8 horas", "se a febre voltar". Um campo que recusa o que está na receita é
* pior do que um que guarda literalmente.
*/
export function MedicationFields({
register,
control,
errors,
section,
prescribers = [],
}: MedicationFieldsProps) {
const { t } = useTranslation()
const nameErrorKey = fieldErrorKey(errors.name)
const startedOnErrorKey = fieldErrorKey(errors.startedOn)
const endedOnErrorKey = fieldErrorKey(errors.endedOn)
return (
<div className="space-y-6">
{section !== 'details' && (
<>
<div>
<Label htmlFor="medication-name">{t('medications.form.nameLabel')}</Label>
<Input
id="medication-name"
placeholder={t('medications.form.namePlaceholder')}
aria-invalid={!!errors.name}
aria-describedby={nameErrorKey ? 'medication-name-error' : undefined}
className="mt-2"
{...register('name')}
/>
{nameErrorKey && (
<p id="medication-name-error" className="text-destructive mt-1 text-sm">
{t(nameErrorKey)}
</p>
)}
</div>
<div className="grid grid-cols-1 gap-6 md:grid-cols-2">
<div>
<Label htmlFor="medication-dosage">{t('medications.form.dosageLabel')}</Label>
<Input
id="medication-dosage"
placeholder={t('medications.form.dosagePlaceholder')}
className="mt-2 font-sans tabular-nums"
{...register('dosage')}
/>
</div>
<div>
<Label htmlFor="medication-frequency">{t('medications.form.frequencyLabel')}</Label>
<Input
id="medication-frequency"
placeholder={t('medications.form.frequencyPlaceholder')}
className="mt-2 font-sans tabular-nums"
{...register('frequency')}
/>
</div>
</div>
</>
)}
{section !== 'medicine' && (
<>
<div className="grid grid-cols-1 gap-6 md:grid-cols-2">
<div>
<Label htmlFor="medication-startedOn">{t('medications.form.startedOnLabel')}</Label>
<Controller
control={control}
name="startedOn"
render={({ field }) => (
<DatePickerField
id="medication-startedOn"
value={field.value ?? ''}
ref={field.ref}
onBlur={field.onBlur}
onValueChange={field.onChange}
aria-invalid={!!errors.startedOn}
aria-describedby={startedOnErrorKey ? 'medication-startedOn-error' : undefined}
className="mt-2"
/>
)}
/>
{startedOnErrorKey && (
<p id="medication-startedOn-error" className="text-destructive mt-1 text-sm">
{t(startedOnErrorKey)}
</p>
)}
</div>
<div>
{/* Vazio é a resposta certa para o que ainda está sendo tomado — e o rótulo diz
"opcional" em vez de "em uso", porque deixar em branco significa "sem fim
registrado", não "a criança está tomando hoje". */}
<Label htmlFor="medication-endedOn">{t('medications.form.endedOnLabel')}</Label>
<Controller
control={control}
name="endedOn"
render={({ field }) => (
<DatePickerField
id="medication-endedOn"
value={field.value ?? ''}
onValueChange={field.onChange}
aria-invalid={!!errors.endedOn}
aria-describedby={endedOnErrorKey ? 'medication-endedOn-error' : undefined}
className="mt-2"
/>
)}
/>
{endedOnErrorKey && (
<p id="medication-endedOn-error" className="text-destructive mt-1 text-sm">
{t(endedOnErrorKey)}
</p>
)}
</div>
</div>
<div className="grid grid-cols-1 gap-6 md:grid-cols-2">
<div>
<Label htmlFor="medication-reason">{t('medications.form.reasonLabel')}</Label>
<Input
id="medication-reason"
placeholder={t('medications.form.reasonPlaceholder')}
className="mt-2"
{...register('reason')}
/>
</div>
<div>
<Label htmlFor="medication-prescriber">{t('medications.form.prescriberLabel')}</Label>
<Controller
control={control}
name="prescriberName"
render={({ field }) => (
<AutocompleteInput
id="medication-prescriber"
placeholder={t('medications.form.prescriberPlaceholder')}
className="mt-2"
value={field.value ?? ''}
onValueChange={field.onChange}
onBlur={field.onBlur}
suggestions={prescribers}
/>
)}
/>
</div>
</div>
<div>
<Label htmlFor="medication-notes">{t('medications.form.notesLabel')}</Label>
<Textarea id="medication-notes" rows={3} className="mt-2" {...register('notes')} />
</div>
</>
)}
</div>
)
}
|