@mui/x-date-pickers 6.8.0 → 6.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (248) hide show
  1. package/AdapterDayjs/AdapterDayjs.d.ts +5 -1
  2. package/AdapterDayjs/AdapterDayjs.js +42 -15
  3. package/AdapterLuxon/AdapterLuxon.d.ts +1 -0
  4. package/AdapterLuxon/AdapterLuxon.js +12 -5
  5. package/AdapterMoment/AdapterMoment.js +18 -9
  6. package/CHANGELOG.md +76 -5534
  7. package/DateCalendar/DateCalendar.js +34 -16
  8. package/DateCalendar/DateCalendar.types.d.ts +2 -2
  9. package/DateCalendar/DayCalendar.d.ts +3 -1
  10. package/DateCalendar/DayCalendar.js +24 -17
  11. package/DateCalendar/PickersCalendarHeader.js +6 -3
  12. package/DateCalendar/useCalendarState.d.ts +2 -1
  13. package/DateCalendar/useCalendarState.js +6 -3
  14. package/DateCalendar/useIsDateDisabled.d.ts +1 -1
  15. package/DateCalendar/useIsDateDisabled.js +5 -3
  16. package/DateField/DateField.js +8 -0
  17. package/DatePicker/DatePicker.js +8 -0
  18. package/DateTimeField/DateTimeField.js +8 -0
  19. package/DateTimePicker/DateTimePicker.js +8 -0
  20. package/DesktopDatePicker/DesktopDatePicker.js +8 -0
  21. package/DesktopDateTimePicker/DesktopDateTimePicker.js +8 -0
  22. package/DesktopTimePicker/DesktopTimePicker.js +8 -0
  23. package/DigitalClock/DigitalClock.js +28 -15
  24. package/MobileDatePicker/MobileDatePicker.js +8 -0
  25. package/MobileDateTimePicker/MobileDateTimePicker.js +8 -0
  26. package/MobileTimePicker/MobileTimePicker.js +8 -0
  27. package/MonthCalendar/MonthCalendar.js +30 -14
  28. package/MonthCalendar/MonthCalendar.types.d.ts +3 -2
  29. package/MultiSectionDigitalClock/MultiSectionDigitalClock.js +28 -15
  30. package/StaticDatePicker/StaticDatePicker.js +8 -0
  31. package/StaticDateTimePicker/StaticDateTimePicker.js +8 -0
  32. package/StaticTimePicker/StaticTimePicker.js +8 -0
  33. package/TimeClock/TimeClock.js +27 -15
  34. package/TimeField/TimeField.js +8 -0
  35. package/TimePicker/TimePicker.js +8 -0
  36. package/YearCalendar/YearCalendar.js +30 -14
  37. package/YearCalendar/YearCalendar.types.d.ts +3 -2
  38. package/dateTimeViewRenderers/dateTimeViewRenderers.d.ts +1 -1
  39. package/dateTimeViewRenderers/dateTimeViewRenderers.js +5 -2
  40. package/dateViewRenderers/dateViewRenderers.d.ts +1 -1
  41. package/dateViewRenderers/dateViewRenderers.js +4 -2
  42. package/index.js +1 -1
  43. package/internals/hooks/date-helpers-hooks.d.ts +4 -2
  44. package/internals/hooks/date-helpers-hooks.js +8 -6
  45. package/internals/hooks/useDesktopPicker/useDesktopPicker.js +2 -0
  46. package/internals/hooks/useField/useField.d.ts +3 -1
  47. package/internals/hooks/useField/useField.js +23 -7
  48. package/internals/hooks/useField/useField.types.d.ts +5 -3
  49. package/internals/hooks/useField/useField.utils.d.ts +13 -11
  50. package/internals/hooks/useField/useField.utils.js +55 -43
  51. package/internals/hooks/useField/useFieldCharacterEditing.d.ts +3 -2
  52. package/internals/hooks/useField/useFieldCharacterEditing.js +6 -5
  53. package/internals/hooks/useField/useFieldState.d.ts +1 -0
  54. package/internals/hooks/useField/useFieldState.js +33 -22
  55. package/internals/hooks/useMobilePicker/useMobilePicker.js +2 -0
  56. package/internals/hooks/usePicker/usePickerValue.js +23 -7
  57. package/internals/hooks/usePicker/usePickerValue.types.d.ts +16 -4
  58. package/internals/hooks/usePicker/usePickerViews.d.ts +2 -1
  59. package/internals/hooks/usePicker/usePickerViews.js +7 -2
  60. package/internals/hooks/useUtils.d.ts +3 -2
  61. package/internals/hooks/useUtils.js +5 -2
  62. package/internals/hooks/useValueWithTimezone.d.ts +33 -0
  63. package/internals/hooks/useValueWithTimezone.js +69 -0
  64. package/internals/index.d.ts +1 -0
  65. package/internals/index.js +1 -0
  66. package/internals/models/props/clock.d.ts +2 -2
  67. package/internals/utils/date-utils.d.ts +4 -3
  68. package/internals/utils/date-utils.js +4 -3
  69. package/internals/utils/fields.js +1 -1
  70. package/internals/utils/getDefaultReferenceDate.d.ts +3 -2
  71. package/internals/utils/getDefaultReferenceDate.js +3 -2
  72. package/internals/utils/validation/extractValidationProps.d.ts +1 -1
  73. package/internals/utils/validation/validateDate.d.ts +3 -2
  74. package/internals/utils/validation/validateDate.js +14 -6
  75. package/internals/utils/validation/validateTime.d.ts +3 -2
  76. package/internals/utils/validation/validateTime.js +8 -8
  77. package/internals/utils/valueManagers.js +2 -1
  78. package/legacy/AdapterDayjs/AdapterDayjs.js +45 -16
  79. package/legacy/AdapterLuxon/AdapterLuxon.js +13 -6
  80. package/legacy/AdapterMoment/AdapterMoment.js +18 -9
  81. package/legacy/DateCalendar/DateCalendar.js +32 -19
  82. package/legacy/DateCalendar/DayCalendar.js +24 -17
  83. package/legacy/DateCalendar/PickersCalendarHeader.js +6 -3
  84. package/legacy/DateCalendar/useCalendarState.js +6 -3
  85. package/legacy/DateCalendar/useIsDateDisabled.js +5 -3
  86. package/legacy/DateField/DateField.js +8 -0
  87. package/legacy/DatePicker/DatePicker.js +8 -0
  88. package/legacy/DateTimeField/DateTimeField.js +8 -0
  89. package/legacy/DateTimePicker/DateTimePicker.js +8 -0
  90. package/legacy/DesktopDatePicker/DesktopDatePicker.js +8 -0
  91. package/legacy/DesktopDateTimePicker/DesktopDateTimePicker.js +8 -0
  92. package/legacy/DesktopTimePicker/DesktopTimePicker.js +8 -0
  93. package/legacy/DigitalClock/DigitalClock.js +26 -16
  94. package/legacy/MobileDatePicker/MobileDatePicker.js +8 -0
  95. package/legacy/MobileDateTimePicker/MobileDateTimePicker.js +8 -0
  96. package/legacy/MobileTimePicker/MobileTimePicker.js +8 -0
  97. package/legacy/MonthCalendar/MonthCalendar.js +32 -20
  98. package/legacy/MultiSectionDigitalClock/MultiSectionDigitalClock.js +27 -16
  99. package/legacy/StaticDatePicker/StaticDatePicker.js +8 -0
  100. package/legacy/StaticDateTimePicker/StaticDateTimePicker.js +8 -0
  101. package/legacy/StaticTimePicker/StaticTimePicker.js +8 -0
  102. package/legacy/TimeClock/TimeClock.js +25 -18
  103. package/legacy/TimeField/TimeField.js +8 -0
  104. package/legacy/TimePicker/TimePicker.js +8 -0
  105. package/legacy/YearCalendar/YearCalendar.js +32 -20
  106. package/legacy/dateTimeViewRenderers/dateTimeViewRenderers.js +5 -2
  107. package/legacy/dateViewRenderers/dateViewRenderers.js +4 -2
  108. package/legacy/index.js +1 -1
  109. package/legacy/internals/hooks/date-helpers-hooks.js +8 -6
  110. package/legacy/internals/hooks/useDesktopPicker/useDesktopPicker.js +2 -0
  111. package/legacy/internals/hooks/useField/useField.js +22 -6
  112. package/legacy/internals/hooks/useField/useField.utils.js +56 -44
  113. package/legacy/internals/hooks/useField/useFieldCharacterEditing.js +6 -5
  114. package/legacy/internals/hooks/useField/useFieldState.js +42 -32
  115. package/legacy/internals/hooks/useMobilePicker/useMobilePicker.js +2 -0
  116. package/legacy/internals/hooks/usePicker/usePickerValue.js +22 -7
  117. package/legacy/internals/hooks/usePicker/usePickerViews.js +7 -2
  118. package/legacy/internals/hooks/useUtils.js +5 -2
  119. package/legacy/internals/hooks/useValueWithTimezone.js +81 -0
  120. package/legacy/internals/index.js +1 -0
  121. package/legacy/internals/utils/date-utils.js +5 -4
  122. package/legacy/internals/utils/fields.js +1 -1
  123. package/legacy/internals/utils/getDefaultReferenceDate.js +3 -2
  124. package/legacy/internals/utils/validation/validateDate.js +12 -6
  125. package/legacy/internals/utils/validation/validateTime.js +8 -8
  126. package/legacy/internals/utils/valueManagers.js +3 -0
  127. package/legacy/locales/csCZ.js +2 -2
  128. package/legacy/tests/describeAdapters/describeAdapters.js +3 -1
  129. package/legacy/tests/describeGregorianAdapter/testCalculations.js +69 -27
  130. package/legacy/tests/describeJalaliAdapter/testCalculations.js +21 -0
  131. package/legacy/timeViewRenderers/timeViewRenderers.js +12 -6
  132. package/locales/csCZ.js +2 -2
  133. package/models/adapters.d.ts +2 -2
  134. package/models/timezone.d.ts +10 -0
  135. package/modern/AdapterDayjs/AdapterDayjs.js +41 -15
  136. package/modern/AdapterLuxon/AdapterLuxon.js +12 -4
  137. package/modern/AdapterMoment/AdapterMoment.js +18 -9
  138. package/modern/DateCalendar/DateCalendar.js +34 -16
  139. package/modern/DateCalendar/DayCalendar.js +24 -17
  140. package/modern/DateCalendar/PickersCalendarHeader.js +6 -3
  141. package/modern/DateCalendar/useCalendarState.js +6 -3
  142. package/modern/DateCalendar/useIsDateDisabled.js +5 -3
  143. package/modern/DateField/DateField.js +8 -0
  144. package/modern/DatePicker/DatePicker.js +8 -0
  145. package/modern/DateTimeField/DateTimeField.js +8 -0
  146. package/modern/DateTimePicker/DateTimePicker.js +8 -0
  147. package/modern/DesktopDatePicker/DesktopDatePicker.js +8 -0
  148. package/modern/DesktopDateTimePicker/DesktopDateTimePicker.js +8 -0
  149. package/modern/DesktopTimePicker/DesktopTimePicker.js +8 -0
  150. package/modern/DigitalClock/DigitalClock.js +28 -15
  151. package/modern/MobileDatePicker/MobileDatePicker.js +8 -0
  152. package/modern/MobileDateTimePicker/MobileDateTimePicker.js +8 -0
  153. package/modern/MobileTimePicker/MobileTimePicker.js +8 -0
  154. package/modern/MonthCalendar/MonthCalendar.js +30 -14
  155. package/modern/MultiSectionDigitalClock/MultiSectionDigitalClock.js +28 -15
  156. package/modern/StaticDatePicker/StaticDatePicker.js +8 -0
  157. package/modern/StaticDateTimePicker/StaticDateTimePicker.js +8 -0
  158. package/modern/StaticTimePicker/StaticTimePicker.js +8 -0
  159. package/modern/TimeClock/TimeClock.js +27 -15
  160. package/modern/TimeField/TimeField.js +8 -0
  161. package/modern/TimePicker/TimePicker.js +8 -0
  162. package/modern/YearCalendar/YearCalendar.js +30 -14
  163. package/modern/dateTimeViewRenderers/dateTimeViewRenderers.js +5 -2
  164. package/modern/dateViewRenderers/dateViewRenderers.js +4 -2
  165. package/modern/index.js +1 -1
  166. package/modern/internals/hooks/date-helpers-hooks.js +8 -6
  167. package/modern/internals/hooks/useDesktopPicker/useDesktopPicker.js +2 -0
  168. package/modern/internals/hooks/useField/useField.js +23 -7
  169. package/modern/internals/hooks/useField/useField.utils.js +55 -43
  170. package/modern/internals/hooks/useField/useFieldCharacterEditing.js +6 -5
  171. package/modern/internals/hooks/useField/useFieldState.js +33 -21
  172. package/modern/internals/hooks/useMobilePicker/useMobilePicker.js +2 -0
  173. package/modern/internals/hooks/usePicker/usePickerValue.js +23 -7
  174. package/modern/internals/hooks/usePicker/usePickerViews.js +7 -2
  175. package/modern/internals/hooks/useUtils.js +5 -2
  176. package/modern/internals/hooks/useValueWithTimezone.js +68 -0
  177. package/modern/internals/index.js +1 -0
  178. package/modern/internals/utils/date-utils.js +4 -3
  179. package/modern/internals/utils/fields.js +1 -1
  180. package/modern/internals/utils/getDefaultReferenceDate.js +3 -2
  181. package/modern/internals/utils/validation/validateDate.js +14 -6
  182. package/modern/internals/utils/validation/validateTime.js +8 -8
  183. package/modern/internals/utils/valueManagers.js +2 -1
  184. package/modern/locales/csCZ.js +2 -2
  185. package/modern/tests/describeAdapters/describeAdapters.js +3 -1
  186. package/modern/tests/describeGregorianAdapter/testCalculations.js +69 -27
  187. package/modern/tests/describeJalaliAdapter/testCalculations.js +21 -0
  188. package/modern/timeViewRenderers/timeViewRenderers.js +12 -6
  189. package/node/AdapterDayjs/AdapterDayjs.js +41 -15
  190. package/node/AdapterLuxon/AdapterLuxon.js +12 -4
  191. package/node/AdapterMoment/AdapterMoment.js +18 -9
  192. package/node/DateCalendar/DateCalendar.js +33 -15
  193. package/node/DateCalendar/DayCalendar.js +24 -17
  194. package/node/DateCalendar/PickersCalendarHeader.js +6 -3
  195. package/node/DateCalendar/useCalendarState.js +6 -3
  196. package/node/DateCalendar/useIsDateDisabled.js +5 -3
  197. package/node/DateField/DateField.js +8 -0
  198. package/node/DatePicker/DatePicker.js +8 -0
  199. package/node/DateTimeField/DateTimeField.js +8 -0
  200. package/node/DateTimePicker/DateTimePicker.js +8 -0
  201. package/node/DesktopDatePicker/DesktopDatePicker.js +8 -0
  202. package/node/DesktopDateTimePicker/DesktopDateTimePicker.js +8 -0
  203. package/node/DesktopTimePicker/DesktopTimePicker.js +8 -0
  204. package/node/DigitalClock/DigitalClock.js +28 -15
  205. package/node/MobileDatePicker/MobileDatePicker.js +8 -0
  206. package/node/MobileDateTimePicker/MobileDateTimePicker.js +8 -0
  207. package/node/MobileTimePicker/MobileTimePicker.js +8 -0
  208. package/node/MonthCalendar/MonthCalendar.js +30 -14
  209. package/node/MultiSectionDigitalClock/MultiSectionDigitalClock.js +28 -15
  210. package/node/StaticDatePicker/StaticDatePicker.js +8 -0
  211. package/node/StaticDateTimePicker/StaticDateTimePicker.js +8 -0
  212. package/node/StaticTimePicker/StaticTimePicker.js +8 -0
  213. package/node/TimeClock/TimeClock.js +26 -14
  214. package/node/TimeField/TimeField.js +8 -0
  215. package/node/TimePicker/TimePicker.js +8 -0
  216. package/node/YearCalendar/YearCalendar.js +30 -14
  217. package/node/dateTimeViewRenderers/dateTimeViewRenderers.js +5 -2
  218. package/node/dateViewRenderers/dateViewRenderers.js +4 -2
  219. package/node/index.js +1 -1
  220. package/node/internals/hooks/date-helpers-hooks.js +8 -6
  221. package/node/internals/hooks/useDesktopPicker/useDesktopPicker.js +2 -0
  222. package/node/internals/hooks/useField/useField.js +23 -7
  223. package/node/internals/hooks/useField/useField.utils.js +55 -43
  224. package/node/internals/hooks/useField/useFieldCharacterEditing.js +6 -5
  225. package/node/internals/hooks/useField/useFieldState.js +33 -21
  226. package/node/internals/hooks/useMobilePicker/useMobilePicker.js +2 -0
  227. package/node/internals/hooks/usePicker/usePickerValue.js +22 -7
  228. package/node/internals/hooks/usePicker/usePickerViews.js +7 -2
  229. package/node/internals/hooks/useUtils.js +5 -2
  230. package/node/internals/hooks/useValueWithTimezone.js +79 -0
  231. package/node/internals/index.js +7 -0
  232. package/node/internals/utils/date-utils.js +4 -3
  233. package/node/internals/utils/fields.js +1 -1
  234. package/node/internals/utils/getDefaultReferenceDate.js +3 -2
  235. package/node/internals/utils/validation/validateDate.js +14 -6
  236. package/node/internals/utils/validation/validateTime.js +8 -8
  237. package/node/internals/utils/valueManagers.js +2 -1
  238. package/node/locales/csCZ.js +2 -2
  239. package/node/tests/describeAdapters/describeAdapters.js +3 -1
  240. package/node/tests/describeGregorianAdapter/testCalculations.js +69 -27
  241. package/node/tests/describeJalaliAdapter/testCalculations.js +21 -0
  242. package/node/timeViewRenderers/timeViewRenderers.js +12 -6
  243. package/package.json +2 -2
  244. package/tests/describeAdapters/describeAdapters.js +3 -1
  245. package/tests/describeGregorianAdapter/testCalculations.js +69 -27
  246. package/tests/describeJalaliAdapter/testCalculations.js +21 -0
  247. package/timeViewRenderers/timeViewRenderers.d.ts +3 -3
  248. package/timeViewRenderers/timeViewRenderers.js +12 -6
