-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/O3-3846: All Calendar Date Pickers should use the same OpenmrsDa…
…tePicker component across entire EMR
- Loading branch information
jwnasambu
committed
Nov 14, 2024
1 parent
ad3bb1f
commit 08523e8
Showing
2 changed files
with
45 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,47 @@ | ||
import { DatePicker, DatePickerInput } from '@carbon/react'; | ||
import { useAppContext } from '@openmrs/esm-framework'; | ||
import dayjs from 'dayjs'; | ||
import React from 'react'; | ||
import { useAppContext, OpenmrsDatePicker } from '@openmrs/esm-framework'; | ||
import dayjs from 'dayjs'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { DateFilterContext } from '../../types'; | ||
import styles from './orders-date-range-picker.scss'; | ||
|
||
const OpenmrsDateRangePicker = ({ dateRange, onDateRangeChange, maxDate }) => { | ||
const { t } = useTranslation(); | ||
|
||
const handleDateChange = (index, date) => { | ||
const updatedDateRange = [...dateRange]; | ||
updatedDateRange[index] = date; | ||
onDateRangeChange(updatedDateRange); | ||
}; | ||
|
||
return ( | ||
<div className={styles.dateRangePicker}> | ||
<p>{t('dateRange', 'Date range')}:</p> | ||
<div className={styles.datePickers}> | ||
{['start', 'finish'].map((type, index) => ( | ||
<OpenmrsDatePicker | ||
key={type} | ||
id={`date-picker-input-id-${type}`} | ||
value={dateRange[index]} | ||
onChange={(date) => handleDateChange(index, date)} | ||
maxDate={maxDate} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export const OrdersDateRangePicker = () => { | ||
const currentDate = new Date(); | ||
const { dateRange, setDateRange } = useAppContext<DateFilterContext>('laboratory-date-filter') ?? { | ||
dateRange: [dayjs().startOf('day').toDate(), new Date()], | ||
setDateRange: () => {}, | ||
}; | ||
|
||
const { t } = useTranslation(); | ||
|
||
const handleOrdersDateRangeChange = (dates: Array<Date>) => { | ||
setDateRange(dates); | ||
}; | ||
|
||
return ( | ||
<div className={styles.datePickerWrapper}> | ||
<p>{t('dateRange', 'Date range')}:</p> | ||
<DatePicker | ||
datePickerType="range" | ||
className={styles.dateRangePicker} | ||
onClose={handleOrdersDateRangeChange} | ||
maxDate={currentDate.toISOString()} | ||
value={dateRange} | ||
> | ||
<DatePickerInput id="date-picker-input-id-start" placeholder="dd/mm/yyyy" size="md" /> | ||
<DatePickerInput id="date-picker-input-id-finish" placeholder="dd/mm/yyyy" size="md" /> | ||
</DatePicker> | ||
<OpenmrsDateRangePicker dateRange={dateRange} onDateRangeChange={setDateRange} maxDate={currentDate} /> | ||
</div> | ||
); | ||
}; |