@mui/x-date-pickers 8.0.0-beta.1 → 8.0.0-beta.2

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 (114) hide show
  1. package/CHANGELOG.md +94 -0
  2. package/DateCalendar/DateCalendar.js +4 -8
  3. package/DateCalendar/DayCalendar.js +2 -2
  4. package/DatePicker/shared.js +3 -9
  5. package/DateTimePicker/shared.js +3 -13
  6. package/MonthCalendar/MonthCalendar.js +4 -9
  7. package/PickersSectionList/PickersSectionList.d.ts +1 -1
  8. package/PickersSectionList/PickersSectionList.types.d.ts +2 -6
  9. package/TimePicker/shared.js +3 -3
  10. package/YearCalendar/YearCalendar.js +4 -10
  11. package/esm/DateCalendar/DateCalendar.js +6 -10
  12. package/esm/DateCalendar/DayCalendar.js +2 -2
  13. package/esm/DatePicker/shared.js +3 -9
  14. package/esm/DateTimePicker/shared.js +4 -14
  15. package/esm/MonthCalendar/MonthCalendar.js +6 -11
  16. package/esm/PickersSectionList/PickersSectionList.d.ts +1 -1
  17. package/esm/PickersSectionList/PickersSectionList.types.d.ts +2 -6
  18. package/esm/TimePicker/shared.js +3 -3
  19. package/esm/YearCalendar/YearCalendar.js +5 -11
  20. package/esm/index.js +1 -1
  21. package/esm/internals/hooks/useField/syncSelectionToDOM.d.ts +9 -0
  22. package/esm/internals/hooks/useField/syncSelectionToDOM.js +50 -0
  23. package/esm/internals/hooks/useField/useField.types.d.ts +8 -1
  24. package/esm/internals/hooks/useField/useField.utils.d.ts +1 -3
  25. package/esm/internals/hooks/useField/useField.utils.js +0 -57
  26. package/esm/internals/hooks/useField/useFieldHiddenInputProps.d.ts +20 -0
  27. package/esm/internals/hooks/useField/useFieldHiddenInputProps.js +31 -0
  28. package/esm/internals/hooks/useField/useFieldInternalPropsWithDefaults.js +8 -10
  29. package/esm/internals/hooks/useField/useFieldRootHandleKeyDown.js +1 -0
  30. package/esm/internals/hooks/useField/useFieldRootProps.d.ts +32 -0
  31. package/esm/internals/hooks/useField/useFieldRootProps.js +150 -0
  32. package/esm/internals/hooks/useField/useFieldSectionContainerProps.d.ts +15 -0
  33. package/esm/internals/hooks/useField/useFieldSectionContainerProps.js +29 -0
  34. package/esm/internals/hooks/useField/useFieldSectionContentProps.d.ts +23 -0
  35. package/esm/internals/hooks/useField/useFieldSectionContentProps.js +226 -0
  36. package/esm/internals/hooks/useField/useFieldV7TextField.js +76 -307
  37. package/esm/internals/index.d.ts +4 -4
  38. package/esm/internals/index.js +3 -3
  39. package/esm/locales/deDE.js +2 -3
  40. package/esm/managers/useDateManager.d.ts +4 -13
  41. package/esm/managers/useDateManager.js +18 -28
  42. package/esm/managers/useDateTimeManager.d.ts +4 -13
  43. package/esm/managers/useDateTimeManager.js +23 -33
  44. package/esm/managers/useTimeManager.d.ts +4 -13
  45. package/esm/managers/useTimeManager.js +14 -24
  46. package/esm/models/manager.d.ts +3 -8
  47. package/esm/validation/validateDate.js +3 -4
  48. package/index.js +1 -1
  49. package/internals/hooks/useField/syncSelectionToDOM.d.ts +9 -0
  50. package/internals/hooks/useField/syncSelectionToDOM.js +56 -0
  51. package/internals/hooks/useField/useField.types.d.ts +8 -1
  52. package/internals/hooks/useField/useField.utils.d.ts +1 -3
  53. package/internals/hooks/useField/useField.utils.js +2 -61
  54. package/internals/hooks/useField/useFieldHiddenInputProps.d.ts +20 -0
  55. package/internals/hooks/useField/useFieldHiddenInputProps.js +39 -0
  56. package/internals/hooks/useField/useFieldInternalPropsWithDefaults.js +8 -10
  57. package/internals/hooks/useField/useFieldRootHandleKeyDown.js +1 -0
  58. package/internals/hooks/useField/useFieldRootProps.d.ts +32 -0
  59. package/internals/hooks/useField/useFieldRootProps.js +156 -0
  60. package/internals/hooks/useField/useFieldSectionContainerProps.d.ts +15 -0
  61. package/internals/hooks/useField/useFieldSectionContainerProps.js +37 -0
  62. package/internals/hooks/useField/useFieldSectionContentProps.d.ts +23 -0
  63. package/internals/hooks/useField/useFieldSectionContentProps.js +234 -0
  64. package/internals/hooks/useField/useFieldV7TextField.js +75 -306
  65. package/internals/index.d.ts +4 -4
  66. package/internals/index.js +18 -18
  67. package/locales/deDE.js +2 -3
  68. package/managers/useDateManager.d.ts +4 -13
  69. package/managers/useDateManager.js +18 -28
  70. package/managers/useDateTimeManager.d.ts +4 -13
  71. package/managers/useDateTimeManager.js +23 -33
  72. package/managers/useTimeManager.d.ts +4 -13
  73. package/managers/useTimeManager.js +15 -25
  74. package/models/manager.d.ts +3 -8
  75. package/modern/DateCalendar/DateCalendar.js +6 -10
  76. package/modern/DateCalendar/DayCalendar.js +2 -2
  77. package/modern/DatePicker/shared.js +3 -9
  78. package/modern/DateTimePicker/shared.js +4 -14
  79. package/modern/MonthCalendar/MonthCalendar.js +6 -11
  80. package/modern/PickersSectionList/PickersSectionList.d.ts +1 -1
  81. package/modern/PickersSectionList/PickersSectionList.types.d.ts +2 -6
  82. package/modern/TimePicker/shared.js +3 -3
  83. package/modern/YearCalendar/YearCalendar.js +5 -11
  84. package/modern/index.js +1 -1
  85. package/modern/internals/hooks/useField/syncSelectionToDOM.d.ts +9 -0
  86. package/modern/internals/hooks/useField/syncSelectionToDOM.js +50 -0
  87. package/modern/internals/hooks/useField/useField.types.d.ts +8 -1
  88. package/modern/internals/hooks/useField/useField.utils.d.ts +1 -3
  89. package/modern/internals/hooks/useField/useField.utils.js +0 -57
  90. package/modern/internals/hooks/useField/useFieldHiddenInputProps.d.ts +20 -0
  91. package/modern/internals/hooks/useField/useFieldHiddenInputProps.js +31 -0
  92. package/modern/internals/hooks/useField/useFieldInternalPropsWithDefaults.js +8 -10
  93. package/modern/internals/hooks/useField/useFieldRootHandleKeyDown.js +1 -0
  94. package/modern/internals/hooks/useField/useFieldRootProps.d.ts +32 -0
  95. package/modern/internals/hooks/useField/useFieldRootProps.js +150 -0
  96. package/modern/internals/hooks/useField/useFieldSectionContainerProps.d.ts +15 -0
  97. package/modern/internals/hooks/useField/useFieldSectionContainerProps.js +29 -0
  98. package/modern/internals/hooks/useField/useFieldSectionContentProps.d.ts +23 -0
  99. package/modern/internals/hooks/useField/useFieldSectionContentProps.js +226 -0
  100. package/modern/internals/hooks/useField/useFieldV7TextField.js +76 -307
  101. package/modern/internals/index.d.ts +4 -4
  102. package/modern/internals/index.js +3 -3
  103. package/modern/locales/deDE.js +2 -3
  104. package/modern/managers/useDateManager.d.ts +4 -13
  105. package/modern/managers/useDateManager.js +18 -28
  106. package/modern/managers/useDateTimeManager.d.ts +4 -13
  107. package/modern/managers/useDateTimeManager.js +23 -33
  108. package/modern/managers/useTimeManager.d.ts +4 -13
  109. package/modern/managers/useTimeManager.js +14 -24
  110. package/modern/models/manager.d.ts +3 -8
  111. package/modern/validation/validateDate.js +3 -4
  112. package/package.json +2 -2
  113. package/tsconfig.build.tsbuildinfo +1 -1
  114. package/validation/validateDate.js +3 -4
