@react-stately/datepicker 3.0.0-nightly.3175 → 3.0.0-rc.1

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-nightly.3175+91ca94fe5",
3
+ "version": "3.0.0-rc.1",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -18,17 +18,17 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@babel/runtime": "^7.6.2",
21
- "@internationalized/date": "3.0.0-nightly.3175+91ca94fe5",
22
- "@react-stately/utils": "3.0.0-nightly.1476+91ca94fe5",
23
- "@react-types/datepicker": "3.0.0-nightly.3175+91ca94fe5",
24
- "@react-types/shared": "3.0.0-nightly.1476+91ca94fe5",
25
- "date-fns": "^1.30.1"
21
+ "@internationalized/date": "3.0.0-rc.1",
22
+ "@react-stately/overlays": "^3.3.0",
23
+ "@react-stately/utils": "^3.5.0",
24
+ "@react-types/datepicker": "3.0.0-rc.1",
25
+ "@react-types/shared": "^3.13.0"
26
26
  },
27
27
  "peerDependencies": {
28
- "react": "^16.8.0 || ^17.0.0-rc.1"
28
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
29
29
  },
30
30
  "publishConfig": {
31
31
  "access": "public"
32
32
  },
33
- "gitHead": "91ca94fe52840b7a32b961ec08208f5fbdf65697"
33
+ "gitHead": "8f921ec5094e7c2b3c301bcb6133372e35a2052b"
34
34
  }
package/src/index.ts CHANGED
@@ -10,7 +10,12 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export * from './useDatePickerState';
14
- export * from './useDatePickerFieldState';
15
- export * from './useDateRangePickerState';
16
- export * from './useTimeFieldState';
13
+ export {useDatePickerState} from './useDatePickerState';
14
+ export {useDateFieldState} from './useDateFieldState';
15
+ export {useDateRangePickerState} from './useDateRangePickerState';
16
+ export {useTimeFieldState} from './useTimeFieldState';
17
+
18
+ export type {DateFieldStateOptions, DateFieldState, DateSegment, SegmentType} from './useDateFieldState';
19
+ export type {DatePickerStateOptions, DatePickerState} from './useDatePickerState';
20
+ export type {DateRangePickerStateOptions, DateRangePickerState} from './useDateRangePickerState';
21
+ export type {TimeFieldStateOptions} from './useTimeFieldState';
@@ -10,42 +10,81 @@
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,
53
+ /** The maximum date or time unit that is displayed in the field. */
54
+ maxGranularity: 'year' | 'month' | Granularity,
55
+ /** Whether the field is disabled. */
38
56
  isDisabled: boolean,
57
+ /** Whether the field is read only. */
39
58
  isReadOnly: boolean,
59
+ /** Whether the field is required. */
40
60
  isRequired: boolean,
41
- increment: (type: Intl.DateTimeFormatPartTypes) => void,
42
- decrement: (type: Intl.DateTimeFormatPartTypes) => void,
43
- incrementPage: (type: Intl.DateTimeFormatPartTypes) => void,
44
- decrementPage: (type: Intl.DateTimeFormatPartTypes) => void,
45
- setSegment: (type: Intl.DateTimeFormatPartTypes, value: number) => void,
46
- confirmPlaceholder: (type?: Intl.DateTimeFormatPartTypes) => void,
47
- clearSegment: (type?: Intl.DateTimeFormatPartTypes) => void,
48
- getFormatOptions(fieldOptions: FieldOptions): Intl.DateTimeFormatOptions
61
+ /** Increments the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
62
+ increment(type: SegmentType): void,
63
+ /** Decrements the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
64
+ decrement(type: SegmentType): void,
65
+ /**
66
+ * Increments the given segment by a larger amount, rounding it to the nearest increment.
67
+ * The amount to increment by depends on the field, for example 15 minutes, 7 days, and 5 years.
68
+ * Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
69
+ */
70
+ incrementPage(type: SegmentType): void,
71
+ /**
72
+ * Decrements the given segment by a larger amount, rounding it to the nearest increment.
73
+ * The amount to decrement by depends on the field, for example 15 minutes, 7 days, and 5 years.
74
+ * Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
75
+ */
76
+ decrementPage(type: SegmentType): void,
77
+ /** Sets the value of the given segment. */
78
+ setSegment(type: SegmentType, value: number): void,
79
+ /**
80
+ * Replaces the value of the date field with the placeholder value.
81
+ * If a segment type is provided, only that segment is confirmed. Otherwise, all segments that have not been entered yet are confirmed.
82
+ */
83
+ confirmPlaceholder(type?: SegmentType): void,
84
+ /** Clears the value of the given segment, reverting it to the placeholder. */
85
+ clearSegment(type: SegmentType): void,
86
+ /** Formats the current date value using the given options. */
87
+ formatValue(fieldOptions: FieldOptions): string
49
88
  }
