@react-stately/datepicker 3.16.0 → 3.17.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.
Files changed (227) hide show
  1. package/dist/import.mjs +5 -5
  2. package/dist/main.js +8 -8
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +5 -5
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +9 -0
  7. package/package.json +17 -20
  8. package/src/index.ts +9 -9
  9. package/dist/IncompleteDate.main.js +0 -335
  10. package/dist/IncompleteDate.main.js.map +0 -1
  11. package/dist/IncompleteDate.mjs +0 -330
  12. package/dist/IncompleteDate.module.js +0 -330
  13. package/dist/IncompleteDate.module.js.map +0 -1
  14. package/dist/ar-AE.main.js +0 -9
  15. package/dist/ar-AE.main.js.map +0 -1
  16. package/dist/ar-AE.mjs +0 -11
  17. package/dist/ar-AE.module.js +0 -11
  18. package/dist/ar-AE.module.js.map +0 -1
  19. package/dist/bg-BG.main.js +0 -9
  20. package/dist/bg-BG.main.js.map +0 -1
  21. package/dist/bg-BG.mjs +0 -11
  22. package/dist/bg-BG.module.js +0 -11
  23. package/dist/bg-BG.module.js.map +0 -1
  24. package/dist/cs-CZ.main.js +0 -9
  25. package/dist/cs-CZ.main.js.map +0 -1
  26. package/dist/cs-CZ.mjs +0 -11
  27. package/dist/cs-CZ.module.js +0 -11
  28. package/dist/cs-CZ.module.js.map +0 -1
  29. package/dist/da-DK.main.js +0 -9
  30. package/dist/da-DK.main.js.map +0 -1
  31. package/dist/da-DK.mjs +0 -11
  32. package/dist/da-DK.module.js +0 -11
  33. package/dist/da-DK.module.js.map +0 -1
  34. package/dist/de-DE.main.js +0 -9
  35. package/dist/de-DE.main.js.map +0 -1
  36. package/dist/de-DE.mjs +0 -11
  37. package/dist/de-DE.module.js +0 -11
  38. package/dist/de-DE.module.js.map +0 -1
  39. package/dist/el-GR.main.js +0 -9
  40. package/dist/el-GR.main.js.map +0 -1
  41. package/dist/el-GR.mjs +0 -11
  42. package/dist/el-GR.module.js +0 -11
  43. package/dist/el-GR.module.js.map +0 -1
  44. package/dist/en-US.main.js +0 -9
  45. package/dist/en-US.main.js.map +0 -1
  46. package/dist/en-US.mjs +0 -11
  47. package/dist/en-US.module.js +0 -11
  48. package/dist/en-US.module.js.map +0 -1
  49. package/dist/es-ES.main.js +0 -9
  50. package/dist/es-ES.main.js.map +0 -1
  51. package/dist/es-ES.mjs +0 -11
  52. package/dist/es-ES.module.js +0 -11
  53. package/dist/es-ES.module.js.map +0 -1
  54. package/dist/et-EE.main.js +0 -9
  55. package/dist/et-EE.main.js.map +0 -1
  56. package/dist/et-EE.mjs +0 -11
  57. package/dist/et-EE.module.js +0 -11
  58. package/dist/et-EE.module.js.map +0 -1
  59. package/dist/fi-FI.main.js +0 -9
  60. package/dist/fi-FI.main.js.map +0 -1
  61. package/dist/fi-FI.mjs +0 -11
  62. package/dist/fi-FI.module.js +0 -11
  63. package/dist/fi-FI.module.js.map +0 -1
  64. package/dist/fr-FR.main.js +0 -9
  65. package/dist/fr-FR.main.js.map +0 -1
  66. package/dist/fr-FR.mjs +0 -11
  67. package/dist/fr-FR.module.js +0 -11
  68. package/dist/fr-FR.module.js.map +0 -1
  69. package/dist/he-IL.main.js +0 -9
  70. package/dist/he-IL.main.js.map +0 -1
  71. package/dist/he-IL.mjs +0 -11
  72. package/dist/he-IL.module.js +0 -11
  73. package/dist/he-IL.module.js.map +0 -1
  74. package/dist/hr-HR.main.js +0 -9
  75. package/dist/hr-HR.main.js.map +0 -1
  76. package/dist/hr-HR.mjs +0 -11
  77. package/dist/hr-HR.module.js +0 -11
  78. package/dist/hr-HR.module.js.map +0 -1
  79. package/dist/hu-HU.main.js +0 -9
  80. package/dist/hu-HU.main.js.map +0 -1
  81. package/dist/hu-HU.mjs +0 -11
  82. package/dist/hu-HU.module.js +0 -11
  83. package/dist/hu-HU.module.js.map +0 -1
  84. package/dist/intlStrings.main.js +0 -108
  85. package/dist/intlStrings.main.js.map +0 -1
  86. package/dist/intlStrings.mjs +0 -110
  87. package/dist/intlStrings.module.js +0 -110
  88. package/dist/intlStrings.module.js.map +0 -1
  89. package/dist/it-IT.main.js +0 -9
  90. package/dist/it-IT.main.js.map +0 -1
  91. package/dist/it-IT.mjs +0 -11
  92. package/dist/it-IT.module.js +0 -11
  93. package/dist/it-IT.module.js.map +0 -1
  94. package/dist/ja-JP.main.js +0 -9
  95. package/dist/ja-JP.main.js.map +0 -1
  96. package/dist/ja-JP.mjs +0 -11
  97. package/dist/ja-JP.module.js +0 -11
  98. package/dist/ja-JP.module.js.map +0 -1
  99. package/dist/ko-KR.main.js +0 -9
  100. package/dist/ko-KR.main.js.map +0 -1
  101. package/dist/ko-KR.mjs +0 -11
  102. package/dist/ko-KR.module.js +0 -11
  103. package/dist/ko-KR.module.js.map +0 -1
  104. package/dist/lt-LT.main.js +0 -9
  105. package/dist/lt-LT.main.js.map +0 -1
  106. package/dist/lt-LT.mjs +0 -11
  107. package/dist/lt-LT.module.js +0 -11
  108. package/dist/lt-LT.module.js.map +0 -1
  109. package/dist/lv-LV.main.js +0 -9
  110. package/dist/lv-LV.main.js.map +0 -1
  111. package/dist/lv-LV.mjs +0 -11
  112. package/dist/lv-LV.module.js +0 -11
  113. package/dist/lv-LV.module.js.map +0 -1
  114. package/dist/nb-NO.main.js +0 -9
  115. package/dist/nb-NO.main.js.map +0 -1
  116. package/dist/nb-NO.mjs +0 -11
  117. package/dist/nb-NO.module.js +0 -11
  118. package/dist/nb-NO.module.js.map +0 -1
  119. package/dist/nl-NL.main.js +0 -9
  120. package/dist/nl-NL.main.js.map +0 -1
  121. package/dist/nl-NL.mjs +0 -11
  122. package/dist/nl-NL.module.js +0 -11
  123. package/dist/nl-NL.module.js.map +0 -1
  124. package/dist/pl-PL.main.js +0 -9
  125. package/dist/pl-PL.main.js.map +0 -1
  126. package/dist/pl-PL.mjs +0 -11
  127. package/dist/pl-PL.module.js +0 -11
  128. package/dist/pl-PL.module.js.map +0 -1
  129. package/dist/placeholders.main.js +0 -414
  130. package/dist/placeholders.main.js.map +0 -1
  131. package/dist/placeholders.mjs +0 -409
  132. package/dist/placeholders.module.js +0 -409
  133. package/dist/placeholders.module.js.map +0 -1
  134. package/dist/pt-BR.main.js +0 -9
  135. package/dist/pt-BR.main.js.map +0 -1
  136. package/dist/pt-BR.mjs +0 -11
  137. package/dist/pt-BR.module.js +0 -11
  138. package/dist/pt-BR.module.js.map +0 -1
  139. package/dist/pt-PT.main.js +0 -9
  140. package/dist/pt-PT.main.js.map +0 -1
  141. package/dist/pt-PT.mjs +0 -11
  142. package/dist/pt-PT.module.js +0 -11
  143. package/dist/pt-PT.module.js.map +0 -1
  144. package/dist/ro-RO.main.js +0 -9
  145. package/dist/ro-RO.main.js.map +0 -1
  146. package/dist/ro-RO.mjs +0 -11
  147. package/dist/ro-RO.module.js +0 -11
  148. package/dist/ro-RO.module.js.map +0 -1
  149. package/dist/ru-RU.main.js +0 -9
  150. package/dist/ru-RU.main.js.map +0 -1
  151. package/dist/ru-RU.mjs +0 -11
  152. package/dist/ru-RU.module.js +0 -11
  153. package/dist/ru-RU.module.js.map +0 -1
  154. package/dist/sk-SK.main.js +0 -9
  155. package/dist/sk-SK.main.js.map +0 -1
  156. package/dist/sk-SK.mjs +0 -11
  157. package/dist/sk-SK.module.js +0 -11
  158. package/dist/sk-SK.module.js.map +0 -1
  159. package/dist/sl-SI.main.js +0 -9
  160. package/dist/sl-SI.main.js.map +0 -1
  161. package/dist/sl-SI.mjs +0 -11
  162. package/dist/sl-SI.module.js +0 -11
  163. package/dist/sl-SI.module.js.map +0 -1
  164. package/dist/sr-SP.main.js +0 -9
  165. package/dist/sr-SP.main.js.map +0 -1
  166. package/dist/sr-SP.mjs +0 -11
  167. package/dist/sr-SP.module.js +0 -11
  168. package/dist/sr-SP.module.js.map +0 -1
  169. package/dist/sv-SE.main.js +0 -9
  170. package/dist/sv-SE.main.js.map +0 -1
  171. package/dist/sv-SE.mjs +0 -11
  172. package/dist/sv-SE.module.js +0 -11
  173. package/dist/sv-SE.module.js.map +0 -1
  174. package/dist/tr-TR.main.js +0 -9
  175. package/dist/tr-TR.main.js.map +0 -1
  176. package/dist/tr-TR.mjs +0 -11
  177. package/dist/tr-TR.module.js +0 -11
  178. package/dist/tr-TR.module.js.map +0 -1
  179. package/dist/types.d.ts +0 -253
  180. package/dist/types.d.ts.map +0 -1
  181. package/dist/uk-UA.main.js +0 -9
  182. package/dist/uk-UA.main.js.map +0 -1
  183. package/dist/uk-UA.mjs +0 -11
  184. package/dist/uk-UA.module.js +0 -11
  185. package/dist/uk-UA.module.js.map +0 -1
  186. package/dist/useDateFieldState.main.js +0 -377
  187. package/dist/useDateFieldState.main.js.map +0 -1
  188. package/dist/useDateFieldState.mjs +0 -372
  189. package/dist/useDateFieldState.module.js +0 -372
  190. package/dist/useDateFieldState.module.js.map +0 -1
  191. package/dist/useDatePickerState.main.js +0 -141
  192. package/dist/useDatePickerState.main.js.map +0 -1
  193. package/dist/useDatePickerState.mjs +0 -136
  194. package/dist/useDatePickerState.module.js +0 -136
  195. package/dist/useDatePickerState.module.js.map +0 -1
  196. package/dist/useDateRangePickerState.main.js +0 -268
  197. package/dist/useDateRangePickerState.main.js.map +0 -1
  198. package/dist/useDateRangePickerState.mjs +0 -263
  199. package/dist/useDateRangePickerState.module.js +0 -263
  200. package/dist/useDateRangePickerState.module.js.map +0 -1
  201. package/dist/useTimeFieldState.main.js +0 -89
  202. package/dist/useTimeFieldState.main.js.map +0 -1
  203. package/dist/useTimeFieldState.mjs +0 -84
  204. package/dist/useTimeFieldState.module.js +0 -84
  205. package/dist/useTimeFieldState.module.js.map +0 -1
  206. package/dist/utils.main.js +0 -200
  207. package/dist/utils.main.js.map +0 -1
  208. package/dist/utils.mjs +0 -189
  209. package/dist/utils.module.js +0 -189
  210. package/dist/utils.module.js.map +0 -1
  211. package/dist/zh-CN.main.js +0 -9
  212. package/dist/zh-CN.main.js.map +0 -1
  213. package/dist/zh-CN.mjs +0 -11
  214. package/dist/zh-CN.module.js +0 -11
  215. package/dist/zh-CN.module.js.map +0 -1
  216. package/dist/zh-TW.main.js +0 -9
  217. package/dist/zh-TW.main.js.map +0 -1
  218. package/dist/zh-TW.mjs +0 -11
  219. package/dist/zh-TW.module.js +0 -11
  220. package/dist/zh-TW.module.js.map +0 -1
  221. package/src/IncompleteDate.ts +0 -392
  222. package/src/placeholders.ts +0 -109
  223. package/src/useDateFieldState.ts +0 -478
  224. package/src/useDatePickerState.ts +0 -203
  225. package/src/useDateRangePickerState.ts +0 -330
  226. package/src/useTimeFieldState.ts +0 -99
  227. package/src/utils.ts +0 -272
