@react-stately/datepicker 3.0.0-alpha.1 → 3.0.0-alpha.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/datepicker",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-alpha.4",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -18,10 +18,10 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@babel/runtime": "^7.6.2",
21
- "@internationalized/date": "3.0.0-alpha.1",
22
- "@react-stately/utils": "^3.3.0",
23
- "@react-types/datepicker": "3.0.0-alpha.1",
24
- "@react-types/shared": "^3.10.0",
21
+ "@internationalized/date": "3.0.0-alpha.4",
22
+ "@react-stately/utils": "^3.4.1",
23
+ "@react-types/datepicker": "3.0.0-alpha.4",
24
+ "@react-types/shared": "^3.11.1",
25
25
  "date-fns": "^1.30.1"
26
26
  },
27
27
  "peerDependencies": {
@@ -30,5 +30,5 @@
30
30
  "publishConfig": {
31
31
  "access": "public"
32
32
  },
33
- "gitHead": "16fc29ac722793ffaea083f13760d82ab341cc8d"
33
+ "gitHead": "e7708349a637642a30d33a11ca4a75ad5829eaa3"
34
34
  }
package/src/index.ts CHANGED
@@ -11,6 +11,6 @@
11
11
  */
12
12
 
13
13
  export * from './useDatePickerState';
14
- export * from './useDatePickerFieldState';
14
+ export * from './useDateFieldState';
15
15
  export * from './useDateRangePickerState';
16
16
  export * from './useTimeFieldState';
@@ -10,39 +10,79 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {Calendar, CalendarDateTime, DateFormatter, getMinimumDayInMonth, getMinimumMonthInYear, GregorianCalendar, toCalendar} from '@internationalized/date';
13
+ import {Calendar, DateFormatter, getMinimumDayInMonth, getMinimumMonthInYear, GregorianCalendar, toCalendar} from '@internationalized/date';
14
14
  import {convertValue, createPlaceholderDate, FieldOptions, getFormatOptions, isInvalid, useDefaultProps} from './utils';
15
15
  import {DatePickerProps, DateValue, Granularity} from '@react-types/datepicker';
16
16
  import {useControlledState} from '@react-stately/utils';
17
17
  import {useEffect, useMemo, useRef, useState} from 'react';
18
18
  import {ValidationState} from '@react-types/shared';
19
19
 
20
+ export type SegmentType = 'era' | 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second' | 'dayPeriod' | 'literal' | 'timeZoneName';
20
21
  export interface DateSegment {
21
- type: Intl.DateTimeFormatPartTypes,
22
+ /** The type of segment. */
23
+ type: SegmentType,
24
+ /** The formatted text for the segment. */
22
25
  text: string,
26
+ /** The numeric value for the segment, if applicable. */
23
27
  value?: number,
28
+ /** The minimum numeric value for the segment, if applicable. */
24
29
  minValue?: number,
30
+ /** The maximum numeric value for the segment, if applicable. */
25
31
  maxValue?: number,
32
+ /** Whether the value is a placeholder. */
26
33
  isPlaceholder: boolean,
34
+ /** Whether the segment is editable. */
27
35
  isEditable: boolean
28
36
  }
29
37
 