50
89
 
51
90
  const EDITABLE_SEGMENTS = {
@@ -73,13 +112,29 @@ const TYPE_MAPPING = {
73
112
  dayperiod: 'dayPeriod'
74
113
  };
75
114
 
76
- interface DatePickerFieldProps<T extends DateValue> extends DatePickerProps<T> {
77
- maxGranularity?: 'year' | 'month' | DatePickerProps<T>['granularity'],
115
+ export interface DateFieldStateOptions extends DatePickerProps<DateValue> {
116
+ /**
117
+ * The maximum unit to display in the date field.
118
+ * @default 'year'
119
+ */
120
+ maxGranularity?: 'year' | 'month' | Granularity,
121
+ /** The locale to display and edit the value according to. */
78
122
  locale: string,
123
+ /**
124
+ * A function that creates a [Calendar](../internationalized/date/Calendar.html)
125
+ * object for a given calendar identifier. Such a function may be imported from the
126
+ * `@internationalized/date` package, or manually implemented to include support for
127
+ * only certain calendars.
128
+ */
79
129
  createCalendar: (name: string) => Calendar
80
130
  }
81
131
 
82
- export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFieldProps<T>): DatePickerFieldState {
132
+ /**
133
+ * Provides state management for a date field component.
134
+ * A date field allows users to enter and edit date and time values using a keyboard.
135
+ * Each part of a date value is displayed in an individually editable segment.
136
+ */
137
+ export function useDateFieldState(props: DateFieldStateOptions): DateFieldState {
83
138
  let {
84
139
  locale,
85
140
  createCalendar,
@@ -98,18 +153,39 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
98
153
  throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
99
154
  }
100
155
 
156
+ let defaultFormatter = useMemo(() => new DateFormatter(locale), [locale]);
157
+ let calendar = useMemo(() => createCalendar(defaultFormatter.resolvedOptions().calendar), [createCalendar, defaultFormatter]);
158
+
159
+ let [value, setDate] = useControlledState<DateValue>(
160
+ props.value,
161
+ props.defaultValue,
162
+ props.onChange
163
+ );
164
+
165
+ let calendarValue = useMemo(() => convertValue(value, calendar), [value, calendar]);
166
+
167
+ // We keep track of the placeholder date separately in state so that onChange is not called
168
+ // until all segments are set. If the value === null (not undefined), then assume the component
169
+ // is controlled, so use the placeholder as the value until all segments are entered so it doesn't
170
+ // change from uncontrolled to controlled and emit a warning.
171
+ let [placeholderDate, setPlaceholderDate] = useState(
172
+ () => createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone)
173
+ );
174
+
175
+ let val = calendarValue || placeholderDate;
176
+ let showEra = calendar.identifier === 'gregory' && val.era === 'BC';
101
177
  let formatOpts = useMemo(() => ({
102
178
  granularity,
103
179
  maxGranularity: props.maxGranularity ?? 'year',
104
180
  timeZone: defaultTimeZone,
105
181
  hideTimeZone,
106
- hourCycle: props.hourCycle
107
- }), [props.maxGranularity, granularity, props.hourCycle, defaultTimeZone, hideTimeZone]);
182
+ hourCycle: props.hourCycle,
183
+ showEra
184
+ }), [props.maxGranularity, granularity, props.hourCycle, defaultTimeZone, hideTimeZone, showEra]);
108
185
  let opts = useMemo(() => getFormatOptions({}, formatOpts), [formatOpts]);
109
186
 
110
187
  let dateFormatter = useMemo(() => new DateFormatter(locale, opts), [locale, opts]);
111
188
  let resolvedOptions = useMemo(() => dateFormatter.resolvedOptions(), [dateFormatter]);
112
- let calendar = useMemo(() => createCalendar(resolvedOptions.calendar), [createCalendar, resolvedOptions.calendar]);
113
189
 
114
190
  // Determine how many editable segments there are for validation purposes.
115
191
  // The result is cached for performance.
@@ -123,14 +199,6 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
123
199
  () => props.value || props.defaultValue ? {...allSegments} : {}
124
200
  );
125
201
 