@@ -1,330 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
-
14
- import {DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationalized/date';
15
- import {DateRange, DateRangePickerProps, DateValue, Granularity, MappedDateValue, TimeValue} from '@react-types/datepicker';
16
- import {FieldOptions, FormatterOptions, getFormatOptions, getPlaceholderTime, getRangeValidationResult, useDefaultProps} from './utils';
17
- import {FormValidationState, useFormValidationState} from '@react-stately/form';
18
- import {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';
19
- import {RangeValue, ValidationState} from '@react-types/shared';
20
- import {useControlledState} from '@react-stately/utils';
21
- import {useMemo, useState} from 'react';
22
-
23
- export interface DateRangePickerStateOptions<T extends DateValue = DateValue> extends DateRangePickerProps<T> {
24
- /**
25
- * Determines whether the date picker popover should close automatically when a date is selected.
26
- * @default true
27
- */
28
- shouldCloseOnSelect?: boolean | (() => boolean)
29
- }
30
-
31
- type TimeRange = RangeValue<TimeValue>;
32
- export interface DateRangePickerState extends OverlayTriggerState, FormValidationState {
33
- /** The currently selected date range. */
34
- value: RangeValue<DateValue | null>,
35
- /** The default selected date range. */
36
- defaultValue: DateRange | null,
37
- /** Sets the selected date range. */
38
- setValue(value: DateRange | null): void,
39
- /**
40
- * The date portion of the selected range. This may be set prior to `value` if the user has
41
- * selected a date range but has not yet selected a time range.
42
- */
43
- dateRange: RangeValue<DateValue | null> | null,
44
- /** Sets the date portion of the selected range. */
45
- setDateRange(value: DateRange): void,
46
- /**
47
- * The time portion of the selected range. This may be set prior to `value` if the user has
48
- * selected a time range but has not yet selected a date range.
49
- */
50
- timeRange: RangeValue<TimeValue | null> | null,
51
- /** Sets the time portion of the selected range. */
52
- setTimeRange(value: TimeRange): void,
53
- /** Sets the date portion of either the start or end of the selected range. */
54
- setDate(part: 'start' | 'end', value: DateValue | null): void,
55
- /** Sets the time portion of either the start or end of the selected range. */
56
- setTime(part: 'start' | 'end', value: TimeValue | null): void,
57
- /** Sets the date and time of either the start or end of the selected range. */
58
- setDateTime(part: 'start' | 'end', value: DateValue | null): void,
59
- /** The granularity for the field, based on the `granularity` prop and current value. */
60
- granularity: Granularity,
61
- /** Whether the date range picker supports selecting times, according to the `granularity` prop and current value. */
62
- hasTime: boolean,
63
- /** Whether the calendar popover is currently open. */
64
- isOpen: boolean,
65
- /** Sets whether the calendar popover is open. */
66
- setOpen(isOpen: boolean): void,
67
- /**
68
- * The current validation state of the date range picker, based on the `validationState`, `minValue`, and `maxValue` props.
69
- * @deprecated Use `isInvalid` instead.
70
- */
71
- validationState: ValidationState | null,
72
- /** Whether the date range picker is invalid, based on the `isInvalid`, `minValue`, and `maxValue` props. */
73
- isInvalid: boolean,
74
- /** Formats the selected range using the given options. */
75
- formatValue(locale: string, fieldOptions: FieldOptions): {start: string, end: string} | null,
76
- /** Gets a formatter based on state's props. */
77
- getDateFormatter(locale: string, formatOptions: FormatterOptions): DateFormatter
78
- }
79
-
80
- /**
81
- * Provides state management for a date range picker component.
82
- * A date range picker combines two DateFields and a RangeCalendar popover to allow
83
- * users to enter or select a date and time range.
84
- */
85
- export function useDateRangePickerState<T extends DateValue = DateValue>(props: DateRangePickerStateOptions<T>): DateRangePickerState {
86
- let overlayState = useOverlayTriggerState(props);
87
- let [controlledValue, setControlledValue] = useControlledState<DateRange | null, RangeValue<MappedDateValue<T>> | null>(props.value, props.defaultValue || null, props.onChange);
88
- let [initialValue] = useState(controlledValue);
89
- let [placeholderValue, setPlaceholderValue] = useState<RangeValue<DateValue | null>>(() => controlledValue || {start: null, end: null});
90
-
91
- // Reset the placeholder if the value prop is set to null.
92
- if (controlledValue == null && placeholderValue.start && placeholderValue.end) {
93
- placeholderValue = {start: null, end: null};
94
- setPlaceholderValue(placeholderValue);
95
- }
96
-
97
- let value = controlledValue || placeholderValue;
98
-
99
- let setValue = (newValue: RangeValue<DateValue | null> | null) => {
100
- value = newValue || {start: null, end: null};
101
- setPlaceholderValue(value);
102
- if (isCompleteRange(value)) {
103
- setControlledValue(value);
104
- } else {
105
- setControlledValue(null);
106
- }
107
- };
108
-
109
- let v = (value?.start || value?.end || props.placeholderValue || null);
110
- let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);
111
- let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';
112
- let shouldCloseOnSelect = props.shouldCloseOnSelect ?? true;
113
-
114
- let [dateRange, setSelectedDateRange] = useState<RangeValue<DateValue | null> | null>(null);
115
- let [timeRange, setSelectedTimeRange] = useState<RangeValue<TimeValue | null> | null>(null);
116
-
117
- if (value && isCompleteRange(value)) {
118
- dateRange = value;
119
- if ('hour' in value.start) {
120
- timeRange = value as TimeRange;
121
- }
122
- }
123
-
124
- let commitValue = (dateRange: DateRange, timeRange: TimeRange) => {
125
- setValue({
126
- start: 'timeZone' in timeRange.start ? timeRange.start.set(toCalendarDate(dateRange.start)) : toCalendarDateTime(dateRange.start, timeRange.start),
127
- end: 'timeZone' in timeRange.end ? timeRange.end.set(toCalendarDate(dateRange.end)) : toCalendarDateTime(dateRange.end, timeRange.end)
128
- });
129
- setSelectedDateRange(null);
130
- setSelectedTimeRange(null);
131
- validation.commitValidation();
132
- };
133
-
134
- // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
135
- let setDateRange = (range: RangeValue<DateValue | null>) => {
136
- let shouldClose = typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;
137
- if (hasTime) {
138
- // Set a placeholder time if the popover is closing so we don't leave the field in an incomplete state.
139
- if (isCompleteRange(range) && (shouldClose || (timeRange?.start && timeRange?.end))) {
140
- commitValue(range, {
141
- start: timeRange?.start || getPlaceholderTime(props.placeholderValue),
142
- end: timeRange?.end || getPlaceholderTime(props.placeholderValue)
143
- });
144
- } else {
145
- setSelectedDateRange(range);
146
- }
147
- } else if (isCompleteRange(range)) {
148
- setValue(range);
149
- validation.commitValidation();
150
- } else {
151
- setSelectedDateRange(range);
152
- }
153
-
154
- if (shouldClose) {
155
- overlayState.setOpen(false);
156
- }
157
- };
158
-
159
- let setTimeRange = (range: RangeValue<TimeValue | null>) => {
160
- if (isCompleteRange(dateRange) && isCompleteRange(range)) {
161
- commitValue(dateRange, range);
162
- } else {
163
- setSelectedTimeRange(range);
164
- }
165
- };
166
-
167
- let showEra = (value?.start?.calendar.identifier === 'gregory' && value.start.era === 'BC') || (value?.end?.calendar.identifier === 'gregory' && value.end.era === 'BC');
168
- let formatOpts = useMemo(() => ({
169
- granularity,
170
- timeZone: defaultTimeZone,
171
- hideTimeZone: props.hideTimeZone,
172
- hourCycle: props.hourCycle,
173
- shouldForceLeadingZeros: props.shouldForceLeadingZeros,
174
- showEra
175
- }), [granularity, props.hourCycle, props.shouldForceLeadingZeros, defaultTimeZone, props.hideTimeZone, showEra]);
176
-
177
- let {minValue, maxValue, isDateUnavailable} = props;
178
- let builtinValidation = useMemo(() => getRangeValidationResult(
179
- value,
180
- minValue,
181
- maxValue,
182
- isDateUnavailable,
183
- formatOpts
184
- ), [value, minValue, maxValue, isDateUnavailable, formatOpts]);
185
-
186
- let validation = useFormValidationState({
187
- ...props,
188
- value: controlledValue as RangeValue<MappedDateValue<T>> | null,
189
- name: useMemo(() => [props.startName, props.endName].filter(n => n != null), [props.startName, props.endName]),
190
- builtinValidation
191
- });
192
-
193
- let isValueInvalid = validation.displayValidation.isInvalid;
194
- let validationState: ValidationState | null = props.validationState || (isValueInvalid ? 'invalid' : null);
195
-
196
- return {
197
- ...validation,
198
- value,
199
- defaultValue: props.defaultValue ?? initialValue,
200
- setValue,
201
- dateRange,
202
- timeRange,
203
- granularity,
204
- hasTime,
205
- setDate(part, date) {
206
- if (part === 'start') {
207
- setDateRange({start: date, end: dateRange?.end ?? null});
208
- } else {
209
- setDateRange({start: dateRange?.start ?? null, end: date});
210
- }
211
- },
212
- setTime(part, time) {
213
- if (part === 'start') {
214
- setTimeRange({start: time, end: timeRange?.end ?? null});
215
- } else {
216
- setTimeRange({start: timeRange?.start ?? null, end: time});
217
- }
218
- },
219
- setDateTime(part, dateTime) {
220
- if (part === 'start') {
221
- setValue({start: dateTime, end: value?.end ?? null});
222
- } else {
223
- setValue({start: value?.start ?? null, end: dateTime});
224
- }
225
- },
226
- setDateRange,
227
- setTimeRange,
228
- ...overlayState,
229
- setOpen(isOpen) {
230
- // Commit the selected date range when the calendar is closed. Use a placeholder time if one wasn't set.
231
- // If only the time range was set and not the date range, don't commit. The state will be preserved until
232
- // the user opens the popover again.
233
- if (!isOpen && !(value?.start && value?.end) && isCompleteRange(dateRange) && hasTime) {
234
- commitValue(dateRange, {
235
- start: timeRange?.start || getPlaceholderTime(props.placeholderValue),
236
- end: timeRange?.end || getPlaceholderTime(props.placeholderValue)
237
- });
238
- }
239
-
240
- overlayState.setOpen(isOpen);
241
- },
242
- validationState,
243
- isInvalid: isValueInvalid,
244
- formatValue(locale, fieldOptions) {
245
- if (!value || !value.start || !value.end) {
246
- return null;
247
- }
248
-
249
- let startTimeZone = 'timeZone' in value.start ? value.start.timeZone : undefined;
250
- let startGranularity = props.granularity || (value.start && 'minute' in value.start ? 'minute' : 'day');
251
- let endTimeZone = 'timeZone' in value.end ? value.end.timeZone : undefined;
252
- let endGranularity = props.granularity || (value.end && 'minute' in value.end ? 'minute' : 'day');
253
-
254
- let startOptions = getFormatOptions(fieldOptions, {
255
- granularity: startGranularity,
256
- timeZone: startTimeZone,
257
- hideTimeZone: props.hideTimeZone,
258
- hourCycle: props.hourCycle,
259
- showEra: (value.start.calendar.identifier === 'gregory' && value.start.era === 'BC') ||
260
- (value.end.calendar.identifier === 'gregory' && value.end.era === 'BC')
261
- });
262
-
263
- let startDate = value.start.toDate(startTimeZone || 'UTC');
264
- let endDate = value.end.toDate(endTimeZone || 'UTC');
265
-
266
- let startFormatter = new DateFormatter(locale, startOptions);
267
- let endFormatter: Intl.DateTimeFormat;
268
- if (startTimeZone === endTimeZone && startGranularity === endGranularity && value.start.compare(value.end) !== 0) {
269
- // Use formatRange, as it results in shorter output when some of the fields
270
- // are shared between the start and end dates (e.g. the same month).
271
- // Formatting will fail if the end date is before the start date. Fall back below when that happens.
272
- try {
273
- let parts = startFormatter.formatRangeToParts(startDate, endDate);
274
-
275
- // Find the separator between the start and end date. This is determined
276
- // by finding the last shared literal before the end range.
277
- let separatorIndex = -1;
278
- for (let i = 0; i < parts.length; i++) {
279
- let part = parts[i];
280
- if (part.source === 'shared' && part.type === 'literal') {
281
- separatorIndex = i;
282
- } else if (part.source === 'endRange') {
283
- break;
284
- }
285
- }
286
-
287
- // Now we can combine the parts into start and end strings.
288
- let start = '';
289
- let end = '';
290
- for (let i = 0; i < parts.length; i++) {
291
- if (i < separatorIndex) {
292
- start += parts[i].value;
293
- } else if (i > separatorIndex) {
294
- end += parts[i].value;
295
- }
296
- }
297
-
298
- return {start, end};
299
- } catch {
300
- // ignore
301
- }
302
-
303
- endFormatter = startFormatter;
304
- } else {
305
- let endOptions = getFormatOptions(fieldOptions, {
306
- granularity: endGranularity,
307
- timeZone: endTimeZone,
308
- hideTimeZone: props.hideTimeZone,
309
- hourCycle: props.hourCycle
310
- });
311
-
312
- endFormatter = new DateFormatter(locale, endOptions);
313
- }
314
-
315
- return {
316
- start: startFormatter.format(startDate),
317
- end: endFormatter.format(endDate)
318
- };
319
- },
320
- getDateFormatter(locale, formatOptions: FormatterOptions) {
321
- let newOptions = {...formatOpts, ...formatOptions};
322
- let newFormatOptions = getFormatOptions({}, newOptions);
323
- return new DateFormatter(locale, newFormatOptions);
324
- }
325
- };
326
- }
327
-
328
- function isCompleteRange<T>(value: RangeValue<T | null> | null): value is RangeValue<T> {
329
- return value?.start != null && value.end != null;
330
- }
@@ -1,99 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the 'License');
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {DateFieldState, useDateFieldState} from '.';
14
- import {DateValue, MappedTimeValue, TimePickerProps, TimeValue} from '@react-types/datepicker';
15
- import {getLocalTimeZone, GregorianCalendar, Time, toCalendarDateTime, today, toTime, toZoned} from '@internationalized/date';
16
- import {useCallback, useMemo} from 'react';
17
- import {useControlledState} from '@react-stately/utils';
18
-
19
- export interface TimeFieldStateOptions<T extends TimeValue = TimeValue> extends TimePickerProps<T> {
20
- /** The locale to display and edit the value according to. */
21
- locale: string
22
- }
23
-
24
- export interface TimeFieldState extends DateFieldState {
25
- /** The current time value. */
26
- timeValue: Time
27
- }
28
-
29
- /**
30
- * Provides state management for a time field component.
31
- * A time field allows users to enter and edit time values using a keyboard.
32
- * Each part of a time value is displayed in an individually editable segment.
33
- */
34
- export function useTimeFieldState<T extends TimeValue = TimeValue>(props: TimeFieldStateOptions<T>): TimeFieldState {
35
- let {
36
- placeholderValue = new Time(),
37
- minValue,
38
- maxValue,
39
- defaultValue,
40
- granularity,
41
- validate
42
- } = props;
43
-
44
- let [value, setValue] = useControlledState<TimeValue | null, MappedTimeValue<T> | null>(
45
- props.value,
46
- defaultValue ?? null,
47
- props.onChange
48
- );
49
-
50
- let v = value || placeholderValue;
51
- let day = v && 'day' in v ? v : undefined;
52
- let defaultValueTimeZone = defaultValue && 'timeZone' in defaultValue ? defaultValue.timeZone : undefined;
53
- let placeholderDate = useMemo(() => {
54
- let valueTimeZone = v && 'timeZone' in v ? v.timeZone : undefined;
55
-
56
- return (valueTimeZone || defaultValueTimeZone) && placeholderValue ? toZoned(convertValue(placeholderValue)!, valueTimeZone || defaultValueTimeZone!) : convertValue(placeholderValue);
57
- }, [placeholderValue, v, defaultValueTimeZone]);
58
- let minDate = useMemo(() => convertValue(minValue, day), [minValue, day]);
59
- let maxDate = useMemo(() => convertValue(maxValue, day), [maxValue, day]);
60
-
61
- let timeValue = useMemo(() => value && 'day' in value ? toTime(value) : value as Time, [value]);
62
- let dateTime = useMemo(() => value == null ? null : convertValue(value), [value]);
63
- let defaultDateTime = useMemo(() => defaultValue == null ? null : convertValue(defaultValue), [defaultValue]);
64
- let onChange = newValue => {
65
- setValue(day || defaultValueTimeZone ? newValue : newValue && toTime(newValue));
66
- };
67
-
68
- let state = useDateFieldState({
69
- ...props,
70
- value: dateTime,
71
- defaultValue: defaultDateTime,
72
- minValue: minDate,
73
- maxValue: maxDate,
74
- onChange,
75
- granularity: granularity || 'minute',
76
- maxGranularity: 'hour',
77
- placeholderValue: placeholderDate ?? undefined,
78
- // Calendar should not matter for time fields.
79
- createCalendar: () => new GregorianCalendar(),
80
- validate: useCallback(() => validate?.(value as any), [validate, value])
81
- });
82
-
83
- return {
84
- ...state,
85
- timeValue
86
- };
87
- }
88
-
89
- function convertValue(value: TimeValue | null | undefined, date: DateValue = today(getLocalTimeZone())) {
90
- if (!value) {
91
- return null;
92
- }
93
-
94
- if ('day' in value) {
95
- return value;
96
- }
97
-
98
- return toCalendarDateTime(date, value);
99
- }
package/src/utils.ts DELETED
@@ -1,272 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {Calendar, DateFormatter, getLocalTimeZone, now, Time, toCalendar, toCalendarDate, toCalendarDateTime} from '@internationalized/date';
14
- import {DatePickerProps, DateValue, Granularity, TimeValue} from '@react-types/datepicker';
15
- // @ts-ignore
16
- import i18nMessages from '../intl/*.json';
17
- import {LocalizedStringDictionary, LocalizedStringFormatter} from '@internationalized/string';
18
- import {mergeValidation, VALID_VALIDITY_STATE} from '@react-stately/form';
19
- import {RangeValue, ValidationResult} from '@react-types/shared';
20
- import {useState} from 'react';
21
-
22
- const dictionary = new LocalizedStringDictionary(i18nMessages);
23
-
24
- function getLocale() {
25
- // Match browser language setting here, NOT react-aria's I18nProvider, so that we match other browser-provided
26
- // validation messages, which to not respect our provider's language.
27
- // @ts-ignore
28
- let locale = typeof navigator !== 'undefined' && (navigator.language || navigator.userLanguage) || 'en-US';
29
-
30
- try {
31
- Intl.DateTimeFormat.supportedLocalesOf([locale]);
32
- } catch {
33
- locale = 'en-US';
34
- }
35
- return locale;
36
- }
37
-
38
- export function getValidationResult(
39
- value: DateValue | null,
40
- minValue: DateValue | null | undefined,
41
- maxValue: DateValue | null | undefined,
42
- isDateUnavailable: ((v: DateValue) => boolean) | undefined,
43
- options: FormatterOptions
44
- ): ValidationResult {
45
- let rangeOverflow = value != null && maxValue != null && value.compare(maxValue) > 0;
46
- let rangeUnderflow = value != null && minValue != null && value.compare(minValue) < 0;
47
- let isUnavailable = (value != null && isDateUnavailable?.(value)) || false;
48
- let isInvalid = rangeOverflow || rangeUnderflow || isUnavailable;
49
- let errors: string[] = [];
50
-
51
- if (isInvalid) {
52
- let locale = getLocale();
53
- let strings = LocalizedStringDictionary.getGlobalDictionaryForPackage('@react-stately/datepicker') || dictionary;
54
- let formatter = new LocalizedStringFormatter(locale, strings);
55
- let dateFormatter = new DateFormatter(locale, getFormatOptions({}, options));
56
- let timeZone = dateFormatter.resolvedOptions().timeZone;
57
-
58
- if (rangeUnderflow && minValue != null) {
59
- errors.push(formatter.format('rangeUnderflow', {minValue: dateFormatter.format(minValue.toDate(timeZone))}));
60
- }
61
-
62
- if (rangeOverflow && maxValue != null) {
63
- errors.push(formatter.format('rangeOverflow', {maxValue: dateFormatter.format(maxValue.toDate(timeZone))}));
64
- }
65
-
66
- if (isUnavailable) {
67
- errors.push(formatter.format('unavailableDate'));
68
- }
69
- }
70
-
71
- return {
72
- isInvalid,
73
- validationErrors: errors,
74
- validationDetails: {
75
- badInput: isUnavailable,
76
- customError: false,
77
- patternMismatch: false,
78
- rangeOverflow,
79
- rangeUnderflow,
80
- stepMismatch: false,
81
- tooLong: false,
82
- tooShort: false,
83
- typeMismatch: false,
84
- valueMissing: false,
85
- valid: !isInvalid
86
- }
87
- };
88
- }
89
-
90
- export function getRangeValidationResult(
91
- value: RangeValue<DateValue | null> | null,
92
- minValue: DateValue | null | undefined,
93
- maxValue: DateValue | null | undefined,
94
- isDateUnavailable: ((v: DateValue) => boolean) | undefined,
95
- options: FormatterOptions
96
- ): ValidationResult {
97
- let startValidation = getValidationResult(
98
- value?.start ?? null,
99
- minValue,
100
- maxValue,
101
- isDateUnavailable,
102
- options
103
- );
104
-
105
- let endValidation = getValidationResult(
106
- value?.end ?? null,
107
- minValue,
108
- maxValue,
109
- isDateUnavailable,
110
- options
111
- );
112
-
113
- let result = mergeValidation(startValidation, endValidation);
114
- if (value?.end != null && value.start != null && value.end.compare(value.start) < 0) {
115
- let strings = LocalizedStringDictionary.getGlobalDictionaryForPackage('@react-stately/datepicker') || dictionary;
116
- result = mergeValidation(result, {
117
- isInvalid: true,
118
- validationErrors: [strings.getStringForLocale('rangeReversed', getLocale())],
119
- validationDetails: {
120
- ...VALID_VALIDITY_STATE,
121
- rangeUnderflow: true,
122
- rangeOverflow: true,
123
- valid: false
124
- }
125
- });
126
- }
127
-
128
- return result;
129
- }
130
-
131
- export type FieldOptions = Pick<Intl.DateTimeFormatOptions, 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second'>;
132
- export interface FormatterOptions {
133
- timeZone?: string,
134
- hideTimeZone?: boolean,
135
- granularity?: DatePickerProps<any>['granularity'],
136
- maxGranularity?: 'year' | 'month' | DatePickerProps<any>['granularity'],
137
- hourCycle?: 12 | 24,
138
- showEra?: boolean,
139
- shouldForceLeadingZeros?: boolean
140
- }
141
-
142
- const DEFAULT_FIELD_OPTIONS: FieldOptions = {
143
- year: 'numeric',
144
- month: 'numeric',
145
- day: 'numeric',
146
- hour: 'numeric',
147
- minute: '2-digit',
148
- second: '2-digit'
149
- };
150
-
151
- const TWO_DIGIT_FIELD_OPTIONS: FieldOptions = {
152
- year: 'numeric',
153
- month: '2-digit',
154
- day: '2-digit',
155
- hour: '2-digit',
156
- minute: '2-digit',
157
- second: '2-digit'
158
- };
159
-
160
- export function getFormatOptions(
161
- fieldOptions: FieldOptions,
162
- options: FormatterOptions
163
- ): Intl.DateTimeFormatOptions {
164
- let defaultFieldOptions = options.shouldForceLeadingZeros ? TWO_DIGIT_FIELD_OPTIONS : DEFAULT_FIELD_OPTIONS;
165
- fieldOptions = {...defaultFieldOptions, ...fieldOptions};
166
- let granularity = options.granularity || 'minute';
167
- let keys = Object.keys(fieldOptions);
168
- let startIdx = keys.indexOf(options.maxGranularity ?? 'year');
169
- if (startIdx < 0) {
170
- startIdx = 0;
171
- }
172
-
173
- let endIdx = keys.indexOf(granularity);
174
- if (endIdx < 0) {
175
- endIdx = 2;
176
- }
177
-
178
- if (startIdx > endIdx) {
179
- throw new Error('maxGranularity must be greater than granularity');
180
- }
181
-
182
- let opts: Intl.DateTimeFormatOptions = keys.slice(startIdx, endIdx + 1).reduce((opts, key) => {
183
- opts[key] = fieldOptions[key];
184
- return opts;
185
- }, {});
186
-
187
- if (options.hourCycle != null) {
188
- opts.hour12 = options.hourCycle === 12;
189
- }
190
-
191
- opts.timeZone = options.timeZone || 'UTC';
192
-
193
- let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';
194
- if (hasTime && options.timeZone && !options.hideTimeZone) {
195
- opts.timeZoneName = 'short';
196
- }
197
-
198
- if (options.showEra && startIdx === 0) {
199
- opts.era = 'short';
200
- }
201
-
202
- return opts;
203
- }
204
-
205
- export function getPlaceholderTime(placeholderValue: DateValue | null | undefined): TimeValue {
206
- if (placeholderValue && 'hour' in placeholderValue) {
207
- return placeholderValue;
208
- }
209
-
210
- return new Time();
211
- }
212
-
213
- export function convertValue(value: DateValue | null | undefined, calendar: Calendar): DateValue | null | undefined {
214
- if (value === null) {
215
- return null;
216
- }
217
-
218
- if (!value) {
219
- return undefined;
220
- }
221
-
222
- return toCalendar(value, calendar);
223
- }
224
-
225
-
226
- export function createPlaceholderDate(placeholderValue: DateValue | null | undefined, granularity: string, calendar: Calendar, timeZone: string | undefined): DateValue {
227
- if (placeholderValue) {
228
- return convertValue(placeholderValue, calendar)!;
229
- }
230
-
231
- let date = toCalendar(now(timeZone ?? getLocalTimeZone()).set({
232
- hour: 0,
233
- minute: 0,
234
- second: 0,
235
- millisecond: 0
236
- }), calendar);
237
-
238
- if (granularity === 'year' || granularity === 'month' || granularity === 'day') {
239
- return toCalendarDate(date);
240
- }
241
-
242
- if (!timeZone) {
243
- return toCalendarDateTime(date);
244
- }
245
-
246
- return date;
247
- }
248
-
249
- export function useDefaultProps(v: DateValue | null, granularity: Granularity | undefined): [Granularity, string | undefined] {
250
- // Compute default granularity and time zone from the value. If the value becomes null, keep the last values.
251
- let defaultTimeZone = (v && 'timeZone' in v ? v.timeZone : undefined);
252
- let defaultGranularity: Granularity = (v && 'minute' in v ? 'minute' : 'day');
253
-
254
- // props.granularity must actually exist in the value if one is provided.
255
- if (v && granularity && !(granularity in v)) {
256
- throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
257
- }
258
-
259
- let [lastValue, setLastValue] = useState<[Granularity, string | undefined]>([defaultGranularity, defaultTimeZone]);
260
-
261
- // If the granularity or time zone changed, update the last value.
262
- if (v && (lastValue[0] !== defaultGranularity || lastValue[1] !== defaultTimeZone)) {
263
- setLastValue([defaultGranularity, defaultTimeZone]);
264
- }
265
-
266
- if (!granularity) {
267
- granularity = v ? defaultGranularity : lastValue[0];
268
- }
269
-
270
- let timeZone = v ? defaultTimeZone : lastValue[1];
271
- return [granularity, timeZone];
272
- }