30
- export interface DatePickerFieldState {
38
+ export interface DateFieldState {
39
+ /** The current field value. */
31
40
  value: DateValue,
41
+ /** The current value, converted to a native JavaScript `Date` object. */
32
42
  dateValue: Date,
33
- setValue: (value: CalendarDateTime) => void,
43
+ /** Sets the field's value. */
44
+ setValue(value: DateValue): void,
45
+ /** A list of segments for the current value. */
34
46
  segments: DateSegment[],
47
+ /** A date formatter configured for the current locale and format. */
35
48
  dateFormatter: DateFormatter,
49
+ /** The current validation state of the date field, based on the `validationState`, `minValue`, and `maxValue` props. */
36
50
  validationState: ValidationState,
51
+ /** The granularity for the field, based on the `granularity` prop and current value. */
37
52
  granularity: Granularity,
38
- increment: (type: Intl.DateTimeFormatPartTypes) => void,
39
- decrement: (type: Intl.DateTimeFormatPartTypes) => void,
40
- incrementPage: (type: Intl.DateTimeFormatPartTypes) => void,
41
- decrementPage: (type: Intl.DateTimeFormatPartTypes) => void,
42
- setSegment: (type: Intl.DateTimeFormatPartTypes, value: number) => void,
43
- confirmPlaceholder: (type?: Intl.DateTimeFormatPartTypes) => void,
44
- clearSegment: (type?: Intl.DateTimeFormatPartTypes) => void,
45
- getFormatOptions(fieldOptions: FieldOptions): Intl.DateTimeFormatOptions
53
+ /** Whether the field is disabled. */
54
+ isDisabled: boolean,
55
+ /** Whether the field is read only. */
56
+ isReadOnly: boolean,
57
+ /** Whether the field is required. */
58
+ isRequired: boolean,
59
+ /** Increments the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
60
+ increment(type: SegmentType): void,
61
+ /** Decrements the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
62
+ decrement(type: SegmentType): void,
63
+ /**
64
+ * Increments the given segment by a larger amount, rounding it to the nearest increment.
65
+ * The amount to increment by depends on the field, for example 15 minutes, 7 days, and 5 years.
66
+ * Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
67
+ */
68
+ incrementPage(type: SegmentType): void,
69
+ /**
70
+ * Decrements the given segment by a larger amount, rounding it to the nearest increment.
71
+ * The amount to decrement by depends on the field, for example 15 minutes, 7 days, and 5 years.
72
+ * Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
73
+ */
74
+ decrementPage(type: SegmentType): void,
75
+ /** Sets the value of the given segment. */
76
+ setSegment(type: SegmentType, value: number): void,
77
+ /**
78
+ * Replaces the value of the date field with the placeholder value.
79
+ * If a segment type is provided, only that segment is confirmed. Otherwise, all segments that have not been entered yet are confirmed.
80
+ */
81
+ confirmPlaceholder(type?: SegmentType): void,
82
+ /** Clears the value of the given segment, reverting it to the placeholder. */
83
+ clearSegment(type: SegmentType): void,
84
+ /** Formats the current date value using the given options. */
85
+ formatValue(fieldOptions: FieldOptions): string
46
86
  }
47
87
 
48
88
  const EDITABLE_SEGMENTS = {
@@ -70,17 +110,36 @@ const TYPE_MAPPING = {
70
110
  dayperiod: 'dayPeriod'
71
111
  };
72
112
 
73
- interface DatePickerFieldProps<T extends DateValue> extends DatePickerProps<T> {
74
- maxGranularity?: 'year' | 'month' | DatePickerProps<T>['granularity'],
113
+ interface DatePickerFieldProps extends DatePickerProps<DateValue> {
114
+ /**
115
+ * The maximum unit to display in the date field.
116
+ * @default 'year'
117
+ */
118
+ maxGranularity?: 'year' | 'month' | Granularity,
119
+ /** The locale to display and edit the value according to. */
75
120
  locale: string,
121
+ /**
122
+ * A function that creates a [Calendar](../internationalized/date/Calendar.html)
123
+ * object for a given calendar identifier. Such a function may be imported from the
124
+ * `@internationalized/date` package, or manually implemented to include support for
125
+ * only certain calendars.
126
+ */
76
127
  createCalendar: (name: string) => Calendar
77
128
  }
78
129
 
79
- export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFieldProps<T>): DatePickerFieldState {
130
+ /**
131
+ * Provides state management for a date field component.
132
+ * A date field allows users to enter and edit date and time values using a keyboard.
133
+ * Each part of a date value is displayed in an individually editable segment.
134
+ */
135
+ export function useDateFieldState(props: DatePickerFieldProps): DateFieldState {
80
136
  let {
81
137
  locale,
82
138
  createCalendar,
83
- hideTimeZone
139
+ hideTimeZone,
140
+ isDisabled,
141
+ isReadOnly,
142
+ isRequired
84
143
  } = props;
85
144
 
86
145
  let v: DateValue = (props.value || props.defaultValue || props.placeholderValue);
@@ -221,6 +280,9 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
221
280
  dateFormatter,
222
281
  validationState,
223
282
  granularity,
283
+ isDisabled,
284
+ isReadOnly,
285
+ isRequired,
224
286
  increment(part) {
225
287
  adjustSegment(part, 1);
226
288
  },
@@ -278,8 +340,14 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
278
340
  setDate(null);
279
341
  setValue(value);
280
342
  },
281
- getFormatOptions(fieldOptions: FieldOptions) {
282
- return getFormatOptions(fieldOptions, formatOpts);
343
+ formatValue(fieldOptions: FieldOptions) {
344
+ if (!calendarValue) {
345
+ return '';
346
+ }
347
+
348
+ let formatOptions = getFormatOptions(fieldOptions, formatOpts);
349
+ let formatter = new DateFormatter(locale, formatOptions);
350
+ return formatter.format(dateValue);
283
351
  }
284
352
  };
285
353
  }
@@ -10,7 +10,7 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {CalendarDate, DateFormatter, toCalendarDateTime, toDateFields} from '@internationalized/date';
13
+ import {CalendarDate, DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationalized/date';
14
14
  import {DatePickerProps, DateValue, Granularity, TimeValue} from '@react-types/datepicker';
15
15
  import {FieldOptions, getFormatOptions, getPlaceholderTime, useDefaultProps} from './utils';
16
16
  import {isInvalid} from './utils';
@@ -18,21 +18,52 @@ import {useControlledState} from '@react-stately/utils';
18
18
  import {useState} from 'react';
19
19
  import {ValidationState} from '@react-types/shared';
20
20
 
21
+ export interface DatePickerOptions extends DatePickerProps<DateValue> {
22
+ /**
23
+ * Determines whether the date picker popover should close automatically when a date is selected.
24
+ * @default true
25
+ */
26
+ shouldCloseOnSelect?: boolean | (() => boolean)
27
+ }
28
+
21
29
  export interface DatePickerState {
30
+ /** The currently selected date. */
22
31
  value: DateValue,
23
- setValue: (value: DateValue) => void,
32
+ /** Sets the selected date. */
33
+ setValue(value: DateValue): void,
34
+ /**
35
+ * The date portion of the value. This may be set prior to `value` if the user has
36
+ * selected a date but has not yet selected a time.
37
+ */
24
38
  dateValue: DateValue,
25
- setDateValue: (value: CalendarDate) => void,
39
+ /** Sets the date portion of the value. */
40
+ setDateValue(value: CalendarDate): void,
41
+ /**
42
+ * The time portion of the value. This may be set prior to `value` if the user has
43
+ * selected a time but has not yet selected a date.
44
+ */
26
45
  timeValue: TimeValue,
27
- setTimeValue: (value: TimeValue) => void,
46
+ /** Sets the time portion of the value. */
47
+ setTimeValue(value: TimeValue): void,
48
+ /** The granularity for the field, based on the `granularity` prop and current value. */
49
+ granularity: Granularity,
50
+ /** Whether the date picker supports selecting a time, according to the `granularity` prop and current value. */
51
+ hasTime: boolean,
52
+ /** Whether the calendar popover is currently open. */
28
53
  isOpen: boolean,
29
- setOpen: (isOpen: boolean) => void,
54
+ /** Sets whether the calendar popover is open. */
55
+ setOpen(isOpen: boolean): void,
56
+ /** The current validation state of the date picker, based on the `validationState`, `minValue`, and `maxValue` props. */
30
57
  validationState: ValidationState,
31
- formatValue(locale: string, fieldOptions: FieldOptions): string,
32
- granularity: Granularity
58
+ /** Formats the selected value using the given options. */
59
+ formatValue(locale: string, fieldOptions: FieldOptions): string
33
60
  }
34
61
 
35
- export function useDatePickerState<T extends DateValue>(props: DatePickerProps<T>): DatePickerState {
62
+ /**
63
+ * Provides state management for a date picker component.
64
+ * A date picker combines a DateField and a Calendar popover to allow users to enter or select a date and time value.
65
+ */
66
+ export function useDatePickerState(props: DatePickerOptions): DatePickerState {
36
67
  let [isOpen, setOpen] = useState(false);
37
68
  let [value, setValue] = useControlledState<DateValue>(props.value, props.defaultValue || null, props.onChange);
38
69
 
@@ -40,6 +71,7 @@ export function useDatePickerState<T extends DateValue>(props: DatePickerProps<T
40
71
  let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);
41
72
  let dateValue = value != null ? value.toDate(defaultTimeZone ?? 'UTC') : null;
42
73
  let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second' || granularity === 'millisecond';
74
+ let shouldCloseOnSelect = props.shouldCloseOnSelect ?? true;
43
75
 
44
76
  let [selectedDate, setSelectedDate] = useState<DateValue>(null);
45
77
  let [selectedTime, setSelectedTime] = useState<TimeValue>(null);
@@ -57,14 +89,15 @@ export function useDatePickerState<T extends DateValue>(props: DatePickerProps<T
57
89
  }
58
90
 
59
91
  let commitValue = (date: DateValue, time: TimeValue) => {
60
- setValue('timeZone' in time ? time.set(toDateFields(date)) : toCalendarDateTime(date, time));
92
+ setValue('timeZone' in time ? time.set(toCalendarDate(date)) : toCalendarDateTime(date, time));
61
93
  };
62
94
 
63
95
  // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
64
96
  let selectDate = (newValue: CalendarDate) => {
97
+ let shouldClose = typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;
65
98
  if (hasTime) {
66
- if (selectedTime) {
67
- commitValue(newValue, selectedTime);
99
+ if (selectedTime || shouldClose) {
100
+ commitValue(newValue, selectedTime || getPlaceholderTime(props.placeholderValue));
68
101
  } else {
69
102
  setSelectedDate(newValue);
70
103
  }
@@ -72,7 +105,7 @@ export function useDatePickerState<T extends DateValue>(props: DatePickerProps<T
72
105
  setValue(newValue);
73
106
  }
74
107
 
75
- if (!hasTime) {
108
+ if (shouldClose) {
76
109
  setOpen(false);
77
110
  }
78
111
  };
@@ -86,7 +119,8 @@ export function useDatePickerState<T extends DateValue>(props: DatePickerProps<T
86
119
  };
87
120
 
88
121
  let validationState: ValidationState = props.validationState ||
89
- (isInvalid(value, props.minValue, props.maxValue) ? 'invalid' : null);
122
+ (isInvalid(value, props.minValue, props.maxValue) ? 'invalid' : null) ||
123
+ (value && props.isDateUnavailable?.(value) ? 'invalid' : null);
90
124
 
91
125
  return {
92
126
  value,
@@ -96,6 +130,7 @@ export function useDatePickerState<T extends DateValue>(props: DatePickerProps<T
96
130
  setDateValue: selectDate,
97
131
  setTimeValue: selectTime,
98
132
  granularity,
133
+ hasTime,
99
134
  isOpen,
100
135
  setOpen(isOpen) {
101
136
  // Commit the selected date when the calendar is closed. Use a placeholder time if one wasn't set.
@@ -11,32 +11,68 @@
11
11
  */
12
12
 
13
13
  import {createPlaceholderDate, FieldOptions, getFormatOptions, getPlaceholderTime, isInvalid, useDefaultProps} from './utils';
14
- import {DateFormatter, toCalendarDateTime, toDateFields} from '@internationalized/date';
14
+ import {DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationalized/date';
15
15
  import {DateRange, DateRangePickerProps, DateValue, Granularity, TimeValue} from '@react-types/datepicker';
16
16
  import {RangeValue, ValidationState} from '@react-types/shared';
17
17
  import {useControlledState} from '@react-stately/utils';
18
18
  import {useRef, useState} from 'react';
19
19
 
20
+ export interface DateRangePickerOptions extends DateRangePickerProps<DateValue> {
21
+ /**
22
+ * Determines whether the date picker popover should close automatically when a date is selected.
23
+ * @default true
24
+ */
25
+ shouldCloseOnSelect?: boolean | (() => boolean)
26
+ }
27
+
20
28
  type TimeRange = RangeValue<TimeValue>;
21
29
  export interface DateRangePickerState {
30
+ /** The currently selected date range. */
22
31
  value: DateRange,
23
- setValue: (value: DateRange) => void,
24
- setDate: (part: keyof DateRange, value: DateValue) => void,
25
- setTime: (part: keyof TimeRange, value: TimeValue) => void,
26
- setDateTime: (part: keyof DateRange, value: DateValue) => void,
32
+ /** Sets the selected date range. */
33
+ setValue(value: DateRange): void,
34
+ /**
35
+ * The date portion of the selected range. This may be set prior to `value` if the user has
36
+ * selected a date range but has not yet selected a time range.
37
+ */
27
38
  dateRange: DateRange,
28
- setDateRange: (value: DateRange) => void,
39
+ /** Sets the date portion of the selected range. */
40
+ setDateRange(value: DateRange): void,
41
+ /**
42
+ * The time portion of the selected range. This may be set prior to `value` if the user has
43
+ * selected a time range but has not yet selected a date range.
44
+ */
29
45
  timeRange: TimeRange,
30
- setTimeRange: (value: TimeRange) => void,
46
+ /** Sets the time portion of the selected range. */
47
+ setTimeRange(value: TimeRange): void,
48
+ /** Sets the date portion of either the start or end of the selected range. */
49
+ setDate(part: 'start' | 'end', value: DateValue): void,
50
+ /** Sets the time portion of either the start or end of the selected range. */
51
+ setTime(part: 'start' | 'end', value: TimeValue): void,
52
+ /** Sets the date and time of either the start or end of the selected range. */
53
+ setDateTime(part: 'start' | 'end', value: DateValue): void,
54
+ /** The granularity for the field, based on the `granularity` prop and current value. */
55
+ granularity: Granularity,
56
+ /** Whether the date range picker supports selecting times, according to the `granularity` prop and current value. */
57
+ hasTime: boolean,
58
+ /** Whether the calendar popover is currently open. */
31
59
  isOpen: boolean,
32
- setOpen: (isOpen: boolean) => void,
60
+ /** Sets whether the calendar popover is open. */
61
+ setOpen(isOpen: boolean): void,
62
+ /** The current validation state of the date picker, based on the `validationState`, `minValue`, and `maxValue` props. */
33
63
  validationState: ValidationState,
64
+ /** Formats the selected range using the given options. */
34
65
  formatValue(locale: string, fieldOptions: FieldOptions): string,
35
- confirmPlaceholder(): void,
36
- granularity: Granularity
66
+ /** Replaces the start and/or end value of the selected range with the placeholder value if unentered. */
67
+ confirmPlaceholder(): void
37
68
  }
38
69
 
39
- export function useDateRangePickerState<T extends DateValue>(props: DateRangePickerProps<T>): DateRangePickerState {
70
+ /**
71
+ * Provides state management for a date range picker component.
72
+ * A date range picker combines two DateFields and a RangeCalendar popover to allow
73
+ * users to enter or select a date and time range.
74
+ */
75
+ export function useDateRangePickerState(props: DateRangePickerOptions): DateRangePickerState {
40
76
  let [isOpen, setOpen] = useState(false);
41
77
  let [controlledValue, setControlledValue] = useControlledState<DateRange>(props.value, props.defaultValue || null, props.onChange);
42
78
  let [placeholderValue, setPlaceholderValue] = useState(() => controlledValue || {start: null, end: null});
@@ -64,6 +100,7 @@ export function useDateRangePickerState<T extends DateValue>(props: DateRangePic
64
100
  let v = (value?.start || value?.end || props.placeholderValue);
65
101
  let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);
66
102
  let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second' || granularity === 'millisecond';
103
+ let shouldCloseOnSelect = props.shouldCloseOnSelect ?? true;
67
104
 
68
105
  let [dateRange, setSelectedDateRange] = useState<DateRange>(null);
69
106
  let [timeRange, setSelectedTimeRange] = useState<TimeRange>(null);
@@ -77,16 +114,20 @@ export function useDateRangePickerState<T extends DateValue>(props: DateRangePic
77
114
 
78
115
  let commitValue = (dateRange: DateRange, timeRange: TimeRange) => {
79
116
  setValue({
80
- start: 'timeZone' in timeRange.start ? timeRange.start.set(toDateFields(dateRange.start)) : toCalendarDateTime(dateRange.start, timeRange.start),
81
- end: 'timeZone' in timeRange.end ? timeRange.end.set(toDateFields(dateRange.end)) : toCalendarDateTime(dateRange.end, timeRange.end)
117
+ start: 'timeZone' in timeRange.start ? timeRange.start.set(toCalendarDate(dateRange.start)) : toCalendarDateTime(dateRange.start, timeRange.start),
118
+ end: 'timeZone' in timeRange.end ? timeRange.end.set(toCalendarDate(dateRange.end)) : toCalendarDateTime(dateRange.end, timeRange.end)
82
119
  });
83
120
  };
84
121
 
85
122
  // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
86
123
  let setDateRange = (range: DateRange) => {
124
+ let shouldClose = typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;
87
125
  if (hasTime) {
88
- if (range.start && range.end && timeRange?.start && timeRange?.end) {
89
- commitValue(range, timeRange);
126
+ if (shouldClose || (range.start && range.end && timeRange?.start && timeRange?.end)) {
127
+ commitValue(range, {
128
+ start: timeRange?.start || getPlaceholderTime(props.placeholderValue),
129
+ end: timeRange?.end || getPlaceholderTime(props.placeholderValue)
130
+ });
90
131
  } else {
91
132
  setSelectedDateRange(range);
92
133
  }
@@ -96,7 +137,7 @@ export function useDateRangePickerState<T extends DateValue>(props: DateRangePic
96
137
  setSelectedDateRange(range);
97
138
  }
98
139
 
99
- if (!hasTime) {
140
+ if (shouldClose) {
100
141
  setOpen(false);
101
142
  }
102
143
  };
@@ -113,7 +154,9 @@ export function useDateRangePickerState<T extends DateValue>(props: DateRangePic
113
154
  || (value != null && (
114
155
  isInvalid(value.start, props.minValue, props.maxValue) ||
115
156
  isInvalid(value.end, props.minValue, props.maxValue) ||
116
- (value.end != null && value.start != null && value.end.compare(value.start) < 0)
157
+ (value.end != null && value.start != null && value.end.compare(value.start) < 0) ||
158
+ (value?.start && props.isDateUnavailable?.(value.start)) ||
159
+ (value?.end && props.isDateUnavailable?.(value.end))
117
160
  ) ? 'invalid' : null);
118
161
 
119
162
  return {
@@ -122,6 +165,7 @@ export function useDateRangePickerState<T extends DateValue>(props: DateRangePic
122
165
  dateRange,
123
166
  timeRange,
124
167
  granularity,
168
+ hasTime,
125
169
  setDate(part, date) {
126
170
  setDateRange({...dateRange, [part]: date});
127
171
  },
@@ -10,18 +10,23 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {Calendar, getLocalTimeZone, Time, toCalendarDateTime, today, toTime} from '@internationalized/date';
13
+ import {DateFieldState, useDateFieldState} from '.';
14
14
  import {DateValue, TimePickerProps, TimeValue} from '@react-types/datepicker';
15
+ import {getLocalTimeZone, GregorianCalendar, Time, toCalendarDateTime, today, toTime} from '@internationalized/date';
15
16
  import {useControlledState} from '@react-stately/utils';
16
- import {useDatePickerFieldState} from '.';
17
17
  import {useMemo} from 'react';
18
18
 
19
- interface TimeFieldProps<T extends TimeValue> extends TimePickerProps<T> {
20
- locale: string,
21
- createCalendar: (name: string) => Calendar
19
+ interface TimeFieldProps extends TimePickerProps<TimeValue> {
20
+ /** The locale to display and edit the value according to. */
21
+ locale: string
22
22
  }
23
23
 
24
- export function useTimeFieldState<T extends TimeValue>(props: TimeFieldProps<T>) {
24
+ /**
25
+ * Provides state management for a time field component.
26
+ * A time field allows users to enter and edit time values using a keyboard.
27
+ * Each part of a time value is displayed in an individually editable segment.
28
+ */
29
+ export function useTimeFieldState(props: TimeFieldProps): DateFieldState {
25
30
  let {
26
31
  placeholderValue = new Time(),
27
32
  minValue,
@@ -46,7 +51,7 @@ export function useTimeFieldState<T extends TimeValue>(props: TimeFieldProps<T>)
46
51
  setValue(v && 'day' in v ? newValue : newValue && toTime(newValue));
47
52
  };
48
53
 
49
- return useDatePickerFieldState({
54
+ return useDateFieldState({
50
55
  ...props,
51
56
  value: dateTime,
52
57
  defaultValue: undefined,
@@ -55,7 +60,9 @@ export function useTimeFieldState<T extends TimeValue>(props: TimeFieldProps<T>)
55
60
  onChange,
56
61
  granularity: granularity || 'minute',
57
62
  maxGranularity: 'hour',
58
- placeholderValue: placeholderDate
63
+ placeholderValue: placeholderDate,
64
+ // Calendar should not matter for time fields.
65
+ createCalendar: () => new GregorianCalendar()
59
66
  });
60
67
  }
61
68