grommet 2.55.0 → 2.56.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/COPYRIGHT.md +3 -1
  2. package/components/Button/Button.js +7 -4
  3. package/components/Button/stories/CustomThemed/ChildrenPlainOption.stories.js +86 -0
  4. package/components/Calendar/Calendar.js +2 -1
  5. package/components/Cards/Cards.js +1 -2
  6. package/components/DateTimeInput/DateTimeInput.js +1093 -0
  7. package/components/DateTimeInput/README.md +2 -0
  8. package/components/DateTimeInput/StyledDateTimeInput.js +74 -0
  9. package/components/DateTimeInput/index.d.ts +47 -0
  10. package/components/DateTimeInput/index.js +6 -0
  11. package/components/DateTimeInput/propTypes.js +42 -0
  12. package/components/DateTimeInput/stories/CustomThemed/Custom.stories.tsx +53 -0
  13. package/components/DateTimeInput/stories/Form.stories.tsx +44 -0
  14. package/components/DateTimeInput/stories/Inline.stories.tsx +45 -0
  15. package/components/DateTimeInput/stories/Locale.stories.tsx +59 -0
  16. package/components/DateTimeInput/stories/Simple.stories.tsx +23 -0
  17. package/components/Form/Form.js +7 -4
  18. package/components/Form/index.d.ts +3 -0
  19. package/components/Form/stories/AggregateValidation.stories.js +6 -8
  20. package/components/FormField/FormField.js +164 -91
  21. package/components/Grommet/index.d.ts +21 -0
  22. package/components/Grommet/propTypes.js +26 -0
  23. package/components/Stepper/StepConnector.js +38 -0
  24. package/components/Stepper/Stepper.js +303 -0
  25. package/components/Stepper/StepperContext.js +43 -0
  26. package/components/Stepper/StepperDescription.js +26 -0
  27. package/components/Stepper/StepperHelperText.js +44 -0
  28. package/components/Stepper/StepperIndicator.js +74 -0
  29. package/components/Stepper/StepperLabel.js +41 -0
  30. package/components/Stepper/StepperStep.js +137 -0
  31. package/components/Stepper/StyledStepper.js +162 -0
  32. package/components/Stepper/index.d.ts +80 -0
  33. package/components/Stepper/index.js +18 -0
  34. package/components/Stepper/propTypes.js +35 -0
  35. package/components/Stepper/stories/DisabledSteps.stories.js +45 -0
  36. package/components/Stepper/stories/ErrorStates.stories.js +45 -0
  37. package/components/Stepper/stories/HorizontalSteps.stories.js +50 -0
  38. package/components/Stepper/stories/Interactive.stories.js +60 -0
  39. package/components/Stepper/stories/KeyboardNavigation.stories.js +64 -0
  40. package/components/Stepper/stories/NestedSubSteps.stories.js +70 -0
  41. package/components/Stepper/stories/ReadOnly.stories.js +43 -0
  42. package/components/Stepper/stories/ThemingLightDark.stories.js +58 -0
  43. package/components/Stepper/stories/VerticalSteps.stories.js +67 -0
  44. package/components/Tab/Tab.js +19 -6
  45. package/components/Tabs/Tabs.js +156 -22
  46. package/components/TimeInput/README.md +2 -0
  47. package/components/TimeInput/StyledTimeInput.js +73 -0
  48. package/components/TimeInput/TimeInput.js +692 -0
  49. package/components/TimeInput/TimeInputPopup.js +489 -0
  50. package/components/TimeInput/index.d.ts +44 -0
  51. package/components/TimeInput/index.js +6 -0
  52. package/components/TimeInput/propTypes.js +39 -0
  53. package/components/TimeInput/stories/Controlled.stories.tsx +29 -0
  54. package/components/TimeInput/stories/Disabled.stories.tsx +13 -0
  55. package/components/TimeInput/stories/Form.stories.tsx +38 -0
  56. package/components/TimeInput/stories/ReadOnly.stories.tsx +13 -0
  57. package/components/TimeInput/stories/Simple.stories.tsx +19 -0
  58. package/components/TimeInput/stories/Step.stories.tsx +14 -0
  59. package/components/TimeInput/stories/ThemingLightDark.stories.tsx +22 -0
  60. package/components/TimeInput/stories/Uncontrolled.stories.tsx +22 -0
  61. package/components/TimeInput/useSectionedTimeField.js +358 -0
  62. package/components/TimeInput/utils.js +192 -0
  63. package/components/Wizard/StyledWizard.js +16 -0
  64. package/components/Wizard/Wizard.js +770 -0
  65. package/components/Wizard/WizardContent.js +90 -0
  66. package/components/Wizard/WizardContext.js +35 -0
  67. package/components/Wizard/WizardFooter.js +123 -0
  68. package/components/Wizard/WizardHeader.js +63 -0
  69. package/components/Wizard/WizardProgress.js +84 -0
  70. package/components/Wizard/WizardStepHeader.js +55 -0
  71. package/components/Wizard/index.d.ts +171 -0
  72. package/components/Wizard/index.js +19 -0
  73. package/components/Wizard/propTypes.js +56 -0
  74. package/components/Wizard/stories/BasicLinear.stories.js +71 -0
  75. package/components/Wizard/stories/Branching.stories.js +96 -0
  76. package/components/Wizard/stories/Composable.stories.js +75 -0
  77. package/components/Wizard/stories/Controlled.stories.js +136 -0
  78. package/components/Wizard/stories/LongContent.stories.js +118 -0
  79. package/components/Wizard/stories/Modal.stories.js +143 -0
  80. package/components/Wizard/stories/NestedSubSteps.stories.js +79 -0
  81. package/components/Wizard/stories/Validation.stories.js +123 -0
  82. package/components/index.d.ts +6 -0
  83. package/components/index.js +24 -0
  84. package/es6/components/Button/Button.js +7 -4
  85. package/es6/components/Button/stories/CustomThemed/ChildrenPlainOption.stories.js +81 -0
  86. package/es6/components/Calendar/Calendar.js +2 -0
  87. package/es6/components/Cards/Cards.js +1 -2
  88. package/es6/components/DateTimeInput/DateTimeInput.js +1090 -0
  89. package/es6/components/DateTimeInput/README.md +2 -0
  90. package/es6/components/DateTimeInput/StyledDateTimeInput.js +68 -0
  91. package/es6/components/DateTimeInput/index.d.ts +47 -0
  92. package/es6/components/DateTimeInput/index.js +3 -0
  93. package/es6/components/DateTimeInput/propTypes.js +36 -0
  94. package/es6/components/DateTimeInput/stories/CustomThemed/Custom.stories.tsx +53 -0
  95. package/es6/components/DateTimeInput/stories/Form.stories.tsx +44 -0
  96. package/es6/components/DateTimeInput/stories/Inline.stories.tsx +45 -0
  97. package/es6/components/DateTimeInput/stories/Locale.stories.tsx +59 -0
  98. package/es6/components/DateTimeInput/stories/Simple.stories.tsx +23 -0
  99. package/es6/components/Form/Form.js +7 -3
  100. package/es6/components/Form/index.d.ts +3 -0
  101. package/es6/components/Form/stories/AggregateValidation.stories.js +6 -9
  102. package/es6/components/FormField/FormField.js +165 -91
  103. package/es6/components/Grommet/index.d.ts +21 -0
  104. package/es6/components/Grommet/propTypes.js +26 -0
  105. package/es6/components/Stepper/StepConnector.js +33 -0
  106. package/es6/components/Stepper/Stepper.js +301 -0
  107. package/es6/components/Stepper/StepperContext.js +39 -0
  108. package/es6/components/Stepper/StepperDescription.js +21 -0
  109. package/es6/components/Stepper/StepperHelperText.js +39 -0
  110. package/es6/components/Stepper/StepperIndicator.js +69 -0
  111. package/es6/components/Stepper/StepperLabel.js +36 -0
  112. package/es6/components/Stepper/StepperStep.js +133 -0
  113. package/es6/components/Stepper/StyledStepper.js +156 -0
  114. package/es6/components/Stepper/index.d.ts +80 -0
  115. package/es6/components/Stepper/index.js +6 -0
  116. package/es6/components/Stepper/propTypes.js +30 -0
  117. package/es6/components/Stepper/stories/DisabledSteps.stories.js +41 -0
  118. package/es6/components/Stepper/stories/ErrorStates.stories.js +41 -0
  119. package/es6/components/Stepper/stories/HorizontalSteps.stories.js +46 -0
  120. package/es6/components/Stepper/stories/Interactive.stories.js +56 -0
  121. package/es6/components/Stepper/stories/KeyboardNavigation.stories.js +60 -0
  122. package/es6/components/Stepper/stories/NestedSubSteps.stories.js +66 -0
  123. package/es6/components/Stepper/stories/ReadOnly.stories.js +39 -0
  124. package/es6/components/Stepper/stories/ThemingLightDark.stories.js +54 -0
  125. package/es6/components/Stepper/stories/VerticalSteps.stories.js +63 -0
  126. package/es6/components/Tab/Tab.js +19 -6
  127. package/es6/components/Tabs/Tabs.js +157 -23
  128. package/es6/components/TimeInput/README.md +2 -0
  129. package/es6/components/TimeInput/StyledTimeInput.js +68 -0
  130. package/es6/components/TimeInput/TimeInput.js +689 -0
  131. package/es6/components/TimeInput/TimeInputPopup.js +485 -0
  132. package/es6/components/TimeInput/index.d.ts +44 -0
  133. package/es6/components/TimeInput/index.js +1 -0
  134. package/es6/components/TimeInput/propTypes.js +33 -0
  135. package/es6/components/TimeInput/stories/Controlled.stories.tsx +29 -0
  136. package/es6/components/TimeInput/stories/Disabled.stories.tsx +13 -0
  137. package/es6/components/TimeInput/stories/Form.stories.tsx +38 -0
  138. package/es6/components/TimeInput/stories/ReadOnly.stories.tsx +13 -0
  139. package/es6/components/TimeInput/stories/Simple.stories.tsx +19 -0
  140. package/es6/components/TimeInput/stories/Step.stories.tsx +14 -0
  141. package/es6/components/TimeInput/stories/ThemingLightDark.stories.tsx +22 -0
  142. package/es6/components/TimeInput/stories/Uncontrolled.stories.tsx +22 -0
  143. package/es6/components/TimeInput/useSectionedTimeField.js +355 -0
  144. package/es6/components/TimeInput/utils.js +187 -0
  145. package/es6/components/Wizard/StyledWizard.js +11 -0
  146. package/es6/components/Wizard/Wizard.js +768 -0
  147. package/es6/components/Wizard/WizardContent.js +87 -0
  148. package/es6/components/Wizard/WizardContext.js +31 -0
  149. package/es6/components/Wizard/WizardFooter.js +120 -0
  150. package/es6/components/Wizard/WizardHeader.js +60 -0
  151. package/es6/components/Wizard/WizardProgress.js +80 -0
  152. package/es6/components/Wizard/WizardStepHeader.js +51 -0
  153. package/es6/components/Wizard/index.d.ts +171 -0
  154. package/es6/components/Wizard/index.js +7 -0
  155. package/es6/components/Wizard/propTypes.js +51 -0
  156. package/es6/components/Wizard/stories/BasicLinear.stories.js +67 -0
  157. package/es6/components/Wizard/stories/Branching.stories.js +93 -0
  158. package/es6/components/Wizard/stories/Composable.stories.js +72 -0
  159. package/es6/components/Wizard/stories/Controlled.stories.js +133 -0
  160. package/es6/components/Wizard/stories/LongContent.stories.js +115 -0
  161. package/es6/components/Wizard/stories/Modal.stories.js +141 -0
  162. package/es6/components/Wizard/stories/NestedSubSteps.stories.js +76 -0
  163. package/es6/components/Wizard/stories/Validation.stories.js +118 -0
  164. package/es6/components/index.d.ts +6 -0
  165. package/es6/components/index.js +6 -0
  166. package/es6/index.d.ts +6 -0
  167. package/es6/languages/default.json +48 -0
  168. package/es6/themes/base.d.ts +223 -0
  169. package/es6/themes/base.js +366 -0
  170. package/es6/utils/DOM.js +11 -2
  171. package/es6/utils/dates.js +10 -0
  172. package/es6/utils/sectionHelpers.js +57 -0
  173. package/es6/utils/useSectionedField.js +45 -0
  174. package/grommet.min.js +2 -1
  175. package/grommet.min.js.LICENSE.txt +1 -0
  176. package/grommet.min.js.map +1 -1
  177. package/index.d.ts +6 -0
  178. package/languages/default.json +48 -0
  179. package/package.json +57 -52
  180. package/themes/base.d.ts +223 -0
  181. package/themes/base.js +366 -1
  182. package/tools/license-header.js +2 -0
  183. package/utils/DOM.js +11 -2
  184. package/utils/dates.js +11 -1
  185. package/utils/sectionHelpers.js +61 -0
  186. package/utils/useSectionedField.js +50 -0
  187. package/components/TextInput/stories/CustomThemed/CustomSuggestions.stories.tsx +0 -180
  188. package/components/TextInput/stories/CustomThemed/Themed.stories.js +0 -79
  189. package/es6/components/TextInput/stories/CustomThemed/CustomSuggestions.stories.tsx +0 -180
  190. package/es6/components/TextInput/stories/CustomThemed/Themed.stories.js +0 -74
