@gravity-ui/date-components 3.4.2 → 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 (401) hide show
  1. package/README.md +31 -0
  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 +84 -118
  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 +180 -132
  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.d.ts +2 -1
  125. package/dist/cjs/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js +13 -5
  126. package/dist/cjs/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js.map +1 -1
  127. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.css +1 -1
  128. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.css.map +1 -1
  129. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.d.ts +2 -1
  130. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.js +12 -6
  131. package/dist/cjs/components/RelativeRangeDatePicker/components/Control/Control.js.map +1 -1
  132. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.d.ts +1 -1
  133. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js +6 -8
  134. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js.map +1 -1
  135. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.d.ts +5 -3
  136. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.js +18 -16
  137. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.js.map +1 -1
  138. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.d.ts +2 -1
  139. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.js +33 -7
  140. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.js.map +1 -1
  141. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.d.ts +3 -3
  142. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.js +19 -18
  143. package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.js.map +1 -1
  144. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/Presets.d.ts +6 -9
  145. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/Presets.js +9 -12
  146. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/Presets.js.map +1 -1
  147. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/defaultPresets.d.ts +2 -2
  148. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/defaultPresets.js.map +1 -1
  149. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/utils.d.ts +9 -4
  150. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/utils.js +27 -16
  151. package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/utils.js.map +1 -1
  152. package/dist/cjs/components/RelativeRangeDatePicker/components/Zones/Zones.d.ts +3 -3
  153. package/dist/cjs/components/RelativeRangeDatePicker/components/Zones/Zones.js +3 -4
  154. package/dist/cjs/components/RelativeRangeDatePicker/components/Zones/Zones.js.map +1 -1
  155. package/dist/cjs/components/RelativeRangeDatePicker/hooks/useRelativeRangeDatePickerState.js +8 -7
  156. package/dist/cjs/components/RelativeRangeDatePicker/hooks/useRelativeRangeDatePickerState.js.map +1 -1
  157. package/dist/cjs/components/RelativeRangeDatePicker/utils.d.ts +2 -2
  158. package/dist/cjs/components/RelativeRangeDatePicker/utils.js +32 -9
  159. package/dist/cjs/components/RelativeRangeDatePicker/utils.js.map +1 -1
  160. package/dist/cjs/components/utils/constants.d.ts +2 -0
  161. package/dist/cjs/components/utils/constants.js +7 -1
  162. package/dist/cjs/components/utils/constants.js.map +1 -1
  163. package/dist/cjs/components/utils/dates.d.ts +6 -1
  164. package/dist/cjs/components/utils/dates.js +6 -1
  165. package/dist/cjs/components/utils/dates.js.map +1 -1
  166. package/dist/cjs/components/utils/filterDOMProps.js +1 -1
  167. package/dist/cjs/components/utils/filterDOMProps.js.map +1 -1
  168. package/dist/cjs/components/utils/mergeProps.js +1 -1
  169. package/dist/cjs/components/utils/mergeProps.js.map +1 -1
  170. package/dist/cjs/components/utils/validation/datePicker.js +1 -1
  171. package/dist/cjs/components/utils/validation/datePicker.js.map +1 -1
  172. package/dist/cjs/components/utils/validation/i18n/ru.json +2 -2
  173. package/dist/cjs/components/utils/validation/relativeRangeDatePicker.d.ts +7 -11
  174. package/dist/cjs/components/utils/validation/relativeRangeDatePicker.js +6 -1
  175. package/dist/cjs/components/utils/validation/relativeRangeDatePicker.js.map +1 -1
  176. package/dist/cjs/hooks/useEventHandler.js +1 -2
  177. package/dist/cjs/hooks/useEventHandler.js.map +1 -1
  178. package/dist/cjs/hooks/useMove.d.ts +2 -2
  179. package/dist/cjs/hooks/useMove.js +6 -9
  180. package/dist/cjs/hooks/useMove.js.map +1 -1
  181. package/dist/cjs/hooks/useResizeObserver.js +1 -1
  182. package/dist/cjs/hooks/useResizeObserver.js.map +1 -1
  183. package/dist/cjs/package.json +1 -1
  184. package/dist/docs/INDEX.md +131 -0
  185. package/dist/docs/components/Calendar.md +125 -0
  186. package/dist/docs/components/DateField.md +206 -0
  187. package/dist/docs/components/DatePicker.md +194 -0
  188. package/dist/docs/components/RangeCalendar.md +125 -0
  189. package/dist/docs/components/RelativeDateField.md +73 -0
  190. package/dist/docs/components/RelativeDatePicker.md +59 -0
  191. package/dist/esm/components/Calendar/Calendar.d.ts +6 -2
  192. package/dist/esm/components/Calendar/Calendar.js +1 -1
  193. package/dist/esm/components/Calendar/Calendar.js.map +1 -1
  194. package/dist/esm/components/CalendarView/Calendar.css +3 -0
  195. package/dist/esm/components/CalendarView/Calendar.css.map +1 -1
  196. package/dist/esm/components/CalendarView/CalendarView.d.ts +2 -1
  197. package/dist/esm/components/CalendarView/CalendarView.js +8 -8
  198. package/dist/esm/components/CalendarView/CalendarView.js.map +1 -1
  199. package/dist/esm/components/CalendarView/hooks/types.d.ts +5 -3
  200. package/dist/esm/components/CalendarView/hooks/types.js.map +1 -1
  201. package/dist/esm/components/CalendarView/hooks/useCalendarCellProps.js +1 -2
  202. package/dist/esm/components/CalendarView/hooks/useCalendarCellProps.js.map +1 -1
  203. package/dist/esm/components/CalendarView/hooks/useCalendarGridProps.js +10 -3
  204. package/dist/esm/components/CalendarView/hooks/useCalendarGridProps.js.map +1 -1
  205. package/dist/esm/components/CalendarView/hooks/useCalendarProps.js +7 -1
  206. package/dist/esm/components/CalendarView/hooks/useCalendarProps.js.map +1 -1
  207. package/dist/esm/components/CalendarView/hooks/useCalendarState.d.ts +5 -3
  208. package/dist/esm/components/CalendarView/hooks/useCalendarState.js +74 -30
  209. package/dist/esm/components/CalendarView/hooks/useCalendarState.js.map +1 -1
  210. package/dist/esm/components/CalendarView/hooks/useRangeCalendarState.js +16 -10
  211. package/dist/esm/components/CalendarView/hooks/useRangeCalendarState.js.map +1 -1
  212. package/dist/esm/components/DateField/DateField.d.ts +1 -1
  213. package/dist/esm/components/DateField/DateField.js +6 -18
  214. package/dist/esm/components/DateField/DateField.js.map +1 -1
  215. package/dist/esm/components/DateField/IncompleteDate.d.ts +28 -0
  216. package/dist/esm/components/DateField/IncompleteDate.js +112 -0
  217. package/dist/esm/components/DateField/IncompleteDate.js.map +1 -0
  218. package/dist/esm/components/DateField/hooks/useBaseDateFieldState.d.ts +34 -68
  219. package/dist/esm/components/DateField/hooks/useBaseDateFieldState.js +17 -248
  220. package/dist/esm/components/DateField/hooks/useBaseDateFieldState.js.map +1 -1
  221. package/dist/esm/components/DateField/hooks/useDateFieldProps.d.ts +2 -0
  222. package/dist/esm/components/DateField/hooks/useDateFieldProps.js +250 -54
  223. package/dist/esm/components/DateField/hooks/useDateFieldProps.js.map +1 -1
  224. package/dist/esm/components/DateField/hooks/useDateFieldState.d.ts +20 -2
  225. package/dist/esm/components/DateField/hooks/useDateFieldState.js +85 -119
  226. package/dist/esm/components/DateField/hooks/useDateFieldState.js.map +1 -1
  227. package/dist/esm/components/DateField/hooks/useFocusManager.d.ts +29 -0
  228. package/dist/esm/components/DateField/hooks/useFocusManager.js +112 -0
  229. package/dist/esm/components/DateField/hooks/useFocusManager.js.map +1 -0
  230. package/dist/esm/components/{RangeDateField → DateField}/hooks/useRangeDateFieldState.d.ts +1 -1
  231. package/dist/esm/components/DateField/hooks/useRangeDateFieldState.js +232 -0
  232. package/dist/esm/components/DateField/hooks/useRangeDateFieldState.js.map +1 -0
  233. package/dist/esm/components/DateField/i18n/ru.json +1 -1
  234. package/dist/esm/components/DateField/index.d.ts +2 -1
  235. package/dist/esm/components/DateField/index.js +1 -1
  236. package/dist/esm/components/DateField/index.js.map +1 -1
  237. package/dist/esm/components/DateField/types.d.ts +2 -22
  238. package/dist/esm/components/DateField/types.js.map +1 -1
  239. package/dist/esm/components/DateField/utils.d.ts +21 -34
  240. package/dist/esm/components/DateField/utils.js +179 -125
  241. package/dist/esm/components/DateField/utils.js.map +1 -1
  242. package/dist/esm/components/DatePicker/DatePicker.d.ts +3 -3
  243. package/dist/esm/components/DatePicker/DatePicker.js +3 -5
  244. package/dist/esm/components/DatePicker/DatePicker.js.map +1 -1
  245. package/dist/esm/components/DatePicker/MobileCalendar.d.ts +1 -1
  246. package/dist/esm/components/DatePicker/MobileCalendar.js +3 -5
  247. package/dist/esm/components/DatePicker/MobileCalendar.js.map +1 -1
  248. package/dist/esm/components/DatePicker/StubButton.d.ts +1 -1
  249. package/dist/esm/components/DatePicker/hooks/datePickerStateFactory.d.ts +1 -1
  250. package/dist/esm/components/DatePicker/hooks/datePickerStateFactory.js +15 -6
  251. package/dist/esm/components/DatePicker/hooks/datePickerStateFactory.js.map +1 -1
  252. package/dist/esm/components/DatePicker/hooks/useDatePickerProps.d.ts +2 -2
  253. package/dist/esm/components/DatePicker/hooks/useDatePickerProps.js +30 -18
  254. package/dist/esm/components/DatePicker/hooks/useDatePickerProps.js.map +1 -1
  255. package/dist/esm/components/HiddenInput/HiddenInput.d.ts +2 -5
  256. package/dist/esm/components/HiddenInput/HiddenInput.js +3 -4
  257. package/dist/esm/components/HiddenInput/HiddenInput.js.map +1 -1
  258. package/dist/esm/components/RangeCalendar/RangeCalendar.d.ts +2 -2
  259. package/dist/esm/components/RangeCalendar/RangeCalendar.js +2 -3
  260. package/dist/esm/components/RangeCalendar/RangeCalendar.js.map +1 -1
  261. package/dist/esm/components/RangeDateField/RangeDateField.d.ts +1 -1
  262. package/dist/esm/components/RangeDateField/RangeDateField.js +7 -20
  263. package/dist/esm/components/RangeDateField/RangeDateField.js.map +1 -1
  264. package/dist/esm/components/RangeDateField/index.d.ts +0 -1
  265. package/dist/esm/components/RangeDateField/index.js +0 -1
  266. package/dist/esm/components/RangeDateField/index.js.map +1 -1
  267. package/dist/esm/components/RangeDatePicker/RangeDatePicker.d.ts +2 -1
  268. package/dist/esm/components/RangeDatePicker/RangeDatePicker.js +4 -6
  269. package/dist/esm/components/RangeDatePicker/RangeDatePicker.js.map +1 -1
  270. package/dist/esm/components/RangeDatePicker/hooks/useRangeDatePickerState.d.ts +1 -1
  271. package/dist/esm/components/RangeDatePicker/hooks/useRangeDatePickerState.js +2 -3
  272. package/dist/esm/components/RangeDatePicker/hooks/useRangeDatePickerState.js.map +1 -1
  273. package/dist/esm/components/RangeDateSelection/RangeDateSelection.d.ts +1 -1
  274. package/dist/esm/components/RangeDateSelection/RangeDateSelection.js +4 -5
  275. package/dist/esm/components/RangeDateSelection/RangeDateSelection.js.map +1 -1
  276. package/dist/esm/components/RangeDateSelection/components/NowLine/NowLine.d.ts +2 -1
  277. package/dist/esm/components/RangeDateSelection/components/Ruler/Ruler.d.ts +1 -1
  278. package/dist/esm/components/RangeDateSelection/components/Ruler/Ruler.js +1 -1
  279. package/dist/esm/components/RangeDateSelection/components/Ruler/Ruler.js.map +1 -1
  280. package/dist/esm/components/RangeDateSelection/components/RulerViewport/RulerViewport.js +2 -2
  281. package/dist/esm/components/RangeDateSelection/components/RulerViewport/RulerViewport.js.map +1 -1
  282. package/dist/esm/components/RangeDateSelection/components/SelectionControl/SelectionControl.d.ts +2 -1
  283. package/dist/esm/components/RangeDateSelection/components/SelectionControl/SelectionControl.js +12 -22
  284. package/dist/esm/components/RangeDateSelection/components/SelectionControl/SelectionControl.js.map +1 -1
  285. package/dist/esm/components/RangeDateSelection/components/Ticks/MiddleTicks.d.ts +1 -1
  286. package/dist/esm/components/RangeDateSelection/components/Ticks/SlitTicks.d.ts +1 -1
  287. package/dist/esm/components/RangeDateSelection/components/Ticks/Ticks.d.ts +2 -2
  288. package/dist/esm/components/RangeDateSelection/components/Ticks/Ticks.js.map +1 -1
  289. package/dist/esm/components/RangeDateSelection/components/Ticks/UnavialableTicks.d.ts +3 -4
  290. package/dist/esm/components/RangeDateSelection/components/Ticks/UnavialableTicks.js +1 -2
  291. package/dist/esm/components/RangeDateSelection/components/Ticks/UnavialableTicks.js.map +1 -1
  292. package/dist/esm/components/RangeDateSelection/hooks/useRangeDateSelectionState.js +9 -10
  293. package/dist/esm/components/RangeDateSelection/hooks/useRangeDateSelectionState.js.map +1 -1
  294. package/dist/esm/components/RangeDateSelection/utils/span.d.ts +2 -1
  295. package/dist/esm/components/RangeDateSelection/utils/span.js.map +1 -1
  296. package/dist/esm/components/RelativeDateField/RelativeDateField.d.ts +1 -1
  297. package/dist/esm/components/RelativeDateField/RelativeDateField.js +4 -40
  298. package/dist/esm/components/RelativeDateField/RelativeDateField.js.map +1 -1
  299. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldProps.d.ts +7 -2
  300. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldProps.js +71 -9
  301. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldProps.js.map +1 -1
  302. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldState.d.ts +2 -0
  303. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldState.js +48 -34
  304. package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldState.js.map +1 -1
  305. package/dist/esm/components/RelativeDatePicker/RelativeDatePicker.d.ts +1 -1
  306. package/dist/esm/components/RelativeDatePicker/RelativeDatePicker.js +7 -4
  307. package/dist/esm/components/RelativeDatePicker/RelativeDatePicker.js.map +1 -1
  308. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.d.ts +1 -1
  309. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.js +52 -22
  310. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerProps.js.map +1 -1
  311. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerState.js +9 -10
  312. package/dist/esm/components/RelativeDatePicker/hooks/useRelativeDatePickerState.js.map +1 -1
  313. package/dist/esm/components/RelativeRangeDatePicker/RelativeRangeDatePicker.d.ts +2 -1
  314. package/dist/esm/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js +13 -5
  315. package/dist/esm/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js.map +1 -1
  316. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.css +1 -1
  317. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.css.map +1 -1
  318. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.d.ts +2 -1
  319. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.js +12 -6
  320. package/dist/esm/components/RelativeRangeDatePicker/components/Control/Control.js.map +1 -1
  321. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.d.ts +1 -1
  322. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js +6 -8
  323. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js.map +1 -1
  324. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.d.ts +5 -3
  325. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.js +18 -17
  326. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDoc.js.map +1 -1
  327. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.d.ts +2 -1
  328. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.js +32 -7
  329. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerForm.js.map +1 -1
  330. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.d.ts +3 -3
  331. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.js +19 -18
  332. package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/useRelativeRangeDatePickerDialogState.js.map +1 -1
  333. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/Presets.d.ts +6 -9
  334. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/Presets.js +8 -10
  335. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/Presets.js.map +1 -1
  336. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/defaultPresets.d.ts +2 -2
  337. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/defaultPresets.js.map +1 -1
  338. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/utils.d.ts +9 -4
  339. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/utils.js +26 -15
  340. package/dist/esm/components/RelativeRangeDatePicker/components/Presets/utils.js.map +1 -1
  341. package/dist/esm/components/RelativeRangeDatePicker/components/Zones/Zones.d.ts +3 -3
  342. package/dist/esm/components/RelativeRangeDatePicker/components/Zones/Zones.js +3 -4
  343. package/dist/esm/components/RelativeRangeDatePicker/components/Zones/Zones.js.map +1 -1
  344. package/dist/esm/components/RelativeRangeDatePicker/hooks/useRelativeRangeDatePickerState.js +8 -7
  345. package/dist/esm/components/RelativeRangeDatePicker/hooks/useRelativeRangeDatePickerState.js.map +1 -1
  346. package/dist/esm/components/RelativeRangeDatePicker/utils.d.ts +2 -2
  347. package/dist/esm/components/RelativeRangeDatePicker/utils.js +33 -9
  348. package/dist/esm/components/RelativeRangeDatePicker/utils.js.map +1 -1
  349. package/dist/esm/components/utils/constants.d.ts +2 -0
  350. package/dist/esm/components/utils/constants.js +6 -0
  351. package/dist/esm/components/utils/constants.js.map +1 -1
  352. package/dist/esm/components/utils/dates.d.ts +6 -1
  353. package/dist/esm/components/utils/dates.js +5 -1
  354. package/dist/esm/components/utils/dates.js.map +1 -1
  355. package/dist/esm/components/utils/filterDOMProps.js +1 -1
  356. package/dist/esm/components/utils/filterDOMProps.js.map +1 -1
  357. package/dist/esm/components/utils/mergeProps.js +1 -1
  358. package/dist/esm/components/utils/mergeProps.js.map +1 -1
  359. package/dist/esm/components/utils/validation/datePicker.js +1 -1
  360. package/dist/esm/components/utils/validation/datePicker.js.map +1 -1
  361. package/dist/esm/components/utils/validation/i18n/ru.json +2 -2
  362. package/dist/esm/components/utils/validation/relativeRangeDatePicker.d.ts +7 -11
  363. package/dist/esm/components/utils/validation/relativeRangeDatePicker.js +6 -1
  364. package/dist/esm/components/utils/validation/relativeRangeDatePicker.js.map +1 -1
  365. package/dist/esm/hooks/useEventHandler.js +1 -2
  366. package/dist/esm/hooks/useEventHandler.js.map +1 -1
  367. package/dist/esm/hooks/useMove.d.ts +2 -2
  368. package/dist/esm/hooks/useMove.js +6 -9
  369. package/dist/esm/hooks/useMove.js.map +1 -1
  370. package/dist/esm/hooks/useResizeObserver.js +1 -1
  371. package/dist/esm/hooks/useResizeObserver.js.map +1 -1
  372. package/dist/esm/package.json +1 -1
  373. package/package.json +34 -27
  374. package/dist/cjs/components/RangeDateField/hooks/useRangeDateFieldState.js +0 -224
  375. package/dist/cjs/components/RangeDateField/hooks/useRangeDateFieldState.js.map +0 -1
  376. package/dist/cjs/components/RangeDateField/utils/createPlaceholderRangeValue.d.ts +0 -5
  377. package/dist/cjs/components/RangeDateField/utils/createPlaceholderRangeValue.js +0 -9
  378. package/dist/cjs/components/RangeDateField/utils/createPlaceholderRangeValue.js.map +0 -1
  379. package/dist/cjs/components/RangeDateField/utils/getRangeEditableSections.d.ts +0 -5
  380. package/dist/cjs/components/RangeDateField/utils/getRangeEditableSections.js +0 -42
  381. package/dist/cjs/components/RangeDateField/utils/getRangeEditableSections.js.map +0 -1
  382. package/dist/cjs/components/RangeDateField/utils/index.d.ts +0 -3
  383. package/dist/cjs/components/RangeDateField/utils/index.js +0 -7
  384. package/dist/cjs/components/RangeDateField/utils/index.js.map +0 -1
  385. package/dist/cjs/components/RangeDateField/utils/isValidRange.d.ts +0 -3
  386. package/dist/cjs/components/RangeDateField/utils/isValidRange.js +0 -7
  387. package/dist/cjs/components/RangeDateField/utils/isValidRange.js.map +0 -1
  388. package/dist/esm/components/RangeDateField/hooks/useRangeDateFieldState.js +0 -220
  389. package/dist/esm/components/RangeDateField/hooks/useRangeDateFieldState.js.map +0 -1
  390. package/dist/esm/components/RangeDateField/utils/createPlaceholderRangeValue.d.ts +0 -5
  391. package/dist/esm/components/RangeDateField/utils/createPlaceholderRangeValue.js +0 -6
  392. package/dist/esm/components/RangeDateField/utils/createPlaceholderRangeValue.js.map +0 -1
  393. package/dist/esm/components/RangeDateField/utils/getRangeEditableSections.d.ts +0 -5
  394. package/dist/esm/components/RangeDateField/utils/getRangeEditableSections.js +0 -39
  395. package/dist/esm/components/RangeDateField/utils/getRangeEditableSections.js.map +0 -1
  396. package/dist/esm/components/RangeDateField/utils/index.d.ts +0 -3
  397. package/dist/esm/components/RangeDateField/utils/index.js +0 -4
  398. package/dist/esm/components/RangeDateField/utils/index.js.map +0 -1
  399. package/dist/esm/components/RangeDateField/utils/isValidRange.d.ts +0 -3
  400. package/dist/esm/components/RangeDateField/utils/isValidRange.js +0 -4
  401. package/dist/esm/components/RangeDateField/utils/isValidRange.js.map +0 -1
