@react-stately/datepicker 3.15.3 → 3.16.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.
@@ -1,9 +1,11 @@
1
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
2
  import {getPlaceholder as $3e3ed55ab2966714$export$d3f5c5e0a5023fa0} from "./placeholders.mjs";
3
- import {DateFormatter as $g03ag$DateFormatter, isEqualCalendar as $g03ag$isEqualCalendar, toCalendar as $g03ag$toCalendar, GregorianCalendar as $g03ag$GregorianCalendar, getMinimumMonthInYear as $g03ag$getMinimumMonthInYear, getMinimumDayInMonth as $g03ag$getMinimumDayInMonth} from "@internationalized/date";
3
+ import {IncompleteDate as $f863c03ccd9aead0$export$ae165b50d181e1ef} from "./IncompleteDate.mjs";
4
+ import {DateFormatter as $g03ag$DateFormatter, isEqualCalendar as $g03ag$isEqualCalendar, toCalendar as $g03ag$toCalendar, GregorianCalendar as $g03ag$GregorianCalendar} from "@internationalized/date";
4
5
  import {useFormValidationState as $g03ag$useFormValidationState} from "@react-stately/form";
6
+ import {NumberFormatter as $g03ag$NumberFormatter} from "@internationalized/number";
5
7
  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";
8
+ import {useMemo as $g03ag$useMemo, useState as $g03ag$useState} from "react";
7
9
 
