@react-stately/datepicker 3.16.1 → 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 -419
  130. package/dist/placeholders.main.js.map +0 -1
  131. package/dist/placeholders.mjs +0 -414
  132. package/dist/placeholders.module.js +0 -414
  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 -110
  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,478 +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, CalendarIdentifier, DateFormatter, GregorianCalendar, isEqualCalendar, toCalendar} from '@internationalized/date';
14
- import {convertValue, createPlaceholderDate, FieldOptions, FormatterOptions, getFormatOptions, getValidationResult, useDefaultProps} from './utils';
15
- import {DatePickerProps, DateValue, Granularity, MappedDateValue} from '@react-types/datepicker';
16
- import {FormValidationState, useFormValidationState} from '@react-stately/form';
17
- import {getPlaceholder} from './placeholders';
18
- import {IncompleteDate} from './IncompleteDate';
19
- import {NumberFormatter} from '@internationalized/number';
20
- import {useControlledState} from '@react-stately/utils';
21
- import {useMemo, useState} from 'react';
22
- import {ValidationState} from '@react-types/shared';
23
-
24
- export type SegmentType = 'era' | 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second' | 'dayPeriod' | 'literal' | 'timeZoneName';
25
- export interface DateSegment {
26
- /** The type of segment. */
27
- type: SegmentType,
28
- /** The formatted text for the segment. */
29
- text: string,
30
- /** The numeric value for the segment, if applicable. */
31
- value?: number | null,
32
- /** The minimum numeric value for the segment, if applicable. */
33
- minValue?: number,
34
- /** The maximum numeric value for the segment, if applicable. */
35
- maxValue?: number,
36
- /** Whether the value is a placeholder. */
37
- isPlaceholder: boolean,
38
- /** A placeholder string for the segment. */
39
- placeholder: string,
40
- /** Whether the segment is editable. */
41
- isEditable: boolean
42
- }
43
-
44
- export interface DateFieldState extends FormValidationState {
45
- /** The current field value. */
46
- value: DateValue | null,
47
- /** The default field value. */
48
- defaultValue: DateValue | null,
49
- /** The current value, converted to a native JavaScript `Date` object. */
50
- dateValue: Date,
51
- /** The calendar system currently in use. */
52
- calendar: Calendar,
53
- /** Sets the field's value. */
54
- setValue(value: DateValue | null): void,
55
- /** A list of segments for the current value. */
56
- segments: DateSegment[],
57
- /** A date formatter configured for the current locale and format. */
58
- dateFormatter: DateFormatter,
59
- /**
60
- * The current validation state of the date field, based on the `validationState`, `minValue`, and `maxValue` props.
61
- * @deprecated Use `isInvalid` instead.
62
- */
63
- validationState: ValidationState | null,
64
- /** Whether the date field is invalid, based on the `isInvalid`, `minValue`, and `maxValue` props. */
65
- isInvalid: boolean,
66
- /** The granularity for the field, based on the `granularity` prop and current value. */
67
- granularity: Granularity,
68
- /** The maximum date or time unit that is displayed in the field. */
69
- maxGranularity: 'year' | 'month' | Granularity,
70
- /** Whether the field is disabled. */
71
- isDisabled: boolean,
72
- /** Whether the field is read only. */
73
- isReadOnly: boolean,
74
- /** Whether the field is required. */
75
- isRequired: boolean,
76
- /** Increments the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
77
- increment(type: SegmentType): void,
78
- /** Decrements the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
79
- decrement(type: SegmentType): void,
80
- /**
81
- * Increments the given segment by a larger amount, rounding it to the nearest increment.
82
- * The amount to increment by depends on the field, for example 15 minutes, 7 days, and 5 years.
83
- * Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
84
- */
85
- incrementPage(type: SegmentType): void,
86
- /**
87
- * Decrements the given segment by a larger amount, rounding it to the nearest increment.
88
- * The amount to decrement by depends on the field, for example 15 minutes, 7 days, and 5 years.
89
- * Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
90
- */
91
- decrementPage(type: SegmentType): void,
92
- /** Increments the given segment to its maxiumum value. */
93
- incrementToMax(type: SegmentType): void,
94
- /** Decrements the given segment to its minimum value. */
95
- decrementToMin(type: SegmentType): void,
96
- /** Sets the value of the given segment. */
97
- setSegment(type: 'era', value: string): void,
98
- setSegment(type: SegmentType, value: number): void,
99
- /** Updates the remaining unfilled segments with the placeholder value. */
100
- confirmPlaceholder(): void,
101
- /** Clears the value of the given segment, reverting it to the placeholder. */
102
- clearSegment(type: SegmentType): void,
103
- /** Formats the current date value using the given options. */
104
- formatValue(fieldOptions: FieldOptions): string,
105
- /** Gets a formatter based on state's props. */
106
- getDateFormatter(locale: string, formatOptions: FormatterOptions): DateFormatter
107
- }
108
-
109
- const EDITABLE_SEGMENTS = {
110
- year: true,
111
- month: true,
112
- day: true,
113
- hour: true,
114
- minute: true,
115
- second: true,
116
- dayPeriod: true,
117
- era: true
118
- };
119
-
120
- const PAGE_STEP = {
121
- year: 5,
122
- month: 2,
123
- day: 7,
124
- hour: 2,
125
- minute: 15,
126
- second: 15
127
- };
128
-
129
- const TYPE_MAPPING = {
130
- // Node seems to convert everything to lowercase...
131
- dayperiod: 'dayPeriod',
132
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts#named_years
133
- relatedYear: 'year',
134
- yearName: 'literal', // not editable
135
- unknown: 'literal'
136
- };
137
-
138
- export interface DateFieldStateOptions<T extends DateValue = DateValue> extends DatePickerProps<T> {
139
- /**
140
- * The maximum unit to display in the date field.
141
- * @default 'year'
142
- */
143
- maxGranularity?: 'year' | 'month' | Granularity,
144
- /** The locale to display and edit the value according to. */
145
- locale: string,
146
- /**
147
- * A function that creates a [Calendar](../internationalized/date/Calendar.html)
148
- * object for a given calendar identifier. Such a function may be imported from the
149
- * `@internationalized/date` package, or manually implemented to include support for
150
- * only certain calendars.
151
- */
152
- createCalendar: (name: CalendarIdentifier) => Calendar
153
- }
154
-
155
- /**
156
- * Provides state management for a date field component.
157
- * A date field allows users to enter and edit date and time values using a keyboard.
158
- * Each part of a date value is displayed in an individually editable segment.
159
- */
160
- export function useDateFieldState<T extends DateValue = DateValue>(props: DateFieldStateOptions<T>): DateFieldState {
161
- let {
162
- locale,
163
- createCalendar,
164
- hideTimeZone,
165
- isDisabled = false,
166
- isReadOnly = false,
167
- isRequired = false,
168
- minValue,
169
- maxValue,
170
- isDateUnavailable
171
- } = props;
172
-
173
- let v: DateValue | null = props.value || props.defaultValue || props.placeholderValue || null;
174
- let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);
175
- let timeZone = defaultTimeZone || 'UTC';
176
-
177
- // props.granularity must actually exist in the value if one is provided.
178
- if (v && !(granularity in v)) {
179
- throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
180
- }
181
-
182
- // Resolve default hour cycle and calendar system.
183
- let [calendar, hourCycle] = useMemo(() => {
184
- let formatter = new DateFormatter(locale, {
185
- dateStyle: 'short',
186
- timeStyle: 'short',
187
- hour12: props.hourCycle != null ? props.hourCycle === 12 : undefined
188
- });
189
- let opts = formatter.resolvedOptions();
190
- let calendar = createCalendar(opts.calendar as CalendarIdentifier);
191
- return [calendar, opts.hourCycle!];
192
- }, [locale, props.hourCycle, createCalendar]);
193
-
194
- let [value, setDate] = useControlledState<DateValue | null, MappedDateValue<T> | null>(
195
- props.value,
196
- props.defaultValue ?? null,
197
- props.onChange
198
- );
199
-
200
- let [initialValue] = useState(value);
201
- let calendarValue = useMemo(() => convertValue(value, calendar) ?? null, [value, calendar]);
202
- let [displayValue, setDisplayValue] = useState(
203
- () => new IncompleteDate(calendar, hourCycle, calendarValue)
204
- );
205
-
206
- let showEra = calendar.identifier === 'gregory' && displayValue.era === 'BC';
207
- let formatOpts = useMemo(() => ({
208
- granularity,
209
- maxGranularity: props.maxGranularity ?? 'year',
210
- timeZone: defaultTimeZone,
211
- hideTimeZone,
212
- hourCycle: props.hourCycle,
213
- showEra,
214
- shouldForceLeadingZeros: props.shouldForceLeadingZeros
215
- }), [props.maxGranularity, granularity, props.hourCycle, props.shouldForceLeadingZeros, defaultTimeZone, hideTimeZone, showEra]);
216
- let opts = useMemo(() => getFormatOptions({}, formatOpts), [formatOpts]);
217
-
218
- let dateFormatter = useMemo(() => new DateFormatter(locale, opts), [locale, opts]);
219
- let resolvedOptions = useMemo(() => dateFormatter.resolvedOptions(), [dateFormatter]);
220
- let placeholder = useMemo(() => createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone), [props.placeholderValue, granularity, calendar, defaultTimeZone]);
221
- let displaySegments = useMemo(() => {
222
- let is12HourClock = hourCycle === 'h11' || hourCycle === 'h12';
223
- let segments: SegmentType[] = ['era', 'year', 'month', 'day', 'hour', ...(is12HourClock ? ['dayPeriod' as const] : []), 'minute', 'second'];
224
- let minIndex = segments.indexOf(props.maxGranularity || 'era');
225
- let maxIndex = segments.indexOf(granularity === 'hour' && is12HourClock ? 'dayPeriod' : granularity);
226
- return segments.slice(minIndex, maxIndex + 1);
227
- }, [props.maxGranularity, granularity, hourCycle]);
228
-
229
- let [lastValue, setLastValue] = useState(calendarValue);
230
- let [lastCalendar, setLastCalendar] = useState(calendar);
231
- let [lastHourCycle, setLastHourCycle] = useState(hourCycle);
232
- if (calendarValue !== lastValue || hourCycle !== lastHourCycle || !isEqualCalendar(calendar, lastCalendar)) {
233
- displayValue = new IncompleteDate(calendar, hourCycle, calendarValue);
234
- setLastValue(calendarValue);
235
- setLastCalendar(calendar);
236
- setLastHourCycle(hourCycle);
237
- setDisplayValue(displayValue);
238
- }
239
-
240
- let setValue = (newValue: DateValue | IncompleteDate | null) => {
241
- if (props.isDisabled || props.isReadOnly) {
242
- return;
243
- }
244
-
245
- if (newValue == null || (newValue instanceof IncompleteDate && newValue.isCleared(displaySegments))) {
246
- setDisplayValue(new IncompleteDate(calendar, hourCycle, calendarValue));
247
- setDate(null);
248
- } else if (!(newValue instanceof IncompleteDate)) {
249
- // The display calendar should not have any effect on the emitted value.
250
- // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
251
- newValue = toCalendar(newValue, v?.calendar || new GregorianCalendar());
252
- setDisplayValue(new IncompleteDate(calendar, hourCycle, calendarValue));
253
- setDate(newValue);
254
- } else {
255
- // If the new value is complete and valid, trigger onChange eagerly.
256
- // If it represents an incomplete or invalid value (e.g. February 30th),
257
- // wait until the field is blurred to trigger onChange.
258
- if (newValue.isComplete(displaySegments)) {
259
- let dateValue = newValue.toValue(calendarValue ?? placeholder);
260
- if (newValue.validate(dateValue, displaySegments)) {
261
- let newDateValue = toCalendar(dateValue, v?.calendar || new GregorianCalendar());
262
- if (!value || newDateValue.compare(value) !== 0) {
263
- setDisplayValue(new IncompleteDate(calendar, hourCycle, calendarValue)); // reset in case prop isn't updated
264
- setDate(newDateValue);
265
- return;
266
- }
267
- }
268
- }
269
-
270
- // Incomplete/invalid value. Set temporary display override.
271
- setDisplayValue(newValue);
272
- }
273
- };
274
-
275
- let dateValue = useMemo(() => {
276
- let v = displayValue.toValue(calendarValue ?? placeholder);
277
- return v.toDate(timeZone);
278
- }, [displayValue, timeZone, calendarValue, placeholder]);
279
-
280
- let segments = useMemo(
281
- () => processSegments(dateValue, displayValue, dateFormatter, resolvedOptions, calendar, locale, granularity),
282
- [dateValue, dateFormatter, resolvedOptions, displayValue, calendar, locale, granularity]
283
- );
284
-
285
- let adjustSegment = (type: SegmentType, amount: number) => {
286
- setValue(displayValue.cycle(type, amount, placeholder, displaySegments));
287
- };
288
-
289
- let builtinValidation = useMemo(() => getValidationResult(
290
- value,
291
- minValue,
292
- maxValue,
293
- isDateUnavailable,
294
- formatOpts
295
- ), [value, minValue, maxValue, isDateUnavailable, formatOpts]);
296
-
297
- let validation = useFormValidationState({
298
- ...props,
299
- value: value as MappedDateValue<T> | null,
300
- builtinValidation
301
- });
302
-
303
- let isValueInvalid = validation.displayValidation.isInvalid;
304
- let validationState: ValidationState | null = props.validationState || (isValueInvalid ? 'invalid' : null);
305
-
306
- return {
307
- ...validation,
308
- value: calendarValue,
309
- defaultValue: props.defaultValue ?? initialValue,
310
- dateValue,
311
- calendar,
312
- setValue,
313
- segments,
314
- dateFormatter,
315
- validationState,
316
- isInvalid: isValueInvalid,
317
- granularity,
318
- maxGranularity: props.maxGranularity ?? 'year',
319
- isDisabled,
320
- isReadOnly,
321
- isRequired,
322
- increment(part) {
323
- adjustSegment(part, 1);
324
- },
325
- decrement(part) {
326
- adjustSegment(part, -1);
327
- },
328
- incrementPage(part) {
329
- adjustSegment(part, PAGE_STEP[part] || 1);
330
- },
331
- decrementPage(part) {
332
- adjustSegment(part, -(PAGE_STEP[part] || 1));
333
- },
334
- incrementToMax(part) {
335
- let maxValue = part === 'hour' && hourCycle === 'h12'
336
- ? 11
337
- : displayValue.getSegmentLimits(part)!.maxValue;
338
- setValue(displayValue.set(part, maxValue, placeholder));
339
- },
340
- decrementToMin(part) {
341
- let minValue = part === 'hour' && hourCycle === 'h12'
342
- ? 12
343
- : displayValue.getSegmentLimits(part)!.minValue;
344
- setValue(displayValue.set(part, minValue, placeholder));
345
- },
346
- setSegment(part, v: string | number) {
347
- setValue(displayValue.set(part, v, placeholder));
348
- },
349
- confirmPlaceholder() {
350
- if (props.isDisabled || props.isReadOnly) {
351
- return;
352
- }
353
-
354
- // If the display value is complete but invalid, we need to constrain it and emit onChange on blur.
355
- if (displayValue.isComplete(displaySegments)) {
356
- let dateValue = displayValue.toValue(calendarValue ?? placeholder);
357
- let newDateValue = toCalendar(dateValue, v?.calendar || new GregorianCalendar());
358
- if (!value || newDateValue.compare(value) !== 0) {
359
- setDate(newDateValue);
360
- }
361
- setDisplayValue(new IncompleteDate(calendar, hourCycle, calendarValue));
362
- }
363
- },
364
- clearSegment(part) {
365
- let value = displayValue;
366
-
367
- if (part !== 'timeZoneName' && part !== 'literal') {
368
- value = displayValue.clear(part);
369
- }
370
-
371
- setValue(value);
372
- },
373
- formatValue(fieldOptions: FieldOptions) {
374
- if (!calendarValue) {
375
- return '';
376
- }
377
-
378
- let formatOptions = getFormatOptions(fieldOptions, formatOpts);
379
- let formatter = new DateFormatter(locale, formatOptions);
380
- return formatter.format(dateValue);
381
- },
382
- getDateFormatter(locale, formatOptions: FormatterOptions) {
383
- let newOptions = {...formatOpts, ...formatOptions};
384
- let newFormatOptions = getFormatOptions({}, newOptions);
385
- return new DateFormatter(locale, newFormatOptions);
386
- }
387
- };
388
- }
389
-
390
- function processSegments(
391
- dateValue: Date,
392
- displayValue: IncompleteDate,
393
- dateFormatter: Intl.DateTimeFormat,
394
- resolvedOptions: Intl.ResolvedDateTimeFormatOptions,
395
- calendar: Calendar,
396
- locale: string,
397
- granularity: Granularity
398
- ) : DateSegment[] {
399
- let timeValue = ['hour', 'minute', 'second'];
400
- let segments = dateFormatter.formatToParts(dateValue);
401
-
402
- // In order to allow formatting temporarily invalid dates during editing (e.g. February 30th),
403
- // use a NumberFormatter to manually format segments directly from raw numbers.
404
- // When the user blurs the date field, the invalid segments will be constrained.
405
- let numberFormatter = new NumberFormatter(locale, {useGrouping: false});
406
- let twoDigitFormatter = new NumberFormatter(locale, {useGrouping: false, minimumIntegerDigits: 2});
407
- for (let segment of segments) {
408
- if (segment.type === 'year' || segment.type === 'month' || segment.type === 'day' || segment.type === 'hour') {
409
- let value = displayValue[segment.type] ?? 0;
410
- if (resolvedOptions[segment.type] === '2-digit') {
411
- segment.value = twoDigitFormatter.format(value);
412
- } else {
413
- segment.value = numberFormatter.format(value);
414
- }
415
- }
416
- }
417
-
418
- let processedSegments: DateSegment[] = [];
419
- for (let segment of segments) {
420
- let type = TYPE_MAPPING[segment.type] || segment.type;
421
- let isEditable = EDITABLE_SEGMENTS[type];
422
- if (type === 'era' && calendar.getEras().length === 1) {
423
- isEditable = false;
424
- }
425
-
426
- let isPlaceholder = EDITABLE_SEGMENTS[type] && displayValue[segment.type] == null;
427
- let placeholder = EDITABLE_SEGMENTS[type] ? getPlaceholder(type, segment.value, locale) : null;
428
-
429
- let dateSegment = {
430
- type,
431
- text: isPlaceholder ? placeholder : segment.value,
432
- ...displayValue.getSegmentLimits(type),
433
- isPlaceholder,
434
- placeholder,
435
- isEditable
436
- } as DateSegment;
437
-
438
- // There is an issue in RTL languages where time fields render (minute:hour) instead of (hour:minute).
439
- // To force an LTR direction on the time field since, we wrap the time segments in LRI (left-to-right) isolate unicode. See https://www.w3.org/International/questions/qa-bidi-unicode-controls.
440
- // These unicode characters will be added to the array of processed segments as literals and will mark the start and end of the embedded direction change.
441
- if (type === 'hour') {
442
- // This marks the start of the embedded direction change.
443
- processedSegments.push({
444
- type: 'literal',
445
- text: '\u2066',
446
- isPlaceholder: false,
447
- placeholder: '',
448
- isEditable: false
449
- });
450
- processedSegments.push(dateSegment);
451
- // This marks the end of the embedded direction change in the case that the granularity it set to "hour".
452
- if (type === granularity) {
453
- processedSegments.push({
454
- type: 'literal',
455
- text: '\u2069',
456
- isPlaceholder: false,
457
- placeholder: '',
458
- isEditable: false
459
- });
460
- }
461
- } else if (timeValue.includes(type) && type === granularity) {
462
- processedSegments.push(dateSegment);
463
- // This marks the end of the embedded direction change.
464
- processedSegments.push({
465
- type: 'literal',
466
- text: '\u2069',
467
- isPlaceholder: false,
468
- placeholder: '',
469
- isEditable: false
470
- });
471
- } else {
472
- // We only want to "wrap" the unicode around segments that are hour, minute, or second. If they aren't, just process as normal.
473
- processedSegments.push(dateSegment);
474
- }
475
- }
476
-
477
- return processedSegments;
478
- }
@@ -1,203 +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 {CalendarDate, DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationalized/date';
14
- import {DatePickerProps, DateValue, Granularity, MappedDateValue, TimeValue} from '@react-types/datepicker';
15
- import {FieldOptions, FormatterOptions, getFormatOptions, getPlaceholderTime, getValidationResult, useDefaultProps} from './utils';
16
- import {FormValidationState, useFormValidationState} from '@react-stately/form';
17
- import {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';
18
- import {useControlledState} from '@react-stately/utils';
19
- import {useMemo, useState} from 'react';
20
- import {ValidationState} from '@react-types/shared';
21
-
22
- export interface DatePickerStateOptions<T extends DateValue> extends DatePickerProps<T> {
23
- /**
24
- * Determines whether the date picker popover should close automatically when a date is selected.
25
- * @default true
26
- */
27
- shouldCloseOnSelect?: boolean | (() => boolean)
28
- }
29
-
30
- export interface DatePickerState extends OverlayTriggerState, FormValidationState {
31
- /** The currently selected date. */
32
- value: DateValue | null,
33
- /** The default date. */
34
- defaultValue: DateValue | null,
35
- /** Sets the selected date. */
36
- setValue(value: DateValue | null): void,
37
- /**
38
- * The date portion of the value. This may be set prior to `value` if the user has
39
- * selected a date but has not yet selected a time.
40
- */
41
- dateValue: DateValue | null,
42
- /** Sets the date portion of the value. */
43
- setDateValue(value: DateValue): void,
44
- /**
45
- * The time portion of the value. This may be set prior to `value` if the user has
46
- * selected a time but has not yet selected a date.
47
- */
48
- timeValue: TimeValue | null,
49
- /** Sets the time portion of the value. */
50
- setTimeValue(value: TimeValue): void,
51
- /** The granularity for the field, based on the `granularity` prop and current value. */
52
- granularity: Granularity,
53
- /** Whether the date picker supports selecting a time, according to the `granularity` prop and current value. */
54
- hasTime: boolean,
55
- /** Whether the calendar popover is currently open. */
56
- isOpen: boolean,
57
- /** Sets whether the calendar popover is open. */
58
- setOpen(isOpen: boolean): void,
59
- /**
60
- * The current validation state of the date picker, based on the `validationState`, `minValue`, and `maxValue` props.
61
- * @deprecated Use `isInvalid` instead.
62
- */
63
- validationState: ValidationState | null,
64
- /** Whether the date picker is invalid, based on the `isInvalid`, `minValue`, and `maxValue` props. */
65
- isInvalid: boolean,
66
- /** Formats the selected value using the given options. */
67
- formatValue(locale: string, fieldOptions: FieldOptions): string,
68
- /** Gets a formatter based on state's props. */
69
- getDateFormatter(locale: string, formatOptions: FormatterOptions): DateFormatter
70
- }
71
-
72
- /**
73
- * Provides state management for a date picker component.
74
- * A date picker combines a DateField and a Calendar popover to allow users to enter or select a date and time value.
75
- */
76
- export function useDatePickerState<T extends DateValue = DateValue>(props: DatePickerStateOptions<T>): DatePickerState {
77
- let overlayState = useOverlayTriggerState(props);
78
- let [value, setValue] = useControlledState<DateValue | null, MappedDateValue<T> | null>(props.value, props.defaultValue || null, props.onChange);
79
- let [initialValue] = useState(value);
80
-
81
- let v = (value || props.placeholderValue || null);
82
- let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);
83
- let dateValue = value != null ? value.toDate(defaultTimeZone ?? 'UTC') : null;
84
- let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';
85
- let shouldCloseOnSelect = props.shouldCloseOnSelect ?? true;
86
-
87
- let [selectedDate, setSelectedDate] = useState<DateValue | null>(null);
88
- let [selectedTime, setSelectedTime] = useState<TimeValue | null>(null);
89
-
90
- if (value) {
91
- selectedDate = value;
92
- if ('hour' in value) {
93
- selectedTime = value;
94
- }
95
- }
96
-
97
- // props.granularity must actually exist in the value if one is provided.
98
- if (v && !(granularity in v)) {
99
- throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString());
100
- }
101
-
102
- let showEra = value?.calendar.identifier === 'gregory' && value.era === 'BC';
103
- let formatOpts = useMemo(() => ({
104
- granularity,
105
- timeZone: defaultTimeZone,
106
- hideTimeZone: props.hideTimeZone,
107
- hourCycle: props.hourCycle,
108
- shouldForceLeadingZeros: props.shouldForceLeadingZeros,
109
- showEra
110
- }), [granularity, props.hourCycle, props.shouldForceLeadingZeros, defaultTimeZone, props.hideTimeZone, showEra]);
111
-
112
- let {minValue, maxValue, isDateUnavailable} = props;
113
- let builtinValidation = useMemo(() => getValidationResult(
114
- value,
115
- minValue,
116
- maxValue,
117
- isDateUnavailable,
118
- formatOpts
119
- ), [value, minValue, maxValue, isDateUnavailable, formatOpts]);
120
-
121
- let validation = useFormValidationState({
122
- ...props,
123
- value: value as MappedDateValue<T> | null,
124
- builtinValidation
125
- });
126
-
127
- let isValueInvalid = validation.displayValidation.isInvalid;
128
- let validationState: ValidationState | null = props.validationState || (isValueInvalid ? 'invalid' : null);
129
-
130
- let commitValue = (date: DateValue, time: TimeValue) => {
131
- setValue('timeZone' in time ? time.set(toCalendarDate(date)) : toCalendarDateTime(date, time));
132
- setSelectedDate(null);
133
- setSelectedTime(null);
134
- validation.commitValidation();
135
- };
136
-
137
- // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
138
- let selectDate = (newValue: CalendarDate) => {
139
- let shouldClose = typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;
140
- if (hasTime) {
141
- if (selectedTime || shouldClose) {
142
- commitValue(newValue, selectedTime || getPlaceholderTime(props.defaultValue || props.placeholderValue));
143
- } else {
144
- setSelectedDate(newValue);
145
- }
146
- } else {
147
- setValue(newValue);
148
- validation.commitValidation();
149
- }
150
-
151
- if (shouldClose) {
152
- overlayState.setOpen(false);
153
- }
154
- };
155
-
156
- let selectTime = (newValue: TimeValue) => {
157
- if (selectedDate && newValue) {
158
- commitValue(selectedDate, newValue);
159
- } else {
160
- setSelectedTime(newValue);
161
- }
162
- };
163
-
164
- return {
165
- ...validation,
166
- value,
167
- defaultValue: props.defaultValue ?? initialValue,
168
- setValue,
169
- dateValue: selectedDate,
170
- timeValue: selectedTime,
171
- setDateValue: selectDate,
172
- setTimeValue: selectTime,
173
- granularity,
174
- hasTime,
175
- ...overlayState,
176
- setOpen(isOpen) {
177
- // Commit the selected date when the calendar is closed. Use a placeholder time if one wasn't set.
178
- // If only the time was set and not the date, don't commit. The state will be preserved until
179
- // the user opens the popover again.
180
- if (!isOpen && !value && selectedDate && hasTime) {
181
- commitValue(selectedDate, selectedTime || getPlaceholderTime(props.defaultValue || props.placeholderValue));
182
- }
183
-
184
- overlayState.setOpen(isOpen);
185
- },
186
- validationState,
187
- isInvalid: isValueInvalid,
188
- formatValue(locale, fieldOptions) {
189
- if (!dateValue) {
190
- return '';
191
- }
192
-
193
- let formatOptions = getFormatOptions(fieldOptions, formatOpts);
194
- let formatter = new DateFormatter(locale, formatOptions);
195
- return formatter.format(dateValue);
196
- },
197
- getDateFormatter(locale, formatOptions: FormatterOptions) {
198
- let newOptions = {...formatOpts, ...formatOptions};
199
- let newFormatOptions = getFormatOptions({}, newOptions);
200
- return new DateFormatter(locale, newFormatOptions);
201
- }
202
- };
203
- }