Skip to content

Commit

Permalink
(fix) Update onDateChange function (openmrs#334)
Browse files Browse the repository at this point in the history
* (fix): Update date change function

* remove console log

* Update date picker onChange handler

* Upgrade frameworj and core

* (fix): update onChange in mocks
  • Loading branch information
NethmiRodrigo authored and Art-Ndiema committed Aug 8, 2024
1 parent 0fc1472 commit fa30035
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/components/inputs/date/date.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import TooltipFieldLabel from '../../tooltip-field-label/tooltip-field-label.com
import styles from './date.scss';
import { useFieldValidationResults } from '../../../hooks/useFieldValidationResults';
import { OpenmrsDatePicker, formatDate, formatTime } from '@openmrs/esm-framework';
import { type CalendarDate, getLocalTimeZone } from '@internationalized/date';

const DateField: React.FC<FormFieldProps> = ({ question, onChange, handler, previousValue }) => {
const { t } = useTranslation();
Expand All @@ -30,8 +31,8 @@ const DateField: React.FC<FormFieldProps> = ({ question, onChange, handler, prev
return false;
}, [encounterContext.sessionMode, question.readonly, question.inlineRendering, layoutType, workspaceLayout]);

const onDateChange = ([date]) => {
const refinedDate = date instanceof Date ? new Date(date.setHours(0, 0, 0, 0)) : new Date(date);
const onDateChange = (date: CalendarDate) => {
const refinedDate = date.toDate(getLocalTimeZone());
setTimeIfPresent(refinedDate, time);
setFieldValue(question.id, refinedDate);
onChange(question.id, refinedDate, setErrors, setWarnings);
Expand All @@ -47,7 +48,7 @@ const DateField: React.FC<FormFieldProps> = ({ question, onChange, handler, prev

useEffect(() => {
if (!isEmpty(previousValue)) {
const refinedDate = previousValue instanceof Date ? new Date(previousValue.setHours(0, 0, 0, 0)) : previousValue;
const refinedDate = new Date(previousValue.toString());
onTimeChange(false, true);
setFieldValue(question.id, refinedDate);
onChange(question.id, refinedDate, setErrors, setWarnings);
Expand Down Expand Up @@ -98,7 +99,7 @@ const DateField: React.FC<FormFieldProps> = ({ question, onChange, handler, prev
<Layer>
<OpenmrsDatePicker
id={question.id}
onChange={(date) => onDateChange([date])}
onChange={onDateChange}
labelText={
<span className={styles.datePickerLabel}>
{question.isRequired ? (
Expand Down
3 changes: 2 additions & 1 deletion src/components/inputs/unspecified/unspecified.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import dayjs from 'dayjs';
import { parseDate } from '@internationalized/date';
import { fireEvent, render, screen } from '@testing-library/react';
import { Formik } from 'formik';
import { type FormField, type EncounterContext, FormContext } from '../../..';
Expand All @@ -19,7 +20,7 @@ jest.mock('@openmrs/esm-framework', () => {
<input
id={id}
value={value ? dayjs(value).format('DD/MM/YYYY') : undefined}
onChange={(evt) => onChange(dayjs(evt.target.value).toDate())}
onChange={(evt) => onChange(parseDate(dayjs(evt.target.value).format('YYYY-MM-DD')))}
/>
</>
);
Expand Down
3 changes: 2 additions & 1 deletion src/form-engine.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import dayjs from 'dayjs';
import userEvent from '@testing-library/user-event';
import { act, cleanup, render, screen, within, fireEvent, waitFor } from '@testing-library/react';
import { restBaseUrl } from '@openmrs/esm-framework';
import { parseDate } from '@internationalized/date';
import { when } from 'jest-when';
import * as api from '../src/api/api';
import { assertFormHasAllFields, findMultiSelectInput, findSelectInput } from './utils/test-utils';
Expand Down Expand Up @@ -68,7 +69,7 @@ jest.mock('@openmrs/esm-framework', () => {
<input
id={id}
value={value ? dayjs(value).format('DD/MM/YYYY') : undefined}
onChange={(evt) => onChange(dayjs(evt.target.value).toDate())}
onChange={(evt) => onChange(parseDate(dayjs(evt.target.value).format('YYYY-MM-DD')))}
/>
{isInvalid && invalidText && <span>{invalidText}</span>}
</>
Expand Down

0 comments on commit fa30035

Please sign in to comment.