@react-stately/datepicker 3.9.2 → 3.9.4

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.
Files changed (43) hide show
  1. package/dist/import.mjs +4 -1514
  2. package/dist/intlStrings.main.js +108 -0
  3. package/dist/intlStrings.main.js.map +1 -0
  4. package/dist/intlStrings.mjs +110 -0
  5. package/dist/intlStrings.module.js +110 -0
  6. package/dist/intlStrings.module.js.map +1 -0
  7. package/dist/main.js +8 -1518
  8. package/dist/main.js.map +1 -1
  9. package/dist/module.js +4 -1514
  10. package/dist/module.js.map +1 -1
  11. package/dist/placeholders.main.js +409 -0
  12. package/dist/placeholders.main.js.map +1 -0
  13. package/dist/placeholders.mjs +404 -0
  14. package/dist/placeholders.module.js +404 -0
  15. package/dist/placeholders.module.js.map +1 -0
  16. package/dist/types.d.ts.map +1 -1
  17. package/dist/useDateFieldState.main.js +441 -0
  18. package/dist/useDateFieldState.main.js.map +1 -0
  19. package/dist/useDateFieldState.mjs +436 -0
  20. package/dist/useDateFieldState.module.js +436 -0
  21. package/dist/useDateFieldState.module.js.map +1 -0
  22. package/dist/useDatePickerState.main.js +130 -0
  23. package/dist/useDatePickerState.main.js.map +1 -0
  24. package/dist/useDatePickerState.mjs +125 -0
  25. package/dist/useDatePickerState.module.js +125 -0
  26. package/dist/useDatePickerState.module.js.map +1 -0
  27. package/dist/useDateRangePickerState.main.js +237 -0
  28. package/dist/useDateRangePickerState.main.js.map +1 -0
  29. package/dist/useDateRangePickerState.mjs +232 -0
  30. package/dist/useDateRangePickerState.module.js +232 -0
  31. package/dist/useDateRangePickerState.module.js.map +1 -0
  32. package/dist/useTimeFieldState.main.js +86 -0
  33. package/dist/useTimeFieldState.main.js.map +1 -0
  34. package/dist/useTimeFieldState.mjs +81 -0
  35. package/dist/useTimeFieldState.module.js +81 -0
  36. package/dist/useTimeFieldState.module.js.map +1 -0
  37. package/dist/utils.main.js +190 -0
  38. package/dist/utils.main.js.map +1 -0
  39. package/dist/utils.mjs +179 -0
  40. package/dist/utils.module.js +179 -0
  41. package/dist/utils.module.js.map +1 -0
  42. package/package.json +9 -9
  43. package/src/utils.ts +2 -1
