@gravity-ui/date-components 3.4.1 → 4.0.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 (407) hide show
  1. package/README.md +32 -1
  2. package/dist/cjs/components/Calendar/Calendar.d.ts +6 -2
  3. package/dist/cjs/components/Calendar/Calendar.js +1 -1
  4. package/dist/cjs/components/Calendar/Calendar.js.map +1 -1
  5. package/dist/cjs/components/CalendarView/Calendar.css +3 -0
  6. package/dist/cjs/components/CalendarView/Calendar.css.map +1 -1
  7. package/dist/cjs/components/CalendarView/CalendarView.d.ts +2 -1
  8. package/dist/cjs/components/CalendarView/CalendarView.js +8 -8
  9. package/dist/cjs/components/CalendarView/CalendarView.js.map +1 -1
  10. package/dist/cjs/components/CalendarView/hooks/types.d.ts +5 -3
  11. package/dist/cjs/components/CalendarView/hooks/types.js.map +1 -1
  12. package/dist/cjs/components/CalendarView/hooks/useCalendarCellProps.js +1 -2
  13. package/dist/cjs/components/CalendarView/hooks/useCalendarCellProps.js.map +1 -1
  14. package/dist/cjs/components/CalendarView/hooks/useCalendarGridProps.js +10 -3
  15. package/dist/cjs/components/CalendarView/hooks/useCalendarGridProps.js.map +1 -1
  16. package/dist/cjs/components/CalendarView/hooks/useCalendarProps.js +7 -1
  17. package/dist/cjs/components/CalendarView/hooks/useCalendarProps.js.map +1 -1
  18. package/dist/cjs/components/CalendarView/hooks/useCalendarState.d.ts +5 -3
  19. package/dist/cjs/components/CalendarView/hooks/useCalendarState.js +74 -30
  20. package/dist/cjs/components/CalendarView/hooks/useCalendarState.js.map +1 -1
  21. package/dist/cjs/components/CalendarView/hooks/useRangeCalendarState.js +16 -9
  22. package/dist/cjs/components/CalendarView/hooks/useRangeCalendarState.js.map +1 -1
  23. package/dist/cjs/components/DateField/DateField.d.ts +1 -1
  24. package/dist/cjs/components/DateField/DateField.js +5 -17
  25. package/dist/cjs/components/DateField/DateField.js.map +1 -1
  26. package/dist/cjs/components/DateField/IncompleteDate.d.ts +28 -0
  27. package/dist/cjs/components/DateField/IncompleteDate.js +116 -0
  28. package/dist/cjs/components/DateField/IncompleteDate.js.map +1 -0
  29. package/dist/cjs/components/DateField/hooks/useBaseDateFieldState.d.ts +34 -68
  30. package/dist/cjs/components/DateField/hooks/useBaseDateFieldState.js +18 -249
  31. package/dist/cjs/components/DateField/hooks/useBaseDateFieldState.js.map +1 -1
  32. package/dist/cjs/components/DateField/hooks/useDateFieldProps.d.ts +2 -0
  33. package/dist/cjs/components/DateField/hooks/useDateFieldProps.js +250 -54
  34. package/dist/cjs/components/DateField/hooks/useDateFieldProps.js.map +1 -1
  35. package/dist/cjs/components/DateField/hooks/useDateFieldState.d.ts +20 -2
  36. package/dist/cjs/components/DateField/hooks/useDateFieldState.js +85 -117
  37. package/dist/cjs/components/DateField/hooks/useDateFieldState.js.map +1 -1
  38. package/dist/cjs/components/DateField/hooks/useFocusManager.d.ts +29 -0
  39. package/dist/cjs/components/DateField/hooks/useFocusManager.js +116 -0
  40. package/dist/cjs/components/DateField/hooks/useFocusManager.js.map +1 -0
  41. package/dist/cjs/components/{RangeDateField → DateField}/hooks/useRangeDateFieldState.d.ts +1 -1
  42. package/dist/cjs/components/DateField/hooks/useRangeDateFieldState.js +236 -0
  43. package/dist/cjs/components/DateField/hooks/useRangeDateFieldState.js.map +1 -0
  44. package/dist/cjs/components/DateField/i18n/ru.json +1 -1
  45. package/dist/cjs/components/DateField/index.d.ts +2 -1
  46. package/dist/cjs/components/DateField/index.js +1 -1
  47. package/dist/cjs/components/DateField/index.js.map +1 -1
  48. package/dist/cjs/components/DateField/types.d.ts +2 -22
  49. package/dist/cjs/components/DateField/types.js.map +1 -1
  50. package/dist/cjs/components/DateField/utils.d.ts +21 -34
  51. package/dist/cjs/components/DateField/utils.js +186 -138
  52. package/dist/cjs/components/DateField/utils.js.map +1 -1
  53. package/dist/cjs/components/DatePicker/DatePicker.d.ts +3 -3
  54. package/dist/cjs/components/DatePicker/DatePicker.js +3 -4
  55. package/dist/cjs/components/DatePicker/DatePicker.js.map +1 -1
  56. package/dist/cjs/components/DatePicker/MobileCalendar.d.ts +1 -1
  57. package/dist/cjs/components/DatePicker/MobileCalendar.js +3 -5
  58. package/dist/cjs/components/DatePicker/MobileCalendar.js.map +1 -1
  59. package/dist/cjs/components/DatePicker/StubButton.d.ts +1 -1
  60. package/dist/cjs/components/DatePicker/hooks/datePickerStateFactory.d.ts +1 -1
  61. package/dist/cjs/components/DatePicker/hooks/datePickerStateFactory.js +15 -6
  62. package/dist/cjs/components/DatePicker/hooks/datePickerStateFactory.js.map +1 -1
  63. package/dist/cjs/components/DatePicker/hooks/useDatePickerProps.d.ts +2 -2
  64. package/dist/cjs/components/DatePicker/hooks/useDatePickerProps.js +30 -17
  65. package/dist/cjs/components/DatePicker/hooks/useDatePickerProps.js.map +1 -1
  66. package/dist/cjs/components/HiddenInput/HiddenInput.d.ts +2 -5
  67. package/dist/cjs/components/HiddenInput/HiddenInput.js +2 -4
  68. package/dist/cjs/components/HiddenInput/HiddenInput.js.map +1 -1
  69. package/dist/cjs/components/RangeCalendar/RangeCalendar.d.ts +2 -2
  70. package/dist/cjs/components/RangeCalendar/RangeCalendar.js +2 -3
  71. package/dist/cjs/components/RangeCalendar/RangeCalendar.js.map +1 -1
  72. package/dist/cjs/components/RangeDateField/RangeDateField.d.ts +1 -1
  73. package/dist/cjs/components/RangeDateField/RangeDateField.js +6 -19
  74. package/dist/cjs/components/RangeDateField/RangeDateField.js.map +1 -1
  75. package/dist/cjs/components/RangeDateField/index.d.ts +0 -1
  76. package/dist/cjs/components/RangeDateField/index.js +0 -1
  77. package/dist/cjs/components/RangeDateField/index.js.map +1 -1
  78. package/dist/cjs/components/RangeDatePicker/RangeDatePicker.d.ts +2 -1
  79. package/dist/cjs/components/RangeDatePicker/RangeDatePicker.js +4 -5
  80. package/dist/cjs/components/RangeDatePicker/RangeDatePicker.js.map +1 -1
  81. package/dist/cjs/components/RangeDatePicker/hooks/useRangeDatePickerState.d.ts +1 -1
  82. package/dist/cjs/components/RangeDatePicker/hooks/useRangeDatePickerState.js +3 -4
  83. package/dist/cjs/components/RangeDatePicker/hooks/useRangeDatePickerState.js.map +1 -1
  84. package/dist/cjs/components/RangeDateSelection/RangeDateSelection.d.ts +1 -1
  85. package/dist/cjs/components/RangeDateSelection/RangeDateSelection.js +4 -5
  86. package/dist/cjs/components/RangeDateSelection/RangeDateSelection.js.map +1 -1
  87. package/dist/cjs/components/RangeDateSelection/components/NowLine/NowLine.d.ts +2 -1
  88. package/dist/cjs/components/RangeDateSelection/components/Ruler/Ruler.d.ts +1 -1
  89. package/dist/cjs/components/RangeDateSelection/components/Ruler/Ruler.js +1 -1
  90. package/dist/cjs/components/RangeDateSelection/components/Ruler/Ruler.js.map +1 -1
  91. package/dist/cjs/components/RangeDateSelection/components/RulerViewport/RulerViewport.js +2 -2
  92. package/dist/cjs/components/RangeDateSelection/components/RulerViewport/RulerViewport.js.map +1 -1
  93. package/dist/cjs/components/RangeDateSelection/components/SelectionControl/SelectionControl.d.ts +2 -1
  94. package/dist/cjs/components/RangeDateSelection/components/SelectionControl/SelectionControl.js +12 -21
  95. package/dist/cjs/components/RangeDateSelection/components/SelectionControl/SelectionControl.js.map +1 -1
  96. package/dist/cjs/components/RangeDateSelection/components/Ticks/MiddleTicks.d.ts +1 -1
  97. package/dist/cjs/components/RangeDateSelection/components/Ticks/SlitTicks.d.ts +1 -1
  98. package/dist/cjs/components/RangeDateSelection/components/Ticks/Ticks.d.ts +2 -2
  99. package/dist/cjs/components/RangeDateSelection/components/Ticks/Ticks.js.map +1 -1
  100. package/dist/cjs/components/RangeDateSelection/components/Ticks/UnavialableTicks.d.ts +3 -4
  101. package/dist/cjs/components/RangeDateSelection/components/Ticks/UnavialableTicks.js +0 -3
  102. package/dist/cjs/components/RangeDateSelection/components/Ticks/UnavialableTicks.js.map +1 -1
  103. package/dist/cjs/components/RangeDateSelection/hooks/useRangeDateSelectionState.js +9 -10
  104. package/dist/cjs/components/RangeDateSelection/hooks/useRangeDateSelectionState.js.map +1 -1
  105. package/dist/cjs/components/RangeDateSelection/utils/span.d.ts +2 -1
  106. package/dist/cjs/components/RangeDateSelection/utils/span.js.map +1 -1
  107. package/dist/cjs/components/RelativeDateField/RelativeDateField.d.ts +1 -1
  108. package/dist/cjs/components/RelativeDateField/RelativeDateField.js +3 -40
  109. package/dist/cjs/components/RelativeDateField/RelativeDateField.js.map +1 -1
  110. package/dist/cjs/components/RelativeDateField/hooks/useRelativeDateFieldProps.d.ts +7 -2
  111. package/dist/cjs/components/RelativeDateField/hooks/useRelativeDateFieldProps.js +71 -9
  112. package/dist/cjs/components/RelativeDateField/hooks/useRelativeDateFieldProps.js.map +1 -1
  113. package/dist/cjs/components/RelativeDateField/hooks/useRelativeDateFieldState.d.ts +2 -0
  114. package/dist/cjs/components/RelativeDateField/hooks/useRelativeDateFieldState.js +48 -34
  115. package/dist/cjs/components/RelativeDateField/hooks/useRelativeDateFieldState.js.map +1 -1
  116. package/dist/cjs/components/RelativeDatePicker/RelativeDatePicker.d.ts +1 -1
  117. package/dist/cjs/components/RelativeDatePicker/RelativeDatePicker.js +7 -4
  118. package/dist/cjs/components/RelativeDatePicker/RelativeDatePicker.js.map +1 -1
  119. package/dist/cjs/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.d.ts +1 -1
  120. package/dist/cjs/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.js +52 -21
  121. package/dist/cjs/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.js.map +1 -1
  122. package/dist/cjs/components/RelativeDatePicker/hooks/useRelativeDatePickerState.js +9 -10
  123. package/dist/cjs/components/RelativeDatePicker/hooks/useRelativeDatePickerState.js.map +1 -1
  124. package/dist/cjs/components/RelativeRangeDatePicker/RelativeRangeDatePicker.css +1 -1
  125. package/dist/cjs/components/RelativeRangeDatePicker/RelativeRangeDatePicker.d.ts +2 -1
  126. package/dist/cjs/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js +13 -5
  127. package/dist/cjs/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js.map +1 -1
  128. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.css +1 -1
  129. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.css.map +1 -1
  130. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.d.ts +2 -1
  131. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.js +12 -6
  132. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.js.map +1 -1
  133. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.d.ts +1 -1
  134. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js +6 -8
  135. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js.map +1 -1
  136. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.d.ts +5 -3
  137. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.js +18 -16
  138. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.js.map +1 -1
  139. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.d.ts +2 -1
  140. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.js +33 -7
  141. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.js.map +1 -1
  142. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.d.ts +3 -3
  143. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.js +19 -18
  144. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.js.map +1 -1
  145. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/Presets.d.ts +6 -9
  146. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/Presets.js +9 -12
  147. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/Presets.js.map +1 -1
  148. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/defaultPresets.d.ts +2 -2
  149. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/defaultPresets.js.map +1 -1
  150. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/utils.d.ts +9 -4
  151. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/utils.js +27 -16
  152. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/utils.js.map +1 -1
  153. package/dist/cjs/components/RelativeRangeDatePicker/components/Zones/Zones.d.ts +3 -3
  154. package/dist/cjs/components/RelativeRangeDatePicker/components/Zones/Zones.js +3 -4
  155. package/dist/cjs/components/RelativeRangeDatePicker/components/Zones/Zones.js.map +1 -1
  156. package/dist/cjs/components/RelativeRangeDatePicker/hooks/useRelativeRangeDatePickerState.js +8 -7
  157. package/dist/cjs/components/RelativeRangeDatePicker/hooks/useRelativeRangeDatePickerState.js.map +1 -1
  158. package/dist/cjs/components/RelativeRangeDatePicker/types.js.map +1 -1
  159. package/dist/cjs/components/RelativeRangeDatePicker/utils.d.ts +2 -2
  160. package/dist/cjs/components/RelativeRangeDatePicker/utils.js +32 -9
  161. package/dist/cjs/components/RelativeRangeDatePicker/utils.js.map +1 -1
  162. package/dist/cjs/components/types/inputs.js.map +1 -1
  163. package/dist/cjs/components/utils/constants.d.ts +2 -0
  164. package/dist/cjs/components/utils/constants.js +7 -1
  165. package/dist/cjs/components/utils/constants.js.map +1 -1
  166. package/dist/cjs/components/utils/dates.d.ts +6 -1
  167. package/dist/cjs/components/utils/dates.js +6 -1
  168. package/dist/cjs/components/utils/dates.js.map +1 -1
  169. package/dist/cjs/components/utils/filterDOMProps.js +1 -1
  170. package/dist/cjs/components/utils/filterDOMProps.js.map +1 -1
  171. package/dist/cjs/components/utils/mergeProps.js +1 -1
  172. package/dist/cjs/components/utils/mergeProps.js.map +1 -1
  173. package/dist/cjs/components/utils/validation/datePicker.js +1 -1
  174. package/dist/cjs/components/utils/validation/datePicker.js.map +1 -1
  175. package/dist/cjs/components/utils/validation/i18n/ru.json +2 -2
  176. package/dist/cjs/components/utils/validation/relativeRangeDatePicker.d.ts +7 -11
  177. package/dist/cjs/components/utils/validation/relativeRangeDatePicker.js +6 -1
  178. package/dist/cjs/components/utils/validation/relativeRangeDatePicker.js.map +1 -1
  179. package/dist/cjs/hooks/useEventHandler.js +1 -2
  180. package/dist/cjs/hooks/useEventHandler.js.map +1 -1
  181. package/dist/cjs/hooks/useMove.d.ts +2 -2
  182. package/dist/cjs/hooks/useMove.js +6 -9
  183. package/dist/cjs/hooks/useMove.js.map +1 -1
  184. package/dist/cjs/hooks/useResizeObserver.js +1 -1
  185. package/dist/cjs/hooks/useResizeObserver.js.map +1 -1
  186. package/dist/cjs/package.json +1 -1
  187. package/dist/docs/INDEX.md +131 -0
  188. package/dist/docs/components/Calendar.md +125 -0
  189. package/dist/docs/components/DateField.md +206 -0
  190. package/dist/docs/components/DatePicker.md +194 -0
  191. package/dist/docs/components/RangeCalendar.md +125 -0
  192. package/dist/docs/components/RelativeDateField.md +73 -0
  193. package/dist/docs/components/RelativeDatePicker.md +59 -0
  194. package/dist/esm/components/Calendar/Calendar.d.ts +6 -2
  195. package/dist/esm/components/Calendar/Calendar.js +1 -1
  196. package/dist/esm/components/Calendar/Calendar.js.map +1 -1
  197. package/dist/esm/components/CalendarView/Calendar.css +3 -0
  198. package/dist/esm/components/CalendarView/Calendar.css.map +1 -1
  199. package/dist/esm/components/CalendarView/CalendarView.d.ts +2 -1
  200. package/dist/esm/components/CalendarView/CalendarView.js +8 -8
  201. package/dist/esm/components/CalendarView/CalendarView.js.map +1 -1
  202. package/dist/esm/components/CalendarView/hooks/types.d.ts +5 -3
  203. package/dist/esm/components/CalendarView/hooks/types.js.map +1 -1
  204. package/dist/esm/components/CalendarView/hooks/useCalendarCellProps.js +1 -2
  205. package/dist/esm/components/CalendarView/hooks/useCalendarCellProps.js.map +1 -1
  206. package/dist/esm/components/CalendarView/hooks/useCalendarGridProps.js +10 -3
  207. package/dist/esm/components/CalendarView/hooks/useCalendarGridProps.js.map +1 -1
  208. package/dist/esm/components/CalendarView/hooks/useCalendarProps.js +7 -1
  209. package/dist/esm/components/CalendarView/hooks/useCalendarProps.js.map +1 -1
  210. package/dist/esm/components/CalendarView/hooks/useCalendarState.d.ts +5 -3
  211. package/dist/esm/components/CalendarView/hooks/useCalendarState.js +74 -30
  212. package/dist/esm/components/CalendarView/hooks/useCalendarState.js.map +1 -1
  213. package/dist/esm/components/CalendarView/hooks/useRangeCalendarState.js +16 -10
  214. package/dist/esm/components/CalendarView/hooks/useRangeCalendarState.js.map +1 -1
  215. package/dist/esm/components/DateField/DateField.d.ts +1 -1
  216. package/dist/esm/components/DateField/DateField.js +6 -18
  217. package/dist/esm/components/DateField/DateField.js.map +1 -1
  218. package/dist/esm/components/DateField/IncompleteDate.d.ts +28 -0
  219. package/dist/esm/components/DateField/IncompleteDate.js +112 -0
  220. package/dist/esm/components/DateField/IncompleteDate.js.map +1 -0
  221. package/dist/esm/components/DateField/hooks/useBaseDateFieldState.d.ts +34 -68
  222. package/dist/esm/components/DateField/hooks/useBaseDateFieldState.js +17 -248
  223. package/dist/esm/components/DateField/hooks/useBaseDateFieldState.js.map +1 -1
  224. package/dist/esm/components/DateField/hooks/useDateFieldProps.d.ts +2 -0
  225. package/dist/esm/components/DateField/hooks/useDateFieldProps.js +250 -54
  226. package/dist/esm/components/DateField/hooks/useDateFieldProps.js.map +1 -1
  227. package/dist/esm/components/DateField/hooks/useDateFieldState.d.ts +20 -2
  228. package/dist/esm/components/DateField/hooks/useDateFieldState.js +87 -119
  229. package/dist/esm/components/DateField/hooks/useDateFieldState.js.map +1 -1
  230. package/dist/esm/components/DateField/hooks/useFocusManager.d.ts +29 -0
  231. package/dist/esm/components/DateField/hooks/useFocusManager.js +112 -0
  232. package/dist/esm/components/DateField/hooks/useFocusManager.js.map +1 -0
  233. package/dist/esm/components/{RangeDateField → DateField}/hooks/useRangeDateFieldState.d.ts +1 -1
  234. package/dist/esm/components/DateField/hooks/useRangeDateFieldState.js +232 -0
  235. package/dist/esm/components/DateField/hooks/useRangeDateFieldState.js.map +1 -0
  236. package/dist/esm/components/DateField/i18n/ru.json +1 -1
  237. package/dist/esm/components/DateField/index.d.ts +2 -1
  238. package/dist/esm/components/DateField/index.js +1 -1
  239. package/dist/esm/components/DateField/index.js.map +1 -1
  240. package/dist/esm/components/DateField/types.d.ts +2 -22
  241. package/dist/esm/components/DateField/types.js.map +1 -1
  242. package/dist/esm/components/DateField/utils.d.ts +21 -34
  243. package/dist/esm/components/DateField/utils.js +185 -131
  244. package/dist/esm/components/DateField/utils.js.map +1 -1
  245. package/dist/esm/components/DatePicker/DatePicker.d.ts +3 -3
  246. package/dist/esm/components/DatePicker/DatePicker.js +3 -5
  247. package/dist/esm/components/DatePicker/DatePicker.js.map +1 -1
  248. package/dist/esm/components/DatePicker/MobileCalendar.d.ts +1 -1
  249. package/dist/esm/components/DatePicker/MobileCalendar.js +3 -5
  250. package/dist/esm/components/DatePicker/MobileCalendar.js.map +1 -1
  251. package/dist/esm/components/DatePicker/StubButton.d.ts +1 -1
  252. package/dist/esm/components/DatePicker/hooks/datePickerStateFactory.d.ts +1 -1
  253. package/dist/esm/components/DatePicker/hooks/datePickerStateFactory.js +15 -6
  254. package/dist/esm/components/DatePicker/hooks/datePickerStateFactory.js.map +1 -1
  255. package/dist/esm/components/DatePicker/hooks/useDatePickerProps.d.ts +2 -2
  256. package/dist/esm/components/DatePicker/hooks/useDatePickerProps.js +30 -18
  257. package/dist/esm/components/DatePicker/hooks/useDatePickerProps.js.map +1 -1
  258. package/dist/esm/components/HiddenInput/HiddenInput.d.ts +2 -5
  259. package/dist/esm/components/HiddenInput/HiddenInput.js +3 -4
  260. package/dist/esm/components/HiddenInput/HiddenInput.js.map +1 -1
  261. package/dist/esm/components/RangeCalendar/RangeCalendar.d.ts +2 -2
  262. package/dist/esm/components/RangeCalendar/RangeCalendar.js +2 -3
  263. package/dist/esm/components/RangeCalendar/RangeCalendar.js.map +1 -1
  264. package/dist/esm/components/RangeDateField/RangeDateField.d.ts +1 -1
  265. package/dist/esm/components/RangeDateField/RangeDateField.js +7 -20
  266. package/dist/esm/components/RangeDateField/RangeDateField.js.map +1 -1
  267. package/dist/esm/components/RangeDateField/index.d.ts +0 -1
  268. package/dist/esm/components/RangeDateField/index.js +0 -1
  269. package/dist/esm/components/RangeDateField/index.js.map +1 -1
  270. package/dist/esm/components/RangeDatePicker/RangeDatePicker.d.ts +2 -1
  271. package/dist/esm/components/RangeDatePicker/RangeDatePicker.js +4 -6
  272. package/dist/esm/components/RangeDatePicker/RangeDatePicker.js.map +1 -1
  273. package/dist/esm/components/RangeDatePicker/hooks/useRangeDatePickerState.d.ts +1 -1
  274. package/dist/esm/components/RangeDatePicker/hooks/useRangeDatePickerState.js +2 -3
  275. package/dist/esm/components/RangeDatePicker/hooks/useRangeDatePickerState.js.map +1 -1
  276. package/dist/esm/components/RangeDateSelection/RangeDateSelection.d.ts +1 -1
  277. package/dist/esm/components/RangeDateSelection/RangeDateSelection.js +4 -5
  278. package/dist/esm/components/RangeDateSelection/RangeDateSelection.js.map +1 -1
  279. package/dist/esm/components/RangeDateSelection/components/NowLine/NowLine.d.ts +2 -1
  280. package/dist/esm/components/RangeDateSelection/components/Ruler/Ruler.d.ts +1 -1
  281. package/dist/esm/components/RangeDateSelection/components/Ruler/Ruler.js +1 -1
  282. package/dist/esm/components/RangeDateSelection/components/Ruler/Ruler.js.map +1 -1
  283. package/dist/esm/components/RangeDateSelection/components/RulerViewport/RulerViewport.js +2 -2
  284. package/dist/esm/components/RangeDateSelection/components/RulerViewport/RulerViewport.js.map +1 -1
  285. package/dist/esm/components/RangeDateSelection/components/SelectionControl/SelectionControl.d.ts +2 -1
  286. package/dist/esm/components/RangeDateSelection/components/SelectionControl/SelectionControl.js +12 -22
  287. package/dist/esm/components/RangeDateSelection/components/SelectionControl/SelectionControl.js.map +1 -1
  288. package/dist/esm/components/RangeDateSelection/components/Ticks/MiddleTicks.d.ts +1 -1
  289. package/dist/esm/components/RangeDateSelection/components/Ticks/SlitTicks.d.ts +1 -1
  290. package/dist/esm/components/RangeDateSelection/components/Ticks/Ticks.d.ts +2 -2
  291. package/dist/esm/components/RangeDateSelection/components/Ticks/Ticks.js.map +1 -1
  292. package/dist/esm/components/RangeDateSelection/components/Ticks/UnavialableTicks.d.ts +3 -4
  293. package/dist/esm/components/RangeDateSelection/components/Ticks/UnavialableTicks.js +1 -2
  294. package/dist/esm/components/RangeDateSelection/components/Ticks/UnavialableTicks.js.map +1 -1
  295. package/dist/esm/components/RangeDateSelection/hooks/useRangeDateSelectionState.js +9 -10
  296. package/dist/esm/components/RangeDateSelection/hooks/useRangeDateSelectionState.js.map +1 -1
  297. package/dist/esm/components/RangeDateSelection/utils/span.d.ts +2 -1
  298. package/dist/esm/components/RangeDateSelection/utils/span.js.map +1 -1
  299. package/dist/esm/components/RelativeDateField/RelativeDateField.d.ts +1 -1
  300. package/dist/esm/components/RelativeDateField/RelativeDateField.js +4 -40
  301. package/dist/esm/components/RelativeDateField/RelativeDateField.js.map +1 -1
  302. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldProps.d.ts +7 -2
  303. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldProps.js +71 -9
  304. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldProps.js.map +1 -1
  305. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldState.d.ts +2 -0
  306. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldState.js +48 -34
  307. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldState.js.map +1 -1
  308. package/dist/esm/components/RelativeDatePicker/RelativeDatePicker.d.ts +1 -1
  309. package/dist/esm/components/RelativeDatePicker/RelativeDatePicker.js +7 -4
  310. package/dist/esm/components/RelativeDatePicker/RelativeDatePicker.js.map +1 -1
  311. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.d.ts +1 -1
  312. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.js +52 -22
  313. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.js.map +1 -1
  314. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerState.js +9 -10
  315. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerState.js.map +1 -1
  316. package/dist/esm/components/RelativeRangeDatePicker/RelativeRangeDatePicker.css +1 -1
  317. package/dist/esm/components/RelativeRangeDatePicker/RelativeRangeDatePicker.d.ts +2 -1
  318. package/dist/esm/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js +13 -5
  319. package/dist/esm/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js.map +1 -1
  320. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.css +1 -1
  321. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.css.map +1 -1
  322. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.d.ts +2 -1
  323. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.js +12 -6
  324. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.js.map +1 -1
  325. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.d.ts +1 -1
  326. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js +6 -8
  327. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js.map +1 -1
  328. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.d.ts +5 -3
  329. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.js +18 -17
  330. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.js.map +1 -1
  331. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.d.ts +2 -1
  332. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.js +32 -7
  333. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.js.map +1 -1
  334. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.d.ts +3 -3
  335. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.js +19 -18
  336. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.js.map +1 -1
  337. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/Presets.d.ts +6 -9
  338. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/Presets.js +8 -10
  339. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/Presets.js.map +1 -1
  340. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/defaultPresets.d.ts +2 -2
  341. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/defaultPresets.js.map +1 -1
  342. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/utils.d.ts +9 -4
  343. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/utils.js +26 -15
  344. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/utils.js.map +1 -1
  345. package/dist/esm/components/RelativeRangeDatePicker/components/Zones/Zones.d.ts +3 -3
  346. package/dist/esm/components/RelativeRangeDatePicker/components/Zones/Zones.js +3 -4
  347. package/dist/esm/components/RelativeRangeDatePicker/components/Zones/Zones.js.map +1 -1
  348. package/dist/esm/components/RelativeRangeDatePicker/hooks/useRelativeRangeDatePickerState.js +8 -7
  349. package/dist/esm/components/RelativeRangeDatePicker/hooks/useRelativeRangeDatePickerState.js.map +1 -1
  350. package/dist/esm/components/RelativeRangeDatePicker/types.js.map +1 -1
  351. package/dist/esm/components/RelativeRangeDatePicker/utils.d.ts +2 -2
  352. package/dist/esm/components/RelativeRangeDatePicker/utils.js +33 -9
  353. package/dist/esm/components/RelativeRangeDatePicker/utils.js.map +1 -1
  354. package/dist/esm/components/types/inputs.js.map +1 -1
  355. package/dist/esm/components/utils/constants.d.ts +2 -0
  356. package/dist/esm/components/utils/constants.js +6 -0
  357. package/dist/esm/components/utils/constants.js.map +1 -1
  358. package/dist/esm/components/utils/dates.d.ts +6 -1
  359. package/dist/esm/components/utils/dates.js +5 -1
  360. package/dist/esm/components/utils/dates.js.map +1 -1
  361. package/dist/esm/components/utils/filterDOMProps.js +1 -1
  362. package/dist/esm/components/utils/filterDOMProps.js.map +1 -1
  363. package/dist/esm/components/utils/mergeProps.js +1 -1
  364. package/dist/esm/components/utils/mergeProps.js.map +1 -1
  365. package/dist/esm/components/utils/validation/datePicker.js +1 -1
  366. package/dist/esm/components/utils/validation/datePicker.js.map +1 -1
  367. package/dist/esm/components/utils/validation/i18n/ru.json +2 -2
  368. package/dist/esm/components/utils/validation/relativeRangeDatePicker.d.ts +7 -11
  369. package/dist/esm/components/utils/validation/relativeRangeDatePicker.js +6 -1
  370. package/dist/esm/components/utils/validation/relativeRangeDatePicker.js.map +1 -1
  371. package/dist/esm/hooks/useEventHandler.js +1 -2
  372. package/dist/esm/hooks/useEventHandler.js.map +1 -1
  373. package/dist/esm/hooks/useMove.d.ts +2 -2
  374. package/dist/esm/hooks/useMove.js +6 -9
  375. package/dist/esm/hooks/useMove.js.map +1 -1
  376. package/dist/esm/hooks/useResizeObserver.js +1 -1
  377. package/dist/esm/hooks/useResizeObserver.js.map +1 -1
  378. package/dist/esm/package.json +1 -1
  379. package/package.json +45 -50
  380. package/dist/cjs/components/RangeDateField/hooks/useRangeDateFieldState.js +0 -218
  381. package/dist/cjs/components/RangeDateField/hooks/useRangeDateFieldState.js.map +0 -1
  382. package/dist/cjs/components/RangeDateField/utils/createPlaceholderRangeValue.d.ts +0 -5
  383. package/dist/cjs/components/RangeDateField/utils/createPlaceholderRangeValue.js +0 -9
  384. package/dist/cjs/components/RangeDateField/utils/createPlaceholderRangeValue.js.map +0 -1
  385. package/dist/cjs/components/RangeDateField/utils/getRangeEditableSections.d.ts +0 -5
  386. package/dist/cjs/components/RangeDateField/utils/getRangeEditableSections.js +0 -42
  387. package/dist/cjs/components/RangeDateField/utils/getRangeEditableSections.js.map +0 -1
  388. package/dist/cjs/components/RangeDateField/utils/index.d.ts +0 -3
  389. package/dist/cjs/components/RangeDateField/utils/index.js +0 -7
  390. package/dist/cjs/components/RangeDateField/utils/index.js.map +0 -1
  391. package/dist/cjs/components/RangeDateField/utils/isValidRange.d.ts +0 -3
  392. package/dist/cjs/components/RangeDateField/utils/isValidRange.js +0 -7
  393. package/dist/cjs/components/RangeDateField/utils/isValidRange.js.map +0 -1
  394. package/dist/esm/components/RangeDateField/hooks/useRangeDateFieldState.js +0 -214
  395. package/dist/esm/components/RangeDateField/hooks/useRangeDateFieldState.js.map +0 -1
  396. package/dist/esm/components/RangeDateField/utils/createPlaceholderRangeValue.d.ts +0 -5
  397. package/dist/esm/components/RangeDateField/utils/createPlaceholderRangeValue.js +0 -6
  398. package/dist/esm/components/RangeDateField/utils/createPlaceholderRangeValue.js.map +0 -1
  399. package/dist/esm/components/RangeDateField/utils/getRangeEditableSections.d.ts +0 -5
  400. package/dist/esm/components/RangeDateField/utils/getRangeEditableSections.js +0 -39
  401. package/dist/esm/components/RangeDateField/utils/getRangeEditableSections.js.map +0 -1
  402. package/dist/esm/components/RangeDateField/utils/index.d.ts +0 -3
  403. package/dist/esm/components/RangeDateField/utils/index.js +0 -4
  404. package/dist/esm/components/RangeDateField/utils/index.js.map +0 -1
  405. package/dist/esm/components/RangeDateField/utils/isValidRange.d.ts +0 -3
  406. package/dist/esm/components/RangeDateField/utils/isValidRange.js +0 -4
  407. package/dist/esm/components/RangeDateField/utils/isValidRange.js.map +0 -1
