Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refactor) O3-3846: Replace Carbon datepickers with reusable OpenmrsDatePicker #2154

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { useTranslation } from 'react-i18next';
import {
Button,
ButtonSet,
DatePicker,
DatePickerInput,
DatePickerSkeleton,
Form,
InlineLoading,
Expand All @@ -23,7 +21,14 @@ import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { WarningFilled } from '@carbon/react/icons';
import { EmptyState, type DefaultPatientWorkspaceProps } from '@openmrs/esm-patient-common-lib';
import { ExtensionSlot, useLayoutType, showSnackbar, ResponsiveWrapper, useConfig } from '@openmrs/esm-framework';
import {
ExtensionSlot,
useLayoutType,
showSnackbar,
ResponsiveWrapper,
useConfig,
OpenmrsDatePicker,
} from '@openmrs/esm-framework';
import { markPatientDeceased, useCausesOfDeath } from '../data.resource';
import { type ChartConfig } from '../config-schema';
import styles from './mark-patient-deceased-form.scss';
Expand Down Expand Up @@ -134,22 +139,13 @@ const MarkPatientDeceasedForm: React.FC<DefaultPatientWorkspaceProps> = ({ close
name="deathDate"
control={control}
render={({ field: { onChange, value } }) => (
<DatePicker
<OpenmrsDatePicker
className={styles.datePicker}
dateFormat="d/m/Y"
datePickerType="single"
id="deceasedDate"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to maintain parity between the props passed to the Carbon datepicker and those passed to OpenmrsDatePicker. In this case, this implementation is missing several props including labelText, isInvalid, and invalidText. It should look like this instead:

<OpenmrsDatePicker
    className={styles.datePicker}
    id="deceasedDate"
    isInvalid={!!errors?.deathDate}
    invalidText={errors?.deathDate?.message}
    labelText={t('dateOfDeath', 'Date of death')}
    maxDate={new Date().toISOString()}
    onChange={(date) => onChange(date)}
    value={value}
  />

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @denniskigen

maxDate={new Date().toISOString()}
onChange={([date]) => onChange(date)}
onChange={(date) => onChange(date)}
value={value}
>
<DatePickerInput
id="deceasedDateInput"
labelText={t('date', 'Date')}
placeholder="dd/mm/yyyy"
style={{ width: '100%' }}
/>
</DatePicker>
/>
)}
/>
{errors?.deathDate && <p className={styles.errorMessage}>{errors?.deathDate?.message}</p>}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React, { useEffect, useId, useMemo } from 'react';
import {
Checkbox,
DatePicker,
DatePickerInput,
NumberInput,
Select,
SelectItem,
Expand All @@ -14,7 +12,7 @@ import {
import dayjs from 'dayjs';
import { useTranslation } from 'react-i18next';
import { Controller, type ControllerRenderProps, useFormContext } from 'react-hook-form';
import { useConfig } from '@openmrs/esm-framework';
import { OpenmrsDatePicker, useConfig } from '@openmrs/esm-framework';
import { type ChartConfig } from '../../config-schema';
import { useConceptAnswersForVisitAttributeType, useVisitAttributeType } from '../hooks/useVisitAttributeType';
import { type VisitFormData } from './visit-form.resource';
Expand Down Expand Up @@ -204,21 +202,13 @@ const AttributeTypeField: React.FC<AttributeTypeFieldProps> = ({
);
case 'org.openmrs.customdatatype.datatype.DateDatatype':
return (
<DatePicker
<OpenmrsDatePicker
{...fieldProps}
dateFormat="d/m/Y"
datePickerType="single"
onChange={([date]) => onChange(dayjs(date).format('YYYY-MM-DD'))}
>
<DatePickerInput
id="date-picker-default-id"
placeholder="dd/mm/yyyy"
labelText={labelText}
type="text"
invalid={!!errors.visitAttributes?.[uuid]}
invalidText={errors.visitAttributes?.[uuid]?.message}
/>
</DatePicker>
onChange={(date) => onChange(dayjs(date).format('YYYY-MM-DD'))}
labelText={labelText}
invalid={!!errors.visitAttributes?.[uuid]}
invalidText={errors.visitAttributes?.[uuid]?.message}
/>
);
default:
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import classNames from 'classnames';
import dayjs from 'dayjs';
import { DatePicker, DatePickerInput, SelectItem, TimePicker, TimePickerSelect } from '@carbon/react';
import { SelectItem, TimePicker, TimePickerSelect } from '@carbon/react';
import { Controller, useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { ResponsiveWrapper } from '@openmrs/esm-framework';
import { OpenmrsDatePicker, ResponsiveWrapper } from '@openmrs/esm-framework';
import { type amPm } from '@openmrs/esm-patient-common-lib';
import { type VisitFormData } from './visit-form.resource';
import styles from './visit-form.scss';
Expand Down Expand Up @@ -49,25 +49,17 @@ const VisitDateTimeField: React.FC<VisitDateTimeFieldProps> = ({
control={control}
render={({ field: { onChange, value } }) => (
<ResponsiveWrapper>
<DatePicker
<OpenmrsDatePicker
className={styles.datePicker}
dateFormat="d/m/Y"
datePickerType="single"
id={dateFieldName}
maxDate={maxDateObj}
minDate={minDateObj}
onChange={([date]) => onChange(date)}
onChange={onChange}
value={value ? dayjs(value).format('DD/MM/YYYY') : null}
jwnasambu marked this conversation as resolved.
Show resolved Hide resolved
>
<DatePickerInput
id={`${dateFieldName}Input`}
invalid={Boolean(errors[dateFieldName])}
invalidText={errors[dateFieldName]?.message}
labelText={t('date', 'Date')}
placeholder="dd/mm/yyyy"
style={{ width: '100%' }}
/>
</DatePicker>
labelText={t('date', 'Date')}
invalid={Boolean(errors[dateFieldName])}
invalidText={errors[dateFieldName]?.message}
/>
</ResponsiveWrapper>
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import classNames from 'classnames';
import dayjs from 'dayjs';
import 'dayjs/plugin/utc';
import {
DatePicker,
DatePickerInput,
FormGroup,
FormLabel,
InlineLoading,
Expand All @@ -18,7 +16,7 @@ import {
} from '@carbon/react';
import { WarningFilled } from '@carbon/react/icons';
import { useFormContext, Controller } from 'react-hook-form';
import { showSnackbar, useDebounce, useSession, ResponsiveWrapper } from '@openmrs/esm-framework';
import { showSnackbar, useDebounce, useSession, ResponsiveWrapper, OpenmrsDatePicker } from '@openmrs/esm-framework';
import { type DefaultPatientWorkspaceProps } from '@openmrs/esm-patient-common-lib';
import {
type CodedCondition,
Expand Down Expand Up @@ -277,18 +275,14 @@ const ConditionsWidget: React.FC<ConditionsWidgetProps> = ({
control={control}
render={({ field: { onChange, onBlur, value } }) => (
<ResponsiveWrapper>
<DatePicker
<OpenmrsDatePicker
id="onsetDate"
datePickerType="single"
dateFormat="d/m/Y"
maxDate={dayjs().utc().format()}
placeholder="dd/mm/yyyy"
onChange={([date]) => onChange(date)}
onChange={onChange}
onBlur={onBlur}
value={value}
>
<DatePickerInput id="onsetDateInput" labelText={t('onsetDate', 'Onset date')} />
</DatePicker>
labelText={t('onsetDate', 'Onset date')}
/>
</ResponsiveWrapper>
)}
/>
Expand Down Expand Up @@ -322,19 +316,15 @@ const ConditionsWidget: React.FC<ConditionsWidgetProps> = ({
render={({ field: { onBlur, onChange, value } }) => (
<>
<ResponsiveWrapper>
<DatePicker
<OpenmrsDatePicker
id="endDate"
datePickerType="single"
dateFormat="d/m/Y"
minDate={new Date(watch('onsetDateTime')).toISOString()}
maxDate={dayjs().utc().format()}
placeholder="dd/mm/yyyy"
onChange={([date]) => onChange(date)}
onChange={onChange}
onBlur={onBlur}
value={value}
>
<DatePickerInput id="abatementDateTime" labelText={t('endDate', 'End date')} />
jwnasambu marked this conversation as resolved.
Show resolved Hide resolved
</DatePicker>
labelText={t('endDate', 'End date')}
/>
</ResponsiveWrapper>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import dayjs from 'dayjs';
import {
Button,
ButtonSet,
DatePicker,
DatePickerInput,
Dropdown,
Form,
InlineNotification,
Expand All @@ -24,6 +22,7 @@ import {
toDateObjectStrict,
showSnackbar,
ResponsiveWrapper,
OpenmrsDatePicker,
} from '@openmrs/esm-framework';
import { useForm, Controller, FormProvider } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
Expand Down Expand Up @@ -229,25 +228,16 @@ const ImmunizationsForm: React.FC<DefaultPatientWorkspaceProps> = ({
name="vaccinationDate"
control={control}
render={({ field: { onChange, value } }) => (
<DatePicker
<OpenmrsDatePicker
id="vaccinationDate"
maxDate={new Date().toISOString()}
dateFormat={datePickerFormat}
datePickerType="single"
value={value}
onChange={([date]) => onChange(date)}
style={{ paddingBottom: '1rem' }}
>
<DatePickerInput
id="vaccinationDateInput"
placeholder="dd/mm/yyyy"
labelText={t('vaccinationDate', 'Vaccination date')}
type="text"
invalid={!!errors['vaccinationDate']}
invalidText={errors['vaccinationDate']?.message}
style={{ width: '100%' }}
/>
</DatePicker>
onChange={(date) => onChange(date)}
style={{ paddingBottom: '1rem', width: '100%' }}
invalid={!!errors['vaccinationDate']}
invalidText={errors['vaccinationDate']?.message}
labelText={t('vaccinationDate', 'Vaccination date')}
/>
)}
/>
</ResponsiveWrapper>
Expand Down Expand Up @@ -380,22 +370,14 @@ const ImmunizationsForm: React.FC<DefaultPatientWorkspaceProps> = ({
control={control}
render={({ field: { onChange, value } }) => (
<div className={styles.row}>
<DatePicker
<OpenmrsDatePicker
id="vaccinationExpiration"
className="vaccinationExpiration"
minDate={immunizationToEditMeta ? null : new Date().toISOString()}
dateFormat={datePickerFormat}
datePickerType="single"
value={value}
onChange={([date]) => onChange(date)}
>
<DatePickerInput
id="date-picker-calendar-id"
placeholder="dd/mm/yyyy"
labelText={t('expirationDate', 'Expiration date')}
type="text"
/>
</DatePicker>
onChange={(date) => onChange(date)}
labelText={t('expirationDate', 'Expiration date')}
/>
</div>
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
Checkbox,
Column,
ComboBox,
DatePicker,
DatePickerInput,
IconButton,
Form,
FormGroup,
Expand All @@ -30,6 +28,7 @@ import {
ExtensionSlot,
formatDate,
getPatientName,
OpenmrsDatePicker,
parseDate,
useConfig,
useLayoutType,
Expand Down Expand Up @@ -597,21 +596,13 @@ export function DrugOrderForm({ initialOrderBasketItem, onSave, onCancel, prompt
name="startDate"
control={control}
render={({ field: { onBlur, value, onChange, ref } }) => (
<DatePicker
datePickerType="single"
<OpenmrsDatePicker
maxDate={new Date().toISOString()}
value={value}
onChange={([newStartDate]) => onChange(newStartDate)}
onChange={(newStartDate) => onChange(newStartDate)}
onBlur={onBlur}
ref={ref}
>
<DatePickerInput
id="startDatePicker"
placeholder="dd/mm/yyyy"
labelText={t('startDate', 'Start date')}
size={isTablet ? 'lg' : 'sm'}
/>
</DatePicker>
/>
)}
/>
</InputWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
Button,
ButtonSet,
Column,
DatePicker,
DatePickerInput,
Form,
FormGroup,
InlineLoading,
Expand All @@ -38,6 +36,7 @@ import {
useConfig,
useLayoutType,
useSession,
OpenmrsDatePicker,
} from '@openmrs/esm-framework';
import { type DefaultPatientWorkspaceProps, useAllowedFileExtensions } from '@openmrs/esm-patient-common-lib';
import type { ConfigObject } from '../config-schema';
Expand Down Expand Up @@ -455,19 +454,12 @@ const VisitNotesForm: React.FC<DefaultPatientWorkspaceProps> = ({
control={control}
render={({ field: { onChange, value } }) => (
<ResponsiveWrapper>
<DatePicker
dateFormat="d/m/Y"
datePickerType="single"
<OpenmrsDatePicker
maxDate={new Date().toISOString()}
value={value}
onChange={([date]) => onChange(date)}
>
<DatePickerInput
id="visitDateTimePicker"
labelText={t('visitDate', 'Visit date')}
placeholder="dd/mm/yyyy"
/>
</DatePicker>
onChange={(date) => onChange(date)}
labelText={t('visitDate', 'Visit date')}
/>
</ResponsiveWrapper>
)}
/>
Expand Down
Loading
Loading