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