@@ -16,28 +16,49 @@ export type ShortcutProps<Value> = {
16
16
label : LocalizedString ;
17
17
value : Value ;
18
18
} ;
19
- type SingleDateFieldProps = {
20
- type ?: "single" ;
21
- shortcuts ?: ShortcutProps < Date | null > [ ] ;
22
- } & FieldProps < Date | null > ;
23
- type RangeDateFieldProps = {
24
- type : "range" ;
25
- shortcuts ?: ShortcutProps < [ Date , Date ] | null > [ ] ;
26
- } & FieldProps < [ Date , Date ] | null > ;
27
- type Props = ( SingleDateFieldProps | RangeDateFieldProps ) & {
19
+
20
+ type StandaloneProps < T > = Pick <
21
+ FieldProps < T > ,
22
+ "autoFocus" | "disabled" | "name" | "onBlur" | "onChange" | "value"
23
+ > ;
24
+
25
+ type SingleDateProps = { type ?: "single" ; shortcuts ?: ShortcutProps < Date | null > [ ] } ;
26
+ type RangeDateProps = { type : "range" ; shortcuts ?: ShortcutProps < [ Date , Date ] | null > [ ] } ;
27
+
28
+ type SingleDateFieldProps = SingleDateProps & FieldProps < Date | null > ;
29
+ type SingleDateStandaloneProps = SingleDateProps & StandaloneProps < Date | null > ;
30
+
31
+ type RangeDateFieldProps = RangeDateProps & FieldProps < [ Date , Date ] | null > ;
32
+ type RangeDateStandaloneProps = RangeDateProps & StandaloneProps < [ Date , Date ] | null > ;
33
+
34
+ type DateProps = {
28
35
minDate ?: Date ;
29
36
maxDate ?: Date ;
30
37
shouldDisableDate ?: ( date : Date ) => boolean ;
31
38
readOnly ?: boolean ;
32
39
} ;
33
40
41
+ type PublicDateFieldProps = ( SingleDateFieldProps | RangeDateFieldProps ) & DateProps ;
42
+ type PublicDateInputProps = ( SingleDateStandaloneProps | RangeDateStandaloneProps ) & DateProps ;
43
+
44
+ type InternalDateProps =
45
+ | ( {
46
+ isStandalone : true ;
47
+ } & PublicDateInputProps )
48
+ | ( {
49
+ isStandalone ?: false ;
50
+ } & PublicDateFieldProps ) ;
51
+
34
52
function dateToCalendarDate ( date : Date ) : CalendarDate {
35
53
return new CalendarDate ( date . getFullYear ( ) , date . getMonth ( ) + 1 , date . getDate ( ) ) ;
36
54
}
37
55
38
- function SingleDateField ( { disabled, readOnly, ...props } : Extract < Props , { type ?: "single" } > ) {
56
+ function SingleDate ( {
57
+ disabled,
58
+ readOnly,
59
+ ...props
60
+ } : Extract < InternalDateProps , { type ?: "single" } > ) {
39
61
const localTimeZone = getLocalTimeZone ( ) ;
40
-
41
62
const internalProps = {
42
63
...props ,
43
64
value : props . value ? dateToCalendarDate ( props . value ) : props . value ,
@@ -46,7 +67,7 @@ function SingleDateField({ disabled, readOnly, ...props }: Extract<Props, { type
46
67
} ,
47
68
isDisabled : disabled ,
48
69
isReadOnly : readOnly ,
49
- validationState : props . issues ? "invalid" : "valid" ,
70
+ validationState : ! props . isStandalone && props . issues ? "invalid" : "valid" ,
50
71
minValue : props . minDate ? dateToCalendarDate ( props . minDate ) : undefined ,
51
72
maxValue : props . maxDate ? dateToCalendarDate ( props . maxDate ) : undefined ,
52
73
isDateUnavailable : props . shouldDisableDate
@@ -84,14 +105,8 @@ function SingleDateField({ disabled, readOnly, ...props }: Extract<Props, { type
84
105
</ Inline >
85
106
) ;
86
107
87
- return (
88
- < Field
89
- { ...props }
90
- disabled = { disabled }
91
- labelProps = { labelProps }
92
- assistiveTextProps = { descriptionProps }
93
- errorMessageProps = { errorMessageProps }
94
- >
108
+ const datePicker = (
109
+ < >
95
110
< Box { ...groupProps } ref = { ref } >
96
111
< Input
97
112
type = "single"
@@ -109,17 +124,35 @@ function SingleDateField({ disabled, readOnly, ...props }: Extract<Props, { type
109
124
shortcuts = { shortcuts }
110
125
/>
111
126
) }
127
+ </ >
128
+ ) ;
129
+
130
+ return props . isStandalone ? (
131
+ datePicker
132
+ ) : (
133
+ < Field
134
+ { ...props }
135
+ disabled = { disabled }
136
+ labelProps = { labelProps }
137
+ assistiveTextProps = { descriptionProps }
138
+ errorMessageProps = { errorMessageProps }
139
+ >
140
+ { datePicker }
112
141
</ Field >
113
142
) ;
114
143
}
115
144
116
- function RangeDateField ( { disabled, readOnly, ...props } : Extract < Props , { type : "range" } > ) {
145
+ function RangeDateField ( {
146
+ disabled,
147
+ readOnly,
148
+ ...props
149
+ } : Extract < InternalDateProps , { type : "range" } > ) {
117
150
const localTimeZone = getLocalTimeZone ( ) ;
118
151
const internalProps = {
119
152
...props ,
120
153
isDisabled : disabled ,
121
154
isReadOnly : readOnly ,
122
- validationState : props . issues ? "invalid" : "valid" ,
155
+ validationState : ! props . isStandalone && props . issues ? "invalid" : "valid" ,
123
156
minValue : props . minDate ? dateToCalendarDate ( props . minDate ) : undefined ,
124
157
maxValue : props . maxDate ? dateToCalendarDate ( props . maxDate ) : undefined ,
125
158
isDateUnavailable : props . shouldDisableDate
@@ -171,14 +204,8 @@ function RangeDateField({ disabled, readOnly, ...props }: Extract<Props, { type:
171
204
</ Inline >
172
205
) ;
173
206
174
- return (
175
- < Field
176
- { ...props }
177
- disabled = { disabled }
178
- labelProps = { labelProps }
179
- assistiveTextProps = { descriptionProps }
180
- errorMessageProps = { errorMessageProps }
181
- >
207
+ const datePicker = (
208
+ < >
182
209
< Box { ...groupProps } ref = { ref } >
183
210
< Input
184
211
type = "range"
@@ -196,12 +223,34 @@ function RangeDateField({ disabled, readOnly, ...props }: Extract<Props, { type:
196
223
shortcuts = { shortcuts }
197
224
/>
198
225
) }
226
+ </ >
227
+ ) ;
228
+
229
+ return props . isStandalone ? (
230
+ datePicker
231
+ ) : (
232
+ < Field
233
+ { ...props }
234
+ disabled = { disabled }
235
+ labelProps = { labelProps }
236
+ assistiveTextProps = { descriptionProps }
237
+ errorMessageProps = { errorMessageProps }
238
+ >
239
+ { datePicker }
199
240
</ Field >
200
241
) ;
201
242
}
202
243
203
- export function DateField ( props : Props ) {
204
- return props . type === "range" ? < RangeDateField { ...props } /> : < SingleDateField { ...props } /> ;
244
+ export function DateField ( props : PublicDateFieldProps ) {
245
+ return props . type === "range" ? < RangeDateField { ...props } /> : < SingleDate { ...props } /> ;
246
+ }
247
+
248
+ export function DateInput ( props : PublicDateInputProps ) {
249
+ return props . type === "range" ? (
250
+ < RangeDateField { ...props } isStandalone />
251
+ ) : (
252
+ < SingleDate { ...props } isStandalone />
253
+ ) ;
205
254
}
206
255
207
- export type { Props as DateFieldProps } ;
256
+ export type { PublicDateFieldProps as DateFieldProps , PublicDateInputProps as DateInputProps } ;
0 commit comments