@@ -0,0 +1,226 @@
1
+ import * as React from 'react';
2
+ import useEventCallback from '@mui/utils/useEventCallback';
3
+ import useId from '@mui/utils/useId';
4
+ import { useUtils } from "../useUtils.js";
5
+ import { usePickerTranslations } from "../../../hooks/index.js";
6
+ import { syncSelectionToDOM } from "./syncSelectionToDOM.js";
7
+ /**
8
+ * Generate the props to pass to the content element of each section of the field.
9
+ * It is not used by the non-accessible DOM structure (with an <input /> element for editing).
10
+ * It should be used in the MUI accessible DOM structure and the Base UI implementation.
11
+ * @param {UseFieldRootPropsParameters} parameters The parameters of the hook.
12
+ * @returns {UseFieldRootPropsReturnValue} The props to forward to the content element of each section of the field.
13
+ */
14
+ export function useFieldSectionContentProps(parameters) {
15
+ const utils = useUtils();
16
+ const translations = usePickerTranslations();
17
+ const id = useId();
18
+ const {
19
+ focused,
20
+ domGetters,
21
+ stateResponse,
22
+ applyCharacterEditing,
23
+ manager: {
24
+ internal_fieldValueManager: fieldValueManager
25
+ },
26
+ stateResponse: {
27
+ // States and derived states
28
+ parsedSelectedSections,
29
+ sectionsValueBoundaries,
30
+ state,
31
+ value,
32
+ // Methods to update the states
33
+ clearActiveSection,
34
+ setCharacterQuery,
35
+ setSelectedSections,
36
+ updateSectionValue,
37
+ updateValueFromValueStr
38
+ },
39
+ internalPropsWithDefaults: {
40
+ disabled = false,
41
+ readOnly = false
42
+ }
43
+ } = parameters;
44
+ const isContainerEditable = parsedSelectedSections === 'all';
45
+ const isEditable = !isContainerEditable && !disabled && !readOnly;
46
+
47
+ /**
48
+ * If a section content has been updated with a value we don't want to keep,
49
+ * Then we need to imperatively revert it (we can't let React do it because the value did not change in his internal representation).
50
+ */
51
+ const revertDOMSectionChange = useEventCallback(sectionIndex => {
52
+ if (!domGetters.isReady()) {
53
+ return;
54
+ }
55
+ const section = state.sections[sectionIndex];
56
+ domGetters.getSectionContent(sectionIndex).innerHTML = section.value || section.placeholder;
57
+ syncSelectionToDOM({
58
+ focused,
59
+ domGetters,
60
+ stateResponse
61
+ });
62
+ });
63
+ const handleInput = useEventCallback(event => {
64
+ if (!domGetters.isReady()) {
65
+ return;
66
+ }
67
+ const target = event.target;
68
+ const keyPressed = target.textContent ?? '';
69
+ const sectionIndex = domGetters.getSectionIndexFromDOMElement(target);
70
+ const section = state.sections[sectionIndex];
71
+ if (readOnly) {
72
+ revertDOMSectionChange(sectionIndex);
73
+ return;
74
+ }
75
+ if (keyPressed.length === 0) {
76
+ if (section.value === '') {
77
+ revertDOMSectionChange(sectionIndex);
78
+ return;
79
+ }
80
+ const inputType = event.nativeEvent.inputType;
81
+ if (inputType === 'insertParagraph' || inputType === 'insertLineBreak') {
82
+ revertDOMSectionChange(sectionIndex);
83
+ return;
84
+ }
85
+ revertDOMSectionChange(sectionIndex);
86
+ clearActiveSection();
87
+ return;
88
+ }
89
+ applyCharacterEditing({
90
+ keyPressed,
91
+ sectionIndex
92
+ });
93
+
94
+ // The DOM value needs to remain the one React is expecting.
95
+ revertDOMSectionChange(sectionIndex);
96
+ });
97
+ const handleMouseUp = useEventCallback(event => {
98
+ // Without this, the browser will remove the selected when clicking inside an already-selected section.
99
+ event.preventDefault();
100
+ });
101
+ const handlePaste = useEventCallback(event => {
102
+ // prevent default to avoid the input `onInput` handler being called
103
+ event.preventDefault();
104
+ if (readOnly || disabled || typeof parsedSelectedSections !== 'number') {
105
+ return;
106
+ }
107
+ const activeSection = state.sections[parsedSelectedSections];
108
+ const pastedValue = event.clipboardData.getData('text');
109
+ const lettersOnly = /^[a-zA-Z]+$/.test(pastedValue);
110
+ const digitsOnly = /^[0-9]+$/.test(pastedValue);
111
+ const digitsAndLetterOnly = /^(([a-zA-Z]+)|)([0-9]+)(([a-zA-Z]+)|)$/.test(pastedValue);
112
+ const isValidPastedValue = activeSection.contentType === 'letter' && lettersOnly || activeSection.contentType === 'digit' && digitsOnly || activeSection.contentType === 'digit-with-letter' && digitsAndLetterOnly;
113
+ if (isValidPastedValue) {
114
+ setCharacterQuery(null);
115
+ updateSectionValue({
116
+ section: activeSection,
117
+ newSectionValue: pastedValue,
118
+ shouldGoToNextSection: true
119
+ });
120
+ }
121
+ // If the pasted value corresponds to a single section, but not the expected type, we skip the modification
122
+ else if (!lettersOnly && !digitsOnly) {
123
+ setCharacterQuery(null);
124
+ updateValueFromValueStr(pastedValue);
125
+ }
126
+ });
127
+ const handleDragOver = useEventCallback(event => {
128
+ event.preventDefault();
129
+ event.dataTransfer.dropEffect = 'none';
130
+ });
131
+ const createFocusHandler = useEventCallback(sectionIndex => () => {
132
+ setSelectedSections(sectionIndex);
133
+ });
134
+ return React.useCallback((section, sectionIndex) => {
135
+ const sectionBoundaries = sectionsValueBoundaries[section.type]({
136
+ currentDate: fieldValueManager.getDateFromSection(value, section),
137
+ contentType: section.contentType,
138
+ format: section.format
139
+ });
140
+ return {
141
+ // Event handlers
142
+ onInput: handleInput,
143
+ onPaste: handlePaste,
144
+ onMouseUp: handleMouseUp,
145
+ onDragOver: handleDragOver,
146
+ onFocus: createFocusHandler(sectionIndex),
147
+ // Aria attributes
148
+ 'aria-labelledby': `${id}-${section.type}`,
149
+ 'aria-readonly': readOnly,
150
+ 'aria-valuenow': getSectionValueNow(section, utils),
151
+ 'aria-valuemin': sectionBoundaries.minimum,
152
+ 'aria-valuemax': sectionBoundaries.maximum,
153
+ 'aria-valuetext': section.value ? getSectionValueText(section, utils) : translations.empty,
154
+ 'aria-label': translations[section.type],
155
+ 'aria-disabled': disabled,
156
+ // Other
157
+ tabIndex: isContainerEditable || sectionIndex > 0 ? -1 : 0,
158
+ contentEditable: !isContainerEditable && !disabled && !readOnly,
159
+ role: 'spinbutton',
160
+ id: `${id}-${section.type}`,
161
+ spellCheck: isEditable ? false : undefined,
162
+ autoCapitalize: isEditable ? 'off' : undefined,
163
+ autoCorrect: isEditable ? 'off' : undefined,
164
+ [parseInt(React.version, 10) >= 17 ? 'enterKeyHint' : 'enterkeyhint']: isEditable ? 'next' : undefined,
165
+ children: section.value || section.placeholder,
166
+ inputMode: section.contentType === 'letter' ? 'text' : 'numeric'
167
+ };
168
+ }, [sectionsValueBoundaries, id, isContainerEditable, disabled, readOnly, isEditable, translations, utils, handleInput, handlePaste, handleMouseUp, handleDragOver, createFocusHandler, fieldValueManager, value]);
169
+ }
170
+ function getSectionValueText(section, utils) {
171
+ if (!section.value) {
172
+ return undefined;
173
+ }
174
+ switch (section.type) {
175
+ case 'month':
176
+ {
177
+ if (section.contentType === 'digit') {
178
+ return utils.format(utils.setMonth(utils.date(), Number(section.value) - 1), 'month');
179
+ }
180
+ const parsedDate = utils.parse(section.value, section.format);
181
+ return parsedDate ? utils.format(parsedDate, 'month') : undefined;
182
+ }
183
+ case 'day':
184
+ return section.contentType === 'digit' ? utils.format(utils.setDate(utils.startOfYear(utils.date()), Number(section.value)), 'dayOfMonthFull') : section.value;
185
+ case 'weekDay':
186
+ // TODO: improve by providing the label of the week day
187
+ return undefined;
188
+ default:
189
+ return undefined;
190
+ }
191
+ }
192
+ function getSectionValueNow(section, utils) {
193
+ if (!section.value) {
194
+ return undefined;
195
+ }
196
+ switch (section.type) {
197
+ case 'weekDay':
198
+ {
199
+ if (section.contentType === 'letter') {
200
+ // TODO: improve by resolving the week day number from a letter week day
201
+ return undefined;
202
+ }
203
+ return Number(section.value);
204
+ }
205
+ case 'meridiem':
206
+ {
207
+ const parsedDate = utils.parse(`01:00 ${section.value}`, `${utils.formats.hours12h}:${utils.formats.minutes} ${section.format}`);
208
+ if (parsedDate) {
209
+ return utils.getHours(parsedDate) >= 12 ? 1 : 0;
210
+ }
211
+ return undefined;
212
+ }
213
+ case 'day':
214
+ return section.contentType === 'digit-with-letter' ? parseInt(section.value, 10) : Number(section.value);
215
+ case 'month':
216
+ {
217
+ if (section.contentType === 'digit') {
218
+ return Number(section.value);
219
+ }
220
+ const parsedDate = utils.parse(section.value, section.format);
221
+ return parsedDate ? utils.getMonth(parsedDate) + 1 : undefined;
222
+ }
223
+ default:
224
+ return section.contentType !== 'letter' ? Number(section.value) : undefined;
225
+ }
226
+ }