1
- import React , { useMemo } from 'react'
1
+ import React , { useCallback , useMemo } from 'react'
2
2
import PropTypes from 'prop-types'
3
3
import {
4
4
DatePicker ,
@@ -33,10 +33,15 @@ const DateTime: React.FC<DateTimeProps<Date, string>> = ({
33
33
slots = { } ,
34
34
localeFormat = 'ro' ,
35
35
helperText,
36
+ timezone = 'UTC' ,
37
+ minDate,
38
+ maxDate,
36
39
error,
37
40
...rest
38
41
} ) => {
39
42
const locale = useMemo ( ( ) => adapterLocale ?? localeMap [ localeFormat ] ?? ro , [ adapterLocale , localeFormat ] )
43
+ const getValidDate = useCallback ( ( value : any ) => ( value ? new Date ( value ) : null ) , [ ] )
44
+
40
45
const commonSlotProps = useMemo (
41
46
( ) => ( {
42
47
...slotProps ,
@@ -53,10 +58,13 @@ const DateTime: React.FC<DateTimeProps<Date, string>> = ({
53
58
equals ( 'date' ) ,
54
59
( ) => (
55
60
< DatePicker
56
- value = { value }
61
+ value = { getValidDate ( value ) }
57
62
onChange = { onChange }
58
63
slotProps = { commonSlotProps as DatePickerSlotProps < Date , false > }
59
64
slots = { slots as DatePickerSlots < Date > }
65
+ timezone = { timezone }
66
+ minDate = { getValidDate ( minDate ) }
67
+ maxDate = { getValidDate ( maxDate ) }
60
68
{ ...( rest as DatePickerProps < Date > ) }
61
69
/>
62
70
)
@@ -65,10 +73,13 @@ const DateTime: React.FC<DateTimeProps<Date, string>> = ({
65
73
equals ( 'dateTime' ) ,
66
74
( ) => (
67
75
< DateTimePicker
68
- value = { value }
76
+ value = { getValidDate ( value ) }
69
77
onChange = { onChange }
70
78
slotProps = { commonSlotProps as DateTimePickerSlotProps < Date , false > }
71
79
slots = { slots as DateTimePickerSlots < Date > }
80
+ timezone = { timezone }
81
+ minDate = { getValidDate ( minDate ) }
82
+ maxDate = { getValidDate ( maxDate ) }
72
83
{ ...( rest as DateTimePickerProps < Date > ) }
73
84
/>
74
85
)
@@ -77,16 +88,17 @@ const DateTime: React.FC<DateTimeProps<Date, string>> = ({
77
88
equals ( 'time' ) ,
78
89
( ) => (
79
90
< TimePicker
80
- value = { value }
91
+ value = { getValidDate ( value ) }
81
92
onChange = { onChange }
82
93
slotProps = { commonSlotProps as TimePickerSlotProps < Date , false > }
83
94
slots = { slots as TimePickerSlots < Date > }
95
+ timezone = { timezone }
84
96
{ ...( rest as TimePickerProps < Date > ) }
85
97
/>
86
98
)
87
99
]
88
100
] ) ( showPicker ) ,
89
- [ commonSlotProps , onChange , rest , showPicker , slots , value ]
101
+ [ showPicker , getValidDate , value , onChange , commonSlotProps , slots , timezone , minDate , maxDate , rest ]
90
102
)
91
103
92
104
return (
@@ -112,7 +124,7 @@ DateTime.propTypes = {
112
124
* @default null
113
125
* Value of the picker
114
126
*/
115
- value : PropTypes . instanceOf ( Date ) ,
127
+ value : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . instanceOf ( Date ) ] ) ,
116
128
/**
117
129
* Callback fired when the value (the selected date) changes @DateIOType.
118
130
*/
@@ -144,7 +156,20 @@ DateTime.propTypes = {
144
156
/**
145
157
* The helper text content.
146
158
*/
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 ) ] )
148
173
}
149
174
150
175
export default DateTime
0 commit comments