126
- // We keep track of the placeholder date separately in state so that onChange is not called
127
- // until all segments are set. If the value === null (not undefined), then assume the component
128
- // is controlled, so use the placeholder as the value until all segments are entered so it doesn't
129
- // change from uncontrolled to controlled and emit a warning.
130
- let [placeholderDate, setPlaceholderDate] = useState(
131
- () => createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone)
132
- );
133
-
134
202
  // Reset placeholder when calendar changes
135
203
  let lastCalendarIdentifier = useRef(calendar.identifier);
136
204
  useEffect(() => {
@@ -144,14 +212,6 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
144
212
  }
145
213
  }, [calendar, granularity, validSegments, defaultTimeZone, props.placeholderValue]);
146
214
 
147
- let [value, setDate] = useControlledState<DateValue>(
148
- props.value,
149
- props.defaultValue,
150
- props.onChange
151
- );
152
-
153
- let calendarValue = useMemo(() => convertValue(value, calendar), [value, calendar]);
154
-
155
215
  // If there is a value prop, and some segments were previously placeholders, mark them all as valid.
156
216
  if (value && Object.keys(validSegments).length < Object.keys(allSegments).length) {
157
217
  validSegments = {...allSegments};
@@ -227,6 +287,7 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
227
287
  dateFormatter,
228
288
  validationState,
229
289
  granularity,
290
+ maxGranularity: props.maxGranularity ?? 'year',
230
291
  isDisabled,
231
292
  isReadOnly,
232
293
  isRequired,
@@ -287,8 +348,14 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
287
348
  setDate(null);
288
349
  setValue(value);
289
350
  },
290
- getFormatOptions(fieldOptions: FieldOptions) {
291
- return getFormatOptions(fieldOptions, formatOpts);
351
+ formatValue(fieldOptions: FieldOptions) {
352
+ if (!calendarValue) {
353
+ return '';
354
+ }
355
+
356
+ let formatOptions = getFormatOptions(fieldOptions, formatOpts);
357
+ let formatter = new DateFormatter(locale, formatOptions);
358
+ return formatter.format(dateValue);
292
359
  }
293
360
  };
294
361
  }
@@ -15,10 +15,11 @@ import {DatePickerProps, DateValue, Granularity, TimeValue} from '@react-types/d
15
15
  import {FieldOptions, getFormatOptions, getPlaceholderTime, useDefaultProps} from './utils';
16
16
  import {isInvalid} from './utils';
17
17
  import {useControlledState} from '@react-stately/utils';
18
+ import {useOverlayTriggerState} from '@react-stately/overlays';
18
19
  import {useState} from 'react';
19
20
  import {ValidationState} from '@react-types/shared';
20
21
 