@@ -32,9 +32,9 @@ const getDeltaFromKeyCode = keyCode => {
32
32
  return 0;
33
33
  }
34
34
  };
35
- export const getDaysInWeekStr = (utils, format) => {
35
+ export const getDaysInWeekStr = (utils, timezone, format) => {
36
36
  const elements = [];
37
- const now = utils.date();
37
+ const now = utils.dateWithTimezone(undefined, timezone);
38
38
  const startDate = utils.startOfWeek(now);
39
39
  const endDate = utils.endOfWeek(now);
40
40
  let current = startDate;
@@ -44,19 +44,19 @@ export const getDaysInWeekStr = (utils, format) => {
44
44
  }
45
45
  return elements.map(weekDay => utils.formatByString(weekDay, format));
46
46
  };
47
- export const getLetterEditingOptions = (utils, sectionType, format) => {
47
+ export const getLetterEditingOptions = (utils, timezone, sectionType, format) => {
48
48
  switch (sectionType) {
49
49
  case 'month':
50
50
  {
51
- return getMonthsInYear(utils, utils.date()).map(month => utils.formatByString(month, format));
51
+ return getMonthsInYear(utils, utils.dateWithTimezone(undefined, timezone)).map(month => utils.formatByString(month, format));
52
52
  }
53
53
  case 'weekDay':
54
54
  {
55
- return getDaysInWeekStr(utils, format);
55
+ return getDaysInWeekStr(utils, timezone, format);
56
56
  }
57
57
  case 'meridiem':
58
58
  {
59
- const now = utils.date();
59
+ const now = utils.dateWithTimezone(undefined, timezone);
60
60
  return [utils.startOfDay(now), utils.endOfDay(now)].map(date => utils.formatByString(date, format));
61
61
  }
62
62
  default:
@@ -77,7 +77,7 @@ export const cleanLeadingZeros = (utils, valueStr, size) => {
77
77
  }
78
78
  return cleanValueStr;
79
79
  };
80
- export const cleanDigitSectionValue = (utils, value, sectionBoundaries, section) => {
80
+ export const cleanDigitSectionValue = (utils, timezone, value, sectionBoundaries, section) => {
81
81
  if (process.env.NODE_ENV !== 'production') {
82
82
  if (section.type !== 'day' && section.contentType === 'digit-with-letter') {
83
83
  throw new Error([`MUI: The token "${section.format}" is a digit format with letter in it.'
@@ -96,7 +96,7 @@ export const cleanDigitSectionValue = (utils, value, sectionBoundaries, section)
96
96
  }
97
97
  return valueStr;
98
98
  };
99
- export const adjustSectionValue = (utils, section, keyCode, sectionsValueBoundaries, activeDate) => {
99
+ export const adjustSectionValue = (utils, timezone, section, keyCode, sectionsValueBoundaries, activeDate, stepsAttribues) => {
100
100
  const delta = getDeltaFromKeyCode(keyCode);
101
101
  const isStart = keyCode === 'Home';
102
102
  const isEnd = keyCode === 'End';
@@ -107,28 +107,39 @@ export const adjustSectionValue = (utils, section, keyCode, sectionsValueBoundar
107
107
  format: section.format,
108
108
  contentType: section.contentType
109
109
  });
110
- const getCleanValue = value => cleanDigitSectionValue(utils, value, sectionBoundaries, section);
110
+ const getCleanValue = value => cleanDigitSectionValue(utils, timezone, value, sectionBoundaries, section);
111
+ const step = section.type === 'minutes' && stepsAttribues?.minutesStep ? stepsAttribues.minutesStep : 1;
112
+ const currentSectionValue = parseInt(section.value, 10);
113
+ let newSectionValueNumber = currentSectionValue + delta * step;
111
114
  if (shouldSetAbsolute) {
112
115
  if (section.type === 'year' && !isEnd && !isStart) {
113
- return utils.formatByString(utils.date(), section.format);
116
+ return utils.formatByString(utils.dateWithTimezone(undefined, timezone), section.format);
114
117
  }
115
118
  if (delta > 0 || isStart) {
116
- return getCleanValue(sectionBoundaries.minimum);
119
+ newSectionValueNumber = sectionBoundaries.minimum;
120
+ } else {
121
+ newSectionValueNumber = sectionBoundaries.maximum;
122
+ }
123
+ }
124
+ if (newSectionValueNumber % step !== 0) {
125
+ if (delta < 0 || isStart) {
126
+ newSectionValueNumber += step - (step + newSectionValueNumber) % step; // for JS -3 % 5 = -3 (should be 2)
127
+ }
128
+
129
+ if (delta > 0 || isEnd) {
130
+ newSectionValueNumber -= newSectionValueNumber % step;
117
131
  }
118
- return getCleanValue(sectionBoundaries.maximum);
119
132
  }
120
- const currentSectionValue = parseInt(section.value, 10);
121
- const newSectionValueNumber = currentSectionValue + delta;
122
133
  if (newSectionValueNumber > sectionBoundaries.maximum) {
123
- return getCleanValue(sectionBoundaries.minimum);
134
+ return getCleanValue(sectionBoundaries.minimum + (newSectionValueNumber - sectionBoundaries.maximum - 1) % (sectionBoundaries.maximum - sectionBoundaries.minimum + 1));
124
135
  }
125
136
  if (newSectionValueNumber < sectionBoundaries.minimum) {
126
- return getCleanValue(sectionBoundaries.maximum);
137
+ return getCleanValue(sectionBoundaries.maximum - (sectionBoundaries.minimum - newSectionValueNumber - 1) % (sectionBoundaries.maximum - sectionBoundaries.minimum + 1));
127
138
  }
128
139
  return getCleanValue(newSectionValueNumber);
129
140
  };
130
141
  const adjustLetterSection = () => {
131
- const options = getLetterEditingOptions(utils, section.type, section.format);
142
+ const options = getLetterEditingOptions(utils, timezone, section.type, section.format);
132
143
  if (options.length === 0) {
133
144
  return section.value;
134
145
  }
@@ -196,12 +207,12 @@ export const addPositionPropertiesToSections = (sections, isRTL) => {
196
207
  }
197
208
  return newSections;
198
209
  };
199
- const getSectionPlaceholder = (utils, localeText, sectionConfig, currentTokenValue) => {
210
+ const getSectionPlaceholder = (utils, timezone, localeText, sectionConfig, currentTokenValue) => {
200
211
  switch (sectionConfig.type) {
201
212
  case 'year':
202
213
  {
203
214
  return localeText.fieldYearPlaceholder({
204
- digitAmount: utils.formatByString(utils.date(), currentTokenValue).length
215
+ digitAmount: utils.formatByString(utils.dateWithTimezone(undefined, timezone), currentTokenValue).length
205
216
  });
206
217
  }
207
218
  case 'month':
@@ -250,45 +261,46 @@ export const changeSectionValueFormat = (utils, valueStr, currentFormat, newForm
250
261
  }
251
262
  return utils.formatByString(utils.parse(valueStr, currentFormat), newFormat);
252
263
  };
253
- const isFourDigitYearFormat = (utils, format) => utils.formatByString(utils.date(), format).length === 4;
254
- export const doesSectionFormatHaveLeadingZeros = (utils, contentType, sectionType, format) => {
264
+ const isFourDigitYearFormat = (utils, timezone, format) => utils.formatByString(utils.dateWithTimezone(undefined, timezone), format).length === 4;
265
+ export const doesSectionFormatHaveLeadingZeros = (utils, timezone, contentType, sectionType, format) => {
255
266
  if (contentType !== 'digit') {
256
267
  return false;
257
268
  }
269
+ const now = utils.dateWithTimezone(undefined, timezone);
258
270
  switch (sectionType) {
259
271
  // We can't use `changeSectionValueFormat`, because `utils.parse('1', 'YYYY')` returns `1971` instead of `1`.
260
272
  case 'year':
261
273
  {
262
- if (isFourDigitYearFormat(utils, format)) {
263
- const formatted0001 = utils.formatByString(utils.setYear(utils.date(), 1), format);
274
+ if (isFourDigitYearFormat(utils, timezone, format)) {
275
+ const formatted0001 = utils.formatByString(utils.setYear(now, 1), format);
264
276
  return formatted0001 === '0001';
265
277
  }
266
- const formatted2001 = utils.formatByString(utils.setYear(utils.date(), 2001), format);
278
+ const formatted2001 = utils.formatByString(utils.setYear(now, 2001), format);
267
279
  return formatted2001 === '01';
268
280
  }
269
281
  case 'month':
270
282
  {
271
- return utils.formatByString(utils.startOfYear(utils.date()), format).length > 1;
283
+ return utils.formatByString(utils.startOfYear(now), format).length > 1;
272
284
  }
273
285
  case 'day':
274
286
  {
275
- return utils.formatByString(utils.startOfMonth(utils.date()), format).length > 1;
287
+ return utils.formatByString(utils.startOfMonth(now), format).length > 1;
276
288
  }
277
289
  case 'weekDay':
278
290
  {
279
- return utils.formatByString(utils.startOfWeek(utils.date()), format).length > 1;
291
+ return utils.formatByString(utils.startOfWeek(now), format).length > 1;
280
292
  }
281
293
  case 'hours':
282
294
  {
283
- return utils.formatByString(utils.setHours(utils.date(), 1), format).length > 1;
295
+ return utils.formatByString(utils.setHours(now, 1), format).length > 1;
284
296
  }
285
297
  case 'minutes':
286
298
  {
287
- return utils.formatByString(utils.setMinutes(utils.date(), 1), format).length > 1;
299
+ return utils.formatByString(utils.setMinutes(now, 1), format).length > 1;
288
300
  }
289
301
  case 'seconds':
290
302
  {
291
- return utils.formatByString(utils.setMinutes(utils.date(), 1), format).length > 1;
303
+ return utils.formatByString(utils.setMinutes(now, 1), format).length > 1;
292
304
  }
293
305
  default:
294
306
  {
@@ -313,7 +325,7 @@ const getEscapedPartsFromFormat = (utils, format) => {
313
325
  }
314
326
  return escapedParts;
315
327
  };
316
- export const splitFormatIntoSections = (utils, localeText, format, date, formatDensity, shouldRespectLeadingZeros, isRTL) => {
328
+ export const splitFormatIntoSections = (utils, timezone, localeText, format, date, formatDensity, shouldRespectLeadingZeros, isRTL) => {
317
329
  let startSeparator = '';
318
330
  const sections = [];
319
331
  const now = utils.date();
@@ -322,7 +334,7 @@ export const splitFormatIntoSections = (utils, localeText, format, date, formatD
322
334
  return null;
323
335
  }
324
336
  const sectionConfig = getDateSectionConfigFromFormatToken(utils, token);
325
- const hasLeadingZerosInFormat = doesSectionFormatHaveLeadingZeros(utils, sectionConfig.contentType, sectionConfig.type, token);
337
+ const hasLeadingZerosInFormat = doesSectionFormatHaveLeadingZeros(utils, timezone, sectionConfig.contentType, sectionConfig.type, token);
326
338
  const hasLeadingZerosInInput = shouldRespectLeadingZeros ? hasLeadingZerosInFormat : sectionConfig.contentType === 'digit';
327
339
  const isValidDate = date != null && utils.isValid(date);
328
340
  let sectionValue = isValidDate ? utils.formatByString(date, token) : '';
@@ -344,7 +356,7 @@ export const splitFormatIntoSections = (utils, localeText, format, date, formatD
344
356
  format: token,
345
357
  maxLength,
346
358
  value: sectionValue,
347
- placeholder: getSectionPlaceholder(utils, localeText, sectionConfig, token),
359
+ placeholder: getSectionPlaceholder(utils, timezone, localeText, sectionConfig, token),
348
360
  hasLeadingZeros: hasLeadingZerosInFormat,
349
361
  hasLeadingZerosInFormat,
350
362
  hasLeadingZerosInInput,
@@ -455,8 +467,8 @@ export const createDateStrForInputFromSections = (sections, isRTL) => {
455
467
  // wrap into an isolated group such that separators can split the string in smaller ones by adding \u2069\u2068
456
468
  return `\u2066${dateStr}\u2069`;
457
469
  };
458
- export const getSectionsBoundaries = utils => {
459
- const today = utils.date();
470
+ export const getSectionsBoundaries = (utils, timezone) => {
471
+ const today = utils.dateWithTimezone(undefined, timezone);
460
472
  const endOfYear = utils.endOfYear(today);
461
473
  const {
462
474
  maxDaysInMonth,
@@ -479,7 +491,7 @@ export const getSectionsBoundaries = utils => {
479
491
  format
480
492
  }) => ({
481
493
  minimum: 0,
482
- maximum: isFourDigitYearFormat(utils, format) ? 9999 : 99
494
+ maximum: isFourDigitYearFormat(utils, timezone, format) ? 9999 : 99
483
495
  }),
484
496
  month: () => ({
485
497
  minimum: 1,
@@ -498,7 +510,7 @@ export const getSectionsBoundaries = utils => {
498
510
  contentType
499
511
  }) => {
500
512
  if (contentType === 'digit') {
501
- const daysInWeek = getDaysInWeekStr(utils, format).map(Number);
513
+ const daysInWeek = getDaysInWeekStr(utils, timezone, format).map(Number);
502
514
  return {
503
515
  minimum: Math.min(...daysInWeek),
504
516
  maximum: Math.max(...daysInWeek)
@@ -560,7 +572,7 @@ export const validateSections = (sections, valueType) => {
560
572
  }
561
573
  }
562
574
  };
563
- const transferDateSectionValue = (utils, section, dateToTransferFrom, dateToTransferTo) => {
575
+ const transferDateSectionValue = (utils, timezone, section, dateToTransferFrom, dateToTransferTo) => {
564
576
  switch (section.type) {
565
577
  case 'year':
566
578
  {
@@ -572,7 +584,7 @@ const transferDateSectionValue = (utils, section, dateToTransferFrom, dateToTran
572
584
  }
573
585
  case 'weekDay':
574
586
  {
575
- const formattedDaysInWeek = getDaysInWeekStr(utils, section.format);
587
+ const formattedDaysInWeek = getDaysInWeekStr(utils, timezone, section.format);
576
588
  const dayInWeekStrOfActiveDate = utils.formatByString(dateToTransferFrom, section.format);
577
589
  const dayInWeekOfActiveDate = formattedDaysInWeek.indexOf(dayInWeekStrOfActiveDate);
578
590
  const dayInWeekOfNewSectionValue = formattedDaysInWeek.indexOf(section.value);
@@ -623,16 +635,16 @@ const reliableSectionModificationOrder = {
623
635
  seconds: 7,
624
636
  meridiem: 8
625
637
  };
626
- export const mergeDateIntoReferenceDate = (utils, dateToTransferFrom, sections, referenceDate, shouldLimitToEditedSections) =>
638
+ export const mergeDateIntoReferenceDate = (utils, timezone, dateToTransferFrom, sections, referenceDate, shouldLimitToEditedSections) =>
627
639
  // cloning sections before sort to avoid mutating it
628
640
  [...sections].sort((a, b) => reliableSectionModificationOrder[a.type] - reliableSectionModificationOrder[b.type]).reduce((mergedDate, section) => {
629
641
  if (!shouldLimitToEditedSections || section.modified) {
630
- return transferDateSectionValue(utils, section, dateToTransferFrom, mergedDate);
642
+ return transferDateSectionValue(utils, timezone, section, dateToTransferFrom, mergedDate);
631
643
  }
632
644
  return mergedDate;
633
645
  }, referenceDate);
634
646
  export const isAndroid = () => navigator.userAgent.toLowerCase().indexOf('android') > -1;
635
- export const clampDaySectionIfPossible = (utils, sections, sectionsValueBoundaries) => {
647
+ export const clampDaySectionIfPossible = (utils, timezone, sections, sectionsValueBoundaries) => {
636
648
  // We can only clamp the day value if:
637
649
  // 1. if all the sections are filled (except the week day section which can be empty)
638
650
  // 2. there is a day section
@@ -652,7 +664,7 @@ export const clampDaySectionIfPossible = (utils, sections, sectionsValueBoundari
652
664
  contentType: section.contentType
653
665
  });
654
666
  return _extends({}, section, {
655
- value: cleanDigitSectionValue(utils, dayBoundaries.minimum, dayBoundaries, section)
667
+ value: cleanDigitSectionValue(utils, timezone, dayBoundaries.minimum, dayBoundaries, section)
656
668
  });
657
669
  });
658
670
  const startOfMonth = getDateFromDateSections(utils, sectionsForStartOfMonth);
@@ -40,7 +40,8 @@ export const useFieldCharacterEditing = ({
40
40
  sections,
41
41
  updateSectionValue,
42
42
  sectionsValueBoundaries,
43
- setTempAndroidValueStr
43
+ setTempAndroidValueStr,
44
+ timezone
44
45
  }) => {
45
46
  const utils = useUtils();
46
47
  const [query, setQuery] = React.useState(null);
@@ -109,7 +110,7 @@ export const useFieldCharacterEditing = ({
109
110
  };
110
111
  };
111
112
  const testQueryOnFormatAndFallbackFormat = (queryValue, activeSection, fallbackFormat, formatFallbackValue) => {
112
- const getOptions = format => getLetterEditingOptions(utils, activeSection.type, format);
113
+ const getOptions = format => getLetterEditingOptions(utils, timezone, activeSection.type, format);
113
114
  if (activeSection.contentType === 'letter') {
114
115
  return findMatchingOptions(activeSection.format, getOptions(activeSection.format), queryValue);
115
116
  }
@@ -182,7 +183,7 @@ export const useFieldCharacterEditing = ({
182
183
  };
183
184
  }
184
185
  const shouldGoToNextSection = Number(`${queryValue}0`) > sectionBoundaries.maximum || queryValue.length === sectionBoundaries.maximum.toString().length;
185
- const newSectionValue = cleanDigitSectionValue(utils, queryValueNumber, sectionBoundaries, section);
186
+ const newSectionValue = cleanDigitSectionValue(utils, timezone, queryValueNumber, sectionBoundaries, section);
186
187
  return {
187
188
  sectionValue: newSectionValue,
188
189
  shouldGoToNextSection
@@ -196,7 +197,7 @@ export const useFieldCharacterEditing = ({
196
197
  // When editing a letter-format month and the user presses a digit,
197
198
  // We can support the numeric editing by using the digit-format month and re-formatting the result.
198
199
  if (activeSection.type === 'month') {
199
- const hasLeadingZerosInFormat = doesSectionFormatHaveLeadingZeros(utils, 'digit', 'month', 'MM');
200
+ const hasLeadingZerosInFormat = doesSectionFormatHaveLeadingZeros(utils, timezone, 'digit', 'month', 'MM');
200
201
  const response = getNewSectionValue(queryValue, {
201
202
  type: activeSection.type,
202
203
  format: 'MM',
@@ -221,7 +222,7 @@ export const useFieldCharacterEditing = ({
221
222
  if (isQueryResponseWithoutValue(response)) {
222
223
  return response;
223
224
  }
224
- const formattedValue = getDaysInWeekStr(utils, activeSection.format)[Number(response.sectionValue) - 1];
225
+ const formattedValue = getDaysInWeekStr(utils, timezone, activeSection.format)[Number(response.sectionValue) - 1];
225
226
  return _extends({}, response, {
226
227
  sectionValue: formattedValue
227
228
  });
@@ -4,6 +4,7 @@ import useControlled from '@mui/utils/useControlled';
4
4
  import { useTheme } from '@mui/material/styles';
5
5
  import { useUtils, useLocaleText, useLocalizationContext } from '../useUtils';
6
6
  import { addPositionPropertiesToSections, splitFormatIntoSections, clampDaySectionIfPossible, mergeDateIntoReferenceDate, getSectionsBoundaries, validateSections, getDateFromDateSections } from './useField.utils';
7
+ import { useValueWithTimezone } from '../useValueWithTimezone';
7
8
  import { getSectionTypeGranularity } from '../../utils/getDefaultReferenceDate';
8
9
  export const useFieldState = params => {
9
10
  const utils = useUtils();
@@ -26,13 +27,23 @@ export const useFieldState = params => {
26
27
  formatDensity = 'dense',
27
28
  selectedSections: selectedSectionsProp,
28
29
  onSelectedSectionsChange,
29
- shouldRespectLeadingZeros = false
30
+ shouldRespectLeadingZeros = false,
31
+ timezone: timezoneProp
30
32
  }
31
33
  } = params;
32
- const firstDefaultValue = React.useRef(defaultValue);
33
- const valueFromTheOutside = valueProp ?? firstDefaultValue.current ?? valueManager.emptyValue;
34
- const sectionsValueBoundaries = React.useMemo(() => getSectionsBoundaries(utils), [utils]);
35
- const getSectionsFromValue = React.useCallback((value, fallbackSections = null) => fieldValueManager.getSectionsFromValue(utils, value, fallbackSections, isRTL, date => splitFormatIntoSections(utils, localeText, format, date, formatDensity, shouldRespectLeadingZeros, isRTL)), [fieldValueManager, format, localeText, isRTL, shouldRespectLeadingZeros, utils, formatDensity]);
34
+ const {
35
+ timezone,
36
+ value: valueFromTheOutside,
37
+ handleValueChange
38
+ } = useValueWithTimezone({
39
+ timezone: timezoneProp,
40
+ value: valueProp,
41
+ defaultValue,
42
+ onChange,
43
+ valueManager
44
+ });
45
+ const sectionsValueBoundaries = React.useMemo(() => getSectionsBoundaries(utils, timezone), [utils, timezone]);
46
+ const getSectionsFromValue = React.useCallback((value, fallbackSections = null) => fieldValueManager.getSectionsFromValue(utils, value, fallbackSections, isRTL, date => splitFormatIntoSections(utils, timezone, localeText, format, date, formatDensity, shouldRespectLeadingZeros, isRTL)), [fieldValueManager, format, localeText, isRTL, shouldRespectLeadingZeros, utils, formatDensity, timezone]);
36
47
  const placeholder = React.useMemo(() => fieldValueManager.getValueStrFromSections(getSectionsFromValue(valueManager.emptyValue), isRTL), [fieldValueManager, getSectionsFromValue, valueManager.emptyValue, isRTL]);
37
48
  const [state, setState] = React.useState(() => {
38
49
  const sections = getSectionsFromValue(valueFromTheOutside);
@@ -49,7 +60,8 @@ export const useFieldState = params => {
49
60
  value: valueFromTheOutside,
50
61
  utils,
51
62
  props: internalProps,
52
- granularity
63
+ granularity,
64
+ timezone
53
65
  });
54
66
  return _extends({}, stateWithoutReferenceDate, {
55
67
  referenceValue
@@ -105,18 +117,17 @@ export const useFieldState = params => {
105
117
  referenceValue,
106
118
  tempValueStrAndroid: null
107
119
  }));
108
- if (onChange) {
109
- const context = {
110
- validationError: validator({
111
- adapter,
120
+ const context = {
121
+ validationError: validator({
122
+ adapter,
123
+ value,
124
+ props: _extends({}, internalProps, {
112
125
  value,
113
- props: _extends({}, internalProps, {
114
- value
115
- })
126
+ timezone
116
127
  })
117
- };
118
- onChange(value, context);
119
- }
128
+ })
129
+ };
130
+ handleValueChange(value, context);
120
131
  };
121
132
  const setSectionValue = (sectionIndex, newSectionValue) => {
122
133
  const newSections = [...state.sections];
@@ -167,8 +178,8 @@ export const useFieldState = params => {
167
178
  if (date == null || !utils.isValid(date)) {
168
179
  return null;
169
180
  }
170
- const sections = splitFormatIntoSections(utils, localeText, format, date, formatDensity, shouldRespectLeadingZeros, isRTL);
171
- return mergeDateIntoReferenceDate(utils, date, sections, referenceDate, false);
181
+ const sections = splitFormatIntoSections(utils, timezone, localeText, format, date, formatDensity, shouldRespectLeadingZeros, isRTL);
182
+ return mergeDateIntoReferenceDate(utils, timezone, date, sections, referenceDate, false);
172
183
  };
173
184
  const newValue = fieldValueManager.parseValueStr(valueStr, state.referenceValue, parseDateStr);
174
185
  const newReferenceValue = fieldValueManager.updateReferenceValue(utils, newValue, state.referenceValue);
@@ -207,7 +218,7 @@ export const useFieldState = params => {
207
218
  * This can be useful if the month has fewer days than the day value currently provided.
208
219
  */
209
220
  if (!utils.isValid(newActiveDate)) {
210
- const clampedSections = clampDaySectionIfPossible(utils, newActiveDateSections, sectionsValueBoundaries);
221
+ const clampedSections = clampDaySectionIfPossible(utils, timezone, newActiveDateSections, sectionsValueBoundaries);
211
222
  if (clampedSections != null) {
212
223
  shouldRegenSections = true;
213
224
  newActiveDate = getDateFromDateSections(utils, clampedSections);
@@ -222,7 +233,7 @@ export const useFieldState = params => {
222
233
  * This makes sure that we don't lose some information of the initial date (like the time on a date field).
223
234
  */
224
235
  if (newActiveDate != null && utils.isValid(newActiveDate)) {
225
- const mergedDate = mergeDateIntoReferenceDate(utils, newActiveDate, newActiveDateSections, activeDateManager.referenceDate, true);
236
+ const mergedDate = mergeDateIntoReferenceDate(utils, timezone, newActiveDate, newActiveDateSections, activeDateManager.referenceDate, true);
226
237
  values = activeDateManager.getNewValuesFromNewActiveDate(mergedDate);
227
238
  shouldPublish = true;
228
239
  } else {
@@ -286,6 +297,7 @@ export const useFieldState = params => {
286
297
  updateValueFromValueStr,
287
298
  setTempAndroidValueStr,
288
299
  sectionsValueBoundaries,
289
- placeholder
300
+ placeholder,
301
+ timezone
290
302
  };
291
303
  };
@@ -32,6 +32,7 @@ export const useMobilePicker = _ref => {
32
32
  sx,
33
33
  format,
34
34
  formatDensity,
35
+ timezone,
35
36
  label,
36
37
  inputRef,
37
38
  readOnly,
@@ -71,6 +72,7 @@ export const useMobilePicker = _ref => {
71
72
  sx,
72
73
  format,
73
74
  formatDensity,
75
+ timezone,
74
76
  label
75
77
  }),
76
78
  ownerState: props
@@ -5,6 +5,8 @@ import useEventCallback from '@mui/utils/useEventCallback';
5
5
  import { useOpenState } from '../useOpenState';
6
6
  import { useLocalizationContext, useUtils } from '../useUtils';
7
7
  import { useValidation } from '../useValidation';
8
+ import { useValueWithTimezone } from '../useValueWithTimezone';
9
+
8
10
  /**
9
11
  * Decide if the new value should be published
10
12
  * The published value will be passed to `onChange` if defined.
@@ -122,7 +124,8 @@ export const usePickerValue = ({
122
124
  defaultValue: inDefaultValue,
123
125
  closeOnSelect = wrapperVariant === 'desktop',
124
126
  selectedSections: selectedSectionsProp,
125
- onSelectedSectionsChange
127
+ onSelectedSectionsChange,
128
+ timezone: timezoneProp
126
129
  } = props;
127
130
  const {
128
131
  current: defaultValue
@@ -175,8 +178,19 @@ export const usePickerValue = ({
175
178
  hasBeenModifiedSinceMount: false
176
179
  };
177
180
  });
181
+ const {
182
+ timezone,
183
+ handleValueChange
184
+ } = useValueWithTimezone({
185
+ timezone: timezoneProp,
186
+ value: inValue,
187
+ defaultValue,
188
+ onChange,
189
+ valueManager
190
+ });
178
191
  useValidation(_extends({}, props, {
179
- value: dateState.draft
192
+ value: dateState.draft,
193
+ timezone
180
194
  }), validator, valueManager.isSameError, valueManager.defaultErrorState);
181
195
  const updateDate = useEventCallback(action => {
182
196
  const updaterParams = {
@@ -195,18 +209,19 @@ export const usePickerValue = ({
195
209
  lastCommittedValue: shouldCommit ? action.value : prev.lastCommittedValue,
196
210
  hasBeenModifiedSinceMount: true
197
211
  }));
198
- if (shouldPublish && onChange) {
212
+ if (shouldPublish) {
199
213
  const validationError = action.name === 'setValueFromField' ? action.context.validationError : validator({
200
214
  adapter,
201
215
  value: action.value,
202
216
  props: _extends({}, props, {
203
- value: action.value
217
+ value: action.value,
218
+ timezone
204
219
  })
205
220
  });
206
221
  const context = {
207
222
  validationError
208
223
  };
209
- onChange(action.value, context);
224
+ handleValueChange(action.value, context);
210
225
  }
211
226
  if (shouldCommit && onAccept) {
212
227
  onAccept(action.value);
@@ -256,7 +271,7 @@ export const usePickerValue = ({
256
271
  });
257
272
  const handleSetToday = useEventCallback(() => {
258
273
  updateDate({
259
- value: valueManager.getTodayValue(utils, valueType),
274
+ value: valueManager.getTodayValue(utils, timezone, valueType),
260
275
  name: 'setValueFromAction',
261
276
  pickerAction: 'today'
262
277
  });
@@ -310,7 +325,8 @@ export const usePickerValue = ({
310
325
  adapter,
311
326
  value: testedValue,
312
327
  props: _extends({}, props, {
313
- value: testedValue
328
+ value: testedValue,
329
+ timezone
314
330
  })
315
331
  });
316
332
  return !valueManager.hasError(error);
@@ -43,7 +43,8 @@ export const usePickerViews = ({
43
43
  openTo,
44
44
  onViewChange,
45
45
  disableOpenPicker,
46
- viewRenderers
46
+ viewRenderers,
47
+ timezone
47
48
  } = props;
48
49
  const propsToForwardToView = _objectWithoutPropertiesLoose(props, _excluded);
49
50
  const {
@@ -95,11 +96,14 @@ export const usePickerViews = ({
95
96
  setPopperView(view);
96
97
  }
97
98
  useEnhancedEffect(() => {
99
+ // Handle case of `DateTimePicker` without time renderers
98
100
  if (currentViewMode === 'field' && open) {
99
101
  onClose();
100
- onSelectedSectionsChange('hours');
101
102
  setTimeout(() => {
103
+ // focusing the input before the range selection is done
104
+ // calling `onSelectedSectionsChange` outside of timeout results in an inconsistent behavior between Safari And Chrome
102
105
  inputRef?.current.focus();
106
+ onSelectedSectionsChange(view);
103
107
  });
104
108
  }
105
109
  }, [view]); // eslint-disable-line react-hooks/exhaustive-deps
@@ -144,6 +148,7 @@ export const usePickerViews = ({
144
148
  }
145
149
  return renderer(_extends({}, propsToForwardToView, additionalViewProps, propsFromPickerValue, {
146
150
  views,
151
+ timezone,
147
152
  onChange: setValueAndGoToNextView,
148
153
  view: popperView,
149
154
  onViewChange: setView,
@@ -18,8 +18,11 @@ export const useLocalizationContext = () => {
18
18
  export const useUtils = () => useLocalizationContext().utils;
19
19
  export const useDefaultDates = () => useLocalizationContext().defaultDates;
20
20
  export const useLocaleText = () => useLocalizationContext().localeText;
21
- export const useNow = () => {
21
+ export const useNow = timezone => {
22
22
  const utils = useUtils();
23
- const now = React.useRef(utils.date());
23
+ const now = React.useRef();
24
+ if (now.current === undefined) {
25
+ now.current = utils.dateWithTimezone(undefined, timezone);
26
+ }
24
27
  return now.current;
25
28
  };
@@ -0,0 +1,68 @@
1
+ import * as React from 'react';
2
+ import useEventCallback from '@mui/utils/useEventCallback';
3
+ import useControlled from '@mui/utils/useControlled';
4
+ import { useUtils } from './useUtils';
5
+ /**
6
+ * Hooks making sure that:
7
+ * - The value returned by `onChange` always have the timezone of `props.value` or `props.defaultValue` if defined
8
+ * - The value rendered is always the one from `props.timezone` if defined
9
+ */
10
+ export const useValueWithTimezone = ({
11
+ timezone: timezoneProp,
12
+ value: valueProp,
13
+ defaultValue,
14
+ onChange,
15
+ valueManager
16
+ }) => {
17
+ const utils = useUtils();
18
+ const firstDefaultValue = React.useRef(defaultValue);
19
+ const inputValue = valueProp ?? firstDefaultValue.current ?? valueManager.emptyValue;
20
+ const inputTimezone = React.useMemo(() => valueManager.getTimezone(utils, inputValue), [utils, valueManager, inputValue]);
21
+ const setInputTimezone = useEventCallback(newValue => {
22
+ if (inputTimezone == null) {
23
+ return newValue;
24
+ }
25
+ return valueManager.setTimezone(utils, inputTimezone, newValue);
26
+ });
27
+ const timezoneToRender = timezoneProp ?? inputTimezone ?? 'default';
28
+ const valueWithTimezoneToRender = React.useMemo(() => valueManager.setTimezone(utils, timezoneToRender, inputValue), [valueManager, utils, timezoneToRender, inputValue]);
29
+ const handleValueChange = useEventCallback((newValue, ...otherParams) => {
30
+ const newValueWithInputTimezone = setInputTimezone(newValue);
31
+ onChange?.(newValueWithInputTimezone, ...otherParams);
32
+ });
33
+ return {
34
+ value: valueWithTimezoneToRender,
35
+ handleValueChange,
36
+ timezone: timezoneToRender
37
+ };
38
+ };
39
+
40
+ /**
41
+ * Wrapper around `useControlled` and `useValueWithTimezone`
42
+ */
43
+ export const useControlledValueWithTimezone = ({
44
+ name,
45
+ timezone: timezoneProp,
46
+ value: valueProp,
47
+ defaultValue,
48
+ onChange: onChangeProp,
49
+ valueManager
50
+ }) => {
51
+ const [valueWithInputTimezone, setValue] = useControlled({
52
+ name,
53
+ state: 'value',
54
+ controlled: valueProp,
55
+ default: defaultValue ?? valueManager.emptyValue
56
+ });
57
+ const onChange = useEventCallback((newValue, ...otherParams) => {
58
+ setValue(newValue);
59
+ onChangeProp?.(newValue, ...otherParams);
60
+ });
61
+ return useValueWithTimezone({
62
+ timezone: timezoneProp,
63
+ value: valueWithInputTimezone,
64
+ defaultValue: undefined,
65
+ onChange,
66
+ valueManager
67
+ });
68
+ };
@@ -9,6 +9,7 @@ export { pickersArrowSwitcherClasses } from './components/PickersArrowSwitcher/p
9
9
  export { pickersPopperClasses } from './components/pickersPopperClasses';
10
10
  export { PickersToolbarButton } from './components/PickersToolbarButton';
11
11
  export { DAY_MARGIN, DIALOG_WIDTH } from './constants/dimensions';
12
+ export { useControlledValueWithTimezone } from './hooks/useValueWithTimezone';
12
13
  export { useField, createDateStrForInputFromSections, addPositionPropertiesToSections } from './hooks/useField';
13
14
  export { usePicker } from './hooks/usePicker';
14
15
  export { useStaticPicker } from './hooks/useStaticPicker';