@react-stately/datepicker 3.10.2 → 3.11.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.
@@ -11,7 +11,7 @@
11
11
  */
12
12
 
13
13
  import {DateFieldState, useDateFieldState} from '.';
14
- import {DateValue, TimePickerProps, TimeValue} from '@react-types/datepicker';
14
+ import {DateValue, MappedTimeValue, TimePickerProps, TimeValue} from '@react-types/datepicker';
15
15
  import {getLocalTimeZone, GregorianCalendar, Time, toCalendarDateTime, today, toTime, toZoned} from '@internationalized/date';
16
16
  import {useCallback, useMemo} from 'react';
17
17
  import {useControlledState} from '@react-stately/utils';
@@ -40,9 +40,9 @@ export function useTimeFieldState<T extends TimeValue = TimeValue>(props: TimeFi
40
40
  validate
41
41
  } = props;
42
42
 
43
- let [value, setValue] = useControlledState<TimeValue>(
43
+ let [value, setValue] = useControlledState<TimeValue | null, MappedTimeValue<T> | null>(
44
44
  props.value,
45
- props.defaultValue,
45
+ props.defaultValue ?? null,
46
46
  props.onChange
47
47
  );
48
48
 
@@ -52,7 +52,7 @@ export function useTimeFieldState<T extends TimeValue = TimeValue>(props: TimeFi
52
52
  let placeholderDate = useMemo(() => {
53
53
  let valueTimeZone = v && 'timeZone' in v ? v.timeZone : undefined;
54
54
 
55
- return (valueTimeZone || defaultValueTimeZone) && placeholderValue ? toZoned(convertValue(placeholderValue), valueTimeZone || defaultValueTimeZone) : convertValue(placeholderValue);
55
+ return (valueTimeZone || defaultValueTimeZone) && placeholderValue ? toZoned(convertValue(placeholderValue)!, valueTimeZone || defaultValueTimeZone!) : convertValue(placeholderValue);
56
56
  }, [placeholderValue, v, defaultValueTimeZone]);
57
57
  let minDate = useMemo(() => convertValue(minValue, day), [minValue, day]);
58
58
  let maxDate = useMemo(() => convertValue(maxValue, day), [maxValue, day]);
@@ -72,7 +72,7 @@ export function useTimeFieldState<T extends TimeValue = TimeValue>(props: TimeFi
72
72
  onChange,
73
73
  granularity: granularity || 'minute',
74
74
  maxGranularity: 'hour',
75
- placeholderValue: placeholderDate,
75
+ placeholderValue: placeholderDate ?? undefined,
76
76
  // Calendar should not matter for time fields.
77
77
  createCalendar: () => new GregorianCalendar(),
78
78
  validate: useCallback(() => validate?.(value as any), [validate, value])
@@ -84,7 +84,7 @@ export function useTimeFieldState<T extends TimeValue = TimeValue>(props: TimeFi
84
84
  };
85
85
  }
86
86
 
87
- function convertValue(value: TimeValue, date: DateValue = today(getLocalTimeZone())) {
87
+ function convertValue(value: TimeValue | null | undefined, date: DateValue = today(getLocalTimeZone())) {
88
88
  if (!value) {
89
89
  return null;
90
90
  }
package/src/utils.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {Calendar, DateFormatter, now, Time, toCalendar, toCalendarDate, toCalendarDateTime} from '@internationalized/date';
13
+ import {Calendar, DateFormatter, getLocalTimeZone, now, Time, toCalendar, toCalendarDate, toCalendarDateTime} from '@internationalized/date';
14
14
  import {DatePickerProps, DateValue, Granularity, TimeValue} from '@react-types/datepicker';
15
15
  // @ts-ignore
16
16
  import i18nMessages from '../intl/*.json';
@@ -29,17 +29,17 @@ function getLocale() {
29
29
  }
30
30
 
31
31
  export function getValidationResult(
32
- value: DateValue,
33
- minValue: DateValue,
34
- maxValue: DateValue,
35
- isDateUnavailable: (v: DateValue) => boolean,
32
+ value: DateValue | null,
33
+ minValue: DateValue | null | undefined,
34
+ maxValue: DateValue | null | undefined,
35
+ isDateUnavailable: ((v: DateValue) => boolean) | undefined,
36
36
  options: FormatterOptions
37
37
  ): ValidationResult {
38
38
  let rangeOverflow = value != null && maxValue != null && value.compare(maxValue) > 0;
39
39
  let rangeUnderflow = value != null && minValue != null && value.compare(minValue) < 0;
40
40
  let isUnavailable = (value != null && isDateUnavailable?.(value)) || false;
41
41
  let isInvalid = rangeOverflow || rangeUnderflow || isUnavailable;
42
- let errors = [];
42
+ let errors: string[] = [];
43
43
 
44
44
  if (isInvalid) {
45
45
  let locale = getLocale();
@@ -48,11 +48,11 @@ export function getValidationResult(
48
48
  let dateFormatter = new DateFormatter(locale, getFormatOptions({}, options));
49
49
  let timeZone = dateFormatter.resolvedOptions().timeZone;
50
50
 
51
- if (rangeUnderflow) {
51
+ if (rangeUnderflow && minValue != null) {
52
52
  errors.push(formatter.format('rangeUnderflow', {minValue: dateFormatter.format(minValue.toDate(timeZone))}));
53
53
  }
54
54
 
55
- if (rangeOverflow) {
55
+ if (rangeOverflow && maxValue != null) {
56
56
  errors.push(formatter.format('rangeOverflow', {maxValue: dateFormatter.format(maxValue.toDate(timeZone))}));
57
57
  }
58
58
 
@@ -81,14 +81,14 @@ export function getValidationResult(
81
81
  }
82
82
 
83
83
  export function getRangeValidationResult(
84
- value: RangeValue<DateValue>,
85
- minValue: DateValue,
86
- maxValue: DateValue,
87
- isDateUnavailable: (v: DateValue) => boolean,
84
+ value: RangeValue<DateValue | null> | null,
85
+ minValue: DateValue | null | undefined,
86
+ maxValue: DateValue | null | undefined,
87
+ isDateUnavailable: ((v: DateValue) => boolean) | undefined,
88
88
  options: FormatterOptions
89
89
  ) {
90
90
  let startValidation = getValidationResult(
91
- value?.start,
91
+ value?.start ?? null,
92
92
  minValue,
93
93
  maxValue,
94
94
  isDateUnavailable,
@@ -96,7 +96,7 @@ export function getRangeValidationResult(
96
96
  );
97
97
 
98
98
  let endValidation = getValidationResult(
99
- value?.end,
99
+ value?.end ?? null,
100
100
  minValue,
101
101
  maxValue,
102
102
  isDateUnavailable,
@@ -104,7 +104,7 @@ export function getRangeValidationResult(
104
104
  );
105
105
 
106
106
  let result = mergeValidation(startValidation, endValidation);
107
- if (value.end != null && value.start != null && value.end.compare(value.start) < 0) {
107
+ if (value?.end != null && value.start != null && value.end.compare(value.start) < 0) {
108
108
  let strings = LocalizedStringDictionary.getGlobalDictionaryForPackage('@react-stately/datepicker') || dictionary;
109
109
  result = mergeValidation(result, {
110
110
  isInvalid: true,
@@ -195,7 +195,7 @@ export function getFormatOptions(
195
195
  return opts;
196
196
  }
197
197
 
198
- export function getPlaceholderTime(placeholderValue: DateValue): TimeValue {
198
+ export function getPlaceholderTime(placeholderValue: DateValue | null | undefined): TimeValue {
199
199
  if (placeholderValue && 'hour' in placeholderValue) {
200
200
  return placeholderValue;
201
201
  }
@@ -203,7 +203,7 @@ export function getPlaceholderTime(placeholderValue: DateValue): TimeValue {
203
203
  return new Time();
204
204
  }
205
205
 
206
- export function convertValue(value: DateValue, calendar: Calendar): DateValue {
206
+ export function convertValue(value: DateValue | null | undefined, calendar: Calendar): DateValue | null | undefined {
207
207
  if (value === null) {
208
208
  return null;
209
209
  }
@@ -216,12 +216,12 @@ export function convertValue(value: DateValue, calendar: Calendar): DateValue {
216
216
  }
217
217
 
218
218
 
219
- export function createPlaceholderDate(placeholderValue: DateValue, granularity: string, calendar: Calendar, timeZone: string) {
219
+ export function createPlaceholderDate(placeholderValue: DateValue | null | undefined, granularity: string, calendar: Calendar, timeZone: string | undefined): DateValue {
220
220
  if (placeholderValue) {
221
- return convertValue(placeholderValue, calendar);
221
+ return convertValue(placeholderValue, calendar)!;
222
222
  }
223
223
 
224
- let date = toCalendar(now(timeZone).set({
224
+ let date = toCalendar(now(timeZone ?? getLocalTimeZone()).set({
225
225
  hour: 0,
226
226
  minute: 0,
227
227
  second: 0,
@@ -239,7 +239,7 @@ export function createPlaceholderDate(placeholderValue: DateValue, granularity:
239
239
  return date;
240
240
  }
241
241
 
242
- export function useDefaultProps(v: DateValue, granularity: Granularity): [Granularity, string] {
242
+ export function useDefaultProps(v: DateValue | null, granularity: Granularity | undefined): [Granularity, string | undefined] {
243
243
  // Compute default granularity and time zone from the value. If the value becomes null, keep the last values.
244
244
  let defaultTimeZone = (v && 'timeZone' in v ? v.timeZone : undefined);
245
245
  let defaultGranularity: Granularity = (v && 'minute' in v ? 'minute' : 'day');
@@ -249,7 +249,7 @@ export function useDefaultProps(v: DateValue, granularity: Granularity): [Granul
249
249
  throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
250
250
  }
251
251
 
252
- let [lastValue, setLastValue] = useState<[Granularity, string]>([defaultGranularity, defaultTimeZone]);
252
+ let [lastValue, setLastValue] = useState<[Granularity, string | undefined]>([defaultGranularity, defaultTimeZone]);
253
253
 
254
254
  // If the granularity or time zone changed, update the last value.
255
255
  if (v && (lastValue[0] !== defaultGranularity || lastValue[1] !== defaultTimeZone)) {