21
- export interface DatePickerOptions extends DatePickerProps<DateValue> {
22
+ export interface DatePickerStateOptions extends DatePickerProps<DateValue> {
22
23
  /**
23
24
  * Determines whether the date picker popover should close automatically when a date is selected.
24
25
  * @default true
@@ -27,22 +28,44 @@ export interface DatePickerOptions extends DatePickerProps<DateValue> {
27
28
  }
28
29
 
29
30
  export interface DatePickerState {
31
+ /** The currently selected date. */
30
32
  value: DateValue,
31
- setValue: (value: DateValue) => void,
33
+ /** Sets the selected date. */
34
+ setValue(value: DateValue): void,
35
+ /**
36
+ * The date portion of the value. This may be set prior to `value` if the user has
37
+ * selected a date but has not yet selected a time.
38
+ */
32
39
  dateValue: DateValue,
33
- setDateValue: (value: CalendarDate) => void,
40
+ /** Sets the date portion of the value. */
41
+ setDateValue(value: CalendarDate): void,
42
+ /**
43
+ * The time portion of the value. This may be set prior to `value` if the user has
44
+ * selected a time but has not yet selected a date.
45
+ */
34
46
  timeValue: TimeValue,
35
- setTimeValue: (value: TimeValue) => void,
47
+ /** Sets the time portion of the value. */
48
+ setTimeValue(value: TimeValue): void,
49
+ /** The granularity for the field, based on the `granularity` prop and current value. */
50
+ granularity: Granularity,
51
+ /** Whether the date picker supports selecting a time, according to the `granularity` prop and current value. */
36
52
  hasTime: boolean,
53
+ /** Whether the calendar popover is currently open. */
37
54
  isOpen: boolean,
38
- setOpen: (isOpen: boolean) => void,
55
+ /** Sets whether the calendar popover is open. */
56
+ setOpen(isOpen: boolean): void,
57
+ /** The current validation state of the date picker, based on the `validationState`, `minValue`, and `maxValue` props. */
39
58
  validationState: ValidationState,
40
- formatValue(locale: string, fieldOptions: FieldOptions): string,
41
- granularity: Granularity
59
+ /** Formats the selected value using the given options. */
60
+ formatValue(locale: string, fieldOptions: FieldOptions): string
42
61
  }
43
62
 
44
- export function useDatePickerState(props: DatePickerOptions): DatePickerState {
45
- let [isOpen, setOpen] = useState(false);
63
+ /**
64
+ * Provides state management for a date picker component.
65
+ * A date picker combines a DateField and a Calendar popover to allow users to enter or select a date and time value.
66
+ */
67
+ export function useDatePickerState(props: DatePickerStateOptions): DatePickerState {
68
+ let overlayState = useOverlayTriggerState(props);
46
69
  let [value, setValue] = useControlledState<DateValue>(props.value, props.defaultValue || null, props.onChange);
47
70
 
48
71
  let v = (value || props.placeholderValue);
@@ -84,7 +107,7 @@ export function useDatePickerState(props: DatePickerOptions): DatePickerState {
84
107
  }
85
108
 
86
109
  if (shouldClose) {
87
- setOpen(false);
110
+ overlayState.setOpen(false);
88
111
  }
89
112
  };
90
113
 
@@ -109,7 +132,7 @@ export function useDatePickerState(props: DatePickerOptions): DatePickerState {
109
132
  setTimeValue: selectTime,
110
133
  granularity,
111
134
  hasTime,
112
- isOpen,
135
+ isOpen: overlayState.isOpen,
113
136
  setOpen(isOpen) {
114
137
  // Commit the selected date when the calendar is closed. Use a placeholder time if one wasn't set.
115
138
  // If only the time was set and not the date, don't commit. The state will be preserved until
@@ -118,7 +141,7 @@ export function useDatePickerState(props: DatePickerOptions): DatePickerState {
118
141
  commitValue(selectedDate, selectedTime || getPlaceholderTime(props.placeholderValue));
119
142
  }
120
143
 
121
- setOpen(isOpen);
144
+ overlayState.setOpen(isOpen);
122
145
  },
123
146
  validationState,
124
147
  formatValue(locale, fieldOptions) {
@@ -130,7 +153,8 @@ export function useDatePickerState(props: DatePickerOptions): DatePickerState {
130
153
  granularity,
131
154
  timeZone: defaultTimeZone,
132
155
  hideTimeZone: props.hideTimeZone,
133
- hourCycle: props.hourCycle
156
+ hourCycle: props.hourCycle,
157
+ showEra: value.calendar.identifier === 'gregory' && value.era === 'BC'
134
158
  });
135
159
 
136
160
  let formatter = new DateFormatter(locale, formatOptions);
@@ -15,9 +15,10 @@ import {DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationali
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
+ import {useOverlayTriggerState} from '@react-stately/overlays';
18
19
  import {useRef, useState} from 'react';
19
20
 
20
- export interface DateRangePickerOptions extends DateRangePickerProps<DateValue> {
21
+ export interface DateRangePickerStateOptions extends DateRangePickerProps<DateValue> {
21
22
  /**
22
23
  * Determines whether the date picker popover should close automatically when a date is selected.
23
24
  * @default true
@@ -27,26 +28,53 @@ export interface DateRangePickerOptions extends DateRangePickerProps<DateValue>
27
28
 
28
29
  type TimeRange = RangeValue<TimeValue>;
29
30
  export interface DateRangePickerState {
31
+ /** The currently selected date range. */
30
32
  value: DateRange,
31
- setValue: (value: DateRange) => void,
32
- setDate: (part: keyof DateRange, value: DateValue) => void,
33
- setTime: (part: keyof TimeRange, value: TimeValue) => void,
34
- setDateTime: (part: keyof DateRange, value: DateValue) => void,
33
+ /** Sets the selected date range. */
34
+ setValue(value: DateRange): void,
35
+ /**
36
+ * The date portion of the selected range. This may be set prior to `value` if the user has
37
+ * selected a date range but has not yet selected a time range.
38
+ */
35
39
  dateRange: DateRange,
36
- setDateRange: (value: DateRange) => void,
40
+ /** Sets the date portion of the selected range. */
41
+ setDateRange(value: DateRange): void,
42
+ /**
43
+ * The time portion of the selected range. This may be set prior to `value` if the user has
44
+ * selected a time range but has not yet selected a date range.
45
+ */
37
46
  timeRange: TimeRange,
38
- setTimeRange: (value: TimeRange) => void,
47
+ /** Sets the time portion of the selected range. */
48
+ setTimeRange(value: TimeRange): void,
49
+ /** Sets the date portion of either the start or end of the selected range. */
50
+ setDate(part: 'start' | 'end', value: DateValue): void,
51
+ /** Sets the time portion of either the start or end of the selected range. */
52
+ setTime(part: 'start' | 'end', value: TimeValue): void,
53
+ /** Sets the date and time of either the start or end of the selected range. */
54
+ setDateTime(part: 'start' | 'end', value: DateValue): void,
55
+ /** The granularity for the field, based on the `granularity` prop and current value. */
56
+ granularity: Granularity,
57
+ /** Whether the date range picker supports selecting times, according to the `granularity` prop and current value. */
39
58
  hasTime: boolean,
59
+ /** Whether the calendar popover is currently open. */
40
60
  isOpen: boolean,
41
- setOpen: (isOpen: boolean) => void,
61
+ /** Sets whether the calendar popover is open. */
62
+ setOpen(isOpen: boolean): void,
63
+ /** The current validation state of the date picker, based on the `validationState`, `minValue`, and `maxValue` props. */
42
64
  validationState: ValidationState,
43
- formatValue(locale: string, fieldOptions: FieldOptions): string,
44
- confirmPlaceholder(): void,
45
- granularity: Granularity
65
+ /** Formats the selected range using the given options. */
66
+ formatValue(locale: string, fieldOptions: FieldOptions): {start: string, end: string},
67
+ /** Replaces the start and/or end value of the selected range with the placeholder value if unentered. */
68
+ confirmPlaceholder(): void
46
69
  }
47
70
 
48
- export function useDateRangePickerState(props: DateRangePickerOptions): DateRangePickerState {
49
- let [isOpen, setOpen] = useState(false);
71
+ /**
72
+ * Provides state management for a date range picker component.
73
+ * A date range picker combines two DateFields and a RangeCalendar popover to allow
74
+ * users to enter or select a date and time range.
75
+ */
76
+ export function useDateRangePickerState(props: DateRangePickerStateOptions): DateRangePickerState {
77
+ let overlayState = useOverlayTriggerState(props);
50
78
  let [controlledValue, setControlledValue] = useControlledState<DateRange>(props.value, props.defaultValue || null, props.onChange);
51
79
  let [placeholderValue, setPlaceholderValue] = useState(() => controlledValue || {start: null, end: null});
52
80
 
@@ -111,7 +139,7 @@ export function useDateRangePickerState(props: DateRangePickerOptions): DateRang
111
139
  }
112
140
 
113
141
  if (shouldClose) {
114
- setOpen(false);
142
+ overlayState.setOpen(false);
115
143
  }
116
144
  };
117
145
 
@@ -150,7 +178,7 @@ export function useDateRangePickerState(props: DateRangePickerOptions): DateRang
150
178
  },
151
179
  setDateRange,
152
180
  setTimeRange,
153
- isOpen,
181
+ isOpen: overlayState.isOpen,
154
182
  setOpen(isOpen) {
155
183
  // Commit the selected date range when the calendar is closed. Use a placeholder time if one wasn't set.
156
184
  // If only the time range was set and not the date range, don't commit. The state will be preserved until
@@ -162,12 +190,12 @@ export function useDateRangePickerState(props: DateRangePickerOptions): DateRang
162
190
  });
163
191
  }
164
192
 
165
- setOpen(isOpen);
193
+ overlayState.setOpen(isOpen);
166
194
  },
167
195
  validationState,
168
196
  formatValue(locale, fieldOptions) {
169
197
  if (!value || !value.start || !value.end) {
170
- return '';
198
+ return null;
171
199
  }
172
200
 
173
201
  let startTimeZone = 'timeZone' in value.start ? value.start.timeZone : undefined;
@@ -179,17 +207,47 @@ export function useDateRangePickerState(props: DateRangePickerOptions): DateRang
179
207
  granularity: startGranularity,
180
208
  timeZone: startTimeZone,
181
209
  hideTimeZone: props.hideTimeZone,
182
- hourCycle: props.hourCycle
210
+ hourCycle: props.hourCycle,
211
+ showEra: (value.start.calendar.identifier === 'gregory' && value.start.era === 'BC') ||
212
+ (value.end.calendar.identifier === 'gregory' && value.end.era === 'BC')
183
213
  });
