@mui/x-date-pickers 8.0.0-alpha.13 → 8.0.0-alpha.14

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 (197) hide show
  1. package/CHANGELOG.md +131 -0
  2. package/DatePicker/shared.d.ts +1 -1
  3. package/DateTimePicker/DateTimePicker.types.d.ts +2 -2
  4. package/DateTimePicker/DateTimePickerToolbar.d.ts +1 -1
  5. package/DateTimePicker/DateTimePickerToolbar.js +1 -1
  6. package/DateTimePicker/shared.d.ts +18 -10
  7. package/DateTimePicker/shared.js +23 -2
  8. package/DesktopDateTimePicker/DesktopDateTimePicker.js +2 -11
  9. package/DesktopDateTimePicker/DesktopDateTimePicker.types.d.ts +4 -7
  10. package/DesktopTimePicker/DesktopTimePicker.types.d.ts +2 -2
  11. package/MobileDateTimePicker/MobileDateTimePicker.d.ts +1 -2
  12. package/MobileDateTimePicker/MobileDateTimePicker.js +61 -6
  13. package/MobileDateTimePicker/MobileDateTimePicker.types.d.ts +1 -3
  14. package/PickersTextField/PickersTextField.js +3 -1
  15. package/StaticDateTimePicker/StaticDateTimePicker.js +60 -6
  16. package/StaticDateTimePicker/StaticDateTimePicker.types.d.ts +1 -2
  17. package/TimePicker/shared.d.ts +1 -1
  18. package/esm/DatePicker/shared.d.ts +1 -1
  19. package/esm/DateTimePicker/DateTimePicker.types.d.ts +2 -2
  20. package/esm/DateTimePicker/DateTimePickerToolbar.d.ts +1 -1
  21. package/esm/DateTimePicker/DateTimePickerToolbar.js +1 -1
  22. package/esm/DateTimePicker/shared.d.ts +18 -10
  23. package/esm/DateTimePicker/shared.js +23 -2
  24. package/esm/DesktopDateTimePicker/DesktopDateTimePicker.js +3 -12
  25. package/esm/DesktopDateTimePicker/DesktopDateTimePicker.types.d.ts +4 -7
  26. package/esm/DesktopTimePicker/DesktopTimePicker.types.d.ts +2 -2
  27. package/esm/MobileDateTimePicker/MobileDateTimePicker.d.ts +1 -2
  28. package/esm/MobileDateTimePicker/MobileDateTimePicker.js +62 -7
  29. package/esm/MobileDateTimePicker/MobileDateTimePicker.types.d.ts +1 -3
  30. package/esm/PickersTextField/PickersTextField.js +3 -1
  31. package/esm/StaticDateTimePicker/StaticDateTimePicker.js +61 -7
  32. package/esm/StaticDateTimePicker/StaticDateTimePicker.types.d.ts +1 -2
  33. package/esm/TimePicker/shared.d.ts +1 -1
  34. package/esm/index.js +1 -1
  35. package/esm/internals/components/PickerFieldUI.js +6 -2
  36. package/esm/internals/components/PickerPopper/PickerPopper.js +3 -3
  37. package/esm/internals/components/PickerProvider.d.ts +137 -6
  38. package/esm/internals/components/PickerProvider.js +1 -1
  39. package/esm/internals/hooks/useDesktopPicker/useDesktopPicker.types.d.ts +4 -7
  40. package/esm/internals/hooks/useField/useField.js +7 -8
  41. package/esm/internals/hooks/useField/useField.types.d.ts +46 -43
  42. package/esm/internals/hooks/useField/useField.utils.js +4 -1
  43. package/esm/internals/hooks/useField/useFieldCharacterEditing.js +2 -2
  44. package/esm/internals/hooks/useField/useFieldState.d.ts +2 -1
  45. package/esm/internals/hooks/useField/useFieldState.js +136 -83
  46. package/esm/internals/hooks/useField/useFieldV6TextField.js +1 -1
  47. package/esm/internals/hooks/useField/useFieldV7TextField.js +2 -1
  48. package/esm/internals/hooks/useMobilePicker/useMobilePicker.types.d.ts +4 -7
  49. package/esm/internals/hooks/useNullableFieldPrivateContext.d.ts +5 -2
  50. package/esm/internals/hooks/usePicker/hooks/useOrientation.d.ts +2 -0
  51. package/esm/internals/hooks/usePicker/hooks/useOrientation.js +34 -0
  52. package/esm/internals/hooks/usePicker/hooks/useValueAndOpenStates.d.ts +19 -0
  53. package/esm/internals/hooks/usePicker/hooks/useValueAndOpenStates.js +210 -0
  54. package/esm/internals/hooks/usePicker/index.d.ts +1 -3
  55. package/esm/internals/hooks/usePicker/usePicker.d.ts +5 -5
  56. package/esm/internals/hooks/usePicker/usePicker.js +276 -34
  57. package/esm/internals/hooks/usePicker/usePicker.types.d.ts +181 -9
  58. package/esm/internals/hooks/useStaticPicker/useStaticPicker.types.d.ts +3 -4
  59. package/esm/internals/hooks/useValueWithTimezone.d.ts +1 -1
  60. package/esm/internals/index.d.ts +3 -6
  61. package/esm/internals/models/manager.d.ts +108 -4
  62. package/esm/internals/models/props/basePickerProps.d.ts +2 -2
  63. package/esm/internals/models/props/time.d.ts +1 -1
  64. package/esm/internals/utils/date-time-utils.d.ts +2 -2
  65. package/esm/internals/utils/valueManagers.d.ts +1 -1
  66. package/esm/internals/utils/valueManagers.js +10 -18
  67. package/esm/managers/useDateManager.d.ts +3 -4
  68. package/esm/managers/useDateTimeManager.d.ts +3 -4
  69. package/esm/managers/useTimeManager.d.ts +3 -4
  70. package/esm/models/adapters.d.ts +2 -1
  71. package/esm/models/manager.d.ts +4 -5
  72. package/esm/validation/useValidation.d.ts +1 -1
  73. package/index.js +1 -1
  74. package/internals/components/PickerFieldUI.js +6 -2
  75. package/internals/components/PickerPopper/PickerPopper.js +3 -3
  76. package/internals/components/PickerProvider.d.ts +137 -6
  77. package/internals/components/PickerProvider.js +1 -1
  78. package/internals/hooks/useDesktopPicker/useDesktopPicker.types.d.ts +4 -7
  79. package/internals/hooks/useField/useField.js +7 -8
  80. package/internals/hooks/useField/useField.types.d.ts +46 -43
  81. package/internals/hooks/useField/useField.utils.js +4 -1
  82. package/internals/hooks/useField/useFieldCharacterEditing.js +2 -2
  83. package/internals/hooks/useField/useFieldState.d.ts +2 -1
  84. package/internals/hooks/useField/useFieldState.js +135 -82
  85. package/internals/hooks/useField/useFieldV6TextField.js +1 -1
  86. package/internals/hooks/useField/useFieldV7TextField.js +2 -1
  87. package/internals/hooks/useMobilePicker/useMobilePicker.types.d.ts +4 -7
  88. package/internals/hooks/useNullableFieldPrivateContext.d.ts +5 -2
  89. package/internals/hooks/usePicker/hooks/useOrientation.d.ts +2 -0
  90. package/internals/hooks/usePicker/hooks/useOrientation.js +42 -0
  91. package/internals/hooks/usePicker/hooks/useValueAndOpenStates.d.ts +19 -0
  92. package/internals/hooks/usePicker/{usePickerValue.js → hooks/useValueAndOpenStates.js} +97 -114
  93. package/internals/hooks/usePicker/index.d.ts +1 -3
  94. package/internals/hooks/usePicker/usePicker.d.ts +5 -5
  95. package/internals/hooks/usePicker/usePicker.js +278 -34
  96. package/internals/hooks/usePicker/usePicker.types.d.ts +181 -9
  97. package/internals/hooks/useStaticPicker/useStaticPicker.types.d.ts +3 -4
  98. package/internals/hooks/useValueWithTimezone.d.ts +1 -1
  99. package/internals/index.d.ts +3 -6
  100. package/internals/models/manager.d.ts +108 -4
  101. package/internals/models/props/basePickerProps.d.ts +2 -2
  102. package/internals/models/props/time.d.ts +1 -1
  103. package/internals/utils/date-time-utils.d.ts +2 -2
  104. package/internals/utils/valueManagers.d.ts +1 -1
  105. package/internals/utils/valueManagers.js +10 -18
  106. package/managers/useDateManager.d.ts +3 -4
  107. package/managers/useDateTimeManager.d.ts +3 -4
  108. package/managers/useTimeManager.d.ts +3 -4
  109. package/models/adapters.d.ts +2 -1
  110. package/models/manager.d.ts +4 -5
  111. package/modern/DatePicker/shared.d.ts +1 -1
  112. package/modern/DateTimePicker/DateTimePicker.types.d.ts +2 -2
  113. package/modern/DateTimePicker/DateTimePickerToolbar.d.ts +1 -1
  114. package/modern/DateTimePicker/DateTimePickerToolbar.js +1 -1
  115. package/modern/DateTimePicker/shared.d.ts +18 -10
  116. package/modern/DateTimePicker/shared.js +23 -2
  117. package/modern/DesktopDateTimePicker/DesktopDateTimePicker.js +3 -12
  118. package/modern/DesktopDateTimePicker/DesktopDateTimePicker.types.d.ts +4 -7
  119. package/modern/DesktopTimePicker/DesktopTimePicker.types.d.ts +2 -2
  120. package/modern/MobileDateTimePicker/MobileDateTimePicker.d.ts +1 -2
  121. package/modern/MobileDateTimePicker/MobileDateTimePicker.js +62 -7
  122. package/modern/MobileDateTimePicker/MobileDateTimePicker.types.d.ts +1 -3
  123. package/modern/PickersTextField/PickersTextField.js +3 -1
  124. package/modern/StaticDateTimePicker/StaticDateTimePicker.js +61 -7
  125. package/modern/StaticDateTimePicker/StaticDateTimePicker.types.d.ts +1 -2
  126. package/modern/TimePicker/shared.d.ts +1 -1
  127. package/modern/index.js +1 -1
  128. package/modern/internals/components/PickerFieldUI.js +6 -2
  129. package/modern/internals/components/PickerPopper/PickerPopper.js +3 -3
  130. package/modern/internals/components/PickerProvider.d.ts +137 -6
  131. package/modern/internals/components/PickerProvider.js +1 -1
  132. package/modern/internals/hooks/useDesktopPicker/useDesktopPicker.types.d.ts +4 -7
  133. package/modern/internals/hooks/useField/useField.js +7 -8
  134. package/modern/internals/hooks/useField/useField.types.d.ts +46 -43
  135. package/modern/internals/hooks/useField/useField.utils.js +4 -1
  136. package/modern/internals/hooks/useField/useFieldCharacterEditing.js +2 -2
  137. package/modern/internals/hooks/useField/useFieldState.d.ts +2 -1
  138. package/modern/internals/hooks/useField/useFieldState.js +136 -83
  139. package/modern/internals/hooks/useField/useFieldV6TextField.js +1 -1
  140. package/modern/internals/hooks/useField/useFieldV7TextField.js +2 -1
  141. package/modern/internals/hooks/useMobilePicker/useMobilePicker.types.d.ts +4 -7
  142. package/modern/internals/hooks/useNullableFieldPrivateContext.d.ts +5 -2
  143. package/modern/internals/hooks/usePicker/hooks/useOrientation.d.ts +2 -0
  144. package/modern/internals/hooks/usePicker/hooks/useOrientation.js +34 -0
  145. package/modern/internals/hooks/usePicker/hooks/useValueAndOpenStates.d.ts +19 -0
  146. package/modern/internals/hooks/usePicker/hooks/useValueAndOpenStates.js +210 -0
  147. package/modern/internals/hooks/usePicker/index.d.ts +1 -3
  148. package/modern/internals/hooks/usePicker/usePicker.d.ts +5 -5
  149. package/modern/internals/hooks/usePicker/usePicker.js +276 -34
  150. package/modern/internals/hooks/usePicker/usePicker.types.d.ts +181 -9
  151. package/modern/internals/hooks/useStaticPicker/useStaticPicker.types.d.ts +3 -4
  152. package/modern/internals/hooks/useValueWithTimezone.d.ts +1 -1
  153. package/modern/internals/index.d.ts +3 -6
  154. package/modern/internals/models/manager.d.ts +108 -4
  155. package/modern/internals/models/props/basePickerProps.d.ts +2 -2
  156. package/modern/internals/models/props/time.d.ts +1 -1
  157. package/modern/internals/utils/date-time-utils.d.ts +2 -2
  158. package/modern/internals/utils/valueManagers.d.ts +1 -1
  159. package/modern/internals/utils/valueManagers.js +10 -18
  160. package/modern/managers/useDateManager.d.ts +3 -4
  161. package/modern/managers/useDateTimeManager.d.ts +3 -4
  162. package/modern/managers/useTimeManager.d.ts +3 -4
  163. package/modern/models/adapters.d.ts +2 -1
  164. package/modern/models/manager.d.ts +4 -5
  165. package/modern/validation/useValidation.d.ts +1 -1
  166. package/package.json +1 -1
  167. package/tsconfig.build.tsbuildinfo +1 -1
  168. package/validation/useValidation.d.ts +1 -1
  169. package/esm/internals/hooks/useOpenState.d.ts +0 -14
  170. package/esm/internals/hooks/useOpenState.js +0 -37
  171. package/esm/internals/hooks/usePicker/usePickerProvider.d.ts +0 -65
  172. package/esm/internals/hooks/usePicker/usePickerProvider.js +0 -125
  173. package/esm/internals/hooks/usePicker/usePickerValue.d.ts +0 -12
  174. package/esm/internals/hooks/usePicker/usePickerValue.js +0 -226
  175. package/esm/internals/hooks/usePicker/usePickerValue.types.d.ts +0 -327
  176. package/esm/internals/hooks/usePicker/usePickerValue.types.js +0 -1
  177. package/esm/internals/hooks/usePicker/usePickerViews.d.ts +0 -137
  178. package/esm/internals/hooks/usePicker/usePickerViews.js +0 -188
  179. package/internals/hooks/useOpenState.d.ts +0 -14
  180. package/internals/hooks/useOpenState.js +0 -46
  181. package/internals/hooks/usePicker/usePickerProvider.d.ts +0 -65
  182. package/internals/hooks/usePicker/usePickerProvider.js +0 -135
  183. package/internals/hooks/usePicker/usePickerValue.d.ts +0 -12
  184. package/internals/hooks/usePicker/usePickerValue.types.d.ts +0 -327
  185. package/internals/hooks/usePicker/usePickerValue.types.js +0 -5
  186. package/internals/hooks/usePicker/usePickerViews.d.ts +0 -137
  187. package/internals/hooks/usePicker/usePickerViews.js +0 -195
  188. package/modern/internals/hooks/useOpenState.d.ts +0 -14
  189. package/modern/internals/hooks/useOpenState.js +0 -37
  190. package/modern/internals/hooks/usePicker/usePickerProvider.d.ts +0 -65
  191. package/modern/internals/hooks/usePicker/usePickerProvider.js +0 -125
  192. package/modern/internals/hooks/usePicker/usePickerValue.d.ts +0 -12
  193. package/modern/internals/hooks/usePicker/usePickerValue.js +0 -226
  194. package/modern/internals/hooks/usePicker/usePickerValue.types.d.ts +0 -327
  195. package/modern/internals/hooks/usePicker/usePickerValue.types.js +0 -1
  196. package/modern/internals/hooks/usePicker/usePickerViews.d.ts +0 -137
  197. package/modern/internals/hooks/usePicker/usePickerViews.js +0 -188
