Skip to content

Commit f115316

Browse files
authored
Merge pull request #152 from osstotalsoft/feature/updatedDateTimeUTCandValue
Update DateTime component
2 parents 424e160 + cacee1b commit f115316

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/components/inputs/DateTime/DateTime.tsx

+32-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo } from 'react'
1+
import React, { useCallback, useMemo } from 'react'
22
import PropTypes from 'prop-types'
33
import {
44
DatePicker,
@@ -33,10 +33,15 @@ const DateTime: React.FC<DateTimeProps<Date, string>> = ({
3333
slots = {},
3434
localeFormat = 'ro',
3535
helperText,
36+
timezone = 'UTC',
37+
minDate,
38+
maxDate,
3639
error,
3740
...rest
3841
}) => {
3942
const locale = useMemo(() => adapterLocale ?? localeMap[localeFormat] ?? ro, [adapterLocale, localeFormat])
43+
const getValidDate = useCallback((value: any) => (value ? new Date(value) : null), [])
44+
4045
const commonSlotProps = useMemo(
4146
() => ({
4247
...slotProps,
@@ -53,10 +58,13 @@ const DateTime: React.FC<DateTimeProps<Date, string>> = ({
5358
equals('date'),
5459
() => (
5560
<DatePicker
56-
value={value}
61+
value={getValidDate(value)}
5762
onChange={onChange}
5863
slotProps={commonSlotProps as DatePickerSlotProps<Date, false>}
5964
slots={slots as DatePickerSlots<Date>}
65+
timezone={timezone}
66+
minDate={getValidDate(minDate)}
67+
maxDate={getValidDate(maxDate)}
6068
{...(rest as DatePickerProps<Date>)}
6169
/>
6270
)
@@ -65,10 +73,13 @@ const DateTime: React.FC<DateTimeProps<Date, string>> = ({
6573
equals('dateTime'),
6674
() => (
6775
<DateTimePicker
68-
value={value}
76+
value={getValidDate(value)}
6977
onChange={onChange}
7078
slotProps={commonSlotProps as DateTimePickerSlotProps<Date, false>}
7179
slots={slots as DateTimePickerSlots<Date>}
80+
timezone={timezone}
81+
minDate={getValidDate(minDate)}
82+
maxDate={getValidDate(maxDate)}
7283
{...(rest as DateTimePickerProps<Date>)}
7384
/>
7485
)
@@ -77,16 +88,17 @@ const DateTime: React.FC<DateTimeProps<Date, string>> = ({
7788
equals('time'),
7889
() => (
7990
<TimePicker
80-
value={value}
91+
value={getValidDate(value)}
8192
onChange={onChange}
8293
slotProps={commonSlotProps as TimePickerSlotProps<Date, false>}
8394
slots={slots as TimePickerSlots<Date>}
95+
timezone={timezone}
8496
{...(rest as TimePickerProps<Date>)}
8597
/>
8698
)
8799
]
88100
])(showPicker),
89-
[commonSlotProps, onChange, rest, showPicker, slots, value]
101+
[showPicker, getValidDate, value, onChange, commonSlotProps, slots, timezone, minDate, maxDate, rest]
90102
)
91103

92104
return (
@@ -112,7 +124,7 @@ DateTime.propTypes = {
112124
* @default null
113125
* Value of the picker
114126
*/
115-
value: PropTypes.instanceOf(Date),
127+
value: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
116128
/**
117129
* Callback fired when the value (the selected date) changes @DateIOType.
118130
*/
@@ -144,7 +156,20 @@ DateTime.propTypes = {
144156
/**
145157
* The helper text content.
146158
*/
147-
helperText: PropTypes.node
159+
helperText: PropTypes.node,
160+
/**
161+
* @default 'UTC'
162+
* The timezone used for the picker.
163+
*/
164+
timezone: PropTypes.oneOf(['UTC', 'default', 'system']),
165+
/**
166+
* The minimum selectable date.
167+
*/
168+
minDate: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
169+
/**
170+
* The maximum selectable date.
171+
*/
172+
maxDate: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)])
148173
}
149174

150175
export default DateTime

src/components/inputs/DateTime/types.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ export type LocaleMapType = {
1515
ro: Locale
1616
}
1717

18-
export type DateTimeProps<TDate, TError> = (
19-
| Omit<DatePickerProps<Date>, 'onChange'>
20-
| Omit<DateTimePickerProps<Date>, 'onChange'>
21-
| Omit<TimePickerProps<Date>, 'onChange'>
22-
) &
18+
export type DateTimeProps<TDate, TError> = Omit<
19+
DatePickerProps<Date> | DateTimePickerProps<Date> | TimePickerProps<Date>,
20+
'value' | 'onChange' | 'minDate' | 'maxDate'
21+
> &
2322
Omit<LocalizationProviderProps<Date, Locale>, 'adapterLocale'> & {
2423
/**
2524
* Date library adapter class function.
@@ -62,4 +61,19 @@ export type DateTimeProps<TDate, TError> = (
6261
* The helper text content.
6362
*/
6463
helperText?: React.ReactNode
64+
65+
/**
66+
*The value currently displayed in the field
67+
*/
68+
value: string | Date
69+
70+
/**
71+
* The minimum selectable date.
72+
*/
73+
minDate?: string | Date
74+
75+
/**
76+
* The maximum selectable date.
77+
*/
78+
maxDate?: string | Date
6579
}

0 commit comments

Comments
 (0)