From 565a9495c1f487e97d2d83cebafa85ad83d54ad1 Mon Sep 17 00:00:00 2001 From: Jovan Ssebaggala Date: Wed, 13 Nov 2024 18:18:38 +0300 Subject: [PATCH] (feat) O3-3632 - Add config for maxDate and minDate to DateObsField (#1261) --- .../src/config-schema.ts | 14 +++++++++++++- .../field/obs/obs-field.component.tsx | 14 +++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/esm-patient-registration-app/src/config-schema.ts b/packages/esm-patient-registration-app/src/config-schema.ts index 7dc42f171..ccb4bc6ba 100644 --- a/packages/esm-patient-registration-app/src/config-schema.ts +++ b/packages/esm-patient-registration-app/src/config-schema.ts @@ -1,4 +1,5 @@ import { Type, validator, validators } from '@openmrs/esm-framework'; +import _default from 'yup/lib/locale'; export interface SectionDefinition { id: string; @@ -12,7 +13,8 @@ export interface FieldDefinition { label?: string; uuid: string; placeholder?: string; - dateFormat?: string; + allowFutureDates?: boolean; + allowPastDates?: boolean; showHeading: boolean; validation?: { required: boolean; @@ -176,6 +178,16 @@ export const esmPatientRegistrationSchema = { _default: '', _description: 'Placeholder that will appear in the input.', }, + allowFutureDates: { + _type: Type.Boolean, + _default: true, + _description: 'Indicates whether the date input field should allow the selection of future dates or not.', + }, + allowPastDates: { + _type: Type.Boolean, + _default: true, + _description: 'Indicates whether the date input field should allow the selection of past dates or not.', + }, validation: { required: { _type: Type.Boolean, _default: false }, matches: { diff --git a/packages/esm-patient-registration-app/src/patient-registration/field/obs/obs-field.component.tsx b/packages/esm-patient-registration-app/src/patient-registration/field/obs/obs-field.component.tsx index 7eecbf520..6e4881511 100644 --- a/packages/esm-patient-registration-app/src/patient-registration/field/obs/obs-field.component.tsx +++ b/packages/esm-patient-registration-app/src/patient-registration/field/obs/obs-field.component.tsx @@ -57,8 +57,8 @@ export function ObsField({ fieldDefinition }: ObsFieldProps) { concept={concept} label={fieldDefinition.label} required={fieldDefinition.validation.required} - dateFormat={fieldDefinition.dateFormat} - placeholder={fieldDefinition.placeholder} + allowPastDates={fieldDefinition.allowPastDates} + allowFutureDates={fieldDefinition.allowFutureDates} /> ); case 'Coded': @@ -159,14 +159,16 @@ interface DateObsFieldProps { concept: ConceptResponse; label: string; required?: boolean; - dateFormat?: string; - placeholder?: string; + allowPastDates?: boolean; + allowFutureDates?: boolean; } -function DateObsField({ concept, label, required, placeholder }: DateObsFieldProps) { +function DateObsField({ concept, label, required, allowPastDates, allowFutureDates }: DateObsFieldProps) { const { t } = useTranslation(); const fieldName = `obs.${concept.uuid}`; const { setFieldValue } = useContext(PatientRegistrationContext); + const futureDatesAllowed = allowFutureDates ?? true; + const pastDatesAllowed = allowPastDates ?? true; const onDateChange = (date: Date) => { setFieldValue(fieldName, date); @@ -188,6 +190,8 @@ function DateObsField({ concept, label, required, placeholder }: DateObsFieldPro isInvalid={errors[fieldName] && touched[fieldName]} invalidText={t(meta.error)} value={field.value} + minDate={!pastDatesAllowed ? new Date() : undefined} + maxDate={!futureDatesAllowed ? new Date() : undefined} /> );