@@ -0,0 +1,1090 @@
1
+ var _excluded = ["defaultValue", "disabled", "format", "id", "inline", "locale", "messages", "minuteStep", "name", "onChange", "plain", "focusIndicator", "readOnly", "showSeconds", "value"];
2
+ function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
3
+ function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
4
+ // SPDX-FileCopyrightText: © Hewlett Packard Enterprise Development LP
5
+ // SPDX-License-Identifier: Apache-2.0
6
+ import React, { forwardRef, useCallback, useContext, useEffect, useId, useMemo, useRef, useState } from 'react';
7
+ import { Calendar as GrommetCalendarIcon } from 'grommet-icons/icons/Calendar';
8
+ import { AnnounceContext } from '../../contexts/AnnounceContext';
9
+ import { MessageContext } from '../../contexts/MessageContext';
10
+ import { normalizeStep, pad } from '../../utils/dates';
11
+ import { useForwardedRef } from '../../utils';
12
+ import { useSectionedField } from '../../utils/useSectionedField';
13
+ import { useThemeValue } from '../../utils/useThemeValue';
14
+ import { Box } from '../Box';
15
+ import { Button } from '../Button';
16
+ import { Calendar } from '../Calendar';
17
+ import { Drop } from '../Drop';
18
+ import { FormContext } from '../Form';
19
+ import { Keyboard } from '../Keyboard';
20
+ import { TimeInput } from '../TimeInput';
21
+ import { defaultHourForFormat } from '../TimeInput/utils';
22
+ import { getSectionKeyFromType, getSectionNameFromType, getSectionTokenFromType } from '../../utils/sectionHelpers';
23
+ import { StyledDateTimeInput, StyledDateTimeInputContainer, StyledDateTimeInputDisplay, StyledDateTimeInputField, StyledDateTimeInputSegment, StyledDateTimeInputSeparator } from './StyledDateTimeInput';
24
+ import { DateTimeInputPropTypes } from './propTypes';
25
+ var SECTION_DAY = 0;
26
+ var SECTION_MONTH = 1;
27
+ var SECTION_YEAR = 2;
28
+ var SECTION_HOUR = 3;
29
+ var SECTION_MINUTE = 4;
30
+ var SECTION_SECOND = 5;
31
+ var SECTION_PERIOD = 6;
32
+ var pad4 = function pad4(value) {
33
+ return String(value).padStart(4, '0');
34
+ };
35
+ var getDaysInMonth = function getDaysInMonth(year, month) {
36
+ return new Date(year, month, 0).getDate();
37
+ };
38
+ var sectionTypeFromSection = function sectionTypeFromSection(section) {
39
+ if (section === SECTION_DAY) return 'day';
40
+ if (section === SECTION_MONTH) return 'month';
41
+ if (section === SECTION_YEAR) return 'year';
42
+ if (section === SECTION_HOUR) return 'hours';
43
+ if (section === SECTION_MINUTE) return 'minutes';
44
+ if (section === SECTION_SECOND) return 'seconds';
45
+ return 'meridiem';
46
+ };
47
+ var defaultSections = function defaultSections(format, showSeconds) {
48
+ var base = _extends({
49
+ day: undefined,
50
+ month: undefined,
51
+ year: undefined,
52
+ hour: undefined,
53
+ minute: undefined
54
+ }, showSeconds ? {
55
+ second: undefined
56
+ } : {});
57
+ if (format === '12') {
58
+ return _extends({}, base, {
59
+ period: undefined
60
+ });
61
+ }
62
+ return base;
63
+ };
64
+ var hasAnyValue = function hasAnyValue(sections) {
65
+ return sections.day !== undefined || sections.month !== undefined || sections.year !== undefined || sections.hour !== undefined || sections.minute !== undefined || sections.second !== undefined || sections.period !== undefined;
66
+ };
67
+ var sectionForPart = function sectionForPart(partType) {
68
+ if (partType === 'day') return SECTION_DAY;
69
+ if (partType === 'month') return SECTION_MONTH;
70
+ if (partType === 'year') return SECTION_YEAR;
71
+ if (partType === 'hour') return SECTION_HOUR;
72
+ if (partType === 'minute') return SECTION_MINUTE;
73
+ if (partType === 'second') return SECTION_SECOND;
74
+ if (partType === 'dayPeriod') return SECTION_PERIOD;
75
+ return undefined;
76
+ };
77
+ var getSectionOrder = function getSectionOrder(format, showSeconds) {
78
+ var timeSections = showSeconds ? [SECTION_HOUR, SECTION_MINUTE, SECTION_SECOND] : [SECTION_HOUR, SECTION_MINUTE];
79
+ if (format === '12') {
80
+ return [SECTION_DAY, SECTION_MONTH, SECTION_YEAR].concat(timeSections, [SECTION_PERIOD]);
81
+ }
82
+ return [SECTION_DAY, SECTION_MONTH, SECTION_YEAR].concat(timeSections);
83
+ };
84
+ var separatorBeforeSection = function separatorBeforeSection(section) {
85
+ if (section === SECTION_MONTH || section === SECTION_YEAR) return '/';
86
+ if (section === SECTION_HOUR) return ' ';
87
+ if (section === SECTION_MINUTE || section === SECTION_SECOND) return ':';
88
+ return ' ';
89
+ };
90
+ var getLocaleSectionLayout = function getLocaleSectionLayout(format, showSeconds, locale) {
91
+ var fallbackOrder = getSectionOrder(format, showSeconds);
92
+ var fallbackPrefixes = {};
93
+ fallbackOrder.forEach(function (section, index) {
94
+ fallbackPrefixes[section] = index === 0 ? '' : separatorBeforeSection(section);
95
+ });
96
+ try {
97
+ var formatter = new Intl.DateTimeFormat(locale || undefined, _extends({
98
+ year: 'numeric',
99
+ month: '2-digit',
100
+ day: '2-digit',
101
+ hour: '2-digit',
102
+ minute: '2-digit'
103
+ }, showSeconds ? {
104
+ second: '2-digit'
105
+ } : {}, {
106
+ hour12: format === '12'
107
+ }));
108
+ var parts = formatter.formatToParts(new Date());
109
+ var seen = new Set();
110
+ var sectionOrder = [];
111
+ var separatorMap = {};
112
+ var pendingLiteral = '';
113
+ parts.forEach(function (part) {
114
+ if (part.type === 'literal') {
115
+ pendingLiteral += part.value;
116
+ return;
117
+ }
118
+ var section = sectionForPart(part.type);
119
+ if (section === undefined) return;
120
+ if (section === SECTION_PERIOD && format !== '12') return;
121
+ if (section === SECTION_SECOND && !showSeconds) return;
122
+ if (!seen.has(section)) {
123
+ // Normalize the date/time boundary separator: strip commas (e.g. ", "
124
+ // from en-US) so the display matches the design spec (space only).
125
+ var raw = sectionOrder.length === 0 ? '' : pendingLiteral;
126
+ separatorMap[section] = section === SECTION_HOUR ? raw.replace(/,/g, '').trimStart() || ' ' : raw;
127
+ sectionOrder.push(section);
128
+ seen.add(section);
129
+ }
130
+ pendingLiteral = '';
131
+ });
132
+ fallbackOrder.forEach(function (section) {
133
+ if (!seen.has(section)) {
134
+ separatorMap[section] = sectionOrder.length === 0 ? '' : separatorBeforeSection(section);
135
+ sectionOrder.push(section);
136
+ }
137
+ });
138
+ return {
139
+ sectionOrder: sectionOrder,
140
+ separatorMap: separatorMap
141
+ };
142
+ } catch (_unused) {
143
+ return {
144
+ sectionOrder: fallbackOrder,
145
+ separatorMap: fallbackPrefixes
146
+ };
147
+ }
148
+ };
149
+ var getLocaleTimeFormat = function getLocaleTimeFormat(locale) {
150
+ try {
151
+ var _Intl$DateTimeFormat$ = new Intl.DateTimeFormat(locale || undefined, {
152
+ hour: 'numeric'
153
+ }).resolvedOptions(),
154
+ hour12 = _Intl$DateTimeFormat$.hour12;
155
+ return hour12 === false ? '24' : '12';
156
+ } catch (_unused2) {
157
+ return '12';
158
+ }
159
+ };
160
+ var toLocalSections = function toLocalSections(value, format) {
161
+ if (!value || typeof value !== 'string') return undefined;
162
+ var parsed = new Date(value);
163
+ if (Number.isNaN(parsed.getTime())) return undefined;
164
+ var year = parsed.getFullYear();
165
+ var month = parsed.getMonth() + 1;
166
+ var day = parsed.getDate();
167
+ var hour24 = parsed.getHours();
168
+ var minute = parsed.getMinutes();
169
+ var second = parsed.getSeconds();
170
+ if (format === '12') {
171
+ var period = hour24 < 12 ? 'AM' : 'PM';
172
+ var hour = hour24 % 12 || 12;
173
+ return {
174
+ day: day,
175
+ month: month,
176
+ year: year,
177
+ hour: hour,
178
+ minute: minute,
179
+ second: second,
180
+ period: period
181
+ };
182
+ }
183
+ return {
184
+ day: day,
185
+ month: month,
186
+ year: year,
187
+ hour: hour24,
188
+ minute: minute,
189
+ second: second
190
+ };
191
+ };
192
+ var toUtcISOString = function toUtcISOString(sections, format, showSeconds) {
193
+ var day = sections.day,
194
+ month = sections.month,
195
+ year = sections.year,
196
+ hour = sections.hour,
197
+ minute = sections.minute,
198
+ second = sections.second,
199
+ _sections$period = sections.period,
200
+ period = _sections$period === void 0 ? 'AM' : _sections$period;
201
+ if (day === undefined || month === undefined || year === undefined || hour === undefined || minute === undefined || showSeconds && second === undefined) {
202
+ return undefined;
203
+ }
204
+ var hour24 = hour;
205
+ if (format === '12') {
206
+ hour24 = hour % 12;
207
+ if (period === 'PM') hour24 += 12;
208
+ }
209
+ var daysInMonth = getDaysInMonth(year, month);
210
+ if (day < 1 || day > daysInMonth) return undefined;
211
+ var resolvedSecond = showSeconds ? second : second != null ? second : 0;
212
+ var candidate = new Date(year, month - 1, day, hour24, minute, resolvedSecond, 0);
213
+ if (candidate.getFullYear() !== year || candidate.getMonth() + 1 !== month || candidate.getDate() !== day) {
214
+ return undefined;
215
+ }
216
+ return candidate.toISOString();
217
+ };
218
+ var getCalendarDate = function getCalendarDate(sections) {
219
+ var day = sections.day,
220
+ month = sections.month,
221
+ year = sections.year;
222
+ if (day === undefined || month === undefined || year === undefined) {
223
+ return undefined;
224
+ }
225
+ var daysInMonth = getDaysInMonth(year, month);
226
+ if (day < 1 || day > daysInMonth) return undefined;
227
+
228
+ // Calendar selection should persist even before time sections are complete.
229
+ return pad4(year) + "-" + pad(month) + "-" + pad(day);
230
+ };
231
+ var parseCalendarSelection = function parseCalendarSelection(nextValue) {
232
+ if (!nextValue) return undefined;
233
+ var nextDate = Array.isArray(nextValue) ? nextValue[0] : nextValue;
234
+ if (!nextDate || typeof nextDate !== 'string') return undefined;
235
+ var match = /^(\d{4})-(\d{2})-(\d{2})/.exec(nextDate);
236
+ if (match) {
237
+ return {
238
+ year: Number(match[1]),
239
+ month: Number(match[2]),
240
+ day: Number(match[3])
241
+ };
242
+ }
243
+ var parsed = new Date(nextDate);
244
+ if (Number.isNaN(parsed.getTime())) return undefined;
245
+ return {
246
+ year: parsed.getFullYear(),
247
+ month: parsed.getMonth() + 1,
248
+ day: parsed.getDate()
249
+ };
250
+ };
251
+ var getSeededTimeSections = function getSeededTimeSections(sections, format, showSeconds) {
252
+ var _sections$hour, _sections$minute, _sections$second, _sections$period2;
253
+ var hourMin = defaultHourForFormat(format);
254
+ return _extends({
255
+ hour: (_sections$hour = sections.hour) != null ? _sections$hour : hourMin,
256
+ minute: (_sections$minute = sections.minute) != null ? _sections$minute : 0
257
+ }, showSeconds ? {
258
+ second: (_sections$second = sections.second) != null ? _sections$second : 0
259
+ } : {}, format === '12' ? {
260
+ period: (_sections$period2 = sections.period) != null ? _sections$period2 : 'AM'
261
+ } : {});
262
+ };
263
+ var hasCompleteDateSections = function hasCompleteDateSections(sections) {
264
+ return sections.day !== undefined && sections.month !== undefined && sections.year !== undefined;
265
+ };
266
+ var getSeededDateSections = function getSeededDateSections(sections) {
267
+ if (hasCompleteDateSections(sections)) {
268
+ return {
269
+ day: sections.day,
270
+ month: sections.month,
271
+ year: sections.year
272
+ };
273
+ }
274
+ var today = new Date();
275
+ return {
276
+ day: today.getDate(),
277
+ month: today.getMonth() + 1,
278
+ year: today.getFullYear()
279
+ };
280
+ };
281
+ var getSectionName = function getSectionName(section, formatMessage, messages) {
282
+ var sectionType = sectionTypeFromSection(section);
283
+ return getSectionNameFromType({
284
+ sectionType: sectionType,
285
+ messagePrefix: 'dateTimeInput',
286
+ formatMessage: formatMessage,
287
+ messages: messages
288
+ });
289
+ };
290
+ var getSectionLimits = function getSectionLimits(section, format, sections) {
291
+ if (section === SECTION_DAY) {
292
+ var year = sections.year || new Date().getFullYear();
293
+ var month = sections.month || 1;
294
+ return {
295
+ min: 1,
296
+ max: getDaysInMonth(year, month)
297
+ };
298
+ }
299
+ if (section === SECTION_MONTH) return {
300
+ min: 1,
301
+ max: 12
302
+ };
303
+ if (section === SECTION_YEAR) return {
304
+ min: 1,
305
+ max: 9999
306
+ };
307
+ if (section === SECTION_HOUR) return {
308
+ min: format === '12' ? 1 : 0,
309
+ max: format === '12' ? 12 : 23
310
+ };
311
+ if (section === SECTION_PERIOD) return {
312
+ min: 0,
313
+ max: 1
314
+ };
315
+ return {
316
+ min: 0,
317
+ max: 59
318
+ };
319
+ };
320
+ var digitsPerSection = function digitsPerSection(section) {
321
+ if (section === SECTION_YEAR) return 4;
322
+ if (section === SECTION_PERIOD) return 0;
323
+ return 2;
324
+ };
325
+ var formatSectionText = function formatSectionText(section, value) {
326
+ if (value === undefined || value === '') return getSectionTokenFromType(sectionTypeFromSection(section));
327
+ if (section === SECTION_YEAR) return pad4(value);
328
+ if (section === SECTION_PERIOD) return value;
329
+ return pad(value);
330
+ };
331
+ var DateTimeInput = /*#__PURE__*/forwardRef(function (_ref, refArg) {
332
+ var _theme$dateTimeInput, _theme$global$edgeSiz, _theme$global$edgeSiz2, _theme$dateTimeInput2, _theme$dateTimeInput3, _theme$dateTimeInput4, _theme$dateTimeInput5, _theme$dateTimeInput6, _theme$dateTimeInput7, _theme$dateTimeInput8;
333
+ var defaultValue = _ref.defaultValue,
334
+ disabled = _ref.disabled,
335
+ format = _ref.format,
336
+ id = _ref.id,
337
+ _ref$inline = _ref.inline,
338
+ inline = _ref$inline === void 0 ? false : _ref$inline,
339
+ locale = _ref.locale,
340
+ messages = _ref.messages,
341
+ _ref$minuteStep = _ref.minuteStep,
342
+ minuteStep = _ref$minuteStep === void 0 ? 1 : _ref$minuteStep,
343
+ name = _ref.name,
344
+ onChange = _ref.onChange,
345
+ plainProp = _ref.plain,
346
+ focusIndicatorProp = _ref.focusIndicator,
347
+ _ref$readOnly = _ref.readOnly,
348
+ readOnly = _ref$readOnly === void 0 ? false : _ref$readOnly,
349
+ _ref$showSeconds = _ref.showSeconds,
350
+ showSeconds = _ref$showSeconds === void 0 ? false : _ref$showSeconds,
351
+ valueArg = _ref.value,
352
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded);
353
+ var _useThemeValue = useThemeValue(),
354
+ theme = _useThemeValue.theme,
355
+ passThemeFlag = _useThemeValue.passThemeFlag;
356
+ var announce = useContext(AnnounceContext);
357
+ var _useContext = useContext(MessageContext),
358
+ formatMessage = _useContext.format;
359
+ var formContext = useContext(FormContext);
360
+ var useFormInput = formContext.useFormInput;
361
+ var resolvedFormat = useMemo(function () {
362
+ return format || getLocaleTimeFormat(locale);
363
+ }, [format, locale]);
364
+ var separatorPadToken = (_theme$dateTimeInput = theme.dateTimeInput) == null || (_theme$dateTimeInput = _theme$dateTimeInput.separator) == null ? void 0 : _theme$dateTimeInput.pad;
365
+ var dateTimeSeparatorPadding = ((_theme$global$edgeSiz = theme.global.edgeSize) == null ? void 0 : _theme$global$edgeSiz[separatorPadToken]) || separatorPadToken || ((_theme$global$edgeSiz2 = theme.global.edgeSize) == null ? void 0 : _theme$global$edgeSiz2.xxsmall);
366
+ var inputRef = useForwardedRef(refArg);
367
+ var containerRef = useRef();
368
+ var triggerRef = useRef();
369
+ var segmentRefs = useRef({});
370
+ var activeSectionRef = useRef(SECTION_DAY);
371
+ var suppressSegmentFocusRef = useRef(false);
372
+ var suppressTimePartialSyncRef = useRef(false);
373
+ var editStateRef = useRef({
374
+ section: SECTION_DAY,
375
+ buffer: ''
376
+ });
377
+ var _useFormInput = useFormInput({
378
+ name: name,
379
+ value: valueArg,
380
+ initialValue: defaultValue || ''
381
+ }),
382
+ value = _useFormInput[0],
383
+ setValue = _useFormInput[1];
384
+ useEffect(function () {
385
+ if (process.env.NODE_ENV !== 'production') {
386
+ console.warn('Warning: DateTimeInput is currently in beta. The API is subject ' + 'to change in future releases.');
387
+ }
388
+ }, []);
389
+ var _useMemo = useMemo(function () {
390
+ return getLocaleSectionLayout(resolvedFormat, showSeconds, locale);
391
+ }, [resolvedFormat, showSeconds, locale]),
392
+ sectionOrder = _useMemo.sectionOrder,
393
+ separatorMap = _useMemo.separatorMap;
394
+ var firstSection = sectionOrder[0] || SECTION_DAY;
395
+ var lastSection = sectionOrder[sectionOrder.length - 1] || SECTION_SECOND;
396
+ var parseValue = useCallback(function (nextValue) {
397
+ return toLocalSections(nextValue, resolvedFormat);
398
+ }, [resolvedFormat]);
399
+ var getDefaultValue = useCallback(function () {
400
+ return defaultSections(resolvedFormat, showSeconds);
401
+ }, [resolvedFormat, showSeconds]);
402
+ var _useSectionedField = useSectionedField({
403
+ value: value,
404
+ parseValue: parseValue,
405
+ defaultValue: getDefaultValue,
406
+ sectionOrder: sectionOrder
407
+ }),
408
+ activeSection = _useSectionedField.activeSection,
409
+ pendingDigits = _useSectionedField.pendingDigits,
410
+ preserveIncompleteSectionsRef = _useSectionedField.preserveIncompleteSectionsRef,
411
+ sections = _useSectionedField.sections,
412
+ setActiveSection = _useSectionedField.setActiveSection,
413
+ setPendingDigits = _useSectionedField.setPendingDigits,
414
+ setSections = _useSectionedField.setSections;
415
+ var _useState = useState(false),
416
+ segmentFocused = _useState[0],
417
+ setSegmentFocused = _useState[1];
418
+ var _useState2 = useState(false),
419
+ open = _useState2[0],
420
+ setOpen = _useState2[1];
421
+ var normalizedMinuteStep = useMemo(function () {
422
+ return normalizeStep(minuteStep);
423
+ }, [minuteStep]);
424
+ useEffect(function () {
425
+ activeSectionRef.current = activeSection;
426
+ }, [activeSection]);
427
+ var commitSections = useCallback(function (nextSections) {
428
+ setSections(nextSections);
429
+ var complete = sectionOrder.every(function (section) {
430
+ return nextSections[getSectionKeyFromType(sectionTypeFromSection(section))] !== undefined;
431
+ });
432
+ var anyValue = hasAnyValue(nextSections);
433
+ var nextValue = complete ? toUtcISOString(nextSections, resolvedFormat, showSeconds) : undefined;
434
+ preserveIncompleteSectionsRef.current = (!nextValue || !complete) && anyValue;
435
+
436
+ // Structured input contract: emit only valid committed values.
437
+ // Emit undefined only when user fully clears the component.
438
+ if (nextValue) {
439
+ setValue(nextValue);
440
+ onChange == null || onChange({
441
+ value: nextValue
442
+ });
443
+ return;
444
+ }
445
+ if (!anyValue) {
446
+ setValue(undefined);
447
+ onChange == null || onChange({
448
+ value: undefined
449
+ });
450
+ }
451
+ }, [onChange, preserveIncompleteSectionsRef, resolvedFormat, sectionOrder, setSections, setValue, showSeconds]);
452
+ var setSectionValue = useCallback(function (section, rawValue) {
453
+ var _extends2;
454
+ var key = getSectionKeyFromType(sectionTypeFromSection(section));
455
+ var nextSections = _extends({}, sections, (_extends2 = {}, _extends2[key] = rawValue, _extends2));
456
+ if (section === SECTION_MONTH || section === SECTION_YEAR) {
457
+ var dayLimit = getSectionLimits(SECTION_DAY, resolvedFormat, nextSections).max;
458
+ if (nextSections.day !== undefined && nextSections.day > dayLimit) {
459
+ nextSections.day = dayLimit;
460
+ }
461
+ }
462
+ commitSections(nextSections);
463
+ }, [commitSections, resolvedFormat, sections]);
464
+ var moveSection = useCallback(function (direction) {
465
+ var activeIndex = Math.max(0, sectionOrder.indexOf(activeSection));
466
+ var count = sectionOrder.length;
467
+ var nextIndex = (activeIndex + direction + count) % count;
468
+ var nextSection = sectionOrder[nextIndex];
469
+ setActiveSection(nextSection);
470
+ return nextSection;
471
+ }, [activeSection, sectionOrder, setActiveSection]);
472
+ var focusSection = useCallback(function (section) {
473
+ var target = segmentRefs.current[section];
474
+ if (target) target.focus();
475
+ }, []);
476
+ var incrementSection = useCallback(function (section, delta) {
477
+ if (section === SECTION_PERIOD) {
478
+ setSectionValue(section, sections.period === 'AM' ? 'PM' : 'AM');
479
+ return;
480
+ }
481
+
482
+ // When year is empty, seed it to the current year first to avoid
483
+ // wrapping to 0001/9999 on the initial arrow interaction.
484
+ if (section === SECTION_YEAR && sections.year === undefined) {
485
+ setSectionValue(SECTION_YEAR, new Date().getFullYear());
486
+ return;
487
+ }
488
+ var _getSectionLimits = getSectionLimits(section, resolvedFormat, sections),
489
+ min = _getSectionLimits.min,
490
+ max = _getSectionLimits.max;
491
+ var key = getSectionKeyFromType(sectionTypeFromSection(section));
492
+ var current = sections[key] === undefined ? min : sections[key];
493
+ var step = section === SECTION_MINUTE ? normalizedMinuteStep : 1;
494
+ var next;
495
+ if (section === SECTION_MINUTE && step > 1) {
496
+ var options = Array.from({
497
+ length: Math.ceil((max - min + 1) / step)
498
+ }, function (_, index) {
499
+ return min + index * step;
500
+ }).filter(function (valueAtIndex) {
501
+ return valueAtIndex <= max;
502
+ });
503
+ var currentIndex = options.indexOf(current);
504
+ if (currentIndex !== -1) {
505
+ var wrappedIndex = ((currentIndex + delta) % options.length + options.length) % options.length;
506
+ next = options[wrappedIndex];
507
+ } else if (delta > 0) {
508
+ var _options$find;
509
+ next = (_options$find = options.find(function (option) {
510
+ return option > current;
511
+ })) != null ? _options$find : options[0];
512
+ } else {
513
+ var _descending$find;
514
+ var descending = [].concat(options).reverse();
515
+ next = (_descending$find = descending.find(function (option) {
516
+ return option < current;
517
+ })) != null ? _descending$find : options[options.length - 1];
518
+ }
519
+ } else {
520
+ next = current + delta * step;
521
+ if (next > max) next = min;
522
+ if (next < min) next = max;
523
+ }
524
+ setSectionValue(section, next);
525
+ }, [normalizedMinuteStep, resolvedFormat, sections, setSectionValue]);
526
+ var commitPendingBuffer = useCallback(function () {
527
+ var _editStateRef$current = editStateRef.current,
528
+ section = _editStateRef$current.section,
529
+ buffer = _editStateRef$current.buffer;
530
+ if (!buffer) return;
531
+ var numeric = Number(buffer);
532
+ var _getSectionLimits2 = getSectionLimits(section, resolvedFormat, sections),
533
+ min = _getSectionLimits2.min,
534
+ max = _getSectionLimits2.max;
535
+ if (!Number.isNaN(numeric) && numeric >= min && numeric <= max) {
536
+ setSectionValue(section, numeric);
537
+ }
538
+ editStateRef.current = {
539
+ section: section,
540
+ buffer: ''
541
+ };
542
+ setPendingDigits({});
543
+ }, [resolvedFormat, sections, setPendingDigits, setSectionValue]);
544
+ var applyDigit = useCallback(function (digit) {
545
+ if (activeSection === SECTION_PERIOD) return activeSection;
546
+ var key = getSectionKeyFromType(sectionTypeFromSection(activeSection));
547
+ var needed = digitsPerSection(activeSection);
548
+ var sameSection = editStateRef.current.section === activeSection;
549
+ if (!sameSection) commitPendingBuffer();
550
+ var currentBuffer = sameSection ? editStateRef.current.buffer : '';
551
+ var nextBuffer = "" + currentBuffer + digit;
552
+ editStateRef.current = {
553
+ section: activeSection,
554
+ buffer: nextBuffer
555
+ };
556
+ if (nextBuffer.length < needed) {
557
+ var _getSectionLimits3 = getSectionLimits(activeSection, resolvedFormat, sections),
558
+ _max = _getSectionLimits3.max;
559
+ if (Number(nextBuffer) * 10 <= _max) {
560
+ var _setPendingDigits;
561
+ setPendingDigits((_setPendingDigits = {}, _setPendingDigits[key] = nextBuffer, _setPendingDigits));
562
+ return activeSection;
563
+ }
564
+ // first digit * 10 exceeds max — commit it directly
565
+ }
566
+ var numeric = Number(nextBuffer);
567
+ var _getSectionLimits4 = getSectionLimits(activeSection, resolvedFormat, sections),
568
+ min = _getSectionLimits4.min,
569
+ max = _getSectionLimits4.max;
570
+ if (Number.isNaN(numeric) || numeric < min || numeric > max) {
571
+ announce(formatMessage({
572
+ id: 'dateTimeInput.invalidDateTime',
573
+ messages: messages
574
+ }), 'assertive');
575
+ editStateRef.current = {
576
+ section: activeSection,
577
+ buffer: ''
578
+ };
579
+ setPendingDigits({});
580
+ return activeSection;
581
+ }
582
+ setPendingDigits({});
583
+ editStateRef.current = {
584
+ section: activeSection,
585
+ buffer: ''
586
+ };
587
+ setSectionValue(activeSection, numeric);
588
+ var sectionIndex = sectionOrder.indexOf(activeSection);
589
+ var isLastSection = sectionIndex === sectionOrder.length - 1;
590
+ if (!isLastSection) return moveSection(1);
591
+ return activeSection;
592
+ }, [activeSection, announce, commitPendingBuffer, formatMessage, messages, moveSection, resolvedFormat, sectionOrder, sections, setPendingDigits, setSectionValue]);
593
+ var clearActiveSection = useCallback(function () {
594
+ setPendingDigits({});
595
+ editStateRef.current = {
596
+ section: activeSection,
597
+ buffer: ''
598
+ };
599
+ setSectionValue(activeSection, undefined);
600
+ }, [activeSection, setPendingDigits, setSectionValue]);
601
+ var getDisplayText = useCallback(function (section) {
602
+ var key = getSectionKeyFromType(sectionTypeFromSection(section));
603
+ var pending = pendingDigits[key];
604
+ if (pending !== undefined) {
605
+ if (section === SECTION_YEAR) return pending.padEnd(4, 'y');
606
+ if (section === SECTION_PERIOD) return pending;
607
+ return pending.padStart(2, '0');
608
+ }
609
+ return formatSectionText(section, sections[key]);
610
+ }, [pendingDigits, sections]);
611
+ var displaySections = useMemo(function () {
612
+ return sectionOrder.map(function (section, index) {
613
+ var _separatorMap$section;
614
+ var key = getSectionKeyFromType(sectionTypeFromSection(section));
615
+ return {
616
+ section: section,
617
+ prefix: index === 0 ? '' : (_separatorMap$section = separatorMap[section]) != null ? _separatorMap$section : separatorBeforeSection(section),
618
+ text: getDisplayText(section),
619
+ filled: sections[key] !== undefined || pendingDigits[key] !== undefined
620
+ };
621
+ });
622
+ }, [getDisplayText, pendingDigits, sectionOrder, sections, separatorMap]);
623
+ var placeholder = useMemo(function () {
624
+ return sectionOrder.map(function (section, index) {
625
+ var _separatorMap$section2;
626
+ var token = getSectionTokenFromType(sectionTypeFromSection(section));
627
+ if (index === 0) return token;
628
+ var prefix = (_separatorMap$section2 = separatorMap[section]) != null ? _separatorMap$section2 : separatorBeforeSection(section);
629
+ return "" + prefix + token;
630
+ }).join('');
631
+ }, [sectionOrder, separatorMap]);
632
+ var hasDisplayValue = hasAnyValue(sections) || Object.keys(pendingDigits).length > 0;
633
+ var inputValue = hasDisplayValue ? displaySections.map(function (_ref2) {
634
+ var prefix = _ref2.prefix,
635
+ text = _ref2.text;
636
+ return "" + prefix + text;
637
+ }).join('') : placeholder;
638
+ var sectionValueAnnouncement = useCallback(function (section) {
639
+ var nameText = getSectionName(section, formatMessage, messages);
640
+ var key = getSectionKeyFromType(sectionTypeFromSection(section));
641
+ var raw = sections[key];
642
+ if (raw === undefined) {
643
+ return formatMessage({
644
+ id: 'dateTimeInput.activeSection',
645
+ messages: messages,
646
+ values: {
647
+ section: nameText
648
+ }
649
+ });
650
+ }
651
+ return formatMessage({
652
+ id: 'dateTimeInput.activeSectionValue',
653
+ messages: messages,
654
+ values: {
655
+ value: raw
656
+ }
657
+ });
658
+ }, [formatMessage, messages, sections]);
659
+ var openPicker = useCallback(function () {
660
+ if (disabled || readOnly) return;
661
+ if (!segmentFocused) {
662
+ activeSectionRef.current = firstSection;
663
+ setActiveSection(firstSection);
664
+ }
665
+ setOpen(true);
666
+ announce(formatMessage({
667
+ id: 'dateTimeInput.openDrop',
668
+ messages: messages
669
+ }));
670
+ }, [announce, disabled, firstSection, formatMessage, messages, readOnly, segmentFocused, setActiveSection]);
671
+ var closePicker = useCallback(function () {
672
+ setSegmentFocused(false);
673
+ suppressSegmentFocusRef.current = true;
674
+ setOpen(false);
675
+ requestAnimationFrame(function () {
676
+ if (inline) {
677
+ var _triggerRef$current;
678
+ (_triggerRef$current = triggerRef.current) == null || _triggerRef$current.focus();
679
+ return;
680
+ }
681
+ focusSection(activeSectionRef.current);
682
+ });
683
+ }, [focusSection, inline]);
684
+ var onDisplaySectionMouseDown = useCallback(function (section, event) {
685
+ if (readOnly || disabled) return;
686
+ if (event.button !== 0 || event.defaultPrevented) return;
687
+ event.preventDefault();
688
+ event.stopPropagation();
689
+ commitPendingBuffer();
690
+ setSegmentFocused(true);
691
+ setActiveSection(section);
692
+ focusSection(section);
693
+ }, [commitPendingBuffer, disabled, focusSection, readOnly, setActiveSection]);
694
+ var onDisplayMouseDown = useCallback(function (event) {
695
+ var _sectionNode$dataset;
696
+ if (readOnly || event.button !== 0 || event.defaultPrevented) return;
697
+ var sectionNode = event.target.closest == null ? void 0 : event.target.closest('[data-section]');
698
+ if ((sectionNode == null || (_sectionNode$dataset = sectionNode.dataset) == null ? void 0 : _sectionNode$dataset.section) !== undefined) {
699
+ var section = Number(sectionNode.dataset.section);
700
+ if (!Number.isNaN(section)) {
701
+ onDisplaySectionMouseDown(section, event);
702
+ return;
703
+ }
704
+ }
705
+ event.preventDefault();
706
+ commitPendingBuffer();
707
+ setSegmentFocused(true);
708
+ setActiveSection(firstSection);
709
+ focusSection(firstSection);
710
+ }, [commitPendingBuffer, firstSection, focusSection, onDisplaySectionMouseDown, readOnly, setActiveSection]);
711
+ var onSegmentFocus = useCallback(function (section) {
712
+ if (suppressSegmentFocusRef.current) {
713
+ suppressSegmentFocusRef.current = false;
714
+ } else {
715
+ setSegmentFocused(true);
716
+ }
717
+ if (readOnly || disabled) return;
718
+ setActiveSection(section);
719
+ }, [disabled, readOnly, setActiveSection]);
720
+ var onSegmentBlur = useCallback(function () {
721
+ requestAnimationFrame(function () {
722
+ var _document = document,
723
+ activeElement = _document.activeElement;
724
+ var isSegmentActive = Object.values(segmentRefs.current).includes(activeElement);
725
+ if (!isSegmentActive && activeElement === document.body && !readOnly && !disabled) {
726
+ focusSection(activeSection);
727
+ return;
728
+ }
729
+ if (!isSegmentActive) {
730
+ setSegmentFocused(false);
731
+ }
732
+ });
733
+ }, [activeSection, disabled, focusSection, readOnly]);
734
+ var onSegmentKeyDown = useCallback(function (section, event) {
735
+ if (readOnly || disabled) return;
736
+ if (!segmentFocused) setSegmentFocused(true);
737
+ var key = event.key;
738
+ if (activeSection !== section) setActiveSection(section);
739
+ if (key === 'ArrowRight') {
740
+ event.preventDefault();
741
+ commitPendingBuffer();
742
+ var next = moveSection(1);
743
+ focusSection(next);
744
+ return;
745
+ }
746
+ if (key === 'ArrowLeft') {
747
+ event.preventDefault();
748
+ commitPendingBuffer();
749
+ var _next = moveSection(-1);
750
+ focusSection(_next);
751
+ return;
752
+ }
753
+ if (key === 'Home') {
754
+ event.preventDefault();
755
+ commitPendingBuffer();
756
+ setActiveSection(firstSection);
757
+ focusSection(firstSection);
758
+ return;
759
+ }
760
+ if (key === 'End') {
761
+ event.preventDefault();
762
+ commitPendingBuffer();
763
+ setActiveSection(lastSection);
764
+ focusSection(lastSection);
765
+ return;
766
+ }
767
+ if (key === 'ArrowUp') {
768
+ event.preventDefault();
769
+ incrementSection(section, open ? -1 : 1);
770
+ return;
771
+ }
772
+ if (key === 'ArrowDown') {
773
+ if (event.altKey) {
774
+ event.preventDefault();
775
+ openPicker();
776
+ return;
777
+ }
778
+ event.preventDefault();
779
+ incrementSection(section, open ? 1 : -1);
780
+ return;
781
+ }
782
+ if (key === 'Delete' || key === 'Backspace') {
783
+ event.preventDefault();
784
+ clearActiveSection();
785
+ return;
786
+ }
787
+ if (key === 'Enter') {
788
+ event.preventDefault();
789
+ if (open) closePicker();
790
+ return;
791
+ }
792
+ if (key === 'Escape' && open) {
793
+ event.preventDefault();
794
+ closePicker();
795
+ return;
796
+ }
797
+ if (key === ' ' || key === 'Spacebar') {
798
+ event.preventDefault();
799
+ openPicker();
800
+ return;
801
+ }
802
+ if (resolvedFormat === '12' && section === SECTION_PERIOD) {
803
+ var lower = key.toLowerCase();
804
+ if (lower === 'a') {
805
+ event.preventDefault();
806
+ setSectionValue(SECTION_PERIOD, 'AM');
807
+ } else if (lower === 'p') {
808
+ event.preventDefault();
809
+ setSectionValue(SECTION_PERIOD, 'PM');
810
+ }
811
+ return;
812
+ }
813
+ if (/^\d$/.test(key)) {
814
+ event.preventDefault();
815
+ var _next2 = applyDigit(Number(key));
816
+ if (_next2 !== section) {
817
+ focusSection(_next2);
818
+ } else {
819
+ event.currentTarget.focus();
820
+ }
821
+ }
822
+ }, [activeSection, applyDigit, clearActiveSection, closePicker, commitPendingBuffer, disabled, firstSection, focusSection, incrementSection, lastSection, moveSection, open, openPicker, readOnly, resolvedFormat, segmentFocused, setActiveSection, setSectionValue]);
823
+ useEffect(function () {
824
+ if (!segmentFocused || readOnly || disabled) return;
825
+ var activeSegment = segmentRefs.current[activeSection];
826
+ if (activeSegment && document.activeElement !== activeSegment) {
827
+ activeSegment.focus();
828
+ }
829
+ }, [activeSection, disabled, readOnly, segmentFocused]);
830
+ var handleCalendarSelect = useCallback(function (nextDateValue) {
831
+ var parsed = parseCalendarSelection(nextDateValue);
832
+ if (!parsed) return;
833
+ var seededTimeSections = getSeededTimeSections(sections, resolvedFormat, showSeconds);
834
+ var nextSections = _extends({}, sections, {
835
+ day: parsed.day,
836
+ month: parsed.month,
837
+ year: parsed.year
838
+ }, seededTimeSections);
839
+ var isSameSelection = nextSections.day === sections.day && nextSections.month === sections.month && nextSections.year === sections.year && nextSections.hour === sections.hour && nextSections.minute === sections.minute && nextSections.second === sections.second && nextSections.period === sections.period;
840
+ if (isSameSelection) return;
841
+ suppressTimePartialSyncRef.current = true;
842
+ setPendingDigits({});
843
+ editStateRef.current = {
844
+ section: activeSection,
845
+ buffer: ''
846
+ };
847
+ activeSectionRef.current = SECTION_HOUR;
848
+ setActiveSection(SECTION_HOUR);
849
+ commitSections(nextSections);
850
+ }, [activeSection, commitSections, resolvedFormat, sections, setActiveSection, setPendingDigits, showSeconds]);
851
+ var handleTimeSelect = useCallback(function (_ref3) {
852
+ var nextTime = _ref3.value;
853
+ setPendingDigits({});
854
+ editStateRef.current = {
855
+ section: activeSection,
856
+ buffer: ''
857
+ };
858
+ if (!nextTime) {
859
+ // Incomplete picker selection — preserve existing sections.
860
+ return;
861
+ }
862
+ var match = /^(\d{2}):(\d{2}):(\d{2})$/.exec(nextTime);
863
+ if (!match) return;
864
+ var hour24 = Number(match[1]);
865
+ var minute = Number(match[2]);
866
+ var second = Number(match[3]);
867
+ var nextHour;
868
+ var nextPeriod;
869
+ if (resolvedFormat === '12') {
870
+ nextPeriod = hour24 < 12 ? 'AM' : 'PM';
871
+ nextHour = hour24 % 12 || 12;
872
+ } else {
873
+ nextHour = hour24;
874
+ nextPeriod = undefined;
875
+ }
876
+ commitSections(_extends({}, sections, getSeededDateSections(sections), {
877
+ hour: nextHour,
878
+ minute: minute,
879
+ second: second,
880
+ period: nextPeriod
881
+ }));
882
+ }, [activeSection, commitSections, resolvedFormat, sections, setPendingDigits]);
883
+
884
+ // TimeInput section constants (0–3) → DateTimeInput section constants (3–6)
885
+ var TIME_TO_DT_SECTION = useMemo(function () {
886
+ return [SECTION_HOUR, SECTION_MINUTE, SECTION_SECOND, SECTION_PERIOD];
887
+ }, []);
888
+ var handleTimePartialChange = useCallback(function (timeSections, changedTimeSectionIndex) {
889
+ if (suppressTimePartialSyncRef.current) {
890
+ suppressTimePartialSyncRef.current = false;
891
+ return;
892
+ }
893
+
894
+ // Sync the active spinbutton segment to the column being edited
895
+ var dtSection = TIME_TO_DT_SECTION[changedTimeSectionIndex];
896
+ if (dtSection !== undefined) setActiveSection(dtSection);
897
+
898
+ // Update display state with partial time fields immediately so the
899
+ // input shows values as each column is selected, not only on
900
+ // completion.
901
+ setSections(function (prev) {
902
+ var _timeSections$hour, _timeSections$minute, _timeSections$second, _timeSections$period;
903
+ var mergedTimeSections = _extends({}, prev, {
904
+ hour: (_timeSections$hour = timeSections.hour) != null ? _timeSections$hour : prev.hour,
905
+ minute: (_timeSections$minute = timeSections.minute) != null ? _timeSections$minute : prev.minute
906
+ }, showSeconds ? {
907
+ second: (_timeSections$second = timeSections.second) != null ? _timeSections$second : prev.second
908
+ } : {}, resolvedFormat === '12' ? {
909
+ period: (_timeSections$period = timeSections.period) != null ? _timeSections$period : prev.period
910
+ } : {});
911
+ return _extends({}, prev, getSeededDateSections(prev), getSeededTimeSections(mergedTimeSections, resolvedFormat, showSeconds));
912
+ });
913
+ }, [resolvedFormat, setActiveSection, setSections, showSeconds, TIME_TO_DT_SECTION]);
914
+ var timeValue = useMemo(function () {
915
+ var _sections$minute2, _sections$second2;
916
+ if (sections.hour === undefined) {
917
+ return undefined;
918
+ }
919
+ if (showSeconds && sections.second === undefined) return undefined;
920
+ var minute = (_sections$minute2 = sections.minute) != null ? _sections$minute2 : 0;
921
+ var resolvedSecond = (_sections$second2 = sections.second) != null ? _sections$second2 : 0;
922
+ if (resolvedFormat === '12') {
923
+ var hour24 = sections.hour % 12;
924
+ if ((sections.period || 'AM') === 'PM') hour24 += 12;
925
+ return pad(hour24) + ":" + pad(minute) + ":" + pad(resolvedSecond);
926
+ }
927
+ return pad(sections.hour) + ":" + pad(minute) + ":" + pad(resolvedSecond);
928
+ }, [resolvedFormat, sections, showSeconds]);
929
+ var showActiveSection = (segmentFocused || open) && !readOnly && !disabled;
930
+ var _formContext$useFormF = formContext.useFormField({}),
931
+ inForm = _formContext$useFormF.inForm;
932
+ var formFieldLabelId = inForm && id ? "grommet-" + id + "__label" : undefined;
933
+ var groupLabel = formFieldLabelId ? undefined : formatMessage({
934
+ id: 'dateTimeInput.inputLabel',
935
+ messages: messages
936
+ });
937
+ var CalendarIcon = ((_theme$dateTimeInput2 = theme.dateTimeInput) == null || (_theme$dateTimeInput2 = _theme$dateTimeInput2.icon) == null ? void 0 : _theme$dateTimeInput2.calendar) || GrommetCalendarIcon;
938
+ var dropTarget = inline ? triggerRef.current : containerRef.current;
939
+ var generatedId = useId();
940
+ var dropId = (id || generatedId) + "__drop";
941
+ return /*#__PURE__*/React.createElement(Keyboard, {
942
+ onEsc: open ? closePicker : undefined
943
+ }, /*#__PURE__*/React.createElement(Box, null, inline ? /*#__PURE__*/React.createElement(Box, {
944
+ direction: "row",
945
+ align: "center"
946
+ }, /*#__PURE__*/React.createElement(Button, {
947
+ ref: triggerRef,
948
+ icon: /*#__PURE__*/React.createElement(CalendarIcon, null),
949
+ plain: true,
950
+ disabled: disabled || readOnly,
951
+ "aria-label": formatMessage({
952
+ id: 'dateTimeInput.chooseDateTime',
953
+ messages: messages
954
+ }),
955
+ "aria-haspopup": "dialog",
956
+ "aria-expanded": open,
957
+ "aria-controls": dropId,
958
+ onClick: open ? closePicker : openPicker
959
+ })) : /*#__PURE__*/React.createElement(StyledDateTimeInputContainer, _extends({
960
+ ref: containerRef,
961
+ direction: "row",
962
+ border: !plainProp,
963
+ fill: true,
964
+ round: (_theme$dateTimeInput3 = theme.dateTimeInput) == null || (_theme$dateTimeInput3 = _theme$dateTimeInput3.container) == null ? void 0 : _theme$dateTimeInput3.round,
965
+ disabled: disabled,
966
+ readOnlyProp: readOnly,
967
+ focusIndicator: focusIndicatorProp != null ? focusIndicatorProp : true
968
+ }, passThemeFlag), /*#__PURE__*/React.createElement(StyledDateTimeInputField, passThemeFlag, /*#__PURE__*/React.createElement(StyledDateTimeInputDisplay, _extends({
969
+ role: "group",
970
+ "aria-label": groupLabel,
971
+ "aria-labelledby": formFieldLabelId,
972
+ onMouseDown: onDisplayMouseDown
973
+ }, passThemeFlag), displaySections.map(function (_ref4) {
974
+ var section = _ref4.section,
975
+ prefix = _ref4.prefix,
976
+ text = _ref4.text,
977
+ filled = _ref4.filled;
978
+ var sectionLimits = getSectionLimits(section, resolvedFormat, sections);
979
+ var key = getSectionKeyFromType(sectionTypeFromSection(section));
980
+ var numericValue;
981
+ if (section === SECTION_PERIOD) {
982
+ numericValue = sections[key] === 'PM' ? 1 : 0;
983
+ } else {
984
+ var _sections$key;
985
+ numericValue = (_sections$key = sections[key]) != null ? _sections$key : sectionLimits.min;
986
+ }
987
+ return /*#__PURE__*/React.createElement(React.Fragment, {
988
+ key: section
989
+ }, !!prefix && /*#__PURE__*/React.createElement(StyledDateTimeInputSeparator, _extends({
990
+ $filled: hasDisplayValue,
991
+ $paddingInline: dateTimeSeparatorPadding
992
+ }, passThemeFlag), prefix), /*#__PURE__*/React.createElement(StyledDateTimeInputSegment, _extends({
993
+ ref: function ref(segmentNode) {
994
+ segmentRefs.current[section] = segmentNode;
995
+ },
996
+ tabIndex: !readOnly && !disabled && activeSection === section ? 0 : -1,
997
+ $active: showActiveSection && activeSection === section,
998
+ $filled: filled,
999
+ onFocus: function onFocus() {
1000
+ return onSegmentFocus(section);
1001
+ },
1002
+ onBlur: onSegmentBlur,
1003
+ onKeyDown: function onKeyDown(event) {
1004
+ return onSegmentKeyDown(section, event);
1005
+ },
1006
+ "data-section": section,
1007
+ role: "spinbutton",
1008
+ "aria-label": getSectionName(section, formatMessage, messages),
1009
+ "aria-disabled": disabled || undefined,
1010
+ "aria-readonly": readOnly || undefined,
1011
+ "aria-valuenow": numericValue,
1012
+ "aria-valuemin": sectionLimits.min,
1013
+ "aria-valuemax": sectionLimits.max,
1014
+ "aria-valuetext": sectionValueAnnouncement(section)
1015
+ }, passThemeFlag), text));
1016
+ })), /*#__PURE__*/React.createElement(StyledDateTimeInput, _extends({
1017
+ tabIndex: -1
1018
+ }, rest, {
1019
+ id: id,
1020
+ ref: inputRef,
1021
+ value: inputValue,
1022
+ "aria-hidden": "true",
1023
+ disabled: disabled,
1024
+ readOnly: true,
1025
+ focusIndicator: false,
1026
+ plain: true
1027
+ }))), !readOnly && /*#__PURE__*/React.createElement(Button, {
1028
+ ref: triggerRef,
1029
+ icon: /*#__PURE__*/React.createElement(CalendarIcon, null),
1030
+ plain: true,
1031
+ disabled: disabled,
1032
+ margin: (_theme$dateTimeInput4 = theme.dateTimeInput) == null || (_theme$dateTimeInput4 = _theme$dateTimeInput4.button) == null ? void 0 : _theme$dateTimeInput4.margin,
1033
+ "aria-label": formatMessage({
1034
+ id: 'dateTimeInput.chooseDateTime',
1035
+ messages: messages
1036
+ }),
1037
+ "aria-haspopup": "dialog",
1038
+ "aria-expanded": open,
1039
+ "aria-controls": dropId,
1040
+ onClick: open ? closePicker : openPicker
1041
+ })), name && /*#__PURE__*/React.createElement("input", {
1042
+ "aria-hidden": "true",
1043
+ name: name,
1044
+ readOnly: true,
1045
+ tabIndex: -1,
1046
+ type: "hidden",
1047
+ value: value || ''
1048
+ }), open && /*#__PURE__*/React.createElement(Drop, {
1049
+ id: dropId,
1050
+ target: dropTarget,
1051
+ align: {
1052
+ top: 'bottom',
1053
+ left: 'left'
1054
+ },
1055
+ onEsc: closePicker,
1056
+ onClickOutside: closePicker
1057
+ }, /*#__PURE__*/React.createElement(Box, {
1058
+ direction: "row",
1059
+ pad: (_theme$dateTimeInput5 = theme.dateTimeInput) == null || (_theme$dateTimeInput5 = _theme$dateTimeInput5.drop) == null ? void 0 : _theme$dateTimeInput5.pad,
1060
+ gap: (_theme$dateTimeInput6 = theme.dateTimeInput) == null || (_theme$dateTimeInput6 = _theme$dateTimeInput6.drop) == null ? void 0 : _theme$dateTimeInput6.gap
1061
+ }, /*#__PURE__*/React.createElement(Calendar, {
1062
+ date: getCalendarDate(sections),
1063
+ initialFocus: "days",
1064
+ onSelect: handleCalendarSelect
1065
+ }), /*#__PURE__*/React.createElement(Box, {
1066
+ alignSelf: "stretch",
1067
+ flex: false,
1068
+ border: {
1069
+ side: 'start',
1070
+ color: (_theme$dateTimeInput7 = theme.dateTimeInput) == null || (_theme$dateTimeInput7 = _theme$dateTimeInput7.drop) == null || (_theme$dateTimeInput7 = _theme$dateTimeInput7.border) == null ? void 0 : _theme$dateTimeInput7.color,
1071
+ size: (_theme$dateTimeInput8 = theme.dateTimeInput) == null || (_theme$dateTimeInput8 = _theme$dateTimeInput8.drop) == null || (_theme$dateTimeInput8 = _theme$dateTimeInput8.border) == null ? void 0 : _theme$dateTimeInput8.size
1072
+ }
1073
+ }), /*#__PURE__*/React.createElement(TimeInput, {
1074
+ inline: true,
1075
+ format: resolvedFormat,
1076
+ value: timeValue,
1077
+ showSeconds: showSeconds,
1078
+ messages: messages,
1079
+ minuteStep: normalizedMinuteStep,
1080
+ disabled: disabled,
1081
+ readOnly: readOnly,
1082
+ onChange: handleTimeSelect,
1083
+ onPartialChange: handleTimePartialChange
1084
+ })))));
1085
+ });
1086
+ DateTimeInput.displayName = 'DateTimeInput';
1087
+ if (process.env.NODE_ENV !== 'production') {
1088
+ DateTimeInput.propTypes = DateTimeInputPropTypes;
1089
+ }
1090
+ export { DateTimeInput };