@@ -0,0 +1,436 @@
1
+ import {convertValue as $35a22f14a1f04b11$export$61a490a80c552550, createPlaceholderDate as $35a22f14a1f04b11$export$66aa2b09de4b1ea5, getFormatOptions as $35a22f14a1f04b11$export$7e319ea407e63bc0, getValidationResult as $35a22f14a1f04b11$export$f18627323ab57ac0, useDefaultProps as $35a22f14a1f04b11$export$2440da353cedad43} from "./utils.mjs";
2
+ import {getPlaceholder as $3e3ed55ab2966714$export$d3f5c5e0a5023fa0} from "./placeholders.mjs";
3
+ import {DateFormatter as $g03ag$DateFormatter, toCalendar as $g03ag$toCalendar, GregorianCalendar as $g03ag$GregorianCalendar, getMinimumMonthInYear as $g03ag$getMinimumMonthInYear, getMinimumDayInMonth as $g03ag$getMinimumDayInMonth} from "@internationalized/date";
4
+ import {useFormValidationState as $g03ag$useFormValidationState} from "@react-stately/form";
5
+ import {useControlledState as $g03ag$useControlledState} from "@react-stately/utils";
6
+ import {useMemo as $g03ag$useMemo, useState as $g03ag$useState, useRef as $g03ag$useRef, useEffect as $g03ag$useEffect} from "react";
7
+
8
+ /*
9
+ * Copyright 2020 Adobe. All rights reserved.
10
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License. You may obtain a copy
12
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software distributed under
15
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
16
+ * OF ANY KIND, either express or implied. See the License for the specific language
17
+ * governing permissions and limitations under the License.
18
+ */
19
+
20
+
21
+
22
+
23
+
24
+ const $3c0fc76039f1c516$var$EDITABLE_SEGMENTS = {
25
+ year: true,
26
+ month: true,
27
+ day: true,
28
+ hour: true,
29
+ minute: true,
30
+ second: true,
31
+ dayPeriod: true,
32
+ era: true
33
+ };
34
+ const $3c0fc76039f1c516$var$PAGE_STEP = {
35
+ year: 5,
36
+ month: 2,
37
+ day: 7,
38
+ hour: 2,
39
+ minute: 15,
40
+ second: 15
41
+ };
42
+ // Node seems to convert everything to lowercase...
43
+ const $3c0fc76039f1c516$var$TYPE_MAPPING = {
44
+ dayperiod: 'dayPeriod'
45
+ };
46
+ function $3c0fc76039f1c516$export$60e84778edff6d26(props) {
47
+ let { locale: locale, createCalendar: createCalendar, hideTimeZone: hideTimeZone, isDisabled: isDisabled, isReadOnly: isReadOnly, isRequired: isRequired, minValue: minValue, maxValue: maxValue, isDateUnavailable: isDateUnavailable } = props;
48
+ let v = props.value || props.defaultValue || props.placeholderValue;
49
+ let [granularity, defaultTimeZone] = (0, $35a22f14a1f04b11$export$2440da353cedad43)(v, props.granularity);
50
+ let timeZone = defaultTimeZone || 'UTC';
51
+ // props.granularity must actually exist in the value if one is provided.
52
+ if (v && !(granularity in v)) throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
53
+ let defaultFormatter = (0, $g03ag$useMemo)(()=>new (0, $g03ag$DateFormatter)(locale), [
54
+ locale
55
+ ]);
56
+ let calendar = (0, $g03ag$useMemo)(()=>createCalendar(defaultFormatter.resolvedOptions().calendar), [
57
+ createCalendar,
58
+ defaultFormatter
59
+ ]);
60
+ let [value, setDate] = (0, $g03ag$useControlledState)(props.value, props.defaultValue, props.onChange);
61
+ let calendarValue = (0, $g03ag$useMemo)(()=>(0, $35a22f14a1f04b11$export$61a490a80c552550)(value, calendar), [
62
+ value,
63
+ calendar
64
+ ]);
65
+ // We keep track of the placeholder date separately in state so that onChange is not called
66
+ // until all segments are set. If the value === null (not undefined), then assume the component
67
+ // is controlled, so use the placeholder as the value until all segments are entered so it doesn't
68
+ // change from uncontrolled to controlled and emit a warning.
69
+ let [placeholderDate, setPlaceholderDate] = (0, $g03ag$useState)(()=>(0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone));
70
+ let val = calendarValue || placeholderDate;
71
+ let showEra = calendar.identifier === 'gregory' && val.era === 'BC';
72
+ let formatOpts = (0, $g03ag$useMemo)(()=>{
73
+ var _props_maxGranularity;
74
+ return {
75
+ granularity: granularity,
76
+ maxGranularity: (_props_maxGranularity = props.maxGranularity) !== null && _props_maxGranularity !== void 0 ? _props_maxGranularity : 'year',
77
+ timeZone: defaultTimeZone,
78
+ hideTimeZone: hideTimeZone,
79
+ hourCycle: props.hourCycle,
80
+ showEra: showEra,
81
+ shouldForceLeadingZeros: props.shouldForceLeadingZeros
82
+ };
83
+ }, [
84
+ props.maxGranularity,
85
+ granularity,
86
+ props.hourCycle,
87
+ props.shouldForceLeadingZeros,
88
+ defaultTimeZone,
89
+ hideTimeZone,
90
+ showEra
91
+ ]);
92
+ let opts = (0, $g03ag$useMemo)(()=>(0, $35a22f14a1f04b11$export$7e319ea407e63bc0)({}, formatOpts), [
93
+ formatOpts
94
+ ]);
95
+ let dateFormatter = (0, $g03ag$useMemo)(()=>new (0, $g03ag$DateFormatter)(locale, opts), [
96
+ locale,
97
+ opts
98
+ ]);
99
+ let resolvedOptions = (0, $g03ag$useMemo)(()=>dateFormatter.resolvedOptions(), [
100
+ dateFormatter
101
+ ]);
102
+ // Determine how many editable segments there are for validation purposes.
103
+ // The result is cached for performance.
104
+ let allSegments = (0, $g03ag$useMemo)(()=>dateFormatter.formatToParts(new Date()).filter((seg)=>$3c0fc76039f1c516$var$EDITABLE_SEGMENTS[seg.type]).reduce((p, seg)=>(p[seg.type] = true, p), {}), [
105
+ dateFormatter
106
+ ]);
107
+ let [validSegments, setValidSegments] = (0, $g03ag$useState)(()=>props.value || props.defaultValue ? {
108
+ ...allSegments
109
+ } : {});
110
+ let clearedSegment = (0, $g03ag$useRef)();
111
+ // Reset placeholder when calendar changes
112
+ let lastCalendarIdentifier = (0, $g03ag$useRef)(calendar.identifier);
113
+ (0, $g03ag$useEffect)(()=>{
114
+ if (calendar.identifier !== lastCalendarIdentifier.current) {
115
+ lastCalendarIdentifier.current = calendar.identifier;
116
+ setPlaceholderDate((placeholder)=>Object.keys(validSegments).length > 0 ? (0, $g03ag$toCalendar)(placeholder, calendar) : (0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone));
117
+ }
118
+ }, [
119
+ calendar,
120
+ granularity,
121
+ validSegments,
122
+ defaultTimeZone,
123
+ props.placeholderValue
124
+ ]);
125
+ // If there is a value prop, and some segments were previously placeholders, mark them all as valid.
126
+ if (value && Object.keys(validSegments).length < Object.keys(allSegments).length) {
127
+ validSegments = {
128
+ ...allSegments
129
+ };
130
+ setValidSegments(validSegments);
131
+ }
132
+ // If the value is set to null and all segments are valid, reset the placeholder.
133
+ if (value == null && Object.keys(validSegments).length === Object.keys(allSegments).length) {
134
+ validSegments = {};
135
+ setValidSegments(validSegments);
136
+ setPlaceholderDate((0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone));
137
+ }
138
+ // If all segments are valid, use the date from state, otherwise use the placeholder date.
139
+ let displayValue = calendarValue && Object.keys(validSegments).length >= Object.keys(allSegments).length ? calendarValue : placeholderDate;
140
+ let setValue = (newValue)=>{
141
+ if (props.isDisabled || props.isReadOnly) return;
142
+ let validKeys = Object.keys(validSegments);
143
+ let allKeys = Object.keys(allSegments);
144
+ // if all the segments are completed or a timefield with everything but am/pm set the time, also ignore when am/pm cleared
145
+ if (newValue == null) {
146
+ setDate(null);
147
+ setPlaceholderDate((0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone));
148
+ setValidSegments({});
149
+ } else if (validKeys.length >= allKeys.length || validKeys.length === allKeys.length - 1 && allSegments.dayPeriod && !validSegments.dayPeriod && clearedSegment.current !== 'dayPeriod') {
150
+ // The display calendar should not have any effect on the emitted value.
151
+ // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
152
+ newValue = (0, $g03ag$toCalendar)(newValue, (v === null || v === void 0 ? void 0 : v.calendar) || new (0, $g03ag$GregorianCalendar)());
153
+ setDate(newValue);
154
+ } else setPlaceholderDate(newValue);
155
+ clearedSegment.current = null;
156
+ };
157
+ let dateValue = (0, $g03ag$useMemo)(()=>displayValue.toDate(timeZone), [
158
+ displayValue,
159
+ timeZone
160
+ ]);
161
+ let segments = (0, $g03ag$useMemo)(()=>dateFormatter.formatToParts(dateValue).map((segment)=>{
162
+ let isEditable = $3c0fc76039f1c516$var$EDITABLE_SEGMENTS[segment.type];
163
+ if (segment.type === 'era' && calendar.getEras().length === 1) isEditable = false;
164
+ let isPlaceholder = $3c0fc76039f1c516$var$EDITABLE_SEGMENTS[segment.type] && !validSegments[segment.type];
165
+ let placeholder = $3c0fc76039f1c516$var$EDITABLE_SEGMENTS[segment.type] ? (0, $3e3ed55ab2966714$export$d3f5c5e0a5023fa0)(segment.type, segment.value, locale) : null;
166
+ return {
167
+ type: $3c0fc76039f1c516$var$TYPE_MAPPING[segment.type] || segment.type,
168
+ text: isPlaceholder ? placeholder : segment.value,
169
+ ...$3c0fc76039f1c516$var$getSegmentLimits(displayValue, segment.type, resolvedOptions),
170
+ isPlaceholder: isPlaceholder,
171
+ placeholder: placeholder,
172
+ isEditable: isEditable
173
+ };
174
+ }), [
175
+ dateValue,
176
+ validSegments,
177
+ dateFormatter,
178
+ resolvedOptions,
179
+ displayValue,
180
+ calendar,
181
+ locale
182
+ ]);
183
+ // When the era field appears, mark it valid if the year field is already valid.
184
+ // If the era field disappears, remove it from the valid segments.
185
+ if (allSegments.era && validSegments.year && !validSegments.era) {
186
+ validSegments.era = true;
187
+ setValidSegments({
188
+ ...validSegments
189
+ });
190
+ } else if (!allSegments.era && validSegments.era) {
191
+ delete validSegments.era;
192
+ setValidSegments({
193
+ ...validSegments
194
+ });
195
+ }
196
+ let markValid = (part)=>{
197
+ validSegments[part] = true;
198
+ if (part === 'year' && allSegments.era) validSegments.era = true;
199
+ setValidSegments({
200
+ ...validSegments
201
+ });
202
+ };
203
+ let adjustSegment = (type, amount)=>{
204
+ if (!validSegments[type]) {
205
+ markValid(type);
206
+ let validKeys = Object.keys(validSegments);
207
+ let allKeys = Object.keys(allSegments);
208
+ if (validKeys.length >= allKeys.length || validKeys.length === allKeys.length - 1 && allSegments.dayPeriod && !validSegments.dayPeriod) setValue(displayValue);
209
+ } else setValue($3c0fc76039f1c516$var$addSegment(displayValue, type, amount, resolvedOptions));
210
+ };
211
+ let builtinValidation = (0, $g03ag$useMemo)(()=>(0, $35a22f14a1f04b11$export$f18627323ab57ac0)(value, minValue, maxValue, isDateUnavailable, formatOpts), [
212
+ value,
213
+ minValue,
214
+ maxValue,
215
+ isDateUnavailable,
216
+ formatOpts
217
+ ]);
218
+ let validation = (0, $g03ag$useFormValidationState)({
219
+ ...props,
220
+ value: value,
221
+ builtinValidation: builtinValidation
222
+ });
223
+ let isValueInvalid = validation.displayValidation.isInvalid;
224
+ let validationState = props.validationState || (isValueInvalid ? 'invalid' : null);
225
+ var _props_maxGranularity;
226
+ return {
227
+ ...validation,
228
+ value: calendarValue,
229
+ dateValue: dateValue,
230
+ calendar: calendar,
231
+ setValue: setValue,
232
+ segments: segments,
233
+ dateFormatter: dateFormatter,
234
+ validationState: validationState,
235
+ isInvalid: isValueInvalid,
236
+ granularity: granularity,
237
+ maxGranularity: (_props_maxGranularity = props.maxGranularity) !== null && _props_maxGranularity !== void 0 ? _props_maxGranularity : 'year',
238
+ isDisabled: isDisabled,
239
+ isReadOnly: isReadOnly,
240
+ isRequired: isRequired,
241
+ increment (part) {
242
+ adjustSegment(part, 1);
243
+ },
244
+ decrement (part) {
245
+ adjustSegment(part, -1);
246
+ },
247
+ incrementPage (part) {
248
+ adjustSegment(part, $3c0fc76039f1c516$var$PAGE_STEP[part] || 1);
249
+ },
250
+ decrementPage (part) {
251
+ adjustSegment(part, -($3c0fc76039f1c516$var$PAGE_STEP[part] || 1));
252
+ },
253
+ setSegment (part, v) {
254
+ markValid(part);
255
+ setValue($3c0fc76039f1c516$var$setSegment(displayValue, part, v, resolvedOptions));
256
+ },
257
+ confirmPlaceholder () {
258
+ if (props.isDisabled || props.isReadOnly) return;
259
+ // Confirm the placeholder if only the day period is not filled in.
260
+ let validKeys = Object.keys(validSegments);
261
+ let allKeys = Object.keys(allSegments);
262
+ if (validKeys.length === allKeys.length - 1 && allSegments.dayPeriod && !validSegments.dayPeriod) {
263
+ validSegments = {
264
+ ...allSegments
265
+ };
266
+ setValidSegments(validSegments);
267
+ setValue(displayValue.copy());
268
+ }
269
+ },
270
+ clearSegment (part) {
271
+ delete validSegments[part];
272
+ clearedSegment.current = part;
273
+ setValidSegments({
274
+ ...validSegments
275
+ });
276
+ let placeholder = (0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone);
277
+ let value = displayValue;
278
+ // Reset day period to default without changing the hour.
279
+ if (part === 'dayPeriod' && 'hour' in displayValue && 'hour' in placeholder) {
280
+ let isPM = displayValue.hour >= 12;
281
+ let shouldBePM = placeholder.hour >= 12;
282
+ if (isPM && !shouldBePM) value = displayValue.set({
283
+ hour: displayValue.hour - 12
284
+ });
285
+ else if (!isPM && shouldBePM) value = displayValue.set({
286
+ hour: displayValue.hour + 12
287
+ });
288
+ } else if (part in displayValue) value = displayValue.set({
289
+ [part]: placeholder[part]
290
+ });
291
+ setDate(null);
292
+ setValue(value);
293
+ },
294
+ formatValue (fieldOptions) {
295
+ if (!calendarValue) return '';
296
+ let formatOptions = (0, $35a22f14a1f04b11$export$7e319ea407e63bc0)(fieldOptions, formatOpts);
297
+ let formatter = new (0, $g03ag$DateFormatter)(locale, formatOptions);
298
+ return formatter.format(dateValue);
299
+ }
300
+ };
301
+ }
302
+ function $3c0fc76039f1c516$var$getSegmentLimits(date, type, options) {
303
+ switch(type){
304
+ case 'era':
305
+ {
306
+ let eras = date.calendar.getEras();
307
+ return {
308
+ value: eras.indexOf(date.era),
309
+ minValue: 0,
310
+ maxValue: eras.length - 1
311
+ };
312
+ }
313
+ case 'year':
314
+ return {
315
+ value: date.year,
316
+ minValue: 1,
317
+ maxValue: date.calendar.getYearsInEra(date)
318
+ };
319
+ case 'month':
320
+ return {
321
+ value: date.month,
322
+ minValue: (0, $g03ag$getMinimumMonthInYear)(date),
323
+ maxValue: date.calendar.getMonthsInYear(date)
324
+ };
325
+ case 'day':
326
+ return {
327
+ value: date.day,
328
+ minValue: (0, $g03ag$getMinimumDayInMonth)(date),
329
+ maxValue: date.calendar.getDaysInMonth(date)
330
+ };
331
+ }
332
+ if ('hour' in date) switch(type){
333
+ case 'dayPeriod':
334
+ return {
335
+ value: date.hour >= 12 ? 12 : 0,
336
+ minValue: 0,
337
+ maxValue: 12
338
+ };
339
+ case 'hour':
340
+ if (options.hour12) {
341
+ let isPM = date.hour >= 12;
342
+ return {
343
+ value: date.hour,
344
+ minValue: isPM ? 12 : 0,
345
+ maxValue: isPM ? 23 : 11
346
+ };
347
+ }
348
+ return {
349
+ value: date.hour,
350
+ minValue: 0,
351
+ maxValue: 23
352
+ };
353
+ case 'minute':
354
+ return {
355
+ value: date.minute,
356
+ minValue: 0,
357
+ maxValue: 59
358
+ };
359
+ case 'second':
360
+ return {
361
+ value: date.second,
362
+ minValue: 0,
363
+ maxValue: 59
364
+ };
365
+ }
366
+ return {};
367
+ }
368
+ function $3c0fc76039f1c516$var$addSegment(value, part, amount, options) {
369
+ switch(part){
370
+ case 'era':
371
+ case 'year':
372
+ case 'month':
373
+ case 'day':
374
+ return value.cycle(part, amount, {
375
+ round: part === 'year'
376
+ });
377
+ }
378
+ if ('hour' in value) switch(part){
379
+ case 'dayPeriod':
380
+ {
381
+ let hours = value.hour;
382
+ let isPM = hours >= 12;
383
+ return value.set({
384
+ hour: isPM ? hours - 12 : hours + 12
385
+ });
386
+ }
387
+ case 'hour':
388
+ case 'minute':
389
+ case 'second':
390
+ return value.cycle(part, amount, {
391
+ round: part !== 'hour',
392
+ hourCycle: options.hour12 ? 12 : 24
393
+ });
394
+ }
395
+ }
396
+ function $3c0fc76039f1c516$var$setSegment(value, part, segmentValue, options) {
397
+ switch(part){
398
+ case 'day':
399
+ case 'month':
400
+ case 'year':
401
+ case 'era':
402
+ return value.set({
403
+ [part]: segmentValue
404
+ });
405
+ }
406
+ if ('hour' in value) switch(part){
407
+ case 'dayPeriod':
408
+ {
409
+ let hours = value.hour;
410
+ let wasPM = hours >= 12;
411
+ let isPM = segmentValue >= 12;
412
+ if (isPM === wasPM) return value;
413
+ return value.set({
414
+ hour: wasPM ? hours - 12 : hours + 12
415
+ });
416
+ }
417
+ case 'hour':
418
+ // In 12 hour time, ensure that AM/PM does not change
419
+ if (options.hour12) {
420
+ let hours = value.hour;
421
+ let wasPM = hours >= 12;
422
+ if (!wasPM && segmentValue === 12) segmentValue = 0;
423
+ if (wasPM && segmentValue < 12) segmentValue += 12;
424
+ }
425
+ // fallthrough
426
+ case 'minute':
427
+ case 'second':
428
+ return value.set({
429
+ [part]: segmentValue
430
+ });
431
+ }
432
+ }
433
+
434
+
435
+ export {$3c0fc76039f1c516$export$60e84778edff6d26 as useDateFieldState};
436
+ //# sourceMappingURL=useDateFieldState.module.js.map