@@ -1,189 +1,157 @@
1
1
  import React from 'react';
2
- import { useControlledState } from '@gravity-ui/uikit';
2
+ import { useControlledState, useLang } from '@gravity-ui/uikit';
3
3
  import { createPlaceholderValue, isInvalid } from "../../utils/dates.js";
4
4
  import { useDefaultTimeZone } from "../../utils/useDefaultTimeZone.js";
5
- import { addSegment, adjustDateToFormat, getEditableSections, getFormatInfo, isAllSegmentsValid, markValidSection, parseDateFromString, setSegment, useFormatSections, } from "../utils.js";
5
+ import { IncompleteDate } from "../IncompleteDate.js";
6
+ import { addSegment, adjustDateToFormat, getEditableSections, getFormatInfo, isEditableSectionType, parseDateFromString, setSegment, useFormatSections, } from "../utils.js";
6
7
  import { useBaseDateFieldState } from "./useBaseDateFieldState.js";
7
8
  export function useDateFieldState(props) {
8
- var _a, _b;
9
- const [value, setDate] = useControlledState(props.value, (_a = props.defaultValue) !== null && _a !== void 0 ? _a : null, props.onUpdate);
9
+ const [value, setDate] = useControlledState(props.value, props.defaultValue ?? null, props.onUpdate);
10
10
  const inputTimeZone = useDefaultTimeZone(props.value || props.defaultValue || props.placeholderValue);
11
11
  const timeZone = props.timeZone || inputTimeZone;
12
12
  const handleUpdateDate = (v) => {
13
13
  setDate(v ? v.timeZone(inputTimeZone) : v);
14
14
  };
15
- const [placeholderDate, setPlaceholderDate] = React.useState(() => {
16
- return createPlaceholderValue({
17
- placeholderValue: props.placeholderValue,
18
- timeZone,
19
- });
20
- });
15
+ const [lastPlaceholder, setLastPlaceholder] = React.useState(props.placeholderValue);
16
+ if ((props.placeholderValue &&
17
+ (!lastPlaceholder || !props.placeholderValue.isSame(lastPlaceholder))) ||
18
+ (!props.placeholderValue && lastPlaceholder)) {
19
+ setLastPlaceholder(props.placeholderValue);
20
+ }
21
+ const placeholder = React.useMemo(() => createPlaceholderValue({
22
+ placeholderValue: lastPlaceholder,
23
+ timeZone,
24
+ }), [lastPlaceholder, timeZone]);
21
25
  const format = props.format || 'L';
22
26
  const sections = useFormatSections(format);
23
27
  const formatInfo = React.useMemo(() => getFormatInfo(sections), [sections]);
24
28
  const allSegments = formatInfo.availableUnits;
25
- const validSegmentsState = React.useState(() => value ? Object.assign({}, allSegments) : {});
26
- let validSegments = validSegmentsState[0];
27
- const setValidSegments = validSegmentsState[1];
28
- if (value && !isAllSegmentsValid(allSegments, validSegments)) {
29
- setValidSegments(Object.assign({}, allSegments));
30
- }
31
- if (!value &&
32
- Object.keys(allSegments).length > 0 &&
33
- isAllSegmentsValid(allSegments, validSegments) &&
34
- Object.keys(validSegments).length === Object.keys(allSegments).length) {
35
- validSegments = {};
36
- setValidSegments(validSegments);
37
- setPlaceholderDate(createPlaceholderValue({
38
- placeholderValue: props.placeholderValue,
39
- timeZone,
40
- }));
29
+ const [displayValue, setDisplayValue] = React.useState(() => {
30
+ return new IncompleteDate(value && value.isValid() ? value.timeZone(timeZone) : null);
31
+ });
32
+ const [lastValue, setLastValue] = React.useState(value);
33
+ const [lastTimezone, setLastTimezone] = React.useState(timeZone);
34
+ if ((value && (!lastValue || !value.isSame(lastValue))) ||
35
+ (value && lastTimezone !== timeZone) ||
36
+ (value === null && lastValue !== null)) {
37
+ setLastValue(value);
38
+ setLastTimezone(timeZone);
39
+ setDisplayValue(new IncompleteDate(value?.timeZone(timeZone)));
41
40
  }
42
- const displayValue = value && value.isValid() && isAllSegmentsValid(allSegments, validSegments)
43
- ? value.timeZone(timeZone)
44
- : placeholderDate.timeZone(timeZone);
45
- const sectionsState = useSectionsState(sections, displayValue, validSegments);
46
- const [selectedSections, setSelectedSections] = React.useState(-1);
47
- const selectedSectionIndexes = React.useMemo(() => {
48
- if (selectedSections === -1) {
49
- return null;
50
- }
51
- if (selectedSections === 'all') {
52
- return {
53
- startIndex: 0,
54
- endIndex: sectionsState.editableSections.length - 1,
55
- };
56
- }
57
- if (typeof selectedSections === 'number') {
58
- return { startIndex: selectedSections, endIndex: selectedSections };
59
- }
60
- if (typeof selectedSections === 'string') {
61
- const selectedSectionIndex = sectionsState.editableSections.findIndex((section) => section.type === selectedSections);
62
- return { startIndex: selectedSectionIndex, endIndex: selectedSectionIndex };
63
- }
64
- return selectedSections;
65
- }, [selectedSections, sectionsState.editableSections]);
41
+ const { lang } = useLang();
42
+ const dateValue = React.useMemo(() => {
43
+ return displayValue
44
+ .toDateTime(value?.timeZone(timeZone) ?? placeholder, {
45
+ setDate: formatInfo.hasDate,
46
+ setTime: formatInfo.hasTime,
47
+ })
48
+ .locale(lang);
49
+ }, [displayValue, value, placeholder, formatInfo, timeZone, lang]);
50
+ const sectionsState = useSectionsState(sections, displayValue, dateValue);
66
51
  function setValue(newValue) {
67
52
  if (props.disabled || props.readOnly) {
68
53
  return;
69
54
  }
70
- if (isAllSegmentsValid(allSegments, validSegments)) {
71
- if (!value || !newValue.isSame(value)) {
72
- handleUpdateDate(adjustDateToFormat(newValue, formatInfo));
73
- }
55
+ if (newValue === null ||
56
+ (newValue instanceof IncompleteDate && newValue.isCleared(allSegments))) {
57
+ setDate(null);
58
+ setDisplayValue(new IncompleteDate());
74
59
  }
75
- else {
76
- if (value) {
77
- handleUpdateDate(null);
60
+ else if (newValue instanceof IncompleteDate) {
61
+ if (newValue.isComplete(allSegments)) {
62
+ const newDate = newValue.toDateTime(dateValue, {
63
+ setDate: formatInfo.hasDate,
64
+ setTime: formatInfo.hasTime,
65
+ });
66
+ if (newValue.validate(newDate, allSegments)) {
67
+ if (!value || !newDate.isSame(value)) {
68
+ handleUpdateDate(adjustDateToFormat(newDate, formatInfo));
69
+ }
70
+ }
78
71
  }
79
- setPlaceholderDate(newValue);
72
+ setDisplayValue(newValue);
73
+ }
74
+ else if (!value || !newValue.isSame(value)) {
75
+ handleUpdateDate(newValue);
80
76
  }
81
- }
82
- function markValid(part) {
83
- validSegments = markValidSection(allSegments, validSegments, part);
84
- setValidSegments(Object.assign({}, validSegments));
85
77
  }
86
78
  function setSection(sectionIndex, amount) {
87
79
  const section = sectionsState.editableSections[sectionIndex];
88
80
  if (section) {
89
- markValid(section.type);
90
- setValue(setSegment(section, displayValue, amount));
81
+ setValue(setSegment(section, displayValue, amount, dateValue));
91
82
  }
92
83
  }
93
84
  function adjustSection(sectionIndex, amount) {
94
85
  const section = sectionsState.editableSections[sectionIndex];
95
86
  if (section) {
96
- if (validSegments[section.type]) {
97
- setValue(addSegment(section, displayValue, amount));
98
- }
99
- else {
100
- markValid(section.type);
101
- if (Object.keys(validSegments).length >= Object.keys(allSegments).length) {
102
- setValue(displayValue);
103
- }
104
- }
87
+ setValue(addSegment(section, displayValue, amount, dateValue));
105
88
  }
106
89
  }
107
- function flushValidSection(sectionIndex) {
90
+ function clearSection(sectionIndex) {
108
91
  const section = sectionsState.editableSections[sectionIndex];
109
- if (section) {
110
- delete validSegments[section.type];
92
+ if (section && isEditableSectionType(section.type)) {
93
+ setValue(displayValue.clear(section.type));
111
94
  }
112
- setValidSegments(Object.assign({}, validSegments));
113
- }
114
- function flushAllValidSections() {
115
- validSegments = {};
116
- setValidSegments({});
117
- }
118
- function getSectionValue(_sectionIndex) {
119
- return displayValue;
120
- }
121
- function setSectionValue(_sectionIndex, currentValue) {
122
- setValue(currentValue);
123
- }
124
- function createPlaceholder() {
125
- return createPlaceholderValue({
126
- placeholderValue: props.placeholderValue,
127
- timeZone,
128
- }).timeZone(timeZone);
129
95
  }
130
96
  function setValueFromString(str) {
131
- var _a;
132
- const parseDate = (_a = props.parseDateFromString) !== null && _a !== void 0 ? _a : parseDateFromString;
97
+ const parseDate = props.parseDateFromString ?? parseDateFromString;
133
98
  const date = parseDate(str, format, timeZone);
134
99
  if (date.isValid()) {
135
- handleUpdateDate(date);
100
+ setValue(date);
136
101
  return true;
137
102
  }
138
103
  return false;
139
104
  }
105
+ function confirmPlaceholder() {
106
+ if (props.disabled || props.readOnly) {
107
+ return;
108
+ }
109
+ // If the display value is complete but invalid, we need to constrain it and emit onChange on blur.
110
+ if (displayValue.isComplete(allSegments)) {
111
+ const newValue = displayValue.toDateTime(dateValue, {
112
+ setDate: formatInfo.hasDate,
113
+ setTime: formatInfo.hasTime,
114
+ });
115
+ setValue(adjustDateToFormat(newValue, formatInfo, 'startOf'));
116
+ }
117
+ }
140
118
  const validationState = props.validationState ||
141
119
  (isInvalid(value, props.minValue, props.maxValue) ? 'invalid' : undefined) ||
142
- (value && ((_b = props.isDateUnavailable) === null || _b === void 0 ? void 0 : _b.call(props, value)) ? 'invalid' : undefined);
120
+ (value && props.isDateUnavailable?.(value) ? 'invalid' : undefined);
143
121
  return useBaseDateFieldState({
144
122
  value,
145
- displayValue,
146
- placeholderValue: props.placeholderValue,
147
- timeZone,
123
+ displayValue: dateValue,
148
124
  validationState,
149
125
  editableSections: sectionsState.editableSections,
150
126
  formatInfo,
151
127
  readOnly: props.readOnly,
152
128
  disabled: props.disabled,
153
- selectedSectionIndexes,
154
- selectedSections,
155
- isEmpty: Object.keys(validSegments).length === 0,
156
- flushAllValidSections,
157
- flushValidSection,
158
- setSelectedSections,
129
+ isEmpty: displayValue.isCleared(allSegments),
159
130
  setValue,
160
- setDate: handleUpdateDate,
161
131
  adjustSection,
162
132
  setSection,
163
- getSectionValue,
164
- setSectionValue,
165
- createPlaceholder,
133
+ clearSection,
166
134
  setValueFromString,
135
+ confirmPlaceholder,
167
136
  });
168
137
  }
169
- function useSectionsState(sections, value, validSegments) {
138
+ function useSectionsState(sections, value, placeholder) {
170
139
  const [state, setState] = React.useState(() => {
140
+ const editableSections = getEditableSections(sections, value, placeholder);
171
141
  return {
172
142
  value,
173
143
  sections,
174
- validSegments,
175
- editableSections: getEditableSections(sections, value, validSegments),
144
+ placeholder,
145
+ editableSections,
176
146
  };
177
147
  });
178
- if (sections !== state.sections ||
179
- validSegments !== state.validSegments ||
180
- !value.isSame(state.value) ||
181
- value.timeZone() !== state.value.timeZone()) {
148
+ if (sections !== state.sections || placeholder !== state.placeholder || value !== state.value) {
149
+ const editableSections = getEditableSections(sections, value, placeholder);
182
150
  setState({
183
151
  value,
184
152
  sections,
185
- validSegments,
186
- editableSections: getEditableSections(sections, value, validSegments),
153
+ placeholder,
154
+ editableSections,
187
155
  });
188
156
  }
189
157
  return state;
@@ -1 +1 @@
1
- {"version":3,"file":"useDateFieldState.js","sourceRoot":"../../../../../src","sources":["components/DateField/hooks/useDateFieldState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,EAAC,kBAAkB,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AAQ,EAAE,EAC7C,CAAC;QACC,QAAQ,CAAC;YACL,KAAK;YACL,QAAQ;YACR,aAAa;YACb,gBAAgB,EAAE,mBAAmB,CAAC,QAAQ,EAAE,KAAK,EAAE,aAAa,CAAC;SACxE,CAAC,CAAC;IACP,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import React from 'react';\n\nimport type {DateTime} from '@gravity-ui/date-utils';\nimport {useControlledState} from '@gravity-ui/uikit';\n\nimport type {DateFieldBase} from '../../types/datePicker';\nimport {createPlaceholderValue, isInvalid} from '../../utils/dates';\nimport {useDefaultTimeZone} from '../../utils/useDefaultTimeZone';\nimport type {\n AvailableSections,\n DateFieldSectionType,\n DateFieldSectionWithoutPosition,\n} from '../types';\nimport {\n addSegment,\n adjustDateToFormat,\n getEditableSections,\n getFormatInfo,\n isAllSegmentsValid,\n markValidSection,\n parseDateFromString,\n setSegment,\n useFormatSections,\n} from '../utils';\n\nimport {useBaseDateFieldState} from './useBaseDateFieldState';\nimport type {DateFieldState} from './useBaseDateFieldState';\n\nexport interface DateFieldStateOptions extends DateFieldBase {}\n\nexport function useDateFieldState(props: DateFieldStateOptions): DateFieldState {\n const [value, setDate] = useControlledState(\n props.value,\n props.defaultValue ?? null,\n props.onUpdate,\n );\n\n const inputTimeZone = useDefaultTimeZone(\n props.value || props.defaultValue || props.placeholderValue,\n );\n const timeZone = props.timeZone || inputTimeZone;\n\n const handleUpdateDate = (v: DateTime | null) => {\n setDate(v ? v.timeZone(inputTimeZone) : v);\n };\n\n const [placeholderDate, setPlaceholderDate] = React.useState(() => {\n return createPlaceholderValue({\n placeholderValue: props.placeholderValue,\n timeZone,\n });\n });\n\n const format = props.format || 'L';\n const sections = useFormatSections(format);\n const formatInfo = React.useMemo(() => getFormatInfo(sections), [sections]);\n const allSegments = formatInfo.availableUnits;\n\n const validSegmentsState = React.useState<AvailableSections>(() =>\n value ? {...allSegments} : {},\n );\n\n let validSegments = validSegmentsState[0];\n const setValidSegments = validSegmentsState[1];\n\n if (value && !isAllSegmentsValid(allSegments, validSegments)) {\n setValidSegments({...allSegments});\n }\n\n if (\n !value &&\n Object.keys(allSegments).length > 0 &&\n isAllSegmentsValid(allSegments, validSegments) &&\n Object.keys(validSegments).length === Object.keys(allSegments).length\n ) {\n validSegments = {};\n setValidSegments(validSegments);\n setPlaceholderDate(\n createPlaceholderValue({\n placeholderValue: props.placeholderValue,\n timeZone,\n }),\n );\n }\n\n const displayValue =\n value && value.isValid() && isAllSegmentsValid(allSegments, validSegments)\n ? value.timeZone(timeZone)\n : placeholderDate.timeZone(timeZone);\n const sectionsState = useSectionsState(sections, displayValue, validSegments);\n\n const [selectedSections, setSelectedSections] = React.useState<number | 'all'>(-1);\n\n const selectedSectionIndexes = React.useMemo<{\n startIndex: number;\n endIndex: number;\n } | null>(() => {\n if (selectedSections === -1) {\n return null;\n }\n\n if (selectedSections === 'all') {\n return {\n startIndex: 0,\n endIndex: sectionsState.editableSections.length - 1,\n };\n }\n\n if (typeof selectedSections === 'number') {\n return {startIndex: selectedSections, endIndex: selectedSections};\n }\n\n if (typeof selectedSections === 'string') {\n const selectedSectionIndex = sectionsState.editableSections.findIndex(\n (section) => section.type === selectedSections,\n );\n\n return {startIndex: selectedSectionIndex, endIndex: selectedSectionIndex};\n }\n\n return selectedSections;\n }, [selectedSections, sectionsState.editableSections]);\n\n function setValue(newValue: DateTime) {\n if (props.disabled || props.readOnly) {\n return;\n }\n\n if (isAllSegmentsValid(allSegments, validSegments)) {\n if (!value || !newValue.isSame(value)) {\n handleUpdateDate(adjustDateToFormat(newValue, formatInfo));\n }\n } else {\n if (value) {\n handleUpdateDate(null);\n }\n setPlaceholderDate(newValue);\n }\n }\n\n function markValid(part: DateFieldSectionType) {\n validSegments = markValidSection(allSegments, validSegments, part);\n setValidSegments({...validSegments});\n }\n\n function setSection(sectionIndex: number, amount: number) {\n const section = sectionsState.editableSections[sectionIndex];\n if (section) {\n markValid(section.type);\n setValue(setSegment(section, displayValue, amount));\n }\n }\n\n function adjustSection(sectionIndex: number, amount: number) {\n const section = sectionsState.editableSections[sectionIndex];\n if (section) {\n if (validSegments[section.type]) {\n setValue(addSegment(section, displayValue, amount));\n } else {\n markValid(section.type);\n if (Object.keys(validSegments).length >= Object.keys(allSegments).length) {\n setValue(displayValue);\n }\n }\n }\n }\n\n function flushValidSection(sectionIndex: number) {\n const section = sectionsState.editableSections[sectionIndex];\n if (section) {\n delete validSegments[section.type];\n }\n setValidSegments({...validSegments});\n }\n\n function flushAllValidSections() {\n validSegments = {};\n setValidSegments({});\n }\n\n function getSectionValue(_sectionIndex: number) {\n return displayValue;\n }\n\n function setSectionValue(_sectionIndex: number, currentValue: DateTime) {\n setValue(currentValue);\n }\n\n function createPlaceholder() {\n return createPlaceholderValue({\n placeholderValue: props.placeholderValue,\n timeZone,\n }).timeZone(timeZone);\n }\n\n function setValueFromString(str: string) {\n const parseDate = props.parseDateFromString ?? parseDateFromString;\n const date = parseDate(str, format, timeZone);\n if (date.isValid()) {\n handleUpdateDate(date);\n return true;\n }\n return false;\n }\n\n const validationState =\n props.validationState ||\n (isInvalid(value, props.minValue, props.maxValue) ? 'invalid' : undefined) ||\n (value && props.isDateUnavailable?.(value) ? 'invalid' : undefined);\n\n return useBaseDateFieldState({\n value,\n displayValue,\n placeholderValue: props.placeholderValue,\n timeZone,\n validationState,\n editableSections: sectionsState.editableSections,\n formatInfo,\n readOnly: props.readOnly,\n disabled: props.disabled,\n selectedSectionIndexes,\n selectedSections,\n isEmpty: Object.keys(validSegments).length === 0,\n flushAllValidSections,\n flushValidSection,\n setSelectedSections,\n setValue,\n setDate: handleUpdateDate,\n adjustSection,\n setSection,\n getSectionValue,\n setSectionValue,\n createPlaceholder,\n setValueFromString,\n });\n}\n\nfunction useSectionsState(\n sections: DateFieldSectionWithoutPosition[],\n value: DateTime,\n validSegments: AvailableSections,\n) {\n const [state, setState] = React.useState(() => {\n return {\n value,\n sections,\n validSegments,\n editableSections: getEditableSections(sections, value, validSegments),\n };\n });\n\n if (\n sections !== state.sections ||\n validSegments !== state.validSegments ||\n !value.isSame(state.value) ||\n value.timeZone() !== state.value.timeZone()\n ) {\n setState({\n value,\n sections,\n validSegments,\n editableSections: getEditableSections(sections, value, validSegments),\n });\n }\n\n return state;\n}\n"]}
1
+ {"version":3,"file":"useDateFieldState.js","sourceRoot":"../../../../../src","sources":["components/DateField/hooks/useDateFieldState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,EAAC,kBAAkB,EAAE,OAAO,EAAC,MAAM,mBAAmB,CAAC;AAG9D,OAAO,EAAC,sBAAsB,EAAE,SAAS,EAAC,6BAA0B;AACpE,OAAO,EAAC,kBAAkB,EAAC,0CAAuC;AAClE,OAAO,EAAC,cAAc,EAAC,6BAA0B;AAEjD,OAAO,EACH,UAAU,EACV,kBAAkB,EAClB,mBAAmB,EACnB,aAAa,EACb,qBAAqB,EACrB,mBAAmB,EACnB,UAAU,EACV,iBAAiB,GACpB,oBAAiB;AAElB,OAAO,EAAC,qBAAqB,EAAC,mCAAgC;AAuB9D,MAAM,UAAU,iBAAiB,CAAC,KAA4B;IAC1D,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,GAAG,kBAAkB,CACvC,KAAK,CAAC,KAAK,EACX,KAAK,CAAC,YAAY,IAAI,IAAI,EAC1B,KAAK,CAAC,QAAQ,CACjB,CAAC;IAEF,MAAM,aAAa,GAAG,kBAAkB,CACpC,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,YAAY,IAAI,KAAK,CAAC,gBAAgB,CAC9D,CAAC;IACF,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,IAAI,aAAa,CAAC;IAEjD,MAAM,gBAAgB,GAAG,CAAC,CAAkB,EAAE,EAAE;QAC5C,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC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W;YACX,gBAAgB;SACnB,CAAC,CAAC;IACP,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import React from 'react';\n\nimport type {DateTime} from '@gravity-ui/date-utils';\nimport {useControlledState, useLang} from '@gravity-ui/uikit';\n\nimport type {InputBase, Validation, ValueBase} from '../../types';\nimport {createPlaceholderValue, isInvalid} from '../../utils/dates';\nimport {useDefaultTimeZone} from '../../utils/useDefaultTimeZone';\nimport {IncompleteDate} from '../IncompleteDate';\nimport type {FormatSection} from '../types';\nimport {\n addSegment,\n adjustDateToFormat,\n getEditableSections,\n getFormatInfo,\n isEditableSectionType,\n parseDateFromString,\n setSegment,\n useFormatSections,\n} from '../utils';\n\nimport {useBaseDateFieldState} from './useBaseDateFieldState';\nimport type {DateFieldState} from './useBaseDateFieldState';\n\nexport interface DateFieldStateOptions extends ValueBase<DateTime | null>, InputBase, Validation {\n /** The minimum allowed date that a user may select. */\n minValue?: DateTime;\n /** The maximum allowed date that a user may select. */\n maxValue?: DateTime;\n /** Callback that is called for each date of the calendar. If it returns true, then the date is unavailable. */\n isDateUnavailable?: (date: DateTime) => boolean;\n /** Format of the date when rendered in the input. [Available formats](https://day.js.org/docs/en/display/format) */\n format?: string;\n /** A placeholder date that controls the default values of each segment when the user first interacts with them. Defaults to today's date at midnight. */\n placeholderValue?: DateTime;\n /**\n * Which timezone use to show values. Example: 'default', 'system', 'Europe/Amsterdam'.\n * @default The timezone of the `value` or `defaultValue` or `placeholderValue`, 'default' otherwise.\n */\n timeZone?: string;\n /** Custom parser function for parsing pasted date strings. If not provided, the default parser will be used. */\n parseDateFromString?: (dateStr: string, format: string, timeZone?: string) => DateTime;\n}\n\nexport function useDateFieldState(props: DateFieldStateOptions): DateFieldState {\n const [value, setDate] = useControlledState(\n props.value,\n props.defaultValue ?? null,\n props.onUpdate,\n );\n\n const inputTimeZone = useDefaultTimeZone(\n props.value || props.defaultValue || props.placeholderValue,\n );\n const timeZone = props.timeZone || inputTimeZone;\n\n const handleUpdateDate = (v: DateTime | null) => {\n setDate(v ? v.timeZone(inputTimeZone) : v);\n };\n\n const [lastPlaceholder, setLastPlaceholder] = React.useState(props.placeholderValue);\n if (\n (props.placeholderValue &&\n (!lastPlaceholder || !props.placeholderValue.isSame(lastPlaceholder))) ||\n (!props.placeholderValue && lastPlaceholder)\n ) {\n setLastPlaceholder(props.placeholderValue);\n }\n const placeholder = React.useMemo(\n () =>\n createPlaceholderValue({\n placeholderValue: lastPlaceholder,\n timeZone,\n }),\n [lastPlaceholder, timeZone],\n );\n\n const format = props.format || 'L';\n const sections = useFormatSections(format);\n const formatInfo = React.useMemo(() => getFormatInfo(sections), [sections]);\n const allSegments = formatInfo.availableUnits;\n\n const [displayValue, setDisplayValue] = React.useState(() => {\n return new IncompleteDate(value && value.isValid() ? value.timeZone(timeZone) : null);\n });\n\n const [lastValue, setLastValue] = React.useState(value);\n const [lastTimezone, setLastTimezone] = React.useState(timeZone);\n if (\n (value && (!lastValue || !value.isSame(lastValue))) ||\n (value && lastTimezone !== timeZone) ||\n (value === null && lastValue !== null)\n ) {\n setLastValue(value);\n setLastTimezone(timeZone);\n setDisplayValue(new IncompleteDate(value?.timeZone(timeZone)));\n }\n\n const {lang} = useLang();\n const dateValue = React.useMemo(() => {\n return displayValue\n .toDateTime(value?.timeZone(timeZone) ?? placeholder, {\n setDate: formatInfo.hasDate,\n setTime: formatInfo.hasTime,\n })\n .locale(lang);\n }, [displayValue, value, placeholder, formatInfo, timeZone, lang]);\n\n const sectionsState = useSectionsState(sections, displayValue, dateValue);\n\n function setValue(newValue: DateTime | IncompleteDate | null) {\n if (props.disabled || props.readOnly) {\n return;\n }\n\n if (\n newValue === null ||\n (newValue instanceof IncompleteDate && newValue.isCleared(allSegments))\n ) {\n setDate(null);\n setDisplayValue(new IncompleteDate());\n } else if (newValue instanceof IncompleteDate) {\n if (newValue.isComplete(allSegments)) {\n const newDate = newValue.toDateTime(dateValue, {\n setDate: formatInfo.hasDate,\n setTime: formatInfo.hasTime,\n });\n if (newValue.validate(newDate, allSegments)) {\n if (!value || !newDate.isSame(value)) {\n handleUpdateDate(adjustDateToFormat(newDate, formatInfo));\n }\n }\n }\n setDisplayValue(newValue);\n } else if (!value || !newValue.isSame(value)) {\n handleUpdateDate(newValue);\n }\n }\n\n function setSection(sectionIndex: number, amount: number) {\n const section = sectionsState.editableSections[sectionIndex];\n if (section) {\n setValue(setSegment(section, displayValue, amount, dateValue));\n }\n }\n\n function adjustSection(sectionIndex: number, amount: number) {\n const section = sectionsState.editableSections[sectionIndex];\n if (section) {\n setValue(addSegment(section, displayValue, amount, dateValue));\n }\n }\n\n function clearSection(sectionIndex: number) {\n const section = sectionsState.editableSections[sectionIndex];\n if (section && isEditableSectionType(section.type)) {\n setValue(displayValue.clear(section.type));\n }\n }\n\n function setValueFromString(str: string) {\n const parseDate = props.parseDateFromString ?? parseDateFromString;\n const date = parseDate(str, format, timeZone);\n if (date.isValid()) {\n setValue(date);\n return true;\n }\n return false;\n }\n\n function confirmPlaceholder() {\n if (props.disabled || props.readOnly) {\n return;\n }\n\n // If the display value is complete but invalid, we need to constrain it and emit onChange on blur.\n if (displayValue.isComplete(allSegments)) {\n const newValue = displayValue.toDateTime(dateValue, {\n setDate: formatInfo.hasDate,\n setTime: formatInfo.hasTime,\n });\n setValue(adjustDateToFormat(newValue, formatInfo, 'startOf'));\n }\n }\n\n const validationState =\n props.validationState ||\n (isInvalid(value, props.minValue, props.maxValue) ? 'invalid' : undefined) ||\n (value && props.isDateUnavailable?.(value) ? 'invalid' : undefined);\n\n return useBaseDateFieldState({\n value,\n displayValue: dateValue,\n validationState,\n editableSections: sectionsState.editableSections,\n formatInfo,\n readOnly: props.readOnly,\n disabled: props.disabled,\n isEmpty: displayValue.isCleared(allSegments),\n setValue,\n adjustSection,\n setSection,\n clearSection,\n setValueFromString,\n confirmPlaceholder,\n });\n}\n\nfunction useSectionsState(sections: FormatSection[], value: IncompleteDate, placeholder: DateTime) {\n const [state, setState] = React.useState(() => {\n const editableSections = getEditableSections(sections, value, placeholder);\n return {\n value,\n sections,\n placeholder,\n editableSections,\n };\n });\n\n if (sections !== state.sections || placeholder !== state.placeholder || value !== state.value) {\n const editableSections = getEditableSections(sections, value, placeholder);\n setState({\n value,\n sections,\n placeholder,\n editableSections,\n });\n }\n\n return state;\n}\n"]}
@@ -0,0 +1,29 @@
1
+ import type { DateFieldSection } from "../types.js";
2
+ interface FocusManagerOptions {
3
+ sections: DateFieldSection[];
4
+ }
5
+ interface FocusManager {
6
+ /** Active section index */
7
+ activeSectionIndex: number;
8
+ /** Selected sections */
9
+ selectedSectionIndexes: {
10
+ startIndex: number;
11
+ endIndex: number;
12
+ } | null;
13
+ /** Return true if it is the first section */
14
+ isFistSection: (position: number) => boolean;
15
+ /** Return true if it is the last section */
16
+ isLastSection: (position: number) => boolean;
17
+ /** Focus section in position or all sections. */
18
+ focusSection: (position: number | 'all') => void;
19
+ /** Focuses the next section if present */
20
+ focusNextSection: () => void;
21
+ /** Focuses the previous section if present */
22
+ focusPreviousSection: () => void;
23
+ /** Focuses the first section */
24
+ focusFirstSection: () => void;
25
+ /** Focuses the last section */
26
+ focusLastSection: () => void;
27
+ }
28
+ export declare function useFocusManager({ sections }: FocusManagerOptions): FocusManager;
29
+ export {};
@@ -0,0 +1,112 @@
1
+ import React from 'react';
2
+ import { isEditableSectionType } from "../utils.js";
3
+ export function useFocusManager({ sections }) {
4
+ const [selectedSections, setSelectedSections] = React.useState(-1);
5
+ const connections = React.useMemo(() => connectEditableSections(sections), [sections]);
6
+ const selectedSectionIndexes = React.useMemo(() => {
7
+ if (selectedSections === -1) {
8
+ return null;
9
+ }
10
+ if (selectedSections === 'all') {
11
+ return {
12
+ startIndex: 0,
13
+ endIndex: connections.length - 1,
14
+ };
15
+ }
16
+ if (typeof selectedSections === 'number') {
17
+ return { startIndex: selectedSections, endIndex: selectedSections };
18
+ }
19
+ return selectedSections;
20
+ }, [selectedSections, connections]);
21
+ const activeSectionIndex = getCurrentEditableSectionIndex(connections, selectedSections);
22
+ return {
23
+ activeSectionIndex,
24
+ selectedSectionIndexes,
25
+ isFistSection(position) {
26
+ const p = connections[position];
27
+ if (!p) {
28
+ return false;
29
+ }
30
+ return position === p.previousEditableSection;
31
+ },
32
+ isLastSection(position) {
33
+ const p = connections[position];
34
+ if (!p) {
35
+ return false;
36
+ }
37
+ return position === p.nextEditableSection;
38
+ },
39
+ focusSection(index) {
40
+ if (index === 'all' || index === -1) {
41
+ setSelectedSections(index);
42
+ }
43
+ else {
44
+ setSelectedSections(getCurrentEditableSectionIndex(connections, index));
45
+ }
46
+ },
47
+ focusNextSection() {
48
+ const currentIndex = selectedSections === 'all' ? 0 : selectedSections;
49
+ const newIndex = connections[currentIndex]?.nextEditableSection ?? -1;
50
+ if (newIndex !== -1) {
51
+ setSelectedSections(newIndex);
52
+ }
53
+ },
54
+ focusPreviousSection() {
55
+ const currentIndex = selectedSections === 'all' ? 0 : selectedSections;
56
+ const newIndex = connections[currentIndex]?.previousEditableSection ?? -1;
57
+ if (newIndex !== -1) {
58
+ setSelectedSections(newIndex);
59
+ }
60
+ },
61
+ focusFirstSection() {
62
+ const newIndex = connections[0]?.previousEditableSection ?? -1;
63
+ if (newIndex !== -1) {
64
+ setSelectedSections(newIndex);
65
+ }
66
+ },
67
+ focusLastSection() {
68
+ const newIndex = connections[sections.length - 1]?.nextEditableSection ?? -1;
69
+ if (newIndex !== -1) {
70
+ setSelectedSections(newIndex);
71
+ }
72
+ },
73
+ };
74
+ }
75
+ function connectEditableSections(sections) {
76
+ let previousEditableSection = -1;
77
+ const connections = new Array(sections.length);
78
+ for (let i = 0; i < sections.length; i++) {
79
+ const section = sections[i];
80
+ if (!section) {
81
+ throw new Error('section must be defined');
82
+ }
83
+ const connection = {
84
+ type: section.type,
85
+ previousEditableSection,
86
+ nextEditableSection: previousEditableSection,
87
+ };
88
+ connections[i] = connection;
89
+ if (isEditableSectionType(connection.type)) {
90
+ for (let j = Math.max(0, previousEditableSection); j <= i; j++) {
91
+ const prevSection = connections[j];
92
+ if (prevSection) {
93
+ prevSection.nextEditableSection = i;
94
+ if (prevSection.previousEditableSection === -1) {
95
+ prevSection.previousEditableSection = i;
96
+ }
97
+ }
98
+ }
99
+ previousEditableSection = i;
100
+ }
101
+ }
102
+ return connections;
103
+ }
104
+ function getCurrentEditableSectionIndex(sections, selectedSections) {
105
+ const currentIndex = selectedSections === 'all' || selectedSections === -1 ? 0 : selectedSections;
106
+ const section = sections[currentIndex];
107
+ if (section && !isEditableSectionType(section.type)) {
108
+ return section.nextEditableSection;
109
+ }
110
+ return isEditableSectionType(section?.type) ? currentIndex : -1;
111
+ }
112
+ //# sourceMappingURL=useFocusManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFocusManager.js","sourceRoot":"../../../../../src","sources":["components/DateField/hooks/useFocusManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,EAAC,qBAAqB,EAAC,oBAAiB;AA2B/C,MAAM,UAAU,eAAe,CAAC,EAAC,QAAQ,EAAsB;IAC3D,MAAM,CAAC,gBAAgB,EAAE,mBAAmB,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAiB,CAAC,CAAC,CAAC,CAAC;IAEnF,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,uBAAuB,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEvF,MAAM,sBAAsB,GAAG,KAAK,CAAC,OAAO,CAGlC,GAAG,EAAE;QACX,IAAI,gBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,IAAI,gBAAgB,KAAK,KAAK,EAAE,CAAC;YAC7B,OAAO;gBACH,UAAU,EAAE,CAAC;gBACb,QAAQ,EAAE,WAAW,CAAC,MAAM,GAAG,CAAC;aACnC,CAAC;QACN,CAAC;QAED,IAAI,OAAO,gB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CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACpE,CAAC","sourcesContent":["import React from 'react';\n\nimport type {DateFieldSection, DateFieldSectionType} from '../types';\nimport {isEditableSectionType} from '../utils';\n\ninterface FocusManagerOptions {\n sections: DateFieldSection[];\n}\n\ninterface FocusManager {\n /** Active section index */\n activeSectionIndex: number;\n /** Selected sections */\n selectedSectionIndexes: {startIndex: number; endIndex: number} | null;\n /** Return true if it is the first section */\n isFistSection: (position: number) => boolean;\n /** Return true if it is the last section */\n isLastSection: (position: number) => boolean;\n /** Focus section in position or all sections. */\n focusSection: (position: number | 'all') => void;\n /** Focuses the next section if present */\n focusNextSection: () => void;\n /** Focuses the previous section if present */\n focusPreviousSection: () => void;\n /** Focuses the first section */\n focusFirstSection: () => void;\n /** Focuses the last section */\n focusLastSection: () => void;\n}\n\nexport function useFocusManager({sections}: FocusManagerOptions): FocusManager {\n const [selectedSections, setSelectedSections] = React.useState<number | 'all'>(-1);\n\n const connections = React.useMemo(() => connectEditableSections(sections), [sections]);\n\n const selectedSectionIndexes = React.useMemo<{\n startIndex: number;\n endIndex: number;\n } | null>(() => {\n if (selectedSections === -1) {\n return null;\n }\n\n if (selectedSections === 'all') {\n return {\n startIndex: 0,\n endIndex: connections.length - 1,\n };\n }\n\n if (typeof selectedSections === 'number') {\n return {startIndex: selectedSections, endIndex: selectedSections};\n }\n\n return selectedSections;\n }, [selectedSections, connections]);\n\n const activeSectionIndex = getCurrentEditableSectionIndex(connections, selectedSections);\n\n return {\n activeSectionIndex,\n selectedSectionIndexes,\n isFistSection(position) {\n const p = connections[position];\n if (!p) {\n return false;\n }\n return position === p.previousEditableSection;\n },\n isLastSection(position) {\n const p = connections[position];\n if (!p) {\n return false;\n }\n return position === p.nextEditableSection;\n },\n focusSection(index: number | 'all') {\n if (index === 'all' || index === -1) {\n setSelectedSections(index);\n } else {\n setSelectedSections(getCurrentEditableSectionIndex(connections, index));\n }\n },\n focusNextSection() {\n const currentIndex = selectedSections === 'all' ? 0 : selectedSections;\n const newIndex = connections[currentIndex]?.nextEditableSection ?? -1;\n if (newIndex !== -1) {\n setSelectedSections(newIndex);\n }\n },\n focusPreviousSection() {\n const currentIndex = selectedSections === 'all' ? 0 : selectedSections;\n const newIndex = connections[currentIndex]?.previousEditableSection ?? -1;\n if (newIndex !== -1) {\n setSelectedSections(newIndex);\n }\n },\n focusFirstSection() {\n const newIndex = connections[0]?.previousEditableSection ?? -1;\n if (newIndex !== -1) {\n setSelectedSections(newIndex);\n }\n },\n focusLastSection() {\n const newIndex = connections[sections.length - 1]?.nextEditableSection ?? -1;\n if (newIndex !== -1) {\n setSelectedSections(newIndex);\n }\n },\n };\n}\n\ninterface Connection {\n type: DateFieldSectionType;\n previousEditableSection: number;\n nextEditableSection: number;\n}\n\nfunction connectEditableSections(sections: DateFieldSection[]) {\n let previousEditableSection = -1;\n const connections: Connection[] = new Array(sections.length);\n for (let i = 0; i < sections.length; i++) {\n const section = sections[i];\n if (!section) {\n throw new Error('section must be defined');\n }\n\n const connection = {\n type: section.type,\n previousEditableSection,\n nextEditableSection: previousEditableSection,\n };\n connections[i] = connection;\n\n if (isEditableSectionType(connection.type)) {\n for (let j = Math.max(0, previousEditableSection); j <= i; j++) {\n const prevSection = connections[j];\n if (prevSection) {\n prevSection.nextEditableSection = i;\n if (prevSection.previousEditableSection === -1) {\n prevSection.previousEditableSection = i;\n }\n }\n }\n previousEditableSection = i;\n }\n }\n\n return connections;\n}\n\nfunction getCurrentEditableSectionIndex(sections: Connection[], selectedSections: 'all' | number) {\n const currentIndex =\n selectedSections === 'all' || selectedSections === -1 ? 0 : selectedSections;\n const section = sections[currentIndex];\n if (section && !isEditableSectionType(section.type)) {\n return section.nextEditableSection;\n }\n return isEditableSectionType(section?.type) ? currentIndex : -1;\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  import type { DateTime } from '@gravity-ui/date-utils';
2
- import type { DateFieldState } from "../../DateField/index.js";
3
2
  import type { DateFieldBase } from "../../types/datePicker.js";
4
3
  import type { RangeValue } from "../../types/inputs.js";
4
+ import type { DateFieldState } from "./useBaseDateFieldState.js";
5
5
  export interface RangeDateFieldStateOptions extends DateFieldBase<RangeValue<DateTime>> {
6
6
  delimiter?: string;
7
7
  }