@react-stately/datepicker 3.0.0-nightly.3173 → 3.0.0-rc.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/dist/main.js +66 -61
- package/dist/main.js.map +1 -1
- package/dist/module.js +63 -46
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +148 -35
- package/dist/types.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +9 -4
- package/src/{useDatePickerFieldState.ts → useDateFieldState.ts} +79 -17
- package/src/useDatePickerState.ts +35 -12
- package/src/useDateRangePickerState.ts +79 -20
- package/src/useTimeFieldState.ts +10 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/datepicker",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-rc.0",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@babel/runtime": "^7.6.2",
|
|
21
|
-
"@internationalized/date": "3.0.0-
|
|
22
|
-
"@react-stately/
|
|
23
|
-
"@react-
|
|
24
|
-
"@react-types/
|
|
25
|
-
"
|
|
21
|
+
"@internationalized/date": "3.0.0-rc.0",
|
|
22
|
+
"@react-stately/overlays": "^3.2.0",
|
|
23
|
+
"@react-stately/utils": "^3.4.1",
|
|
24
|
+
"@react-types/datepicker": "3.0.0-rc.0",
|
|
25
|
+
"@react-types/shared": "^3.12.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"react": "^16.8.0 || ^17.0.0-rc.1"
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "6a503b715e0dbbf92038cd7f08b1bcdde4c78e82"
|
|
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
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
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,
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
|
77
|
-
|
|
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
|
-
|
|
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,
|
|
@@ -227,6 +282,7 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
|
|
|
227
282
|
dateFormatter,
|
|
228
283
|
validationState,
|
|
229
284
|
granularity,
|
|
285
|
+
maxGranularity: props.maxGranularity ?? 'year',
|
|
230
286
|
isDisabled,
|
|
231
287
|
isReadOnly,
|
|
232
288
|
isRequired,
|
|
@@ -287,8 +343,14 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
|
|
|
287
343
|
setDate(null);
|
|
288
344
|
setValue(value);
|
|
289
345
|
},
|
|
290
|
-
|
|
291
|
-
|
|
346
|
+
formatValue(fieldOptions: FieldOptions) {
|
|
347
|
+
if (!calendarValue) {
|
|
348
|
+
return '';
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let formatOptions = getFormatOptions(fieldOptions, formatOpts);
|
|
352
|
+
let formatter = new DateFormatter(locale, formatOptions);
|
|
353
|
+
return formatter.format(dateValue);
|
|
292
354
|
}
|
|
293
355
|
};
|
|
294
356
|
}
|
|
@@ -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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
41
|
-
|
|
59
|
+
/** Formats the selected value using the given options. */
|
|
60
|
+
formatValue(locale: string, fieldOptions: FieldOptions): string
|
|
42
61
|
}
|
|
43
62
|
|
|
44
|
-
|
|
45
|
-
|
|
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) {
|
|
@@ -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
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
49
|
-
|
|
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;
|
|
@@ -182,14 +210,42 @@ export function useDateRangePickerState(props: DateRangePickerOptions): DateRang
|
|
|
182
210
|
hourCycle: props.hourCycle
|
|
183
211
|
});
|
|
184
212
|
|
|
213
|
+
let startDate = value.start.toDate(startTimeZone || 'UTC');
|
|
214
|
+
let endDate = value.end.toDate(endTimeZone || 'UTC');
|
|
215
|
+
|
|
185
216
|
let startFormatter = new DateFormatter(locale, startOptions);
|
|
186
217
|
let endFormatter: Intl.DateTimeFormat;
|
|
187
|
-
if (startTimeZone === endTimeZone && startGranularity === endGranularity) {
|
|
218
|
+
if (startTimeZone === endTimeZone && startGranularity === endGranularity && value.start.compare(value.end) !== 0) {
|
|
188
219
|
// Use formatRange, as it results in shorter output when some of the fields
|
|
189
220
|
// are shared between the start and end dates (e.g. the same month).
|
|
190
221
|
// Formatting will fail if the end date is before the start date. Fall back below when that happens.
|
|
191
222
|
try {
|
|
192
|
-
|
|
223
|
+
let parts = startFormatter.formatRangeToParts(startDate, endDate);
|
|
224
|
+
|
|
225
|
+
// Find the separator between the start and end date. This is determined
|
|
226
|
+
// by finding the last shared literal before the end range.
|
|
227
|
+
let separatorIndex = -1;
|
|
228
|
+
for (let i = 0; i < parts.length; i++) {
|
|
229
|
+
let part = parts[i];
|
|
230
|
+
if (part.source === 'shared' && part.type === 'literal') {
|
|
231
|
+
separatorIndex = i;
|
|
232
|
+
} else if (part.source === 'endRange') {
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Now we can combine the parts into start and end strings.
|
|
238
|
+
let start = '';
|
|
239
|
+
let end = '';
|
|
240
|
+
for (let i = 0; i < parts.length; i++) {
|
|
241
|
+
if (i < separatorIndex) {
|
|
242
|
+
start += parts[i].value;
|
|
243
|
+
} else if (i > separatorIndex) {
|
|
244
|
+
end += parts[i].value;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return {start, end};
|
|
193
249
|
} catch (e) {
|
|
194
250
|
// ignore
|
|
195
251
|
}
|
|
@@ -206,7 +262,10 @@ export function useDateRangePickerState(props: DateRangePickerOptions): DateRang
|
|
|
206
262
|
endFormatter = new DateFormatter(locale, endOptions);
|
|
207
263
|
}
|
|
208
264
|
|
|
209
|
-
return
|
|
265
|
+
return {
|
|
266
|
+
start: startFormatter.format(startDate),
|
|
267
|
+
end: endFormatter.format(endDate)
|
|
268
|
+
};
|
|
210
269
|
},
|
|
211
270
|
confirmPlaceholder() {
|
|
212
271
|
// Need to use ref value here because the value can be set in the same tick as
|
package/src/useTimeFieldState.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
|
54
|
+
return useDateFieldState({
|
|
49
55
|
...props,
|
|
50
56
|
value: dateTime,
|
|
51
57
|
defaultValue: undefined,
|