@react-stately/datepicker 3.0.0-alpha.1 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -1,875 +1,754 @@
1
- var {
2
- useControlledState
3
- } = require("@react-stately/utils");
4
-
5
- var {
6
- useRef,
7
- useState,
8
- useEffect,
9
- useMemo
10
- } = require("react");
11
-
12
- var _babelRuntimeHelpersExtends = $parcel$interopDefault(require("@babel/runtime/helpers/extends"));
13
-
14
- var {
15
- DateFormatter,
16
- toCalendarDateTime,
17
- toDateFields,
18
- now,
19
- Time,
20
- toCalendar,
21
- toCalendarDate,
22
- getMinimumDayInMonth,
23
- getMinimumMonthInYear,
24
- GregorianCalendar,
25
- getLocalTimeZone,
26
- today,
27
- toTime
28
- } = require("@internationalized/date");
29
-
30
- function $parcel$interopDefault(a) {
31
- return a && a.__esModule ? a.default : a;
32
- }
33
-
34
- function $d2539a9a7cedbf57925a2a19$export$isInvalid(value, minValue, maxValue) {
35
- return value != null && (minValue != null && value.compare(minValue) < 0 || maxValue != null && value.compare(maxValue) > 0);
36
- }
37
-
38
- const $d2539a9a7cedbf57925a2a19$var$DEFAULT_FIELD_OPTIONS = {
39
- year: 'numeric',
40
- month: 'numeric',
41
- day: 'numeric',
42
- hour: 'numeric',
43
- minute: '2-digit',
44
- second: '2-digit'
45
- };
46
-
47
- function $d2539a9a7cedbf57925a2a19$export$getFormatOptions(fieldOptions, options) {
48
- var _options$maxGranulari;
1
+ var $17UYf$internationalizeddate = require("@internationalized/date");
2
+ var $17UYf$reactstatelyutils = require("@react-stately/utils");
3
+ var $17UYf$react = require("react");
49
4
 
50
- fieldOptions = _babelRuntimeHelpersExtends({}, $d2539a9a7cedbf57925a2a19$var$DEFAULT_FIELD_OPTIONS, fieldOptions);
51
- let granularity = options.granularity || 'minute';
52
- let keys = Object.keys(fieldOptions);
53
- let startIdx = keys.indexOf((_options$maxGranulari = options.maxGranularity) != null ? _options$maxGranulari : 'year');
54
-
55
- if (startIdx < 0) {
56
- startIdx = 0;
57
- }
58
-
59
- let endIdx = keys.indexOf(granularity);
60
-
61
- if (endIdx < 0) {
62
- endIdx = 2;
63
- }
5
+ function $parcel$exportWildcard(dest, source) {
6
+ Object.keys(source).forEach(function(key) {
7
+ if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
8
+ return;
9
+ }
64
10
 
65
- if (startIdx > endIdx) {
66
- throw new Error('maxGranularity must be greater than granularity');
67
- }
11
+ Object.defineProperty(dest, key, {
12
+ enumerable: true,
13
+ get: function get() {
14
+ return source[key];
15
+ }
16
+ });
17
+ });
68
18
 
69
- let opts = keys.slice(startIdx, endIdx + 1).reduce((opts, key) => {
70
- opts[key] = fieldOptions[key];
71
- return opts;
72
- }, {});
19
+ return dest;
20
+ }
21
+ function $parcel$export(e, n, v, s) {
22
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
23
+ }
24
+ var $0c9ae69d60d14242$exports = {};
73
25
 
74
- if (options.hourCycle != null) {
75
- opts.hour12 = options.hourCycle === 12;
76
- }
26
+ $parcel$export($0c9ae69d60d14242$exports, "useDatePickerState", () => $0c9ae69d60d14242$export$87194bb378cc3ac2);
77
27
 
78
- opts.timeZone = options.timeZone || 'UTC';
79
- let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';
80
28
 
81
- if (hasTime && options.timeZone && !options.hideTimeZone) {
82
- opts.timeZoneName = 'short';
83
- }
84
29
 
85
- return opts;
30
+ function $a1a093054004b094$export$eac50920cf2fd59a(value, minValue, maxValue) {
31
+ return value != null && (minValue != null && value.compare(minValue) < 0 || maxValue != null && value.compare(maxValue) > 0);
86
32
  }
