Skip to content

Commit

Permalink
(feat) O3-3632 - Add config for maxDate and minDate to DateObsField (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kajambiya authored Nov 13, 2024
1 parent cd0e638 commit 565a949
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 13 additions & 1 deletion packages/esm-patient-registration-app/src/config-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Type, validator, validators } from '@openmrs/esm-framework';
import _default from 'yup/lib/locale';

export interface SectionDefinition {
id: string;
Expand All @@ -12,7 +13,8 @@ export interface FieldDefinition {
label?: string;
uuid: string;
placeholder?: string;
dateFormat?: string;
allowFutureDates?: boolean;
allowPastDates?: boolean;
showHeading: boolean;
validation?: {
required: boolean;
Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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);
Expand All @@ -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}
/>
</>
);
Expand Down

0 comments on commit 565a949

Please sign in to comment.