184
214
 
215
+ let startDate = value.start.toDate(startTimeZone || 'UTC');
216
+ let endDate = value.end.toDate(endTimeZone || 'UTC');
217
+
185
218
  let startFormatter = new DateFormatter(locale, startOptions);
186
219
  let endFormatter: Intl.DateTimeFormat;
187
- if (startTimeZone === endTimeZone && startGranularity === endGranularity) {
220
+ if (startTimeZone === endTimeZone && startGranularity === endGranularity && value.start.compare(value.end) !== 0) {
188
221
  // Use formatRange, as it results in shorter output when some of the fields
189
222
  // are shared between the start and end dates (e.g. the same month).
190
223
  // Formatting will fail if the end date is before the start date. Fall back below when that happens.
191
224
  try {
192
- return startFormatter.formatRange(value.start.toDate(startTimeZone), value.end.toDate(endTimeZone));
225
+ let parts = startFormatter.formatRangeToParts(startDate, endDate);
226
+
227
+ // Find the separator between the start and end date. This is determined
228
+ // by finding the last shared literal before the end range.
229
+ let separatorIndex = -1;
230
+ for (let i = 0; i < parts.length; i++) {
231
+ let part = parts[i];
232
+ if (part.source === 'shared' && part.type === 'literal') {
233
+ separatorIndex = i;
234
+ } else if (part.source === 'endRange') {
235
+ break;
236
+ }
237
+ }
238
+
239
+ // Now we can combine the parts into start and end strings.
240
+ let start = '';
241
+ let end = '';
242
+ for (let i = 0; i < parts.length; i++) {
243
+ if (i < separatorIndex) {
244
+ start += parts[i].value;
245
+ } else if (i > separatorIndex) {
246
+ end += parts[i].value;
247
+ }
248
+ }
249
+
250
+ return {start, end};
193
251
  } catch (e) {
194
252
  // ignore
195
253
  }
@@ -206,7 +264,10 @@ export function useDateRangePickerState(props: DateRangePickerOptions): DateRang
206
264
  endFormatter = new DateFormatter(locale, endOptions);
207
265
  }
208
266
 
209
- return `${startFormatter.format(value.start.toDate(startTimeZone))} – ${endFormatter.format(value.end.toDate(endTimeZone))}`;
267
+ return {
268
+ start: startFormatter.format(startDate),
269
+ end: endFormatter.format(endDate)
270
+ };
210
271
  },