87
-
88
- function $d2539a9a7cedbf57925a2a19$export$getPlaceholderTime(placeholderValue) {
89
- if (placeholderValue && 'hour' in placeholderValue) {
90
- return placeholderValue;
91
- }
92
-
93
- return new Time();
33
+ const $a1a093054004b094$var$DEFAULT_FIELD_OPTIONS = {
34
+ year: 'numeric',
35
+ month: 'numeric',
36
+ day: 'numeric',
37
+ hour: 'numeric',
38
+ minute: '2-digit',
39
+ second: '2-digit'
40
+ };
41
+ function $a1a093054004b094$export$7e319ea407e63bc0(fieldOptions, options) {
42
+ fieldOptions = {
43
+ ...$a1a093054004b094$var$DEFAULT_FIELD_OPTIONS,
44
+ ...fieldOptions
45
+ };
46
+ let granularity = options.granularity || 'minute';
47
+ let keys = Object.keys(fieldOptions);
48
+ let startIdx = keys.indexOf(options.maxGranularity ?? 'year');
49
+ if (startIdx < 0) startIdx = 0;
50
+ let endIdx = keys.indexOf(granularity);
51
+ if (endIdx < 0) endIdx = 2;
52
+ if (startIdx > endIdx) throw new Error('maxGranularity must be greater than granularity');
53
+ let opts1 = keys.slice(startIdx, endIdx + 1).reduce((opts, key)=>{
54
+ opts[key] = fieldOptions[key];
55
+ return opts;
56
+ }, {
57
+ });
58
+ if (options.hourCycle != null) opts1.hour12 = options.hourCycle === 12;
59
+ opts1.timeZone = options.timeZone || 'UTC';
60
+ let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';
61
+ if (hasTime && options.timeZone && !options.hideTimeZone) opts1.timeZoneName = 'short';
62
+ return opts1;
94
63
  }
95
-
96
- function $d2539a9a7cedbf57925a2a19$export$convertValue(value, calendar) {
97
- if (value === null) {
98
- return null;
99
- }
100
-
101
- if (!value) {
102
- return undefined;
103
- }
104
-
105
- return toCalendar(value, calendar);
64
+ function $a1a093054004b094$export$c5221a78ef73c5e9(placeholderValue) {
65
+ if (placeholderValue && 'hour' in placeholderValue) return placeholderValue;
66
+ return new $17UYf$internationalizeddate.Time();
106
67
  }
107
-
108
- function $d2539a9a7cedbf57925a2a19$export$createPlaceholderDate(placeholderValue, granularity, calendar, timeZone) {
109
- if (placeholderValue) {
110
- return $d2539a9a7cedbf57925a2a19$export$convertValue(placeholderValue, calendar);
111
- }
112
-
113
- let date = toCalendar(now(timeZone).set({
114
- hour: 0,
115
- minute: 0,
116
- second: 0,
117
- millisecond: 0
118
- }), calendar);
119
-
120
- if (granularity === 'year' || granularity === 'month' || granularity === 'day') {
121
- return toCalendarDate(date);
122
- }
123
-
124
- if (!timeZone) {
125
- return toCalendarDateTime(date);
126
- }
127
-
128
- return date;
68
+ function $a1a093054004b094$export$61a490a80c552550(value, calendar) {
69
+ if (value === null) return null;
70
+ if (!value) return undefined;
71
+ return $17UYf$internationalizeddate.toCalendar(value, calendar);
129
72
  }
130
-
131
- function $d2539a9a7cedbf57925a2a19$export$useDefaultProps(v, granularity) {
132
- // Compute default granularity and time zone from the value. If the value becomes null, keep the last values.
133
- let lastValue = useRef(v);
134
-
135
- if (v) {
136
- lastValue.current = v;
137
- }
138
-
139
- v = lastValue.current;
140
- let defaultTimeZone = v && 'timeZone' in v ? v.timeZone : undefined;
141
- granularity = granularity || (v && 'minute' in v ? 'minute' : 'day'); // props.granularity must actually exist in the value if one is provided.
142
-
143
- if (v && !(granularity in v)) {
144
- throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
145
- }
146
-
147
- return [granularity, defaultTimeZone];
73
+ function $a1a093054004b094$export$66aa2b09de4b1ea5(placeholderValue, granularity, calendar, timeZone) {
74
+ if (placeholderValue) return $a1a093054004b094$export$61a490a80c552550(placeholderValue, calendar);
75
+ let date = $17UYf$internationalizeddate.toCalendar($17UYf$internationalizeddate.now(timeZone).set({
76
+ hour: 0,
77
+ minute: 0,
78
+ second: 0,
79
+ millisecond: 0
80
+ }), calendar);
81
+ if (granularity === 'year' || granularity === 'month' || granularity === 'day') return $17UYf$internationalizeddate.toCalendarDate(date);
82
+ if (!timeZone) return $17UYf$internationalizeddate.toCalendarDateTime(date);
83
+ return date;
148
84
  }
149
-
150
- function useDatePickerState(props) {
151
- let [isOpen, setOpen] = useState(false);
152
- let [value, setValue] = useControlledState(props.value, props.defaultValue || null, props.onChange);
153
- let v = value || props.placeholderValue;
154
- let [granularity, defaultTimeZone] = $d2539a9a7cedbf57925a2a19$export$useDefaultProps(v, props.granularity);
155
- let dateValue = value != null ? value.toDate(defaultTimeZone != null ? defaultTimeZone : 'UTC') : null;
156
- let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second' || granularity === 'millisecond';
157
- let [selectedDate, setSelectedDate] = useState(null);
158
- let [selectedTime, setSelectedTime] = useState(null);
159
-
160
- if (value) {
161
- selectedDate = value;
162
-
163
- if ('hour' in value) {
164
- selectedTime = value;
165
- }
166
- } // props.granularity must actually exist in the value if one is provided.
167
-
168
-
169
- if (v && !(granularity in v)) {
170
- throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
171
- }
172
-
173
- let commitValue = (date, time) => {
174
- setValue('timeZone' in time ? time.set(toDateFields(date)) : toCalendarDateTime(date, time));
175
- }; // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
176
-
177
-
178
- let selectDate = newValue => {
179
- if (hasTime) {
180
- if (selectedTime) {
181
- commitValue(newValue, selectedTime);
182
- } else {
183
- setSelectedDate(newValue);
184
- }
185
- } else {
186
- setValue(newValue);
187
- }
188
-
189
- if (!hasTime) {
190
- setOpen(false);
191
- }
192
- };
193
-
194
- let selectTime = newValue => {
195
- if (selectedDate) {
196
- commitValue(selectedDate, newValue);
197
- } else {
198
- setSelectedTime(newValue);
199
- }
200
- };
201
-
202
- let validationState = props.validationState || ($d2539a9a7cedbf57925a2a19$export$isInvalid(value, props.minValue, props.maxValue) ? 'invalid' : null);
203
- return {
204
- value,
205
- setValue,
206
- dateValue: selectedDate,
207
- timeValue: selectedTime,
208
- setDateValue: selectDate,
209
- setTimeValue: selectTime,
210
- granularity,
211
- isOpen,
212
-
213
- setOpen(isOpen) {
214
- // Commit the selected date when the calendar is closed. Use a placeholder time if one wasn't set.
215
- // If only the time was set and not the date, don't commit. The state will be preserved until
216
- // the user opens the popover again.
217
- if (!isOpen && !value && selectedDate && hasTime) {
218
- commitValue(selectedDate, selectedTime || $d2539a9a7cedbf57925a2a19$export$getPlaceholderTime(props.placeholderValue));
219
- }
220
-
221
- setOpen(isOpen);
222
- },
223
-
224
- validationState,
225
-
226
- formatValue(locale, fieldOptions) {
227
- if (!dateValue) {
228
- return '';
229
- }
230
-
231
- let formatOptions = $d2539a9a7cedbf57925a2a19$export$getFormatOptions(fieldOptions, {
85
+ function $a1a093054004b094$export$2440da353cedad43(v, granularity) {
86
+ // Compute default granularity and time zone from the value. If the value becomes null, keep the last values.
87
+ let lastValue = $17UYf$react.useRef(v);
88
+ if (v) lastValue.current = v;
89
+ v = lastValue.current;
90
+ let defaultTimeZone = v && 'timeZone' in v ? v.timeZone : undefined;
91
+ granularity = granularity || (v && 'minute' in v ? 'minute' : 'day');
92
+ // props.granularity must actually exist in the value if one is provided.
93
+ if (v && !(granularity in v)) throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
94
+ return [
232
95
  granularity,
233
- timeZone: defaultTimeZone,
234
- hideTimeZone: props.hideTimeZone,
235
- hourCycle: props.hourCycle
236
- });
237
- let formatter = new DateFormatter(locale, formatOptions);
238
- return formatter.format(dateValue);
239
- }
240
-
241
- };
96
+ defaultTimeZone
97
+ ];
242
98
  }
243
99
 
244
- exports.useDatePickerState = useDatePickerState;
245
- const $b879385ee0fe7268aa1feb476f2d613$var$EDITABLE_SEGMENTS = {
246
- year: true,
247
- month: true,
248
- day: true,
249
- hour: true,
250
- minute: true,
251
- second: true,
252
- dayPeriod: true,
253
- era: true
254
- };
255
- const $b879385ee0fe7268aa1feb476f2d613$var$PAGE_STEP = {
256
- year: 5,
257
- month: 2,
258
- day: 7,
259
- hour: 2,
260
- minute: 15,
261
- second: 15
262
- }; // Node seems to convert everything to lowercase...
263
-
264
- const $b879385ee0fe7268aa1feb476f2d613$var$TYPE_MAPPING = {
265
- dayperiod: 'dayPeriod'
266
- };
267
100
 
268
- function useDatePickerFieldState(props) {
269
- let {
270
- locale,
271
- createCalendar,
272
- hideTimeZone
273
- } = props;
274
- let v = props.value || props.defaultValue || props.placeholderValue;
275
- let [granularity, defaultTimeZone] = $d2539a9a7cedbf57925a2a19$export$useDefaultProps(v, props.granularity);
276
- let timeZone = defaultTimeZone || 'UTC'; // props.granularity must actually exist in the value if one is provided.
277
101
 
278
- if (v && !(granularity in v)) {
279
- throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
280
- }
281
102
 
282
- let formatOpts = useMemo(() => {
283
- var _props$maxGranularity;
284
103
 
104
+ function $0c9ae69d60d14242$export$87194bb378cc3ac2(props) {
105
+ let [isOpen1, setOpen] = $17UYf$react.useState(false);
106
+ let [value, setValue] = $17UYf$reactstatelyutils.useControlledState(props.value, props.defaultValue || null, props.onChange);
107
+ let v = value || props.placeholderValue;
108
+ let [granularity, defaultTimeZone] = $a1a093054004b094$export$2440da353cedad43(v, props.granularity);
109
+ let dateValue = value != null ? value.toDate(defaultTimeZone ?? 'UTC') : null;
110
+ let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second' || granularity === 'millisecond';
111
+ let [selectedDate, setSelectedDate] = $17UYf$react.useState(null);
112
+ let [selectedTime, setSelectedTime] = $17UYf$react.useState(null);
113
+ if (value) {
114
+ selectedDate = value;
115
+ if ('hour' in value) selectedTime = value;
116
+ }
117
+ // props.granularity must actually exist in the value if one is provided.
118
+ if (v && !(granularity in v)) throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
119
+ let commitValue = (date, time)=>{
120
+ setValue('timeZone' in time ? time.set($17UYf$internationalizeddate.toDateFields(date)) : $17UYf$internationalizeddate.toCalendarDateTime(date, time));
121
+ };
122
+ // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
123
+ let selectDate = (newValue)=>{
124
+ if (hasTime) {
125
+ if (selectedTime) commitValue(newValue, selectedTime);
126
+ else setSelectedDate(newValue);
127
+ } else setValue(newValue);
128
+ if (!hasTime) setOpen(false);
129
+ };
130
+ let selectTime = (newValue)=>{
131
+ if (selectedDate) commitValue(selectedDate, newValue);
132
+ else setSelectedTime(newValue);
133
+ };
134
+ let validationState = props.validationState || ($a1a093054004b094$export$eac50920cf2fd59a(value, props.minValue, props.maxValue) ? 'invalid' : null);
285
135
  return {
286
- granularity,
287
- maxGranularity: (_props$maxGranularity = props.maxGranularity) != null ? _props$maxGranularity : 'year',
288
- timeZone: defaultTimeZone,
289
- hideTimeZone,
290
- hourCycle: props.hourCycle
136
+ value: value,
137
+ setValue: setValue,
138
+ dateValue: selectedDate,
139
+ timeValue: selectedTime,
140
+ setDateValue: selectDate,
141
+ setTimeValue: selectTime,
142
+ granularity: granularity,
143
+ isOpen: isOpen1,
144
+ setOpen (isOpen) {
145
+ // Commit the selected date when the calendar is closed. Use a placeholder time if one wasn't set.
146
+ // If only the time was set and not the date, don't commit. The state will be preserved until
147
+ // the user opens the popover again.
148
+ if (!isOpen && !value && selectedDate && hasTime) commitValue(selectedDate, selectedTime || $a1a093054004b094$export$c5221a78ef73c5e9(props.placeholderValue));
149
+ setOpen(isOpen);
150
+ },
151
+ validationState: validationState,
152
+ formatValue (locale, fieldOptions) {
153
+ if (!dateValue) return '';
154
+ let formatOptions = $a1a093054004b094$export$7e319ea407e63bc0(fieldOptions, {
155
+ granularity: granularity,
156
+ timeZone: defaultTimeZone,
157
+ hideTimeZone: props.hideTimeZone,
158
+ hourCycle: props.hourCycle
159
+ });
160
+ let formatter = new $17UYf$internationalizeddate.DateFormatter(locale, formatOptions);
161
+ return formatter.format(dateValue);
162
+ }
291
163
  };
292
- }, [props.maxGranularity, granularity, props.hourCycle, defaultTimeZone, hideTimeZone]);
293
- let opts = useMemo(() => $d2539a9a7cedbf57925a2a19$export$getFormatOptions({}, formatOpts), [formatOpts]);
294
- let dateFormatter = useMemo(() => new DateFormatter(locale, opts), [locale, opts]);
295
- let resolvedOptions = useMemo(() => dateFormatter.resolvedOptions(), [dateFormatter]);
296
- let calendar = useMemo(() => createCalendar(resolvedOptions.calendar), [createCalendar, resolvedOptions.calendar]); // Determine how many editable segments there are for validation purposes.
297
- // The result is cached for performance.
298
-
299
- let allSegments = useMemo(() => dateFormatter.formatToParts(new Date()).filter(seg => $b879385ee0fe7268aa1feb476f2d613$var$EDITABLE_SEGMENTS[seg.type]).reduce((p, seg) => (p[seg.type] = true, p), {}), [dateFormatter]);
300
- let [validSegments, setValidSegments] = useState(() => props.value || props.defaultValue ? _babelRuntimeHelpersExtends({}, allSegments) : {}); // We keep track of the placeholder date separately in state so that onChange is not called
301
- // until all segments are set. If the value === null (not undefined), then assume the component
302
- // is controlled, so use the placeholder as the value until all segments are entered so it doesn't
303
- // change from uncontrolled to controlled and emit a warning.
304
-
305
- let [placeholderDate, setPlaceholderDate] = useState(() => $d2539a9a7cedbf57925a2a19$export$createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone)); // Reset placeholder when calendar changes
306
-
307
- let lastCalendarIdentifier = useRef(calendar.identifier);
308
- useEffect(() => {
309
- if (calendar.identifier !== lastCalendarIdentifier.current) {
310
- lastCalendarIdentifier.current = calendar.identifier;
311
- setPlaceholderDate(placeholder => Object.keys(validSegments).length > 0 ? toCalendar(placeholder, calendar) : $d2539a9a7cedbf57925a2a19$export$createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone));
312
- }
313
- }, [calendar, granularity, validSegments, defaultTimeZone, props.placeholderValue]);
314
- let [value, setDate] = useControlledState(props.value, props.defaultValue, props.onChange);
315
- let calendarValue = useMemo(() => $d2539a9a7cedbf57925a2a19$export$convertValue(value, calendar), [value, calendar]); // If there is a value prop, and some segments were previously placeholders, mark them all as valid.
316
-
317
- if (value && Object.keys(validSegments).length < Object.keys(allSegments).length) {
318
- validSegments = _babelRuntimeHelpersExtends({}, allSegments);
319
- setValidSegments(validSegments);
320
- } // If the value is set to null and all segments are valid, reset the placeholder.
321
-
322
-
323
- if (value == null && Object.keys(validSegments).length === Object.keys(allSegments).length) {
324
- validSegments = {};
325
- setValidSegments(validSegments);
326
- setPlaceholderDate($d2539a9a7cedbf57925a2a19$export$createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone));
327
- } // If all segments are valid, use the date from state, otherwise use the placeholder date.
164
+ }
328
165
 
329
166
 
330
- let displayValue = calendarValue && Object.keys(validSegments).length >= Object.keys(allSegments).length ? calendarValue : placeholderDate;
167
+ var $f23cc841f3588f43$exports = {};
331
168
 
332
- let setValue = newValue => {
333
- if (props.isDisabled || props.isReadOnly) {
334
- return;
335
- }
169
+ $parcel$export($f23cc841f3588f43$exports, "useDatePickerFieldState", () => $f23cc841f3588f43$export$714a241b39a650d6);
336
170
 
337
- if (Object.keys(validSegments).length >= Object.keys(allSegments).length) {
338
- // The display calendar should not have any effect on the emitted value.
339
- // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
340
- newValue = toCalendar(newValue, (v == null ? void 0 : v.calendar) || new GregorianCalendar());
341
- setDate(newValue);
342
- } else {
343
- setPlaceholderDate(newValue);
344
- }
345
- };
346
171
 
347
- let dateValue = useMemo(() => displayValue.toDate(timeZone), [displayValue, timeZone]);
348
- let segments = useMemo(() => dateFormatter.formatToParts(dateValue).map(segment => {
349
- let isEditable = $b879385ee0fe7268aa1feb476f2d613$var$EDITABLE_SEGMENTS[segment.type];
350
172
 
351
- if (segment.type === 'era' && calendar.getEras().length === 1) {
352
- isEditable = false;
353
- }
354
173
 
355
- return _babelRuntimeHelpersExtends({
356
- type: $b879385ee0fe7268aa1feb476f2d613$var$TYPE_MAPPING[segment.type] || segment.type,
357
- text: segment.value
358
- }, $b879385ee0fe7268aa1feb476f2d613$var$getSegmentLimits(displayValue, segment.type, resolvedOptions), {
359
- isPlaceholder: $b879385ee0fe7268aa1feb476f2d613$var$EDITABLE_SEGMENTS[segment.type] && !validSegments[segment.type],
360
- isEditable
361
- });
362
- }), [dateValue, validSegments, dateFormatter, resolvedOptions, displayValue, calendar]);
363
- let hasEra = useMemo(() => segments.some(s => s.type === 'era'), [segments]);
364
-
365
- let markValid = part => {
366
- validSegments[part] = true;
367
-
368
- if (part === 'year' && hasEra) {
369
- validSegments.era = true;
370
- }
371
-
372
- setValidSegments(_babelRuntimeHelpersExtends({}, validSegments));
373
- };
374
-
375
- let adjustSegment = (type, amount) => {
376
- markValid(type);
377
- setValue($b879385ee0fe7268aa1feb476f2d613$var$addSegment(displayValue, type, amount, resolvedOptions));
378
- };
379
-
380
- let validationState = props.validationState || ($d2539a9a7cedbf57925a2a19$export$isInvalid(calendarValue, props.minValue, props.maxValue) ? 'invalid' : null);
381
- return {
382
- value: calendarValue,
383
- dateValue,
384
- setValue,
385
- segments,
386
- dateFormatter,
387
- validationState,
388
- granularity,
389
-
390
- increment(part) {
391
- adjustSegment(part, 1);
392
- },
393
-
394
- decrement(part) {
395
- adjustSegment(part, -1);
396
- },
397
-
398
- incrementPage(part) {
399
- adjustSegment(part, $b879385ee0fe7268aa1feb476f2d613$var$PAGE_STEP[part] || 1);
400
- },
401
-
402
- decrementPage(part) {
403
- adjustSegment(part, -($b879385ee0fe7268aa1feb476f2d613$var$PAGE_STEP[part] || 1));
404
- },
405
-
406
- setSegment(part, v) {
407
- markValid(part);
408
- setValue($b879385ee0fe7268aa1feb476f2d613$var$setSegment(displayValue, part, v, resolvedOptions));
409
- },
410
-
411
- confirmPlaceholder(part) {
412
- if (props.isDisabled || props.isReadOnly) {
413
- return;
414
- }
415
-
416
- if (!part) {
417
- // Confirm the rest of the placeholder if any of the segments are valid.
418
- let numValid = Object.keys(validSegments).length;
419
-
420
- if (numValid > 0 && numValid < Object.keys(allSegments).length) {
421
- validSegments = _babelRuntimeHelpersExtends({}, allSegments);
422
- setValidSegments(validSegments);
423
- setValue(displayValue.copy());
424
- }
425
- } else if (!validSegments[part]) {
426
- markValid(part);
427
- setValue(displayValue.copy());
428
- }
429
- },
430
-
431
- clearSegment(part) {
432
- delete validSegments[part];
433
- setValidSegments(_babelRuntimeHelpersExtends({}, validSegments));
434
- let placeholder = $d2539a9a7cedbf57925a2a19$export$createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone);
435
- let value = displayValue; // Reset day period to default without changing the hour.
436
-
437
- if (part === 'dayPeriod' && 'hour' in displayValue && 'hour' in placeholder) {
438
- let isPM = displayValue.hour >= 12;
439
- let shouldBePM = placeholder.hour >= 12;
440
-
441
- if (isPM && !shouldBePM) {
442
- value = displayValue.set({
443
- hour: displayValue.hour - 12
444
- });
445
- } else if (!isPM && shouldBePM) {
446
- value = displayValue.set({
447
- hour: displayValue.hour + 12
448
- });
174
+ const $f23cc841f3588f43$var$EDITABLE_SEGMENTS = {
175
+ year: true,
176
+ month: true,
177
+ day: true,
178
+ hour: true,
179
+ minute: true,
180
+ second: true,
181
+ dayPeriod: true,
182
+ era: true
183
+ };
184
+ const $f23cc841f3588f43$var$PAGE_STEP = {
185
+ year: 5,
186
+ month: 2,
187
+ day: 7,
188
+ hour: 2,
189
+ minute: 15,
190
+ second: 15
191
+ };
192
+ // Node seems to convert everything to lowercase...
193
+ const $f23cc841f3588f43$var$TYPE_MAPPING = {
194
+ dayperiod: 'dayPeriod'
195
+ };
196
+ function $f23cc841f3588f43$export$714a241b39a650d6(props) {
197
+ let { locale: locale , createCalendar: createCalendar , hideTimeZone: hideTimeZone } = props;
198
+ let v1 = props.value || props.defaultValue || props.placeholderValue;
199
+ let [granularity, defaultTimeZone] = $a1a093054004b094$export$2440da353cedad43(v1, props.granularity);
200
+ let timeZone = defaultTimeZone || 'UTC';
201
+ // props.granularity must actually exist in the value if one is provided.
202
+ if (v1 && !(granularity in v1)) throw new Error('Invalid granularity ' + granularity + ' for value ' + v1.toString());
203
+ let formatOpts = $17UYf$react.useMemo(()=>({
204
+ granularity: granularity,
205
+ maxGranularity: props.maxGranularity ?? 'year',
206
+ timeZone: defaultTimeZone,
207
+ hideTimeZone: hideTimeZone,
208
+ hourCycle: props.hourCycle
209
+ })
210
+ , [
211
+ props.maxGranularity,
212
+ granularity,
213
+ props.hourCycle,
214
+ defaultTimeZone,
215
+ hideTimeZone
216
+ ]);
217
+ let opts = $17UYf$react.useMemo(()=>$a1a093054004b094$export$7e319ea407e63bc0({
218
+ }, formatOpts)
219
+ , [
220
+ formatOpts
221
+ ]);
222
+ let dateFormatter = $17UYf$react.useMemo(()=>new $17UYf$internationalizeddate.DateFormatter(locale, opts)
223
+ , [
224
+ locale,
225
+ opts
226
+ ]);
227
+ let resolvedOptions = $17UYf$react.useMemo(()=>dateFormatter.resolvedOptions()
228
+ , [
229
+ dateFormatter
230
+ ]);
231
+ let calendar = $17UYf$react.useMemo(()=>createCalendar(resolvedOptions.calendar)
232
+ , [
233
+ createCalendar,
234
+ resolvedOptions.calendar
235
+ ]);
236
+ // Determine how many editable segments there are for validation purposes.
237
+ // The result is cached for performance.
238
+ let allSegments = $17UYf$react.useMemo(()=>dateFormatter.formatToParts(new Date()).filter((seg)=>$f23cc841f3588f43$var$EDITABLE_SEGMENTS[seg.type]
239
+ ).reduce((p, seg)=>(p[seg.type] = true, p)
240
+ , {
241
+ })
242
+ , [
243
+ dateFormatter
244
+ ]);
245
+ let [validSegments, setValidSegments] = $17UYf$react.useState(()=>props.value || props.defaultValue ? {
246
+ ...allSegments
247
+ } : {
449
248
  }
450
- } else if (part in displayValue) {
451
- value = displayValue.set({
452
- [part]: placeholder[part]
453
- });
454
- }
455
-
456
- setDate(null);
457
- setValue(value);
458
- },
459
-
460
- getFormatOptions(fieldOptions) {
461
- return $d2539a9a7cedbf57925a2a19$export$getFormatOptions(fieldOptions, formatOpts);
462
- }
463
-
464
- };
465
- }
466
-
467
- exports.useDatePickerFieldState = useDatePickerFieldState;
468
-
469
- function $b879385ee0fe7268aa1feb476f2d613$var$getSegmentLimits(date, type, options) {
470
- switch (type) {
471
- case 'era':
472
- {
473
- let eras = date.calendar.getEras();
474
- return {
475
- value: eras.indexOf(date.era),
476
- minValue: 0,
477
- maxValue: eras.length - 1
478
- };
479
- }
480
-
481
- case 'year':
482
- return {
483
- value: date.year,
484
- minValue: 1,
485
- maxValue: date.calendar.getYearsInEra(date)
486
- };
487
-
488
- case 'month':
489
- return {
490
- value: date.month,
491
- minValue: getMinimumMonthInYear(date),
492
- maxValue: date.calendar.getMonthsInYear(date)
493
- };
494
-
495
- case 'day':
496
- return {
497
- value: date.day,
498
- minValue: getMinimumDayInMonth(date),
499
- maxValue: date.calendar.getDaysInMonth(date)
500
- };
501
- }
502
-
503
- if ('hour' in date) {
504
- switch (type) {
505
- case 'dayPeriod':
506
- return {
507
- value: date.hour >= 12 ? 12 : 0,
508
- minValue: 0,
509
- maxValue: 12
510
- };
511
-
512
- case 'hour':
513
- if (options.hour12) {
514
- let isPM = date.hour >= 12;
515
- return {
516
- value: date.hour,
517
- minValue: isPM ? 12 : 0,
518
- maxValue: isPM ? 23 : 11
519
- };
249
+ );
250
+ // We keep track of the placeholder date separately in state so that onChange is not called
251
+ // until all segments are set. If the value === null (not undefined), then assume the component
252
+ // is controlled, so use the placeholder as the value until all segments are entered so it doesn't
253
+ // change from uncontrolled to controlled and emit a warning.
254
+ let [placeholderDate, setPlaceholderDate] = $17UYf$react.useState(()=>$a1a093054004b094$export$66aa2b09de4b1ea5(props.placeholderValue, granularity, calendar, defaultTimeZone)
255
+ );
256
+ // Reset placeholder when calendar changes
257
+ let lastCalendarIdentifier = $17UYf$react.useRef(calendar.identifier);
258
+ $17UYf$react.useEffect(()=>{
259
+ if (calendar.identifier !== lastCalendarIdentifier.current) {
260
+ lastCalendarIdentifier.current = calendar.identifier;
261
+ setPlaceholderDate((placeholder)=>Object.keys(validSegments).length > 0 ? $17UYf$internationalizeddate.toCalendar(placeholder, calendar) : $a1a093054004b094$export$66aa2b09de4b1ea5(props.placeholderValue, granularity, calendar, defaultTimeZone)
262
+ );
520
263
  }
521
-
522
- return {
523
- value: date.hour,
524
- minValue: 0,
525
- maxValue: 23
526
- };
527
-
528
- case 'minute':
529
- return {
530
- value: date.minute,
531
- minValue: 0,
532
- maxValue: 59
264
+ }, [
265
+ calendar,
266
+ granularity,
267
+ validSegments,
268
+ defaultTimeZone,
269
+ props.placeholderValue
270
+ ]);
271
+ let [value1, setDate] = $17UYf$reactstatelyutils.useControlledState(props.value, props.defaultValue, props.onChange);
272
+ let calendarValue = $17UYf$react.useMemo(()=>$a1a093054004b094$export$61a490a80c552550(value1, calendar)
273
+ , [
274
+ value1,
275
+ calendar
276
+ ]);
277
+ // If there is a value prop, and some segments were previously placeholders, mark them all as valid.
278
+ if (value1 && Object.keys(validSegments).length < Object.keys(allSegments).length) {
279
+ validSegments = {
280
+ ...allSegments
533
281
  };
534
-
535
- case 'second':
536
- return {
537
- value: date.second,
538
- minValue: 0,
539
- maxValue: 59
282
+ setValidSegments(validSegments);
283
+ }
284
+ // If the value is set to null and all segments are valid, reset the placeholder.
285
+ if (value1 == null && Object.keys(validSegments).length === Object.keys(allSegments).length) {
286
+ validSegments = {
540
287
  };
288
+ setValidSegments(validSegments);
289
+ setPlaceholderDate($a1a093054004b094$export$66aa2b09de4b1ea5(props.placeholderValue, granularity, calendar, defaultTimeZone));
541
290
  }
542
- }
543
-
544
- return {};
545
- }
546
-
547
- function $b879385ee0fe7268aa1feb476f2d613$var$addSegment(value, part, amount, options) {
548
- switch (part) {
549
- case 'era':
550
- case 'year':
551
- case 'month':
552
- case 'day':
553
- return value.cycle(part, amount, {
554
- round: part === 'year'
555
- });
556
- }
557
-
558
- if ('hour' in value) {
559
- switch (part) {
560
- case 'dayPeriod':
561
- {
562
- let hours = value.hour;
563
- let isPM = hours >= 12;
564
- return value.set({
565
- hour: isPM ? hours - 12 : hours + 12
566
- });
567
- }
568
-
569
- case 'hour':
570
- case 'minute':
571
- case 'second':
572
- return value.cycle(part, amount, {
573
- round: part !== 'hour',
574
- hourCycle: options.hour12 ? 12 : 24
291
+ // If all segments are valid, use the date from state, otherwise use the placeholder date.
292
+ let displayValue = calendarValue && Object.keys(validSegments).length >= Object.keys(allSegments).length ? calendarValue : placeholderDate;
293
+ let setValue = (newValue)=>{
294
+ if (props.isDisabled || props.isReadOnly) return;
295
+ if (Object.keys(validSegments).length >= Object.keys(allSegments).length) {
296
+ // The display calendar should not have any effect on the emitted value.
297
+ // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
298
+ newValue = $17UYf$internationalizeddate.toCalendar(newValue, v1?.calendar || new $17UYf$internationalizeddate.GregorianCalendar());
299
+ setDate(newValue);
300
+ } else setPlaceholderDate(newValue);
301
+ };
302
+ let dateValue = $17UYf$react.useMemo(()=>displayValue.toDate(timeZone)
303
+ , [
304
+ displayValue,
305
+ timeZone
306
+ ]);
307
+ let segments = $17UYf$react.useMemo(()=>dateFormatter.formatToParts(dateValue).map((segment)=>{
308
+ let isEditable = $f23cc841f3588f43$var$EDITABLE_SEGMENTS[segment.type];
309
+ if (segment.type === 'era' && calendar.getEras().length === 1) isEditable = false;
310
+ return {
311
+ type: $f23cc841f3588f43$var$TYPE_MAPPING[segment.type] || segment.type,
312
+ text: segment.value,
313
+ ...$f23cc841f3588f43$var$getSegmentLimits(displayValue, segment.type, resolvedOptions),
314
+ isPlaceholder: $f23cc841f3588f43$var$EDITABLE_SEGMENTS[segment.type] && !validSegments[segment.type],
315
+ isEditable: isEditable
316
+ };
317
+ })
318
+ , [
319
+ dateValue,
320
+ validSegments,
321
+ dateFormatter,
322
+ resolvedOptions,
323
+ displayValue,
324
+ calendar
325
+ ]);
326
+ let hasEra = $17UYf$react.useMemo(()=>segments.some((s)=>s.type === 'era'
327
+ )
328
+ , [
329
+ segments
330
+ ]);
331
+ let markValid = (part)=>{
332
+ validSegments[part] = true;
333
+ if (part === 'year' && hasEra) validSegments.era = true;
334
+ setValidSegments({
335
+ ...validSegments
575
336
  });
337
+ };
338
+ let adjustSegment = (type, amount)=>{
339
+ markValid(type);
340
+ setValue($f23cc841f3588f43$var$addSegment(displayValue, type, amount, resolvedOptions));
341
+ };
342
+ let validationState = props.validationState || ($a1a093054004b094$export$eac50920cf2fd59a(calendarValue, props.minValue, props.maxValue) ? 'invalid' : null);
343
+ return {
344
+ value: calendarValue,
345
+ dateValue: dateValue,
346
+ setValue: setValue,
347
+ segments: segments,
348
+ dateFormatter: dateFormatter,
349
+ validationState: validationState,
350
+ granularity: granularity,
351
+ increment (part) {
352
+ adjustSegment(part, 1);
353
+ },
354
+ decrement (part) {
355
+ adjustSegment(part, -1);
356
+ },
357
+ incrementPage (part) {
358
+ adjustSegment(part, $f23cc841f3588f43$var$PAGE_STEP[part] || 1);
359
+ },
360
+ decrementPage (part) {
361
+ adjustSegment(part, -($f23cc841f3588f43$var$PAGE_STEP[part] || 1));
362
+ },
363
+ setSegment (part, v) {
364
+ markValid(part);
365
+ setValue($f23cc841f3588f43$var$setSegment(displayValue, part, v, resolvedOptions));
366
+ },
367
+ confirmPlaceholder (part) {
368
+ if (props.isDisabled || props.isReadOnly) return;
369
+ if (!part) {
370
+ // Confirm the rest of the placeholder if any of the segments are valid.
371
+ let numValid = Object.keys(validSegments).length;
372
+ if (numValid > 0 && numValid < Object.keys(allSegments).length) {
373
+ validSegments = {
374
+ ...allSegments
375
+ };
376
+ setValidSegments(validSegments);
377
+ setValue(displayValue.copy());
378
+ }
379
+ } else if (!validSegments[part]) {
380
+ markValid(part);
381
+ setValue(displayValue.copy());
382
+ }
383
+ },
384
+ clearSegment (part) {
385
+ delete validSegments[part];
386
+ setValidSegments({
387
+ ...validSegments
388
+ });
389
+ let placeholder = $a1a093054004b094$export$66aa2b09de4b1ea5(props.placeholderValue, granularity, calendar, defaultTimeZone);
390
+ let value = displayValue;
391
+ // Reset day period to default without changing the hour.
392
+ if (part === 'dayPeriod' && 'hour' in displayValue && 'hour' in placeholder) {
393
+ let isPM = displayValue.hour >= 12;
394
+ let shouldBePM = placeholder.hour >= 12;
395
+ if (isPM && !shouldBePM) value = displayValue.set({
396
+ hour: displayValue.hour - 12
397
+ });
398
+ else if (!isPM && shouldBePM) value = displayValue.set({
399
+ hour: displayValue.hour + 12
400
+ });
401
+ } else if (part in displayValue) value = displayValue.set({
402
+ [part]: placeholder[part]
403
+ });
404
+ setDate(null);
405
+ setValue(value);
406
+ },
407
+ getFormatOptions (fieldOptions) {
408
+ return $a1a093054004b094$export$7e319ea407e63bc0(fieldOptions, formatOpts);
409
+ }
410
+ };
411
+ }
412
+ function $f23cc841f3588f43$var$getSegmentLimits(date, type, options) {
413
+ switch(type){
414
+ case 'era':
415
+ {
416
+ let eras = date.calendar.getEras();
417
+ return {
418
+ value: eras.indexOf(date.era),
419
+ minValue: 0,
420
+ maxValue: eras.length - 1
421
+ };
422
+ }
423
+ case 'year':
424
+ return {
425
+ value: date.year,
426
+ minValue: 1,
427
+ maxValue: date.calendar.getYearsInEra(date)
428
+ };
429
+ case 'month':
430
+ return {
431
+ value: date.month,
432
+ minValue: $17UYf$internationalizeddate.getMinimumMonthInYear(date),
433
+ maxValue: date.calendar.getMonthsInYear(date)
434
+ };
435
+ case 'day':
436
+ return {
437
+ value: date.day,
438
+ minValue: $17UYf$internationalizeddate.getMinimumDayInMonth(date),
439
+ maxValue: date.calendar.getDaysInMonth(date)
440
+ };
441
+ }
442
+ if ('hour' in date) switch(type){
443
+ case 'dayPeriod':
444
+ return {
445
+ value: date.hour >= 12 ? 12 : 0,
446
+ minValue: 0,
447
+ maxValue: 12
448
+ };
449
+ case 'hour':
450
+ if (options.hour12) {
451
+ let isPM = date.hour >= 12;
452
+ return {
453
+ value: date.hour,
454
+ minValue: isPM ? 12 : 0,
455
+ maxValue: isPM ? 23 : 11
456
+ };
457
+ }
458
+ return {
459
+ value: date.hour,
460
+ minValue: 0,
461
+ maxValue: 23
462
+ };
463
+ case 'minute':
464
+ return {
465
+ value: date.minute,
466
+ minValue: 0,
467
+ maxValue: 59
468
+ };
469
+ case 'second':
470
+ return {
471
+ value: date.second,
472
+ minValue: 0,
473
+ maxValue: 59
474
+ };
576
475
  }
577
- }
476
+ return {
477
+ };
578
478
  }
579
-
580
- function $b879385ee0fe7268aa1feb476f2d613$var$setSegment(value, part, segmentValue, options) {
581
- switch (part) {
582
- case 'day':
583
- case 'month':
584
- case 'year':
585
- return value.set({
586
- [part]: segmentValue
587
- });
588
- }
589
-
590
- if ('hour' in value) {
591
- switch (part) {
592
- case 'dayPeriod':
593
- {
594
- let hours = value.hour;
595
- let wasPM = hours >= 12;
596
- let isPM = segmentValue >= 12;
597
-
598
- if (isPM === wasPM) {
599
- return value;
600
- }
601
-
602
- return value.set({
603
- hour: wasPM ? hours - 12 : hours + 12
604
- });
605
- }
606
-
607
- case 'hour':
608
- // In 12 hour time, ensure that AM/PM does not change
609
- if (options.hour12) {
610
- let hours = value.hour;
611
- let wasPM = hours >= 12;
612
-
613
- if (!wasPM && segmentValue === 12) {
614
- segmentValue = 0;
615
- }
616
-
617
- if (wasPM && segmentValue < 12) {
618
- segmentValue += 12;
619
- }
620
- }
621
-
622
- // fallthrough
623
-
624
- case 'minute':
625
- case 'second':
626
- return value.set({
627
- [part]: segmentValue
628
- });
479
+ function $f23cc841f3588f43$var$addSegment(value, part, amount, options) {
480
+ switch(part){
481
+ case 'era':
482
+ case 'year':
483
+ case 'month':
484
+ case 'day':
485
+ return value.cycle(part, amount, {
486
+ round: part === 'year'
487
+ });
488
+ }
489
+ if ('hour' in value) switch(part){
490
+ case 'dayPeriod':
491
+ {
492
+ let hours = value.hour;
493
+ let isPM = hours >= 12;
494
+ return value.set({
495
+ hour: isPM ? hours - 12 : hours + 12
496
+ });
497
+ }
498
+ case 'hour':
499
+ case 'minute':
500
+ case 'second':
501
+ return value.cycle(part, amount, {
502
+ round: part !== 'hour',
503
+ hourCycle: options.hour12 ? 12 : 24
504
+ });
629
505
  }
630
- }
631
506
  }
632
-
633
- function useDateRangePickerState(props) {
634
- let [isOpen, setOpen] = useState(false);
635
- let [controlledValue, setControlledValue] = useControlledState(props.value, props.defaultValue || null, props.onChange);
636
- let [placeholderValue, setPlaceholderValue] = useState(() => controlledValue || {
637
- start: null,
638
- end: null
639
- }); // Reset the placeholder if the value prop is set to null.
640
-
641
- if (controlledValue == null && placeholderValue.start && placeholderValue.end) {
642
- placeholderValue = {
643
- start: null,
644
- end: null
645
- };
646
- setPlaceholderValue(placeholderValue);
647
- }
648
-
649
- let value = controlledValue || placeholderValue;
650
- let valueRef = useRef(value);
651
- valueRef.current = value;
652
-
653
- let setValue = value => {
654
- valueRef.current = value;
655
- setPlaceholderValue(value);
656
-
657
- if (value != null && value.start && value.end) {
658
- setControlledValue(value);
659
- } else {
660
- setControlledValue(null);
507
+ function $f23cc841f3588f43$var$setSegment(value, part, segmentValue, options) {
508
+ switch(part){
509
+ case 'day':
510
+ case 'month':
511
+ case 'year':
512
+ return value.set({
513
+ [part]: segmentValue
514
+ });
661
515
  }
662
- };
663
-
664
- let v = (value == null ? void 0 : value.start) || (value == null ? void 0 : value.end) || props.placeholderValue;
665
- let [granularity, defaultTimeZone] = $d2539a9a7cedbf57925a2a19$export$useDefaultProps(v, props.granularity);
666
- let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second' || granularity === 'millisecond';
667
- let [dateRange, setSelectedDateRange] = useState(null);
668
- let [timeRange, setSelectedTimeRange] = useState(null);
669
-
670
- if (value && value.start && value.end) {
671
- dateRange = value;
672
-
673
- if ('hour' in value.start) {
674
- timeRange = value;
516
+ if ('hour' in value) switch(part){
517
+ case 'dayPeriod':
518
+ {
519
+ let hours = value.hour;
520
+ let wasPM = hours >= 12;
521
+ let isPM = segmentValue >= 12;
522
+ if (isPM === wasPM) return value;
523
+ return value.set({
524
+ hour: wasPM ? hours - 12 : hours + 12
525
+ });
526
+ }
527
+ case 'hour':
528
+ // In 12 hour time, ensure that AM/PM does not change
529
+ if (options.hour12) {
530
+ let hours = value.hour;
531
+ let wasPM = hours >= 12;
532
+ if (!wasPM && segmentValue === 12) segmentValue = 0;
533
+ if (wasPM && segmentValue < 12) segmentValue += 12;
534
+ }
535
+ // fallthrough
536
+ case 'minute':
537
+ case 'second':
538
+ return value.set({
539
+ [part]: segmentValue
540
+ });
675
541
  }
676
- }
542
+ }
677
543
 
678
- let commitValue = (dateRange, timeRange) => {
679
- setValue({
680
- start: 'timeZone' in timeRange.start ? timeRange.start.set(toDateFields(dateRange.start)) : toCalendarDateTime(dateRange.start, timeRange.start),
681
- end: 'timeZone' in timeRange.end ? timeRange.end.set(toDateFields(dateRange.end)) : toCalendarDateTime(dateRange.end, timeRange.end)
682
- });
683
- }; // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
684
544
 
545
+ var $85a6c757c811abb9$exports = {};
685
546
 
686
- let setDateRange = range => {
687
- if (hasTime) {
688
- var _timeRange, _timeRange2;
547
+ $parcel$export($85a6c757c811abb9$exports, "useDateRangePickerState", () => $85a6c757c811abb9$export$e50a61c1de9f574);
689
548
 
690
- if (range.start && range.end && (_timeRange = timeRange) != null && _timeRange.start && (_timeRange2 = timeRange) != null && _timeRange2.end) {
691
- commitValue(range, timeRange);
692
- } else {
693
- setSelectedDateRange(range);
694
- }
695
- } else if (range.start && range.end) {
696
- setValue(range);
697
- } else {
698
- setSelectedDateRange(range);
699
- }
700
549
 
701
- if (!hasTime) {
702
- setOpen(false);
703
- }
704
- };
705
550
 
706
- let setTimeRange = range => {
707
- var _dateRange, _dateRange2;
708
551
 
709
- if ((_dateRange = dateRange) != null && _dateRange.start && (_dateRange2 = dateRange) != null && _dateRange2.end && range.start && range.end) {
710
- commitValue(dateRange, range);
711
- } else {
712
- setSelectedTimeRange(range);
552
+ function $85a6c757c811abb9$export$e50a61c1de9f574(props) {
553
+ let [isOpen1, setOpen] = $17UYf$react.useState(false);
554
+ let [controlledValue, setControlledValue] = $17UYf$reactstatelyutils.useControlledState(props.value, props.defaultValue || null, props.onChange);
555
+ let [placeholderValue, setPlaceholderValue] = $17UYf$react.useState(()=>controlledValue || {
556
+ start: null,
557
+ end: null
558
+ }
559
+ );
560
+ // Reset the placeholder if the value prop is set to null.
561
+ if (controlledValue == null && placeholderValue.start && placeholderValue.end) {
562
+ placeholderValue = {
563
+ start: null,
564
+ end: null
565
+ };
566
+ setPlaceholderValue(placeholderValue);
713
567
  }
714
- };
715
-
716
- let validationState = props.validationState || (value != null && ($d2539a9a7cedbf57925a2a19$export$isInvalid(value.start, props.minValue, props.maxValue) || $d2539a9a7cedbf57925a2a19$export$isInvalid(value.end, props.minValue, props.maxValue) || value.end != null && value.start != null && value.end.compare(value.start) < 0) ? 'invalid' : null);
717
- return {
718
- value,
719
- setValue,
720
- dateRange,
721
- timeRange,
722
- granularity,
723
-
724
- setDate(part, date) {
725
- setDateRange(_babelRuntimeHelpersExtends({}, dateRange, {
726
- [part]: date
727
- }));
728
- },
729
-
730
- setTime(part, time) {
731
- setTimeRange(_babelRuntimeHelpersExtends({}, timeRange, {
732
- [part]: time
733
- }));
734
- },
735
-
736
- setDateTime(part, dateTime) {
737
- setValue(_babelRuntimeHelpersExtends({}, value, {
738
- [part]: dateTime
739
- }));
740
- },
741
-
742
- setDateRange,
743
- setTimeRange,
744
- isOpen,
745
-
746
- setOpen(isOpen) {
747
- var _dateRange3, _dateRange4;
748
-
749
- // Commit the selected date range when the calendar is closed. Use a placeholder time if one wasn't set.
750
- // If only the time range was set and not the date range, don't commit. The state will be preserved until
751
- // the user opens the popover again.
752
- if (!isOpen && !(value != null && value.start && value != null && value.end) && (_dateRange3 = dateRange) != null && _dateRange3.start && (_dateRange4 = dateRange) != null && _dateRange4.end && hasTime) {
753
- var _timeRange3, _timeRange4;
754
-
755
- commitValue(dateRange, {
756
- start: ((_timeRange3 = timeRange) == null ? void 0 : _timeRange3.start) || $d2539a9a7cedbf57925a2a19$export$getPlaceholderTime(props.placeholderValue),
757
- end: ((_timeRange4 = timeRange) == null ? void 0 : _timeRange4.end) || $d2539a9a7cedbf57925a2a19$export$getPlaceholderTime(props.placeholderValue)
568
+ let value1 = controlledValue || placeholderValue;
569
+ let valueRef = $17UYf$react.useRef(value1);
570
+ valueRef.current = value1;
571
+ let setValue = (value)=>{
572
+ valueRef.current = value;
573
+ setPlaceholderValue(value);
574
+ if (value?.start && value.end) setControlledValue(value);
575
+ else setControlledValue(null);
576
+ };
577
+ let v = value1?.start || value1?.end || props.placeholderValue;
578
+ let [granularity, defaultTimeZone] = $a1a093054004b094$export$2440da353cedad43(v, props.granularity);
579
+ let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second' || granularity === 'millisecond';
580
+ let [dateRange1, setSelectedDateRange] = $17UYf$react.useState(null);
581
+ let [timeRange1, setSelectedTimeRange] = $17UYf$react.useState(null);
582
+ if (value1 && value1.start && value1.end) {
583
+ dateRange1 = value1;
584
+ if ('hour' in value1.start) timeRange1 = value1;
585
+ }
586
+ let commitValue = (dateRange, timeRange)=>{
587
+ setValue({
588
+ start: 'timeZone' in timeRange.start ? timeRange.start.set($17UYf$internationalizeddate.toDateFields(dateRange.start)) : $17UYf$internationalizeddate.toCalendarDateTime(dateRange.start, timeRange.start),
589
+ end: 'timeZone' in timeRange.end ? timeRange.end.set($17UYf$internationalizeddate.toDateFields(dateRange.end)) : $17UYf$internationalizeddate.toCalendarDateTime(dateRange.end, timeRange.end)
758
590
  });
759
- }
760
-
761
- setOpen(isOpen);
762
- },
763
-
764
- validationState,
765
-
766
- formatValue(locale, fieldOptions) {
767
- if (!value || !value.start || !value.end) {
768
- return '';
769
- }
770
-
771
- let startTimeZone = 'timeZone' in value.start ? value.start.timeZone : undefined;
772
- let startGranularity = props.granularity || (value.start && 'minute' in value.start ? 'minute' : 'day');
773
- let endTimeZone = 'timeZone' in value.end ? value.end.timeZone : undefined;
774
- let endGranularity = props.granularity || (value.end && 'minute' in value.end ? 'minute' : 'day');
775
- let startOptions = $d2539a9a7cedbf57925a2a19$export$getFormatOptions(fieldOptions, {
776
- granularity: startGranularity,
777
- timeZone: startTimeZone,
778
- hideTimeZone: props.hideTimeZone,
779
- hourCycle: props.hourCycle
780
- });
781
- let startFormatter = new DateFormatter(locale, startOptions);
782
- let endFormatter;
783
-
784
- if (startTimeZone === endTimeZone && startGranularity === endGranularity) {
785
- // Use formatRange, as it results in shorter output when some of the fields
786
- // are shared between the start and end dates (e.g. the same month).
787
- // Formatting will fail if the end date is before the start date. Fall back below when that happens.
788
- try {
789
- return startFormatter.formatRange(value.start.toDate(startTimeZone), value.end.toDate(endTimeZone));
790
- } catch (e) {// ignore
591
+ };
592
+ // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
593
+ let setDateRange = (range)=>{
594
+ if (hasTime) {
595
+ if (range.start && range.end && timeRange1?.start && timeRange1?.end) commitValue(range, timeRange1);
596
+ else setSelectedDateRange(range);
597
+ } else if (range.start && range.end) setValue(range);
598
+ else setSelectedDateRange(range);
599
+ if (!hasTime) setOpen(false);
600
+ };
601
+ let setTimeRange = (range)=>{
602
+ if (dateRange1?.start && dateRange1?.end && range.start && range.end) commitValue(dateRange1, range);
603
+ else setSelectedTimeRange(range);
604
+ };
605
+ let validationState = props.validationState || (value1 != null && ($a1a093054004b094$export$eac50920cf2fd59a(value1.start, props.minValue, props.maxValue) || $a1a093054004b094$export$eac50920cf2fd59a(value1.end, props.minValue, props.maxValue) || value1.end != null && value1.start != null && value1.end.compare(value1.start) < 0) ? 'invalid' : null);
606
+ return {
607
+ value: value1,
608
+ setValue: setValue,
609
+ dateRange: dateRange1,
610
+ timeRange: timeRange1,
611
+ granularity: granularity,
612
+ setDate (part, date) {
613
+ setDateRange({
614
+ ...dateRange1,
615
+ [part]: date
616
+ });
617
+ },
618
+ setTime (part, time) {
619
+ setTimeRange({
620
+ ...timeRange1,
621
+ [part]: time
622
+ });
623
+ },
624
+ setDateTime (part, dateTime) {
625
+ setValue({
626
+ ...value1,
627
+ [part]: dateTime
628
+ });
629
+ },
630
+ setDateRange: setDateRange,
631
+ setTimeRange: setTimeRange,
632
+ isOpen: isOpen1,
633
+ setOpen (isOpen) {
634
+ // Commit the selected date range when the calendar is closed. Use a placeholder time if one wasn't set.
635
+ // If only the time range was set and not the date range, don't commit. The state will be preserved until
636
+ // the user opens the popover again.
637
+ if (!isOpen && !(value1?.start && value1?.end) && dateRange1?.start && dateRange1?.end && hasTime) commitValue(dateRange1, {
638
+ start: timeRange1?.start || $a1a093054004b094$export$c5221a78ef73c5e9(props.placeholderValue),
639
+ end: timeRange1?.end || $a1a093054004b094$export$c5221a78ef73c5e9(props.placeholderValue)
640
+ });
641
+ setOpen(isOpen);
642
+ },
643
+ validationState: validationState,
644
+ formatValue (locale, fieldOptions) {
645
+ if (!value1 || !value1.start || !value1.end) return '';
646
+ let startTimeZone = 'timeZone' in value1.start ? value1.start.timeZone : undefined;
647
+ let startGranularity = props.granularity || (value1.start && 'minute' in value1.start ? 'minute' : 'day');
648
+ let endTimeZone = 'timeZone' in value1.end ? value1.end.timeZone : undefined;
649
+ let endGranularity = props.granularity || (value1.end && 'minute' in value1.end ? 'minute' : 'day');
650
+ let startOptions = $a1a093054004b094$export$7e319ea407e63bc0(fieldOptions, {
651
+ granularity: startGranularity,
652
+ timeZone: startTimeZone,
653
+ hideTimeZone: props.hideTimeZone,
654
+ hourCycle: props.hourCycle
655
+ });
656
+ let startFormatter = new $17UYf$internationalizeddate.DateFormatter(locale, startOptions);
657
+ let endFormatter;
658
+ if (startTimeZone === endTimeZone && startGranularity === endGranularity) {
659
+ // Use formatRange, as it results in shorter output when some of the fields
660
+ // are shared between the start and end dates (e.g. the same month).
661
+ // Formatting will fail if the end date is before the start date. Fall back below when that happens.
662
+ try {
663
+ return startFormatter.formatRange(value1.start.toDate(startTimeZone), value1.end.toDate(endTimeZone));
664
+ } catch (e) {
665
+ // ignore
666
+ }
667
+ endFormatter = startFormatter;
668
+ } else {
669
+ let endOptions = $a1a093054004b094$export$7e319ea407e63bc0(fieldOptions, {
670
+ granularity: endGranularity,
671
+ timeZone: endTimeZone,
672
+ hideTimeZone: props.hideTimeZone,
673
+ hourCycle: props.hourCycle
674
+ });
675
+ endFormatter = new $17UYf$internationalizeddate.DateFormatter(locale, endOptions);
676
+ }
677
+ return `${startFormatter.format(value1.start.toDate(startTimeZone))} – ${endFormatter.format(value1.end.toDate(endTimeZone))}`;
678
+ },
679
+ confirmPlaceholder () {
680
+ // Need to use ref value here because the value can be set in the same tick as
681
+ // a blur, which means the component won't have re-rendered yet.
682
+ let value = valueRef.current;
683
+ if (value && Boolean(value.start) !== Boolean(value.end)) {
684
+ let calendar = (value.start || value.end).calendar;
685
+ let placeholder = $a1a093054004b094$export$66aa2b09de4b1ea5(props.placeholderValue, granularity, calendar, defaultTimeZone);
686
+ setValue({
687
+ start: value.start || placeholder,
688
+ end: value.end || placeholder
689
+ });
690
+ }
791
691
  }
692
+ };
693
+ }
792
694
 
793
- endFormatter = startFormatter;
794
- } else {
795
- let endOptions = $d2539a9a7cedbf57925a2a19$export$getFormatOptions(fieldOptions, {
796
- granularity: endGranularity,
797
- timeZone: endTimeZone,
798
- hideTimeZone: props.hideTimeZone,
799
- hourCycle: props.hourCycle
800
- });
801
- endFormatter = new DateFormatter(locale, endOptions);
802
- }
803
-
804
- return startFormatter.format(value.start.toDate(startTimeZone)) + " \u2013 " + endFormatter.format(value.end.toDate(endTimeZone));
805
- },
806
-
807
- confirmPlaceholder() {
808
- // Need to use ref value here because the value can be set in the same tick as
809
- // a blur, which means the component won't have re-rendered yet.
810
- let value = valueRef.current;
811
-
812
- if (value && Boolean(value.start) !== Boolean(value.end)) {
813
- let calendar = (value.start || value.end).calendar;
814
- let placeholder = $d2539a9a7cedbf57925a2a19$export$createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone);
815
- setValue({
816
- start: value.start || placeholder,
817
- end: value.end || placeholder
818
- });
819
- }
820
- }
821
695
 
822
- };
696
+ var $ef228128a792346b$exports = {};
697
+
698
+ $parcel$export($ef228128a792346b$exports, "useTimeFieldState", () => $ef228128a792346b$export$fd53cef0cc796101);
699
+
700
+
701
+
702
+
703
+ function $ef228128a792346b$export$fd53cef0cc796101(props) {
704
+ let { placeholderValue: placeholderValue = new $17UYf$internationalizeddate.Time() , minValue: minValue , maxValue: maxValue , granularity: granularity } = props;
705
+ let [value, setValue] = $17UYf$reactstatelyutils.useControlledState(props.value, props.defaultValue, props.onChange);
706
+ let v = value || placeholderValue;
707
+ let day = v && 'day' in v ? v : undefined;
708
+ let placeholderDate = $17UYf$react.useMemo(()=>$ef228128a792346b$var$convertValue(placeholderValue)
709
+ , [
710
+ placeholderValue
711
+ ]);
712
+ let minDate = $17UYf$react.useMemo(()=>$ef228128a792346b$var$convertValue(minValue, day)
713
+ , [
714
+ minValue,
715
+ day
716
+ ]);
717
+ let maxDate = $17UYf$react.useMemo(()=>$ef228128a792346b$var$convertValue(maxValue, day)
718
+ , [
719
+ maxValue,
720
+ day
721
+ ]);
722
+ let dateTime = $17UYf$react.useMemo(()=>value == null ? null : $ef228128a792346b$var$convertValue(value)
723
+ , [
724
+ value
725
+ ]);
726
+ let onChange = (newValue)=>{
727
+ setValue(v && 'day' in v ? newValue : newValue && $17UYf$internationalizeddate.toTime(newValue));
728
+ };
729
+ return $f23cc841f3588f43$export$714a241b39a650d6({
730
+ ...props,
731
+ value: dateTime,
732
+ defaultValue: undefined,
733
+ minValue: minDate,
734
+ maxValue: maxDate,
735
+ onChange: onChange,
736
+ granularity: granularity || 'minute',
737
+ maxGranularity: 'hour',
738
+ placeholderValue: placeholderDate
739
+ });
823
740
  }
824
-
825
- exports.useDateRangePickerState = useDateRangePickerState;
826
-
827
- function useTimeFieldState(props) {
828
- let {
829
- placeholderValue = new Time(),
830
- minValue,
831
- maxValue,
832
- granularity
833
- } = props;
834
- let [value, setValue] = useControlledState(props.value, props.defaultValue, props.onChange);
835
- let v = value || placeholderValue;
836
- let day = v && 'day' in v ? v : undefined;
837
- let placeholderDate = useMemo(() => $f9f46b85e0aaf88609046f630d1d0a9$var$convertValue(placeholderValue), [placeholderValue]);
838
- let minDate = useMemo(() => $f9f46b85e0aaf88609046f630d1d0a9$var$convertValue(minValue, day), [minValue, day]);
839
- let maxDate = useMemo(() => $f9f46b85e0aaf88609046f630d1d0a9$var$convertValue(maxValue, day), [maxValue, day]);
840
- let dateTime = useMemo(() => value == null ? null : $f9f46b85e0aaf88609046f630d1d0a9$var$convertValue(value), [value]);
841
-
842
- let onChange = newValue => {
843
- setValue(v && 'day' in v ? newValue : newValue && toTime(newValue));
844
- };
845
-
846
- return useDatePickerFieldState(_babelRuntimeHelpersExtends({}, props, {
847
- value: dateTime,
848
- defaultValue: undefined,
849
- minValue: minDate,
850
- maxValue: maxDate,
851
- onChange,
852
- granularity: granularity || 'minute',
853
- maxGranularity: 'hour',
854
- placeholderValue: placeholderDate
855
- }));
741
+ function $ef228128a792346b$var$convertValue(value, date = $17UYf$internationalizeddate.today($17UYf$internationalizeddate.getLocalTimeZone())) {
742
+ if (!value) return null;
743
+ if ('day' in value) return value;
744
+ return $17UYf$internationalizeddate.toCalendarDateTime(date, value);
856
745
  }
857
746
 
858
- exports.useTimeFieldState = useTimeFieldState;
859
747
 
860
- function $f9f46b85e0aaf88609046f630d1d0a9$var$convertValue(value, date) {
861
- if (date === void 0) {
862
- date = today(getLocalTimeZone());
863
- }
748
+ $parcel$exportWildcard(module.exports, $0c9ae69d60d14242$exports);
749
+ $parcel$exportWildcard(module.exports, $f23cc841f3588f43$exports);
750
+ $parcel$exportWildcard(module.exports, $85a6c757c811abb9$exports);
751
+ $parcel$exportWildcard(module.exports, $ef228128a792346b$exports);
864
752
 
865
- if (!value) {
866
- return null;
867
- }
868
753
 
869
- if ('day' in value) {
870
- return value;
871
- }
872
-
873
- return toCalendarDateTime(date, value);
874
- }
875
754
  //# sourceMappingURL=main.js.map