8
10
  /*
9
11
  * Copyright 2020 Adobe. All rights reserved.
@@ -21,6 +23,8 @@ import {useMemo as $g03ag$useMemo, useState as $g03ag$useState, useRef as $g03ag
21
23
 
22
24
 
23
25
 
26
+
27
+
24
28
  const $3c0fc76039f1c516$var$EDITABLE_SEGMENTS = {
25
29
  year: true,
26
30
  month: true,
@@ -54,12 +58,23 @@ function $3c0fc76039f1c516$export$60e84778edff6d26(props) {
54
58
  let timeZone = defaultTimeZone || 'UTC';
55
59
  // props.granularity must actually exist in the value if one is provided.
56
60
  if (v && !(granularity in v)) throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
57
- let defaultFormatter = (0, $g03ag$useMemo)(()=>new (0, $g03ag$DateFormatter)(locale), [
58
- locale
59
- ]);
60
- let calendar = (0, $g03ag$useMemo)(()=>createCalendar(defaultFormatter.resolvedOptions().calendar), [
61
- createCalendar,
62
- defaultFormatter
61
+ // Resolve default hour cycle and calendar system.
62
+ let [calendar, hourCycle] = (0, $g03ag$useMemo)(()=>{
63
+ let formatter = new (0, $g03ag$DateFormatter)(locale, {
64
+ dateStyle: 'short',
65
+ timeStyle: 'short',
66
+ hour12: props.hourCycle != null ? props.hourCycle === 12 : undefined
67
+ });
68
+ let opts = formatter.resolvedOptions();
69
+ let calendar = createCalendar(opts.calendar);
70
+ return [
71
+ calendar,
72
+ opts.hourCycle
73
+ ];
74
+ }, [
75
+ locale,
76
+ props.hourCycle,
77
+ createCalendar
63
78
  ]);
64
79
  var _props_defaultValue;
65
80
  let [value, setDate] = (0, $g03ag$useControlledState)(props.value, (_props_defaultValue = props.defaultValue) !== null && _props_defaultValue !== void 0 ? _props_defaultValue : null, props.onChange);
@@ -71,13 +86,8 @@ function $3c0fc76039f1c516$export$60e84778edff6d26(props) {
71
86
  value,
72
87
  calendar
73
88
  ]);
74
- // We keep track of the placeholder date separately in state so that onChange is not called
75
- // until all segments are set. If the value === null (not undefined), then assume the component
76
- // is controlled, so use the placeholder as the value until all segments are entered so it doesn't
77
- // change from uncontrolled to controlled and emit a warning.
78
- let [placeholderDate, setPlaceholderDate] = (0, $g03ag$useState)(()=>(0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone));
79
- let val = calendarValue || placeholderDate;
80
- let showEra = calendar.identifier === 'gregory' && val.era === 'BC';
89
+ let [displayValue, setDisplayValue] = (0, $g03ag$useState)(()=>new (0, $f863c03ccd9aead0$export$ae165b50d181e1ef)(calendar, hourCycle, calendarValue));
90
+ let showEra = calendar.identifier === 'gregory' && displayValue.era === 'BC';
81
91
  let formatOpts = (0, $g03ag$useMemo)(()=>{
82
92
  var _props_maxGranularity;
83
93
  return {
@@ -108,76 +118,85 @@ function $3c0fc76039f1c516$export$60e84778edff6d26(props) {
108
118
  let resolvedOptions = (0, $g03ag$useMemo)(()=>dateFormatter.resolvedOptions(), [
109
119
  dateFormatter
110
120
  ]);
111
- // Determine how many editable segments there are for validation purposes.
112
- // The result is cached for performance.
113
- let allSegments = (0, $g03ag$useMemo)(()=>dateFormatter.formatToParts(new Date()).filter((seg)=>$3c0fc76039f1c516$var$EDITABLE_SEGMENTS[seg.type]).reduce((p, seg)=>(p[$3c0fc76039f1c516$var$TYPE_MAPPING[seg.type] || seg.type] = true, p), {}), [
114
- dateFormatter
121
+ let placeholder = (0, $g03ag$useMemo)(()=>(0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone), [
122
+ props.placeholderValue,
123
+ granularity,
124
+ calendar,
125
+ defaultTimeZone
115
126
  ]);
116
- let [validSegments, setValidSegments] = (0, $g03ag$useState)(()=>props.value || props.defaultValue ? {
117
- ...allSegments
118
- } : {});
119
- let clearedSegment = (0, $g03ag$useRef)(null);
120
- // Reset placeholder when calendar changes
121
- let lastCalendar = (0, $g03ag$useRef)(calendar);
122
- (0, $g03ag$useEffect)(()=>{
123
- if (!(0, $g03ag$isEqualCalendar)(calendar, lastCalendar.current)) {
124
- lastCalendar.current = calendar;
125
- setPlaceholderDate((placeholder)=>Object.keys(validSegments).length > 0 ? (0, $g03ag$toCalendar)(placeholder, calendar) : (0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone));
126
- }
127
+ let displaySegments = (0, $g03ag$useMemo)(()=>{
128
+ let is12HourClock = hourCycle === 'h11' || hourCycle === 'h12';
129
+ let segments = [
130
+ 'era',
131
+ 'year',
132
+ 'month',
133
+ 'day',
134
+ 'hour',
135
+ ...is12HourClock ? [
136
+ 'dayPeriod'
137
+ ] : [],
138
+ 'minute',
139
+ 'second'
140
+ ];
141
+ let minIndex = segments.indexOf(props.maxGranularity || 'era');
142
+ let maxIndex = segments.indexOf(granularity === 'hour' && is12HourClock ? 'dayPeriod' : granularity);
143
+ return segments.slice(minIndex, maxIndex + 1);
127
144
  }, [
128
- calendar,
145
+ props.maxGranularity,
129
146
  granularity,
130
- validSegments,
131
- defaultTimeZone,
132
- props.placeholderValue
147
+ hourCycle
133
148
  ]);
134
- // If there is a value prop, and some segments were previously placeholders, mark them all as valid.
135
- if (value && Object.keys(validSegments).length < Object.keys(allSegments).length) {
136
- validSegments = {
137
- ...allSegments
138
- };
139
- setValidSegments(validSegments);
149
+ let [lastValue, setLastValue] = (0, $g03ag$useState)(calendarValue);
150
+ let [lastCalendar, setLastCalendar] = (0, $g03ag$useState)(calendar);
151
+ let [lastHourCycle, setLastHourCycle] = (0, $g03ag$useState)(hourCycle);
152
+ if (calendarValue !== lastValue || hourCycle !== lastHourCycle || !(0, $g03ag$isEqualCalendar)(calendar, lastCalendar)) {
153
+ displayValue = new (0, $f863c03ccd9aead0$export$ae165b50d181e1ef)(calendar, hourCycle, calendarValue);
154
+ setLastValue(calendarValue);
155
+ setLastCalendar(calendar);
156
+ setLastHourCycle(hourCycle);
157
+ setDisplayValue(displayValue);
140
158
  }
141
- // If the value is set to null and all segments are valid, reset the placeholder.
142
- if (value == null && Object.keys(validSegments).length === Object.keys(allSegments).length) {
143
- validSegments = {};
144
- setValidSegments(validSegments);
145
- setPlaceholderDate((0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone));
146
- }
147
- // If all segments are valid, use the date from state, otherwise use the placeholder date.
148
- let displayValue = calendarValue && Object.keys(validSegments).length >= Object.keys(allSegments).length ? calendarValue : placeholderDate;
149
159
  let setValue = (newValue)=>{
150
160
  if (props.isDisabled || props.isReadOnly) return;
151
- let validKeys = Object.keys(validSegments);
152
- let allKeys = Object.keys(allSegments);
153
- // if all the segments are completed or a timefield with everything but am/pm set the time, also ignore when am/pm cleared
154
- if (newValue == null) {
161
+ if (newValue == null || newValue instanceof (0, $f863c03ccd9aead0$export$ae165b50d181e1ef) && newValue.isCleared(displaySegments)) {
162
+ setDisplayValue(new (0, $f863c03ccd9aead0$export$ae165b50d181e1ef)(calendar, hourCycle, calendarValue));
155
163
  setDate(null);
156
- setPlaceholderDate((0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone));
157
- setValidSegments({});
158
- } else if (validKeys.length === 0 && clearedSegment.current == null || validKeys.length >= allKeys.length || validKeys.length === allKeys.length - 1 && allSegments.dayPeriod && !validSegments.dayPeriod && clearedSegment.current !== 'dayPeriod') {
159
- // If the field was empty (no valid segments) or all segments are completed, commit the new value.
160
- // When committing from an empty state, mark every segment as valid so value is committed.
161
- if (validKeys.length === 0) {
162
- validSegments = {
163
- ...allSegments
164
- };
165
- setValidSegments(validSegments);
166
- }
164
+ } else if (!(newValue instanceof (0, $f863c03ccd9aead0$export$ae165b50d181e1ef))) {
167
165
  // The display calendar should not have any effect on the emitted value.
168
166
  // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
169
167
  newValue = (0, $g03ag$toCalendar)(newValue, (v === null || v === void 0 ? void 0 : v.calendar) || new (0, $g03ag$GregorianCalendar)());
168
+ setDisplayValue(new (0, $f863c03ccd9aead0$export$ae165b50d181e1ef)(calendar, hourCycle, calendarValue));
170
169
  setDate(newValue);
171
- } else setPlaceholderDate(newValue);
172
- clearedSegment.current = null;
170
+ } else {
171
+ // If the new value is complete and valid, trigger onChange eagerly.
172
+ // If it represents an incomplete or invalid value (e.g. February 30th),
173
+ // wait until the field is blurred to trigger onChange.
174
+ if (newValue.isComplete(displaySegments)) {
175
+ let dateValue = newValue.toValue(calendarValue !== null && calendarValue !== void 0 ? calendarValue : placeholder);
176
+ if (newValue.validate(dateValue, displaySegments)) {
177
+ let newDateValue = (0, $g03ag$toCalendar)(dateValue, (v === null || v === void 0 ? void 0 : v.calendar) || new (0, $g03ag$GregorianCalendar)());
178
+ if (!value || newDateValue.compare(value) !== 0) {
179
+ setDisplayValue(new (0, $f863c03ccd9aead0$export$ae165b50d181e1ef)(calendar, hourCycle, calendarValue)); // reset in case prop isn't updated
180
+ setDate(newDateValue);
181
+ return;
182
+ }
183
+ }
184
+ }
185
+ // Incomplete/invalid value. Set temporary display override.
186
+ setDisplayValue(newValue);
187
+ }
173
188
  };
174
- let dateValue = (0, $g03ag$useMemo)(()=>displayValue.toDate(timeZone), [
189
+ let dateValue = (0, $g03ag$useMemo)(()=>{
190
+ let v = displayValue.toValue(calendarValue !== null && calendarValue !== void 0 ? calendarValue : placeholder);
191
+ return v.toDate(timeZone);
192
+ }, [
175
193
  displayValue,
176
- timeZone
194
+ timeZone,
195
+ calendarValue,
196
+ placeholder
177
197
  ]);
178
- let segments = (0, $g03ag$useMemo)(()=>$3c0fc76039f1c516$var$processSegments(dateValue, validSegments, dateFormatter, resolvedOptions, displayValue, calendar, locale, granularity), [
198
+ let segments = (0, $g03ag$useMemo)(()=>$3c0fc76039f1c516$var$processSegments(dateValue, displayValue, dateFormatter, resolvedOptions, calendar, locale, granularity), [
179
199
  dateValue,
180
- validSegments,
181
200
  dateFormatter,
182
201
  resolvedOptions,
183
202
  displayValue,
@@ -185,33 +204,8 @@ function $3c0fc76039f1c516$export$60e84778edff6d26(props) {
185
204
  locale,
186
205
  granularity
187
206
  ]);
188
- // When the era field appears, mark it valid if the year field is already valid.
189
- // If the era field disappears, remove it from the valid segments.
190
- if (allSegments.era && validSegments.year && !validSegments.era) {
191
- validSegments.era = true;
192
- setValidSegments({
193
- ...validSegments
194
- });
195
- } else if (!allSegments.era && validSegments.era) {
196
- delete validSegments.era;
197
- setValidSegments({
198
- ...validSegments
199
- });
200
- }
201
- let markValid = (part)=>{
202
- validSegments[part] = true;
203
- if (part === 'year' && allSegments.era) validSegments.era = true;
204
- setValidSegments({
205
- ...validSegments
206
- });
207
- };
208
207
  let adjustSegment = (type, amount)=>{
209
- if (!validSegments[type]) {
210
- markValid(type);
211
- let validKeys = Object.keys(validSegments);
212
- let allKeys = Object.keys(allSegments);
213
- if (validKeys.length >= allKeys.length || validKeys.length === allKeys.length - 1 && allSegments.dayPeriod && !validSegments.dayPeriod) setValue(displayValue);
214
- } else setValue($3c0fc76039f1c516$var$addSegment(displayValue, type, amount, resolvedOptions));
208
+ setValue(displayValue.cycle(type, amount, placeholder, displaySegments));
215
209
  };
216
210
  let builtinValidation = (0, $g03ag$useMemo)(()=>(0, $35a22f14a1f04b11$export$f18627323ab57ac0)(value, minValue, maxValue, isDateUnavailable, formatOpts), [
217
211
  value,
@@ -256,48 +250,30 @@ function $3c0fc76039f1c516$export$60e84778edff6d26(props) {
256
250
  decrementPage (part) {
257
251
  adjustSegment(part, -($3c0fc76039f1c516$var$PAGE_STEP[part] || 1));
258
252
  },
253
+ incrementToMax (part) {
254
+ let maxValue = part === 'hour' && hourCycle === 'h12' ? 11 : displayValue.getSegmentLimits(part).maxValue;
255
+ setValue(displayValue.set(part, maxValue, placeholder));
256
+ },
257
+ decrementToMin (part) {
258
+ let minValue = part === 'hour' && hourCycle === 'h12' ? 12 : displayValue.getSegmentLimits(part).minValue;
259
+ setValue(displayValue.set(part, minValue, placeholder));
260
+ },
259
261
  setSegment (part, v) {
260
- markValid(part);
261
- setValue($3c0fc76039f1c516$var$setSegment(displayValue, part, v, resolvedOptions));
262
+ setValue(displayValue.set(part, v, placeholder));
262
263
  },
263
264
  confirmPlaceholder () {
264
265
  if (props.isDisabled || props.isReadOnly) return;
265
- // Confirm the placeholder if only the day period is not filled in.
266
- let validKeys = Object.keys(validSegments);
267
- let allKeys = Object.keys(allSegments);
268
- if (validKeys.length === allKeys.length - 1 && allSegments.dayPeriod && !validSegments.dayPeriod) {
269
- validSegments = {
270
- ...allSegments
271
- };
272
- setValidSegments(validSegments);
273
- setValue(displayValue.copy());
266
+ // If the display value is complete but invalid, we need to constrain it and emit onChange on blur.
267
+ if (displayValue.isComplete(displaySegments)) {
268
+ let dateValue = displayValue.toValue(calendarValue !== null && calendarValue !== void 0 ? calendarValue : placeholder);
269
+ let newDateValue = (0, $g03ag$toCalendar)(dateValue, (v === null || v === void 0 ? void 0 : v.calendar) || new (0, $g03ag$GregorianCalendar)());
270
+ if (!value || newDateValue.compare(value) !== 0) setDate(newDateValue);
271
+ setDisplayValue(new (0, $f863c03ccd9aead0$export$ae165b50d181e1ef)(calendar, hourCycle, calendarValue));
274
272
  }
275
273
  },
276
274
  clearSegment (part) {
277
- delete validSegments[part];
278
- clearedSegment.current = part;
279
- setValidSegments({
280
- ...validSegments
281
- });
282
- let placeholder = (0, $35a22f14a1f04b11$export$66aa2b09de4b1ea5)(props.placeholderValue, granularity, calendar, defaultTimeZone);
283
275
  let value = displayValue;
284
- // Reset day period to default without changing the hour.
285
- if (part === 'dayPeriod' && 'hour' in displayValue && 'hour' in placeholder) {
286
- let isPM = displayValue.hour >= 12;
287
- let shouldBePM = placeholder.hour >= 12;
288
- if (isPM && !shouldBePM) value = displayValue.set({
289
- hour: displayValue.hour - 12
290
- });
291
- else if (!isPM && shouldBePM) value = displayValue.set({
292
- hour: displayValue.hour + 12
293
- });
294
- } else if (part === 'hour' && 'hour' in displayValue && displayValue.hour >= 12 && validSegments.dayPeriod) value = displayValue.set({
295
- hour: placeholder['hour'] + 12
296
- });
297
- else if (part in displayValue) value = displayValue.set({
298
- [part]: placeholder[part]
299
- });
300
- setDate(null);
276
+ if (part !== 'timeZoneName' && part !== 'literal') value = displayValue.clear(part);
301
277
  setValue(value);
302
278
  },
303
279
  formatValue (fieldOptions) {
@@ -316,24 +292,40 @@ function $3c0fc76039f1c516$export$60e84778edff6d26(props) {
316
292
  }
317
293
  };
318
294
  }
319
- function $3c0fc76039f1c516$var$processSegments(dateValue, validSegments, dateFormatter, resolvedOptions, displayValue, calendar, locale, granularity) {
295
+ function $3c0fc76039f1c516$var$processSegments(dateValue, displayValue, dateFormatter, resolvedOptions, calendar, locale, granularity) {
320
296
  let timeValue = [
321
297
  'hour',
322
298
  'minute',
323
299
  'second'
324
300
  ];
325
301
  let segments = dateFormatter.formatToParts(dateValue);
302
+ // In order to allow formatting temporarily invalid dates during editing (e.g. February 30th),
303
+ // use a NumberFormatter to manually format segments directly from raw numbers.
304
+ // When the user blurs the date field, the invalid segments will be constrained.
305
+ let numberFormatter = new (0, $g03ag$NumberFormatter)(locale, {
306
+ useGrouping: false
307
+ });
308
+ let twoDigitFormatter = new (0, $g03ag$NumberFormatter)(locale, {
309
+ useGrouping: false,
310
+ minimumIntegerDigits: 2
311
+ });
312
+ for (let segment of segments)if (segment.type === 'year' || segment.type === 'month' || segment.type === 'day' || segment.type === 'hour') {
313
+ var _displayValue_segment_type;
314
+ let value = (_displayValue_segment_type = displayValue[segment.type]) !== null && _displayValue_segment_type !== void 0 ? _displayValue_segment_type : 0;
315
+ if (resolvedOptions[segment.type] === '2-digit') segment.value = twoDigitFormatter.format(value);
316
+ else segment.value = numberFormatter.format(value);
317
+ }
326
318
  let processedSegments = [];
327
319
  for (let segment of segments){
328
320
  let type = $3c0fc76039f1c516$var$TYPE_MAPPING[segment.type] || segment.type;
329
321
  let isEditable = $3c0fc76039f1c516$var$EDITABLE_SEGMENTS[type];
330
322
  if (type === 'era' && calendar.getEras().length === 1) isEditable = false;
331
- let isPlaceholder = $3c0fc76039f1c516$var$EDITABLE_SEGMENTS[type] && !validSegments[type];
323
+ let isPlaceholder = $3c0fc76039f1c516$var$EDITABLE_SEGMENTS[type] && displayValue[segment.type] == null;
332
324
  let placeholder = $3c0fc76039f1c516$var$EDITABLE_SEGMENTS[type] ? (0, $3e3ed55ab2966714$export$d3f5c5e0a5023fa0)(type, segment.value, locale) : null;
333
325
  let dateSegment = {
334
326
  type: type,
335
327
  text: isPlaceholder ? placeholder : segment.value,
336
- ...$3c0fc76039f1c516$var$getSegmentLimits(displayValue, type, resolvedOptions),
328
+ ...displayValue.getSegmentLimits(type),
337
329
  isPlaceholder: isPlaceholder,
338
330
  placeholder: placeholder,
339
331
  isEditable: isEditable
@@ -346,7 +338,6 @@ function $3c0fc76039f1c516$var$processSegments(dateValue, validSegments, dateFor
346
338
  processedSegments.push({
347
339
  type: 'literal',
348
340
  text: '\u2066',
349
- ...$3c0fc76039f1c516$var$getSegmentLimits(displayValue, 'literal', resolvedOptions),
350
341
  isPlaceholder: false,
351
342
  placeholder: '',
352
343
  isEditable: false
@@ -356,7 +347,6 @@ function $3c0fc76039f1c516$var$processSegments(dateValue, validSegments, dateFor
356
347
  if (type === granularity) processedSegments.push({
357
348
  type: 'literal',
358
349
  text: '\u2069',
359
- ...$3c0fc76039f1c516$var$getSegmentLimits(displayValue, 'literal', resolvedOptions),
360
350
  isPlaceholder: false,
361
351
  placeholder: '',
362
352
  isEditable: false
@@ -367,7 +357,6 @@ function $3c0fc76039f1c516$var$processSegments(dateValue, validSegments, dateFor
367
357
  processedSegments.push({
368
358
  type: 'literal',
369
359
  text: '\u2069',
370
- ...$3c0fc76039f1c516$var$getSegmentLimits(displayValue, 'literal', resolvedOptions),
371
360
  isPlaceholder: false,
372
361
  placeholder: '',
373
362
  isEditable: false
@@ -377,139 +366,6 @@ function $3c0fc76039f1c516$var$processSegments(dateValue, validSegments, dateFor
377
366
  }
378
367
  return processedSegments;
379
368
  }
380
- function $3c0fc76039f1c516$var$getSegmentLimits(date, type, options) {
381
- switch(type){
382
- case 'era':
383
- {
384
- let eras = date.calendar.getEras();
385
- return {
386
- value: eras.indexOf(date.era),
387
- minValue: 0,
388
- maxValue: eras.length - 1
389
- };
390
- }
391
- case 'year':
392
- return {
393
- value: date.year,
394
- minValue: 1,
395
- maxValue: date.calendar.getYearsInEra(date)
396
- };
397
- case 'month':
398
- return {
399
- value: date.month,
400
- minValue: (0, $g03ag$getMinimumMonthInYear)(date),
401
- maxValue: date.calendar.getMonthsInYear(date)
402
- };
403
- case 'day':
404
- return {
405
- value: date.day,
406
- minValue: (0, $g03ag$getMinimumDayInMonth)(date),
407
- maxValue: date.calendar.getDaysInMonth(date)
408
- };
409
- }
410
- if ('hour' in date) switch(type){
411
- case 'dayPeriod':
412
- return {
413
- value: date.hour >= 12 ? 12 : 0,
414
- minValue: 0,
415
- maxValue: 12
416
- };
417
- case 'hour':
418
- if (options.hour12) {
419
- let isPM = date.hour >= 12;
420
- return {
421
- value: date.hour,
422
- minValue: isPM ? 12 : 0,
423
- maxValue: isPM ? 23 : 11
424
- };
425
- }
426
- return {
427
- value: date.hour,
428
- minValue: 0,
429
- maxValue: 23
430
- };
431
- case 'minute':
432
- return {
433
- value: date.minute,
434
- minValue: 0,
435
- maxValue: 59
436
- };
437
- case 'second':
438
- return {
439
- value: date.second,
440
- minValue: 0,
441
- maxValue: 59
442
- };
443
- }
444
- return {};
445
- }
446
- function $3c0fc76039f1c516$var$addSegment(value, part, amount, options) {
447
- switch(part){
448
- case 'era':
449
- case 'year':
450
- case 'month':
451
- case 'day':
452
- return value.cycle(part, amount, {
453
- round: part === 'year'
454
- });
455
- }
456
- if ('hour' in value) switch(part){
457
- case 'dayPeriod':
458
- {
459
- let hours = value.hour;
460
- let isPM = hours >= 12;
461
- return value.set({
462
- hour: isPM ? hours - 12 : hours + 12
463
- });
464
- }
465
- case 'hour':
466
- case 'minute':
467
- case 'second':
468
- return value.cycle(part, amount, {
469
- round: part !== 'hour',
470
- hourCycle: options.hour12 ? 12 : 24
471
- });
472
- }
473
- throw new Error('Unknown segment: ' + part);
474
- }
475
- function $3c0fc76039f1c516$var$setSegment(value, part, segmentValue, options) {
476
- switch(part){
477
- case 'day':
478
- case 'month':
479
- case 'year':
480
- case 'era':
481
- return value.set({
482
- [part]: segmentValue
483
- });
484
- }
485
- if ('hour' in value && typeof segmentValue === 'number') switch(part){
486
- case 'dayPeriod':
487
- {
488
- let hours = value.hour;
489
- let wasPM = hours >= 12;
490
- let isPM = segmentValue >= 12;
491
- if (isPM === wasPM) return value;
492
- return value.set({
493
- hour: wasPM ? hours - 12 : hours + 12
494
- });
495
- }
496
- case 'hour':
497
- // In 12 hour time, ensure that AM/PM does not change
498
- if (options.hour12) {
499
- let hours = value.hour;
500
- let wasPM = hours >= 12;
501
- if (!wasPM && segmentValue === 12) segmentValue = 0;
502
- if (wasPM && segmentValue < 12) segmentValue += 12;
503
- }
504
- // fallthrough
505
- case 'minute':
506
- case 'second':
507
- return value.set({
508
- [part]: segmentValue
509
- });
510
- }
511
- throw new Error('Unknown segment: ' + part);
512
- }
513
369
 
514
370
 
515
371
  export {$3c0fc76039f1c516$export$60e84778edff6d26 as useDateFieldState};