@@ -9,6 +9,7 @@ exports.useFieldState = void 0;
9
9
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
10
10
  var React = _interopRequireWildcard(require("react"));
11
11
  var _useControlled = _interopRequireDefault(require("@mui/utils/useControlled"));
12
+ var _useTimeout = _interopRequireDefault(require("@mui/utils/useTimeout"));
12
13
  var _RtlProvider = require("@mui/system/RtlProvider");
13
14
  var _usePickerTranslations = require("../../../hooks/usePickerTranslations");
14
15
  var _useUtils = require("../useUtils");
@@ -42,10 +43,11 @@ const useFieldState = params => {
42
43
  }
43
44
  } = params;
44
45
  const {
45
- timezone,
46
- value: valueFromTheOutside,
47
- handleValueChange
48
- } = (0, _useValueWithTimezone.useValueWithTimezone)({
46
+ value,
47
+ handleValueChange,
48
+ timezone
49
+ } = (0, _useValueWithTimezone.useControlledValueWithTimezone)({
50
+ name: 'a field component',
49
51
  timezone: timezoneProp,
50
52
  value: valueProp,
51
53
  defaultValue,
@@ -53,9 +55,13 @@ const useFieldState = params => {
53
55
  onChange,
54
56
  valueManager
55
57
  });
58
+ const valueRef = React.useRef(value);
59
+ React.useEffect(() => {
60
+ valueRef.current = value;
61
+ }, [value]);
56
62
  const localizedDigits = React.useMemo(() => (0, _useField.getLocalizedDigits)(utils), [utils]);
57
63
  const sectionsValueBoundaries = React.useMemo(() => (0, _useField.getSectionsBoundaries)(utils, localizedDigits, timezone), [utils, localizedDigits, timezone]);
58
- const getSectionsFromValue = React.useCallback((value, fallbackSections = null) => fieldValueManager.getSectionsFromValue(utils, value, fallbackSections, date => (0, _buildSectionsFromFormat.buildSectionsFromFormat)({
64
+ const getSectionsFromValue = React.useCallback(valueToAnalyze => fieldValueManager.getSectionsFromValue(valueToAnalyze, date => (0, _buildSectionsFromFormat.buildSectionsFromFormat)({
59
65
  utils,
60
66
  localeText: translations,
61
67
  localizedDigits,
@@ -67,17 +73,22 @@ const useFieldState = params => {
67
73
  isRtl
68
74
  })), [fieldValueManager, format, translations, localizedDigits, isRtl, shouldRespectLeadingZeros, utils, formatDensity, enableAccessibleFieldDOMStructure]);
69
75
  const [state, setState] = React.useState(() => {
70
- const sections = getSectionsFromValue(valueFromTheOutside);
76
+ const sections = getSectionsFromValue(value);
71
77
  (0, _useField.validateSections)(sections, valueType);
72
78
  const stateWithoutReferenceDate = {
73
79
  sections,
74
- value: valueFromTheOutside,
80
+ lastValue: value,
81
+ lastSectionsDependencies: {
82
+ format,
83
+ isRtl,
84
+ locale: utils.locale
85
+ },
75
86
  tempValueStrAndroid: null
76
87
  };
77
88
  const granularity = (0, _getDefaultReferenceDate.getSectionTypeGranularity)(sections);
78
89
  const referenceValue = valueManager.getInitialReferenceValue({
79
90
  referenceDate: referenceDateProp,
80
- value: valueFromTheOutside,
91
+ value,
81
92
  utils,
82
93
  props: internalProps,
83
94
  granularity,
@@ -99,29 +110,16 @@ const useFieldState = params => {
99
110
  };
100
111
  const parsedSelectedSections = React.useMemo(() => (0, _useField.parseSelectedSections)(selectedSections, state.sections), [selectedSections, state.sections]);
101
112
  const activeSectionIndex = parsedSelectedSections === 'all' ? 0 : parsedSelectedSections;
102
- const publishValue = ({
103
- value,
104
- referenceValue,
105
- sections
106
- }) => {
107
- setState(prevState => (0, _extends2.default)({}, prevState, {
108
- sections,
109
- value,
110
- referenceValue,
111
- tempValueStrAndroid: null
112
- }));
113
- if (valueManager.areValuesEqual(utils, state.value, value)) {
114
- return;
115
- }
113
+ const publishValue = newValue => {
116
114
  const context = {
117
115
  validationError: validator({
118
116
  adapter,
119
- value,
117
+ value: newValue,
120
118
  timezone,
121
119
  props: internalProps
122
120
  })
123
121
  };
124
- handleValueChange(value, context);
122
+ handleValueChange(newValue, context);
125
123
  };
126
124
  const setSectionValue = (sectionIndex, newSectionValue) => {
127
125
  const newSections = [...state.sections];
@@ -131,27 +129,49 @@ const useFieldState = params => {
131
129
  });
132
130
  return newSections;
133
131
  };
134
- const clearValue = () => {
135
- publishValue({
136
- value: valueManager.emptyValue,
137
- referenceValue: state.referenceValue,
138
- sections: getSectionsFromValue(valueManager.emptyValue)
132
+ const sectionToUpdateOnNextInvalidDateRef = React.useRef(null);
133
+ const timeoutToUpdateSectionValueOnNextInvalidDate = (0, _useTimeout.default)();
134
+ const setSectionUpdateToApplyOnNextInvalidDate = newSectionValue => {
135
+ if (activeSectionIndex == null) {
136
+ return;
137
+ }
138
+ sectionToUpdateOnNextInvalidDateRef.current = {
139
+ sectionIndex: activeSectionIndex,
140
+ value: newSectionValue
141
+ };
142
+ timeoutToUpdateSectionValueOnNextInvalidDate.start(0, () => {
143
+ sectionToUpdateOnNextInvalidDateRef.current = null;
139
144
  });
140
145
  };
146
+ const clearValue = () => {
147
+ if (valueManager.areValuesEqual(utils, value, valueManager.emptyValue)) {
148
+ setState(prevState => (0, _extends2.default)({}, prevState, {
149
+ sections: prevState.sections.map(section => (0, _extends2.default)({}, section, {
150
+ value: ''
151
+ })),
152
+ tempValueStrAndroid: null
153
+ }));
154
+ } else {
155
+ publishValue(valueManager.emptyValue);
156
+ }
157
+ };
141
158
  const clearActiveSection = () => {
142
159
  if (activeSectionIndex == null) {
143
160
  return;
144
161
  }
145
162
  const activeSection = state.sections[activeSectionIndex];
146
- const activeDateManager = fieldValueManager.getActiveDateManager(utils, state, activeSection);
147
- const nonEmptySectionCountBefore = activeDateManager.getSections(state.sections).filter(section => section.value !== '').length;
148
- const hasNoOtherNonEmptySections = nonEmptySectionCountBefore === (activeSection.value === '' ? 0 : 1);
149
- const newSections = setSectionValue(activeSectionIndex, '');
150
- const newActiveDate = hasNoOtherNonEmptySections ? null : utils.getInvalidDate();
151
- const newValues = activeDateManager.getNewValuesFromNewActiveDate(newActiveDate);
152
- publishValue((0, _extends2.default)({}, newValues, {
153
- sections: newSections
154
- }));
163
+ if (activeSection.value === '') {
164
+ return;
165
+ }
166
+ setSectionUpdateToApplyOnNextInvalidDate('');
167
+ if (fieldValueManager.getDateFromSection(value, activeSection) === null) {
168
+ setState(prevState => (0, _extends2.default)({}, prevState, {
169
+ sections: setSectionValue(activeSectionIndex, ''),
170
+ tempValueStrAndroid: null
171
+ }));
172
+ } else {
173
+ publishValue(fieldValueManager.updateDateInValue(value, activeSection, null));
174
+ }
155
175
  };
156
176
  const updateValueFromValueStr = valueStr => {
157
177
  const parseDateStr = (dateStr, referenceDate) => {
@@ -173,34 +193,31 @@ const useFieldState = params => {
173
193
  return (0, _useField.mergeDateIntoReferenceDate)(utils, date, sections, referenceDate, false);
174
194
  };
175
195
  const newValue = fieldValueManager.parseValueStr(valueStr, state.referenceValue, parseDateStr);
176
- const newReferenceValue = fieldValueManager.updateReferenceValue(utils, newValue, state.referenceValue);
177
- publishValue({
178
- value: newValue,
179
- referenceValue: newReferenceValue,
180
- sections: getSectionsFromValue(newValue, state.sections)
181
- });
196
+ publishValue(newValue);
182
197
  };
198
+ const timeoutToCleanActiveDateSectionsIfValueNull = (0, _useTimeout.default)();
183
199
  const updateSectionValue = ({
184
- activeSection,
200
+ section,
185
201
  newSectionValue,
186
202
  shouldGoToNextSection
187
203
  }) => {
204
+ timeoutToUpdateSectionValueOnNextInvalidDate.clear();
205
+ timeoutToCleanActiveDateSectionsIfValueNull.clear();
206
+ const activeDate = fieldValueManager.getDateFromSection(value, section);
207
+
188
208
  /**
189
- * 1. Decide which section should be focused
209
+ * Decide which section should be focused
190
210
  */
191
211
  if (shouldGoToNextSection && activeSectionIndex < state.sections.length - 1) {
192
212
  setSelectedSections(activeSectionIndex + 1);
193
213
  }
194
214
 
195
215
  /**
196
- * 2. Try to build a valid date from the new section value
216
+ * Try to build a valid date from the new section value
197
217
  */
198
- const activeDateManager = fieldValueManager.getActiveDateManager(utils, state, activeSection);
199
218
  const newSections = setSectionValue(activeSectionIndex, newSectionValue);
200
- const newActiveDateSections = activeDateManager.getSections(newSections);
219
+ const newActiveDateSections = fieldValueManager.getDateSectionsFromValue(newSections, section);
201
220
  const newActiveDate = (0, _useField.getDateFromDateSections)(utils, newActiveDateSections, localizedDigits);
202
- let values;
203
- let shouldPublish;
204
221
 
205
222
  /**
206
223
  * If the new date is valid,
@@ -208,23 +225,43 @@ const useFieldState = params => {
208
225
  * This makes sure that we don't lose some information of the initial date (like the time on a date field).
209
226
  */
210
227
  if (utils.isValid(newActiveDate)) {
211
- const mergedDate = (0, _useField.mergeDateIntoReferenceDate)(utils, newActiveDate, newActiveDateSections, activeDateManager.referenceDate, true);
212
- values = activeDateManager.getNewValuesFromNewActiveDate(mergedDate);
213
- shouldPublish = true;
214
- } else {
215
- values = activeDateManager.getNewValuesFromNewActiveDate(newActiveDate);
216
- shouldPublish = (newActiveDate != null && !utils.isValid(newActiveDate)) !== (activeDateManager.date != null && !utils.isValid(activeDateManager.date));
228
+ const mergedDate = (0, _useField.mergeDateIntoReferenceDate)(utils, newActiveDate, newActiveDateSections, fieldValueManager.getDateFromSection(state.referenceValue, section), true);
229
+ if (activeDate == null) {
230
+ timeoutToCleanActiveDateSectionsIfValueNull.start(0, () => {
231
+ if (valueRef.current === value) {
232
+ setState(prevState => (0, _extends2.default)({}, prevState, {
233
+ sections: fieldValueManager.clearDateSections(state.sections, section),
234
+ tempValueStrAndroid: null
235
+ }));
236
+ }
237
+ });
238
+ }
239
+ return publishValue(fieldValueManager.updateDateInValue(value, section, mergedDate));
217
240
  }
218
241
 
219
242
  /**
220
- * Publish or update the internal state with the new value and sections.
243
+ * If all the sections are filled but the date is invalid,
244
+ * Then we publish an invalid date.
221
245
  */
222
- if (shouldPublish) {
223
- return publishValue((0, _extends2.default)({}, values, {
224
- sections: newSections
225
- }));
246
+ if (newActiveDateSections.every(sectionBis => sectionBis.value !== '')) {
247
+ setSectionUpdateToApplyOnNextInvalidDate(newSectionValue);
248
+ return publishValue(fieldValueManager.updateDateInValue(value, section, newActiveDate));
249
+ }
250
+
251
+ /**
252
+ * If the previous date is not null,
253
+ * Then we publish the date as `null`.
254
+ */
255
+ if (activeDate != null) {
256
+ setSectionUpdateToApplyOnNextInvalidDate(newSectionValue);
257
+ return publishValue(fieldValueManager.updateDateInValue(value, section, null));
226
258
  }
227
- return setState(prevState => (0, _extends2.default)({}, prevState, values, {
259
+
260
+ /**
261
+ * If the previous date is already null,
262
+ * Then we don't publish the date and we update the sections.
263
+ */
264
+ return setState(prevState => (0, _extends2.default)({}, prevState, {
228
265
  sections: newSections,
229
266
  tempValueStrAndroid: null
230
267
  }));
@@ -232,32 +269,48 @@ const useFieldState = params => {
232
269
  const setTempAndroidValueStr = tempValueStrAndroid => setState(prev => (0, _extends2.default)({}, prev, {
233
270
  tempValueStrAndroid
234
271
  }));
235
- React.useEffect(() => {
236
- const sections = getSectionsFromValue(state.value);
272
+
273
+ // If `prop.value` changes, we update the state to reflect the new value
274
+ if (value !== state.lastValue) {
275
+ let sections;
276
+ if (sectionToUpdateOnNextInvalidDateRef.current != null && !utils.isValid(fieldValueManager.getDateFromSection(value, state.sections[sectionToUpdateOnNextInvalidDateRef.current.sectionIndex]))) {
277
+ sections = setSectionValue(sectionToUpdateOnNextInvalidDateRef.current.sectionIndex, sectionToUpdateOnNextInvalidDateRef.current.value);
278
+ } else {
279
+ sections = getSectionsFromValue(value);
280
+ }
281
+ setState(prevState => (0, _extends2.default)({}, prevState, {
282
+ lastValue: value,
283
+ sections,
284
+ sectionsDependencies: {
285
+ format,
286
+ isRtl,
287
+ locale: utils.locale
288
+ },
289
+ referenceValue: fieldValueManager.updateReferenceValue(utils, value, prevState.referenceValue),
290
+ tempValueStrAndroid: null
291
+ }));
292
+ }
293
+ if (isRtl !== state.lastSectionsDependencies.isRtl || format !== state.lastSectionsDependencies.format || utils.locale !== state.lastSectionsDependencies.locale) {
294
+ const sections = getSectionsFromValue(value);
237
295
  (0, _useField.validateSections)(sections, valueType);
238
296
  setState(prevState => (0, _extends2.default)({}, prevState, {
239
- sections
297
+ lastSectionsDependencies: {
298
+ format,
299
+ isRtl,
300
+ locale: utils.locale
301
+ },
302
+ sections,
303
+ tempValueStrAndroid: null
240
304
  }));
241
- }, [format, utils.locale, isRtl]); // eslint-disable-line react-hooks/exhaustive-deps
242
-
305
+ }
243
306
  React.useEffect(() => {
244
- let shouldUpdate;
245
- if (!valueManager.areValuesEqual(utils, state.value, valueFromTheOutside)) {
246
- shouldUpdate = true;
247
- } else {
248
- shouldUpdate = valueManager.getTimezone(utils, state.value) !== valueManager.getTimezone(utils, valueFromTheOutside);
249
- }
250
- if (shouldUpdate) {
251
- setState(prevState => (0, _extends2.default)({}, prevState, {
252
- value: valueFromTheOutside,
253
- referenceValue: fieldValueManager.updateReferenceValue(utils, valueFromTheOutside, prevState.referenceValue),
254
- sections: getSectionsFromValue(valueFromTheOutside)
255
- }));
307
+ if (sectionToUpdateOnNextInvalidDateRef.current != null) {
308
+ sectionToUpdateOnNextInvalidDateRef.current = null;
256
309
  }
257
- }, [valueFromTheOutside]); // eslint-disable-line react-hooks/exhaustive-deps
258
-
310
+ });
259
311
  return {
260
312
  state,
313
+ value,
261
314
  activeSectionIndex,
262
315
  parsedSelectedSections,
263
316
  setSelectedSections,
@@ -215,7 +215,7 @@ const useFieldV6TextField = params => {
215
215
  if (isValidPastedValue) {
216
216
  resetCharacterQuery();
217
217
  updateSectionValue({
218
- activeSection,
218
+ section: activeSection,
219
219
  newSectionValue: pastedValue,
220
220
  shouldGoToNextSection: true
221
221
  });
@@ -262,7 +262,7 @@ const useFieldV7TextField = params => {
262
262
  if (isValidPastedValue) {
263
263
  resetCharacterQuery();
264
264
  updateSectionValue({
265
- activeSection,
265
+ section: activeSection,
266
266
  newSectionValue: pastedValue,
267
267
  shouldGoToNextSection: true
268
268
  });
@@ -300,6 +300,7 @@ const useFieldV7TextField = params => {
300
300
  return;
301
301
  }
302
302
  resetCharacterQuery();
303
+ revertDOMSectionChange(sectionIndex);
303
304
  clearActiveSection();
304
305
  return;
305
306
  }
@@ -2,13 +2,10 @@ import * as React from 'react';
2
2
  import { MakeRequired, SlotComponentPropsFromProps } from '@mui/x-internals/types';
3
3
  import { BasePickerProps } from "../../models/props/basePickerProps.js";
4
4
  import { PickersModalDialogSlots, PickersModalDialogSlotProps } from "../../components/PickersModalDialog.js";
5
- import { UsePickerParams } from "../usePicker/index.js";
5
+ import { UsePickerParameters, UsePickerNonStaticProps, UsePickerProps } from "../usePicker/index.js";
6
6
  import { PickerFieldSlotProps, PickerOwnerState } from "../../../models/index.js";
7
7
  import { ExportedPickersLayoutSlots, ExportedPickersLayoutSlotProps, PickersLayoutSlotProps } from "../../../PickersLayout/PickersLayout.types.js";
8
- import { UsePickerValueNonStaticProps } from "../usePicker/usePickerValue.types.js";
9
- import { UsePickerViewsProps } from "../usePicker/usePickerViews.js";
10
8
  import { DateOrTimeViewWithMeridiem, PickerValue } from "../../models/index.js";
11
- import { UsePickerProviderNonStaticProps } from "../usePicker/usePickerProvider.js";
12
9
  import { PickerFieldUISlotsFromContext, PickerFieldUISlotPropsFromContext } from "../../components/PickerFieldUI.js";
13
10
  export interface UseMobilePickerSlots extends PickersModalDialogSlots, ExportedPickersLayoutSlots<PickerValue>, PickerFieldUISlotsFromContext {
14
11
  /**
@@ -20,8 +17,8 @@ export interface ExportedUseMobilePickerSlotProps<TEnableAccessibleFieldDOMStruc
20
17
  field?: SlotComponentPropsFromProps<PickerFieldSlotProps<PickerValue, TEnableAccessibleFieldDOMStructure>, {}, PickerOwnerState>;
21
18
  }
22
19
  export interface UseMobilePickerSlotProps<TEnableAccessibleFieldDOMStructure extends boolean> extends ExportedUseMobilePickerSlotProps<TEnableAccessibleFieldDOMStructure>, Pick<PickersLayoutSlotProps<PickerValue>, 'toolbar'> {}
23
- export interface MobileOnlyPickerProps extends UsePickerValueNonStaticProps, UsePickerProviderNonStaticProps {}
24
- export interface UseMobilePickerProps<TView extends DateOrTimeViewWithMeridiem, TEnableAccessibleFieldDOMStructure extends boolean, TError, TExternalProps extends UsePickerViewsProps<any, TView, any>> extends BasePickerProps<PickerValue, TView, TError, TExternalProps>, MakeRequired<MobileOnlyPickerProps, 'format'> {
20
+ export interface MobileOnlyPickerProps extends UsePickerNonStaticProps {}
21
+ export interface UseMobilePickerProps<TView extends DateOrTimeViewWithMeridiem, TEnableAccessibleFieldDOMStructure extends boolean, TError, TExternalProps extends UsePickerProps<PickerValue, TView, TError, any>> extends BasePickerProps<PickerValue, TView, TError, TExternalProps>, MakeRequired<MobileOnlyPickerProps, 'format'> {
25
22
  /**
26
23
  * Overridable component slots.
27
24
  * @default {}
@@ -33,6 +30,6 @@ export interface UseMobilePickerProps<TView extends DateOrTimeViewWithMeridiem,
33
30
  */
34
31
  slotProps?: UseMobilePickerSlotProps<TEnableAccessibleFieldDOMStructure>;
35
32
  }
36
- export interface UseMobilePickerParams<TView extends DateOrTimeViewWithMeridiem, TEnableAccessibleFieldDOMStructure extends boolean, TExternalProps extends UseMobilePickerProps<TView, TEnableAccessibleFieldDOMStructure, any, TExternalProps>> extends Pick<UsePickerParams<PickerValue, TView, TExternalProps>, 'valueManager' | 'valueType' | 'validator' | 'ref'> {
33
+ export interface UseMobilePickerParams<TView extends DateOrTimeViewWithMeridiem, TEnableAccessibleFieldDOMStructure extends boolean, TExternalProps extends UseMobilePickerProps<TView, TEnableAccessibleFieldDOMStructure, any, TExternalProps>> extends Pick<UsePickerParameters<PickerValue, TView, TExternalProps>, 'valueManager' | 'valueType' | 'validator' | 'ref'> {
37
34
  props: TExternalProps;
38
35
  }
@@ -1,6 +1,9 @@
1
1
  import * as React from 'react';
2
2
  import type { UseFieldInternalProps } from './useField';
3
- import { UsePickerViewsFieldPrivateContextValue } from "./usePicker/usePickerViews.js";
3
+ import { FieldRef } from "../../models/index.js";
4
+ import { PickerRangeValue, PickerValue } from "../models/index.js";
4
5
  export declare const PickerFieldPrivateContext: React.Context<PickerFieldPrivateContextValue | null>;
5
6
  export declare function useNullableFieldPrivateContext(): PickerFieldPrivateContextValue | null;
6
- export interface PickerFieldPrivateContextValue extends Pick<UseFieldInternalProps<any, any, any>, 'formatDensity' | 'enableAccessibleFieldDOMStructure' | 'selectedSections' | 'onSelectedSectionsChange'>, UsePickerViewsFieldPrivateContextValue {}
7
+ export interface PickerFieldPrivateContextValue extends Pick<UseFieldInternalProps<any, any, any>, 'formatDensity' | 'enableAccessibleFieldDOMStructure' | 'selectedSections' | 'onSelectedSectionsChange'> {
8
+ fieldRef: React.RefObject<FieldRef<PickerValue> | FieldRef<PickerRangeValue> | null>;
9
+ }
@@ -0,0 +1,2 @@
1
+ import { DateOrTimeViewWithMeridiem, PickerOrientation } from "../../../models/index.js";
2
+ export declare function useOrientation(views: readonly DateOrTimeViewWithMeridiem[], customOrientation: PickerOrientation | undefined): PickerOrientation;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.useOrientation = useOrientation;
9
+ var React = _interopRequireWildcard(require("react"));
10
+ var _useEnhancedEffect = _interopRequireDefault(require("@mui/utils/useEnhancedEffect"));
11
+ var _utils = require("../../../utils/utils");
12
+ function getOrientation() {
13
+ if (typeof window === 'undefined') {
14
+ return 'portrait';
15
+ }
16
+ if (window.screen && window.screen.orientation && window.screen.orientation.angle) {
17
+ return Math.abs(window.screen.orientation.angle) === 90 ? 'landscape' : 'portrait';
18
+ }
19
+
20
+ // Support IOS safari
21
+ if (window.orientation) {
22
+ return Math.abs(Number(window.orientation)) === 90 ? 'landscape' : 'portrait';
23
+ }
24
+ return 'portrait';
25
+ }
26
+ function useOrientation(views, customOrientation) {
27
+ const [orientation, setOrientation] = React.useState(getOrientation);
28
+ (0, _useEnhancedEffect.default)(() => {
29
+ const eventHandler = () => {
30
+ setOrientation(getOrientation());
31
+ };
32
+ window.addEventListener('orientationchange', eventHandler);
33
+ return () => {
34
+ window.removeEventListener('orientationchange', eventHandler);
35
+ };
36
+ }, []);
37
+ if ((0, _utils.arrayIncludes)(views, ['hours', 'minutes', 'seconds'])) {
38
+ // could not display 13:34:44 in landscape mode
39
+ return 'portrait';
40
+ }
41
+ return customOrientation ?? orientation;
42
+ }
@@ -0,0 +1,19 @@
1
+ import * as React from 'react';
2
+ import { DateOrTimeViewWithMeridiem, PickerValidValue, PickerValueManager } from "../../../models/index.js";
3
+ import { PickerSelectionState, UsePickerProps, UsePickerState } from "../usePicker.types.js";
4
+ import { InferError } from "../../../../models/index.js";
5
+ import { SetValueActionOptions } from "../../../components/PickerProvider.js";
6
+ import { Validator } from "../../../../validation/index.js";
7
+ export declare function useValueAndOpenStates<TValue extends PickerValidValue, TView extends DateOrTimeViewWithMeridiem, TExternalProps extends UsePickerProps<TValue, TView, any, any>>(parameters: UsePickerDateStateParameters<TValue, TView, TExternalProps>): {
8
+ timezone: string;
9
+ state: UsePickerState<TValue>;
10
+ setValue: (newValue: TValue, options?: SetValueActionOptions<InferError<TExternalProps>>) => void;
11
+ setValueFromView: (newValue: TValue, selectionState?: PickerSelectionState) => void;
12
+ setOpen: (action: React.SetStateAction<boolean>) => void;
13
+ };
14
+ interface UsePickerDateStateParameters<TValue extends PickerValidValue, TView extends DateOrTimeViewWithMeridiem, TExternalProps extends UsePickerProps<TValue, TView, any, any>> {
15
+ props: TExternalProps;
16
+ valueManager: PickerValueManager<TValue, InferError<TExternalProps>>;
17
+ validator: Validator<TValue, InferError<TExternalProps>, TExternalProps>;
18
+ }
19
+ export {};