@@ -6,190 +6,156 @@ const react_1 = tslib_1.__importDefault(require("react"));
6
6
  const uikit_1 = require("@gravity-ui/uikit");
7
7
  const dates_1 = require("../../utils/dates.js");
8
8
  const useDefaultTimeZone_1 = require("../../utils/useDefaultTimeZone.js");
9
+ const IncompleteDate_1 = require("../IncompleteDate.js");
9
10
  const utils_1 = require("../utils.js");
10
11
  const useBaseDateFieldState_1 = require("./useBaseDateFieldState.js");
11
12
  function useDateFieldState(props) {
12
- var _a, _b;
13
- const [value, setDate] = (0, uikit_1.useControlledState)(props.value, (_a = props.defaultValue) !== null && _a !== void 0 ? _a : null, props.onUpdate);
13
+ const [value, setDate] = (0, uikit_1.useControlledState)(props.value, props.defaultValue ?? null, props.onUpdate);
14
14
  const inputTimeZone = (0, useDefaultTimeZone_1.useDefaultTimeZone)(props.value || props.defaultValue || props.placeholderValue);
15
15
  const timeZone = props.timeZone || inputTimeZone;
16
16
  const handleUpdateDate = (v) => {
17
17
  setDate(v ? v.timeZone(inputTimeZone) : v);
18
18
  };
19
- const [placeholderDate, setPlaceholderDate] = react_1.default.useState(() => {
20
- return (0, dates_1.createPlaceholderValue)({
21
- placeholderValue: props.placeholderValue,
22
- timeZone,
23
- });
24
- });
19
+ const [lastPlaceholder, setLastPlaceholder] = react_1.default.useState(props.placeholderValue);
20
+ if ((props.placeholderValue &&
21
+ (!lastPlaceholder || !props.placeholderValue.isSame(lastPlaceholder))) ||
22
+ (!props.placeholderValue && lastPlaceholder)) {
23
+ setLastPlaceholder(props.placeholderValue);
24
+ }
25
+ const placeholder = react_1.default.useMemo(() => (0, dates_1.createPlaceholderValue)({
26
+ placeholderValue: lastPlaceholder,
27
+ timeZone,
28
+ }), [lastPlaceholder, timeZone]);
25
29
  const format = props.format || 'L';
26
30
  const sections = (0, utils_1.useFormatSections)(format);
27
31
  const formatInfo = react_1.default.useMemo(() => (0, utils_1.getFormatInfo)(sections), [sections]);
28
32
  const allSegments = formatInfo.availableUnits;
29
- const validSegmentsState = react_1.default.useState(() => value ? Object.assign({}, allSegments) : {});
30
- let validSegments = validSegmentsState[0];
31
- const setValidSegments = validSegmentsState[1];
32
- if (value && !(0, utils_1.isAllSegmentsValid)(allSegments, validSegments)) {
33
- setValidSegments(Object.assign({}, allSegments));
34
- }
35
- if (!value &&
36
- Object.keys(allSegments).length > 0 &&
37
- (0, utils_1.isAllSegmentsValid)(allSegments, validSegments) &&
38
- Object.keys(validSegments).length === Object.keys(allSegments).length) {
39
- validSegments = {};
40
- setValidSegments(validSegments);
41
- setPlaceholderDate((0, dates_1.createPlaceholderValue)({
42
- placeholderValue: props.placeholderValue,
43
- timeZone,
44
- }));
33
+ const [displayValue, setDisplayValue] = react_1.default.useState(() => {
34
+ return new IncompleteDate_1.IncompleteDate(value && value.isValid() ? value.timeZone(timeZone) : null);
35
+ });
36
+ const [lastValue, setLastValue] = react_1.default.useState(value);
37
+ const [lastTimezone, setLastTimezone] = react_1.default.useState(timeZone);
38
+ if ((value && (!lastValue || !value.isSame(lastValue))) ||
39
+ (value && lastTimezone !== timeZone) ||
40
+ (value === null && lastValue !== null)) {
41
+ setLastValue(value);
42
+ setLastTimezone(timeZone);
43
+ setDisplayValue(new IncompleteDate_1.IncompleteDate(value?.timeZone(timeZone)));
45
44
  }
46
45
  const { lang } = (0, uikit_1.useLang)();
47
- const displayValue = value && value.isValid() && (0, utils_1.isAllSegmentsValid)(allSegments, validSegments)
48
- ? value.timeZone(timeZone).locale(lang)
49
- : placeholderDate.timeZone(timeZone).locale(lang);
50
- const sectionsState = useSectionsState(sections, displayValue, validSegments);
51
- const [selectedSections, setSelectedSections] = react_1.default.useState(-1);
52
- const selectedSectionIndexes = react_1.default.useMemo(() => {
53
- if (selectedSections === -1) {
54
- return null;
55
- }
56
- if (selectedSections === 'all') {
57
- return {
58
- startIndex: 0,
59
- endIndex: sectionsState.editableSections.length - 1,
60
- };
61
- }
62
- if (typeof selectedSections === 'number') {
63
- return { startIndex: selectedSections, endIndex: selectedSections };
64
- }
65
- if (typeof selectedSections === 'string') {
66
- const selectedSectionIndex = sectionsState.editableSections.findIndex((section) => section.type === selectedSections);
67
- return { startIndex: selectedSectionIndex, endIndex: selectedSectionIndex };
68
- }
69
- return selectedSections;
70
- }, [selectedSections, sectionsState.editableSections]);
46
+ const dateValue = react_1.default.useMemo(() => {
47
+ return displayValue
48
+ .toDateTime(value?.timeZone(timeZone) ?? placeholder, {
49
+ setDate: formatInfo.hasDate,
50
+ setTime: formatInfo.hasTime,
51
+ })
52
+ .locale(lang);
53
+ }, [displayValue, value, placeholder, formatInfo, timeZone, lang]);
54
+ const sectionsState = useSectionsState(sections, displayValue, dateValue);
71
55
  function setValue(newValue) {
72
56
  if (props.disabled || props.readOnly) {
73
57
  return;
74
58
  }
75
- if ((0, utils_1.isAllSegmentsValid)(allSegments, validSegments)) {
76
- if (!value || !newValue.isSame(value)) {
77
- handleUpdateDate((0, utils_1.adjustDateToFormat)(newValue, formatInfo));
78
- }
59
+ if (newValue === null ||
60
+ (newValue instanceof IncompleteDate_1.IncompleteDate && newValue.isCleared(allSegments))) {
61
+ setDate(null);
62
+ setDisplayValue(new IncompleteDate_1.IncompleteDate());
79
63
  }
80
- else {
81
- if (value) {
82
- handleUpdateDate(null);
64
+ else if (newValue instanceof IncompleteDate_1.IncompleteDate) {
65
+ if (newValue.isComplete(allSegments)) {
66
+ const newDate = newValue.toDateTime(dateValue, {
67
+ setDate: formatInfo.hasDate,
68
+ setTime: formatInfo.hasTime,
69
+ });
70
+ if (newValue.validate(newDate, allSegments)) {
71
+ if (!value || !newDate.isSame(value)) {
72
+ handleUpdateDate((0, utils_1.adjustDateToFormat)(newDate, formatInfo));
73
+ }
74
+ }
83
75
  }
84
- setPlaceholderDate(newValue);
76
+ setDisplayValue(newValue);
77
+ }
78
+ else if (!value || !newValue.isSame(value)) {
79
+ handleUpdateDate(newValue);
85
80
  }
86
- }
87
- function markValid(part) {
88
- validSegments = (0, utils_1.markValidSection)(allSegments, validSegments, part);
89
- setValidSegments(Object.assign({}, validSegments));
90
81
  }
91
82
  function setSection(sectionIndex, amount) {
92
83
  const section = sectionsState.editableSections[sectionIndex];
93
84
  if (section) {
94
- markValid(section.type);
95
- setValue((0, utils_1.setSegment)(section, displayValue, amount));
85
+ setValue((0, utils_1.setSegment)(section, displayValue, amount, dateValue));
96
86
  }
97
87
  }
98
88
  function adjustSection(sectionIndex, amount) {
99
89
  const section = sectionsState.editableSections[sectionIndex];
100
90
  if (section) {
101
- if (validSegments[section.type]) {
102
- setValue((0, utils_1.addSegment)(section, displayValue, amount));
103
- }
104
- else {
105
- markValid(section.type);
106
- if (Object.keys(validSegments).length >= Object.keys(allSegments).length) {
107
- setValue(displayValue);
108
- }
109
- }
91
+ setValue((0, utils_1.addSegment)(section, displayValue, amount, dateValue));
110
92
  }
111
93
  }
112
- function flushValidSection(sectionIndex) {
94
+ function clearSection(sectionIndex) {
113
95
  const section = sectionsState.editableSections[sectionIndex];
114
- if (section) {
115
- delete validSegments[section.type];
96
+ if (section && (0, utils_1.isEditableSectionType)(section.type)) {
97
+ setValue(displayValue.clear(section.type));
116
98
  }
117
- setValidSegments(Object.assign({}, validSegments));
118
- }
119
- function flushAllValidSections() {
120
- validSegments = {};
121
- setValidSegments({});
122
- }
123
- function getSectionValue(_sectionIndex) {
124
- return displayValue;
125
- }
126
- function setSectionValue(_sectionIndex, currentValue) {
127
- setValue(currentValue);
128
- }
129
- function createPlaceholder() {
130
- return (0, dates_1.createPlaceholderValue)({
131
- placeholderValue: props.placeholderValue,
132
- timeZone,
133
- }).timeZone(timeZone);
134
99
  }
135
100
  function setValueFromString(str) {
136
- var _a;
137
- const parseDate = (_a = props.parseDateFromString) !== null && _a !== void 0 ? _a : utils_1.parseDateFromString;
101
+ const parseDate = props.parseDateFromString ?? utils_1.parseDateFromString;
138
102
  const date = parseDate(str, format, timeZone);
139
103
  if (date.isValid()) {
140
- handleUpdateDate(date);
104
+ setValue(date);
141
105
  return true;
142
106
  }
143
107
  return false;
144
108
  }
109
+ function confirmPlaceholder() {
110
+ if (props.disabled || props.readOnly) {
111
+ return;
112
+ }
113
+ // If the display value is complete but invalid, we need to constrain it and emit onChange on blur.
114
+ if (displayValue.isComplete(allSegments)) {
115
+ const newValue = displayValue.toDateTime(dateValue, {
116
+ setDate: formatInfo.hasDate,
117
+ setTime: formatInfo.hasTime,
118
+ });
119
+ setValue((0, utils_1.adjustDateToFormat)(newValue, formatInfo, 'startOf'));
120
+ }
121
+ }
145
122
  const validationState = props.validationState ||
146
123
  ((0, dates_1.isInvalid)(value, props.minValue, props.maxValue) ? 'invalid' : undefined) ||
147
- (value && ((_b = props.isDateUnavailable) === null || _b === void 0 ? void 0 : _b.call(props, value)) ? 'invalid' : undefined);
124
+ (value && props.isDateUnavailable?.(value) ? 'invalid' : undefined);
148
125
  return (0, useBaseDateFieldState_1.useBaseDateFieldState)({
149
126
  value,
150
- displayValue,
151
- placeholderValue: props.placeholderValue,
152
- timeZone,
127
+ displayValue: dateValue,
153
128
  validationState,
154
129
  editableSections: sectionsState.editableSections,
155
130
  formatInfo,
156
131
  readOnly: props.readOnly,
157
132
  disabled: props.disabled,
158
- selectedSectionIndexes,
159
- selectedSections,
160
- isEmpty: Object.keys(validSegments).length === 0,
161
- flushAllValidSections,
162
- flushValidSection,
163
- setSelectedSections,
133
+ isEmpty: displayValue.isCleared(allSegments),
164
134
  setValue,
165
- setDate: handleUpdateDate,
166
135
  adjustSection,
167
136
  setSection,
168
- getSectionValue,
169
- setSectionValue,
170
- createPlaceholder,
137
+ clearSection,
171
138
  setValueFromString,
139
+ confirmPlaceholder,
172
140
  });
173
141
  }
174
- function useSectionsState(sections, value, validSegments) {
142
+ function useSectionsState(sections, value, placeholder) {
175
143
  const [state, setState] = react_1.default.useState(() => {
144
+ const editableSections = (0, utils_1.getEditableSections)(sections, value, placeholder);
176
145
  return {
177
146
  value,
178
147
  sections,
179
- validSegments,
180
- editableSections: (0, utils_1.getEditableSections)(sections, value, validSegments),
148
+ placeholder,
149
+ editableSections,
181
150
  };
182
151
  });
183
- if (sections !== state.sections ||
184
- validSegments !== state.validSegments ||
185
- !value.isSame(state.value) ||
186
- value.timeZone() !== state.value.timeZone() ||
187
- value.locale() !== state.value.locale()) {
152
+ if (sections !== state.sections || placeholder !== state.placeholder || value !== state.value) {
153
+ const editableSections = (0, utils_1.getEditableSections)(sections, value, placeholder);
188
154
  setState({
189
155
  value,
190
156
  sections,
191
- validSegments,
192
- editableSections: (0, utils_1.getEditableSections)(sections, value, validSegments),
157
+ placeholder,
158
+ editableSections,
193
159
  });
194
160
  }
195
161
  return state;
@@ -1 +1 @@
1
- {"version":3,"file":"useDateFieldState.js","sourceRoot":"../../../../../src","sources":["components/DateField/hooks/useDateFieldState.ts"],"names":[],"mappings":";;AA8BA,8CA8MC;;AA5OD,0DAA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AQ,EAAE;QAC3C,KAAK,CAAC,MAAM,EAAE,KAAK,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,EACzC,CAAC;QACC,QAAQ,CAAC;YACL,KAAK;YACL,QAAQ;YACR,aAAa;YACb,gBAAgB,EAAE,IAAA,2BAAmB,EAAC,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, useLang} 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 {lang} = useLang();\n const displayValue =\n value && value.isValid() && isAllSegmentsValid(allSegments, validSegments)\n ? value.timeZone(timeZone).locale(lang)\n : placeholderDate.timeZone(timeZone).locale(lang);\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 value.locale() !== state.value.locale()\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":";;AA4CA,8CAkKC;;AA9MD,0DAA0B;AAG1B,6CAA8D;AAG9D,gDAAoE;AACpE,0EAAkE;AAClE,yDAAiD;AAEjD,uCASkB;AAElB,sEAA8D;AAuB9D,SAAgB,iBAAiB,CAAC,KAA4B;IAC1D,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,GAAG,IAAA,0BAAkB,EACvC,KAAK,CAAC,KAAK,EACX,KAAK,CAAC,YAAY,IAAI,IAAI,EAC1B,KAAK,CAAC,QAAQ,CACjB,CAAC;IAEF,MAAM,aAAa,GAAG,IAAA,uCAAkB,EACpC,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/C,CAAC,CAAC;IAEF,MAAM,CAAC,eAAe,EAAE,kBAAkB,CAAC,GAAG,eAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;IACrF,IACI,CAAC,KAAK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AAK,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,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useFocusManager = useFocusManager;
4
+ const tslib_1 = require("tslib");
5
+ const react_1 = tslib_1.__importDefault(require("react"));
6
+ const utils_1 = require("../utils.js");
7
+ function useFocusManager({ sections }) {
8
+ const [selectedSections, setSelectedSections] = react_1.default.useState(-1);
9
+ const connections = react_1.default.useMemo(() => connectEditableSections(sections), [sections]);
10
+ const selectedSectionIndexes = react_1.default.useMemo(() => {
11
+ if (selectedSections === -1) {
12
+ return null;
13
+ }
14
+ if (selectedSections === 'all') {
15
+ return {
16
+ startIndex: 0,
17
+ endIndex: connections.length - 1,
18
+ };
19
+ }
20
+ if (typeof selectedSections === 'number') {
21
+ return { startIndex: selectedSections, endIndex: selectedSections };
22
+ }
23
+ return selectedSections;
24
+ }, [selectedSections, connections]);
25
+ const activeSectionIndex = getCurrentEditableSectionIndex(connections, selectedSections);
26
+ return {
27
+ activeSectionIndex,
28
+ selectedSectionIndexes,
29
+ isFistSection(position) {
30
+ const p = connections[position];
31
+ if (!p) {
32
+ return false;
33
+ }
34
+ return position === p.previousEditableSection;
35
+ },
36
+ isLastSection(position) {
37
+ const p = connections[position];
38
+ if (!p) {
39
+ return false;
40
+ }
41
+ return position === p.nextEditableSection;
42
+ },
43
+ focusSection(index) {
44
+ if (index === 'all' || index === -1) {
45
+ setSelectedSections(index);
46
+ }
47
+ else {
48
+ setSelectedSections(getCurrentEditableSectionIndex(connections, index));
49
+ }
50
+ },
51
+ focusNextSection() {
52
+ const currentIndex = selectedSections === 'all' ? 0 : selectedSections;
53
+ const newIndex = connections[currentIndex]?.nextEditableSection ?? -1;
54
+ if (newIndex !== -1) {
55
+ setSelectedSections(newIndex);
56
+ }
57
+ },
58
+ focusPreviousSection() {
59
+ const currentIndex = selectedSections === 'all' ? 0 : selectedSections;
60
+ const newIndex = connections[currentIndex]?.previousEditableSection ?? -1;
61
+ if (newIndex !== -1) {
62
+ setSelectedSections(newIndex);
63
+ }
64
+ },
65
+ focusFirstSection() {
66
+ const newIndex = connections[0]?.previousEditableSection ?? -1;
67
+ if (newIndex !== -1) {
68
+ setSelectedSections(newIndex);
69
+ }
70
+ },
71
+ focusLastSection() {
72
+ const newIndex = connections[sections.length - 1]?.nextEditableSection ?? -1;
73
+ if (newIndex !== -1) {
74
+ setSelectedSections(newIndex);
75
+ }
76
+ },
77
+ };
78
+ }
79
+ function connectEditableSections(sections) {
80
+ let previousEditableSection = -1;
81
+ const connections = new Array(sections.length);
82
+ for (let i = 0; i < sections.length; i++) {
83
+ const section = sections[i];
84
+ if (!section) {
85
+ throw new Error('section must be defined');
86
+ }
87
+ const connection = {
88
+ type: section.type,
89
+ previousEditableSection,
90
+ nextEditableSection: previousEditableSection,
91
+ };
92
+ connections[i] = connection;
93
+ if ((0, utils_1.isEditableSectionType)(connection.type)) {
94
+ for (let j = Math.max(0, previousEditableSection); j <= i; j++) {
95
+ const prevSection = connections[j];
96
+ if (prevSection) {
97
+ prevSection.nextEditableSection = i;
98
+ if (prevSection.previousEditableSection === -1) {
99
+ prevSection.previousEditableSection = i;
100
+ }
101
+ }
102
+ }
103
+ previousEditableSection = i;
104
+ }
105
+ }
106
+ return connections;
107
+ }
108
+ function getCurrentEditableSectionIndex(sections, selectedSections) {
109
+ const currentIndex = selectedSections === 'all' || selectedSections === -1 ? 0 : selectedSections;
110
+ const section = sections[currentIndex];
111
+ if (section && !(0, utils_1.isEditableSectionType)(section.type)) {
112
+ return section.nextEditableSection;
113
+ }
114
+ return (0, utils_1.isEditableSectionType)(section?.type) ? currentIndex : -1;
115
+ }
116
+ //# sourceMappingURL=useFocusManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFocusManager.js","sourceRoot":"../../../../../src","sources":["components/DateField/hooks/useFocusManager.ts"],"names":[],"mappings":";;AA8BA,0CAgFC;;AA9GD,0DAA0B;AAG1B,uCAA+C;AA2B/C,SAAgB,eAAe,CAAC,EAAC,QAAQ,EAAsB;IAC3D,MAAM,CAAC,gBAAgB,EAAE,mBAAmB,CAAC,GAAG,eAAK,CAAC,QAAQ,CAAiB,CAAC,CAAC,CAAC,CAAC;IAEnF,MAAM,WAAW,GAAG,eAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,uBAAuB,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEvF,MAAM,sBAAsB,GAAG,eAAK,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,C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AC;IACvC,CAAC;IACD,OAAO,IAAA,6BAAqB,EAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,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
  }