@react-stately/datepicker 3.4.1 → 3.5.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/src/utils.ts CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  import {Calendar, now, Time, toCalendar, toCalendarDate, toCalendarDateTime} from '@internationalized/date';
14
14
  import {DatePickerProps, DateValue, Granularity, TimeValue} from '@react-types/datepicker';
15
- import {useRef} from 'react';
15
+ import {useState} from 'react';
16
16
 
17
17
  export function isInvalid(value: DateValue, minValue: DateValue, maxValue: DateValue) {
18
18
  return value != null && (
@@ -28,7 +28,8 @@ interface FormatterOptions {
28
28
  granularity?: DatePickerProps<any>['granularity'],
29
29
  maxGranularity?: 'year' | 'month' | DatePickerProps<any>['granularity'],
30
30
  hourCycle?: 12 | 24,
31
- showEra?: boolean
31
+ showEra?: boolean,
32
+ shouldForceLeadingZeros?: boolean
32
33
  }
33
34
 
34
35
  const DEFAULT_FIELD_OPTIONS: FieldOptions = {
@@ -40,11 +41,21 @@ const DEFAULT_FIELD_OPTIONS: FieldOptions = {
40
41
  second: '2-digit'
41
42
  };
42
43
 
44
+ const TWO_DIGIT_FIELD_OPTIONS: FieldOptions = {
45
+ year: 'numeric',
46
+ month: '2-digit',
47
+ day: '2-digit',
48
+ hour: '2-digit',
49
+ minute: '2-digit',
50
+ second: '2-digit'
51
+ };
52
+
43
53
  export function getFormatOptions(
44
54
  fieldOptions: FieldOptions,
45
55
  options: FormatterOptions
46
56
  ): Intl.DateTimeFormatOptions {
47
- fieldOptions = {...DEFAULT_FIELD_OPTIONS, ...fieldOptions};
57
+ let defaultFieldOptions = options.shouldForceLeadingZeros ? TWO_DIGIT_FIELD_OPTIONS : DEFAULT_FIELD_OPTIONS;
58
+ fieldOptions = {...defaultFieldOptions, ...fieldOptions};
48
59
  let granularity = options.granularity || 'minute';
49
60
  let keys = Object.keys(fieldOptions);
50
61
  let startIdx = keys.indexOf(options.maxGranularity ?? 'year');
@@ -130,19 +141,25 @@ export function createPlaceholderDate(placeholderValue: DateValue, granularity:
130
141
 
131
142
  export function useDefaultProps(v: DateValue, granularity: Granularity): [Granularity, string] {
132
143
  // Compute default granularity and time zone from the value. If the value becomes null, keep the last values.
133
- let lastValue = useRef(v);
134
- if (v) {
135
- lastValue.current = v;
136
- }
137
-
138
- v = lastValue.current;
139
144
  let defaultTimeZone = (v && 'timeZone' in v ? v.timeZone : undefined);
140
- granularity = granularity || (v && 'minute' in v ? 'minute' : 'day');
145
+ let defaultGranularity: Granularity = (v && 'minute' in v ? 'minute' : 'day');
141
146
 
142
147
  // props.granularity must actually exist in the value if one is provided.
143
- if (v && !(granularity in v)) {
148
+ if (v && granularity && !(granularity in v)) {
144
149
  throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
145
150
  }
146
151
 
147
- return [granularity, defaultTimeZone];
152
+ let [lastValue, setLastValue] = useState<[Granularity, string]>([defaultGranularity, defaultTimeZone]);
153
+
154
+ // If the granularity or time zone changed, update the last value.
155
+ if (v && (lastValue[0] !== defaultGranularity || lastValue[1] !== defaultTimeZone)) {
156
+ setLastValue([defaultGranularity, defaultTimeZone]);
157
+ }
158
+
159
+ if (!granularity) {
160
+ granularity = v ? defaultGranularity : lastValue[0];
161
+ }
162
+
163
+ let timeZone = v ? defaultTimeZone : lastValue[1];
164
+ return [granularity, timeZone];
148
165
  }