211
272
  confirmPlaceholder() {
212
273
  // Need to use ref value here because the value can be set in the same tick as
@@ -10,17 +10,23 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import {DateFieldState, useDateFieldState} from '.';
13
14
  import {DateValue, TimePickerProps, TimeValue} from '@react-types/datepicker';
14
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> {
19
+ export interface TimeFieldStateOptions extends TimePickerProps<TimeValue> {
20
+ /** The locale to display and edit the value according to. */
20
21
  locale: string
21
22
  }
22
23
 
23
- 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: TimeFieldStateOptions): DateFieldState {
24
30
  let {
25
31
  placeholderValue = new Time(),
26
32
  minValue,
@@ -45,7 +51,7 @@ export function useTimeFieldState<T extends TimeValue>(props: TimeFieldProps<T>)
45
51
  setValue(v && 'day' in v ? newValue : newValue && toTime(newValue));
46
52
  };
47
53
 
48
- return useDatePickerFieldState({
54
+ return useDateFieldState({
49
55
  ...props,
50
56
  value: dateTime,
51
57
  defaultValue: undefined,
package/src/utils.ts CHANGED
@@ -27,7 +27,8 @@ interface FormatterOptions {
27
27
  hideTimeZone?: boolean,
28
28
  granularity?: DatePickerProps<any>['granularity'],
29
29
  maxGranularity?: 'year' | 'month' | DatePickerProps<any>['granularity'],
30
- hourCycle?: 12 | 24
30
+ hourCycle?: 12 | 24,
31
+ showEra?: boolean
31
32
  }
32
33
 
33
34
  const DEFAULT_FIELD_OPTIONS: FieldOptions = {
@@ -76,6 +77,10 @@ export function getFormatOptions(
76
77
  opts.timeZoneName = 'short';
77
78
  }
78
79
 
80
+ if (options.showEra && startIdx === 0) {
81
+ opts.era = 'short';
82
+ }
83
+
79
84
  return opts;
80
85
  }
81
86