@teamix-evo/ui 0.1.1 → 0.3.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 (295) hide show
  1. package/README.md +184 -184
  2. package/manifest.json +680 -492
  3. package/package.json +20 -10
  4. package/src/components/accordion/accordion.meta.md +5 -4
  5. package/src/components/accordion/accordion.stories.tsx +14 -9
  6. package/src/components/accordion/accordion.tsx +104 -8
  7. package/src/components/affix/affix.meta.md +20 -2
  8. package/src/components/affix/affix.stories.tsx +102 -25
  9. package/src/components/affix/affix.tsx +79 -9
  10. package/src/components/alert/alert.meta.md +44 -13
  11. package/src/components/alert/alert.stories.tsx +66 -21
  12. package/src/components/alert/alert.tsx +81 -34
  13. package/src/components/alert-dialog/alert-dialog.meta.md +61 -16
  14. package/src/components/alert-dialog/alert-dialog.stories.tsx +145 -3
  15. package/src/components/alert-dialog/alert-dialog.tsx +60 -13
  16. package/src/components/anchor/anchor.meta.md +8 -3
  17. package/src/components/anchor/anchor.stories.tsx +3 -3
  18. package/src/components/anchor/anchor.tsx +2 -2
  19. package/src/components/app/app.meta.md +9 -4
  20. package/src/components/app/app.stories.tsx +9 -7
  21. package/src/components/aspect-ratio/aspect-ratio.meta.md +4 -3
  22. package/src/components/aspect-ratio/aspect-ratio.stories.tsx +3 -3
  23. package/src/components/auto-complete/auto-complete.meta.md +14 -6
  24. package/src/components/auto-complete/auto-complete.stories.tsx +47 -4
  25. package/src/components/auto-complete/auto-complete.tsx +119 -71
  26. package/src/components/avatar/avatar.meta.md +6 -7
  27. package/src/components/avatar/avatar.stories.tsx +21 -3
  28. package/src/components/avatar/avatar.tsx +24 -23
  29. package/src/components/badge/badge.meta.md +10 -9
  30. package/src/components/badge/badge.stories.tsx +2 -2
  31. package/src/components/badge/badge.tsx +9 -15
  32. package/src/components/breadcrumb/breadcrumb.meta.md +27 -7
  33. package/src/components/breadcrumb/breadcrumb.stories.tsx +127 -4
  34. package/src/components/breadcrumb/breadcrumb.tsx +22 -8
  35. package/src/components/button/button.meta.md +258 -21
  36. package/src/components/button/button.stories.tsx +549 -41
  37. package/src/components/button/button.tsx +335 -33
  38. package/src/components/button/demo/as-child.tsx +24 -0
  39. package/src/components/button/demo/basic.tsx +8 -0
  40. package/src/components/button/demo/block.tsx +16 -0
  41. package/src/components/button/demo/loading.tsx +19 -0
  42. package/src/components/button/demo/shapes.tsx +18 -0
  43. package/src/components/button/demo/sizes.tsx +19 -0
  44. package/src/components/button/demo/variants.tsx +19 -0
  45. package/src/components/button/demo/with-icon.tsx +20 -0
  46. package/src/components/calendar/calendar.meta.md +13 -3
  47. package/src/components/calendar/calendar.stories.tsx +6 -6
  48. package/src/components/calendar/calendar.tsx +73 -8
  49. package/src/components/card/card.meta.md +27 -5
  50. package/src/components/card/card.stories.tsx +42 -3
  51. package/src/components/card/card.tsx +146 -63
  52. package/src/components/carousel/carousel.meta.md +4 -3
  53. package/src/components/carousel/carousel.stories.tsx +11 -6
  54. package/src/components/cascader/cascader.meta.md +47 -17
  55. package/src/components/cascader/cascader.stories.tsx +22 -10
  56. package/src/components/cascader/cascader.tsx +428 -85
  57. package/src/components/checkbox/checkbox.meta.md +75 -7
  58. package/src/components/checkbox/checkbox.stories.tsx +161 -3
  59. package/src/components/checkbox/checkbox.tsx +77 -9
  60. package/src/components/collapsible/collapsible.meta.md +14 -6
  61. package/src/components/collapsible/collapsible.stories.tsx +10 -2
  62. package/src/components/collapsible/collapsible.tsx +93 -6
  63. package/src/components/color-picker/color-picker.meta.md +12 -7
  64. package/src/components/color-picker/color-picker.stories.tsx +86 -7
  65. package/src/components/color-picker/color-picker.tsx +20 -9
  66. package/src/components/command/command.meta.md +29 -13
  67. package/src/components/command/command.stories.tsx +4 -4
  68. package/src/components/command/command.tsx +19 -8
  69. package/src/components/context-menu/context-menu.meta.md +11 -8
  70. package/src/components/context-menu/context-menu.stories.tsx +11 -3
  71. package/src/components/context-menu/context-menu.tsx +21 -8
  72. package/src/components/data-table/data-table.meta.md +6 -5
  73. package/src/components/data-table/data-table.stories.tsx +13 -6
  74. package/src/components/data-table/data-table.tsx +2 -2
  75. package/src/components/date-picker/date-picker.meta.md +88 -19
  76. package/src/components/date-picker/date-picker.stories.tsx +55 -5
  77. package/src/components/date-picker/date-picker.tsx +1489 -91
  78. package/src/components/descriptions/descriptions.meta.md +10 -5
  79. package/src/components/descriptions/descriptions.stories.tsx +3 -3
  80. package/src/components/descriptions/descriptions.tsx +22 -14
  81. package/src/components/dialog/dialog.meta.md +76 -13
  82. package/src/components/dialog/dialog.stories.tsx +182 -20
  83. package/src/components/dialog/dialog.tsx +67 -15
  84. package/src/components/dialog/imperative.tsx +252 -0
  85. package/src/components/drawer/drawer.meta.md +33 -34
  86. package/src/components/drawer/drawer.stories.tsx +29 -12
  87. package/src/components/drawer/drawer.tsx +22 -113
  88. package/src/components/dropdown-menu/dropdown-menu.meta.md +78 -10
  89. package/src/components/dropdown-menu/dropdown-menu.stories.tsx +88 -2
  90. package/src/components/dropdown-menu/dropdown-menu.tsx +24 -10
  91. package/src/components/ellipsis/ellipsis.meta.md +87 -0
  92. package/src/components/ellipsis/ellipsis.stories.tsx +72 -0
  93. package/src/components/ellipsis/ellipsis.tsx +153 -0
  94. package/src/components/empty/empty.meta.md +9 -4
  95. package/src/components/empty/empty.stories.tsx +4 -4
  96. package/src/components/empty/empty.tsx +10 -3
  97. package/src/components/field/field.meta.md +47 -9
  98. package/src/components/field/field.stories.tsx +385 -5
  99. package/src/components/field/field.tsx +263 -35
  100. package/src/components/filter-bar/filter-bar.meta.md +92 -0
  101. package/src/components/filter-bar/filter-bar.stories.tsx +1083 -0
  102. package/src/components/filter-bar/filter-bar.tsx +568 -0
  103. package/src/components/flex/flex.meta.md +54 -6
  104. package/src/components/flex/flex.stories.tsx +107 -20
  105. package/src/components/flex/flex.tsx +27 -4
  106. package/src/components/float-button/float-button.meta.md +8 -3
  107. package/src/components/float-button/float-button.stories.tsx +9 -7
  108. package/src/components/float-button/float-button.tsx +1 -1
  109. package/src/components/form/form.meta.md +39 -17
  110. package/src/components/form/form.stories.tsx +350 -3
  111. package/src/components/form/form.tsx +101 -35
  112. package/src/components/grid/grid.meta.md +7 -2
  113. package/src/components/grid/grid.stories.tsx +6 -4
  114. package/src/components/hover-card/hover-card.meta.md +20 -9
  115. package/src/components/hover-card/hover-card.stories.tsx +34 -5
  116. package/src/components/hover-card/hover-card.tsx +51 -13
  117. package/src/components/icon/DEVELOPMENT.md +809 -0
  118. package/src/components/icon/icon.meta.md +170 -0
  119. package/src/components/icon/icon.stories.tsx +344 -0
  120. package/src/components/icon/icon.tsx +248 -0
  121. package/src/components/image/image.meta.md +9 -4
  122. package/src/components/image/image.stories.tsx +3 -3
  123. package/src/components/image/image.tsx +6 -4
  124. package/src/components/input/demo/basic.tsx +12 -0
  125. package/src/components/input/demo/clearable.tsx +21 -0
  126. package/src/components/input/demo/show-count.tsx +18 -0
  127. package/src/components/input/demo/sizes.tsx +15 -0
  128. package/src/components/input/input.meta.md +39 -33
  129. package/src/components/input/input.stories.tsx +62 -35
  130. package/src/components/input/input.tsx +97 -98
  131. package/src/components/input-group/input-group.meta.md +54 -22
  132. package/src/components/input-group/input-group.stories.tsx +49 -16
  133. package/src/components/input-group/input-group.tsx +44 -8
  134. package/src/components/input-number/input-number.meta.md +64 -7
  135. package/src/components/input-number/input-number.stories.tsx +46 -8
  136. package/src/components/input-number/input-number.tsx +99 -26
  137. package/src/components/input-otp/input-otp.meta.md +4 -3
  138. package/src/components/input-otp/input-otp.stories.tsx +3 -3
  139. package/src/components/input-otp/input-otp.tsx +1 -1
  140. package/src/components/item/item.meta.md +8 -3
  141. package/src/components/item/item.stories.tsx +8 -5
  142. package/src/components/item/item.tsx +7 -6
  143. package/src/components/kbd/kbd.meta.md +13 -4
  144. package/src/components/kbd/kbd.stories.tsx +4 -4
  145. package/src/components/kbd/kbd.tsx +10 -5
  146. package/src/components/label/label.meta.md +18 -10
  147. package/src/components/label/label.stories.tsx +64 -6
  148. package/src/components/label/label.tsx +91 -19
  149. package/src/components/masonry/masonry.meta.md +8 -3
  150. package/src/components/masonry/masonry.stories.tsx +7 -5
  151. package/src/components/masonry/masonry.tsx +1 -0
  152. package/src/components/mentions/mentions.meta.md +36 -6
  153. package/src/components/mentions/mentions.stories.tsx +120 -6
  154. package/src/components/mentions/mentions.tsx +11 -5
  155. package/src/components/menubar/menubar.meta.md +30 -12
  156. package/src/components/menubar/menubar.stories.tsx +62 -2
  157. package/src/components/menubar/menubar.tsx +9 -9
  158. package/src/components/native-select/native-select.meta.md +8 -3
  159. package/src/components/native-select/native-select.stories.tsx +8 -5
  160. package/src/components/native-select/native-select.tsx +1 -1
  161. package/src/components/navigation-menu/navigation-menu.meta.md +19 -9
  162. package/src/components/navigation-menu/navigation-menu.stories.tsx +112 -9
  163. package/src/components/navigation-menu/navigation-menu.tsx +8 -4
  164. package/src/components/notification/notification.meta.md +52 -10
  165. package/src/components/notification/notification.stories.tsx +11 -9
  166. package/src/components/notification/notification.tsx +36 -21
  167. package/src/components/page-header/DEVELOPMENT.md +842 -0
  168. package/src/components/page-header/page-header.meta.md +208 -0
  169. package/src/components/page-header/page-header.stories.tsx +421 -0
  170. package/src/components/page-header/page-header.tsx +281 -0
  171. package/src/components/pagination/pagination.meta.md +140 -37
  172. package/src/components/pagination/pagination.stories.tsx +232 -10
  173. package/src/components/pagination/pagination.tsx +355 -63
  174. package/src/components/popconfirm/popconfirm.meta.md +9 -4
  175. package/src/components/popconfirm/popconfirm.stories.tsx +3 -4
  176. package/src/components/popconfirm/popconfirm.tsx +2 -2
  177. package/src/components/popover/popover.meta.md +62 -5
  178. package/src/components/popover/popover.stories.tsx +83 -7
  179. package/src/components/popover/popover.tsx +77 -28
  180. package/src/components/progress/progress.meta.md +38 -6
  181. package/src/components/progress/progress.stories.tsx +3 -3
  182. package/src/components/progress/progress.tsx +24 -16
  183. package/src/components/radio-group/radio-group.meta.md +79 -7
  184. package/src/components/radio-group/radio-group.stories.tsx +39 -3
  185. package/src/components/radio-group/radio-group.tsx +149 -18
  186. package/src/components/rate/rate.meta.md +35 -4
  187. package/src/components/rate/rate.stories.tsx +13 -5
  188. package/src/components/rate/rate.tsx +37 -10
  189. package/src/components/resizable/resizable.meta.md +7 -4
  190. package/src/components/resizable/resizable.stories.tsx +6 -6
  191. package/src/components/resizable/resizable.tsx +1 -1
  192. package/src/components/result/result.meta.md +7 -2
  193. package/src/components/result/result.stories.tsx +4 -8
  194. package/src/components/result/result.tsx +24 -15
  195. package/src/components/scroll-area/scroll-area.meta.md +4 -3
  196. package/src/components/scroll-area/scroll-area.stories.tsx +12 -4
  197. package/src/components/scroll-area/scroll-area.tsx +3 -3
  198. package/src/components/segmented/segmented.meta.md +7 -4
  199. package/src/components/segmented/segmented.stories.tsx +37 -8
  200. package/src/components/segmented/segmented.tsx +15 -7
  201. package/src/components/select/select.meta.md +197 -52
  202. package/src/components/select/select.stories.tsx +238 -63
  203. package/src/components/select/select.tsx +718 -171
  204. package/src/components/separator/separator.meta.md +4 -3
  205. package/src/components/separator/separator.stories.tsx +3 -3
  206. package/src/components/separator/separator.tsx +3 -7
  207. package/src/components/sheet/sheet.meta.md +32 -16
  208. package/src/components/sheet/sheet.stories.tsx +116 -10
  209. package/src/components/sheet/sheet.tsx +116 -29
  210. package/src/components/sidebar/sidebar.meta.md +37 -18
  211. package/src/components/sidebar/sidebar.stories.tsx +701 -29
  212. package/src/components/sidebar/sidebar.tsx +615 -142
  213. package/src/components/skeleton/skeleton.meta.md +4 -5
  214. package/src/components/skeleton/skeleton.stories.tsx +4 -4
  215. package/src/components/skeleton/skeleton.tsx +7 -7
  216. package/src/components/slider/slider.meta.md +57 -5
  217. package/src/components/slider/slider.stories.tsx +58 -6
  218. package/src/components/slider/slider.tsx +154 -13
  219. package/src/components/sonner/sonner.meta.md +58 -7
  220. package/src/components/sonner/sonner.stories.tsx +78 -5
  221. package/src/components/sonner/sonner.tsx +137 -8
  222. package/src/components/spinner/spinner.meta.md +62 -13
  223. package/src/components/spinner/spinner.stories.tsx +66 -14
  224. package/src/components/spinner/spinner.tsx +111 -9
  225. package/src/components/statistic/statistic.meta.md +7 -2
  226. package/src/components/statistic/statistic.stories.tsx +3 -7
  227. package/src/components/statistic/statistic.tsx +5 -6
  228. package/src/components/steps/steps.meta.md +18 -4
  229. package/src/components/steps/steps.stories.tsx +43 -3
  230. package/src/components/steps/steps.tsx +15 -12
  231. package/src/components/switch/switch.meta.md +51 -5
  232. package/src/components/switch/switch.stories.tsx +6 -6
  233. package/src/components/switch/switch.tsx +109 -41
  234. package/src/components/table/table.meta.md +17 -6
  235. package/src/components/table/table.stories.tsx +10 -5
  236. package/src/components/table/table.tsx +4 -4
  237. package/src/components/tabs/tabs.meta.md +38 -25
  238. package/src/components/tabs/tabs.stories.tsx +111 -25
  239. package/src/components/tabs/tabs.tsx +125 -54
  240. package/src/components/tag/tag.meta.md +105 -40
  241. package/src/components/tag/tag.stories.tsx +189 -16
  242. package/src/components/tag/tag.tsx +222 -21
  243. package/src/components/textarea/textarea.meta.md +35 -19
  244. package/src/components/textarea/textarea.stories.tsx +32 -6
  245. package/src/components/textarea/textarea.tsx +33 -9
  246. package/src/components/time-picker/time-picker.meta.md +124 -32
  247. package/src/components/time-picker/time-picker.stories.tsx +85 -15
  248. package/src/components/time-picker/time-picker.tsx +913 -61
  249. package/src/components/timeline/timeline.meta.md +14 -6
  250. package/src/components/timeline/timeline.stories.tsx +37 -7
  251. package/src/components/timeline/timeline.tsx +35 -14
  252. package/src/components/toggle/toggle.meta.md +5 -4
  253. package/src/components/toggle/toggle.stories.tsx +4 -4
  254. package/src/components/toggle/toggle.tsx +4 -3
  255. package/src/components/toggle-group/toggle-group.meta.md +5 -4
  256. package/src/components/toggle-group/toggle-group.stories.tsx +3 -3
  257. package/src/components/toggle-group/toggle-group.tsx +2 -2
  258. package/src/components/tooltip/tooltip.meta.md +55 -5
  259. package/src/components/tooltip/tooltip.stories.tsx +42 -5
  260. package/src/components/tooltip/tooltip.tsx +81 -21
  261. package/src/components/tour/tour.meta.md +9 -4
  262. package/src/components/tour/tour.stories.tsx +3 -3
  263. package/src/components/tour/tour.tsx +4 -4
  264. package/src/components/transfer/transfer.meta.md +11 -6
  265. package/src/components/transfer/transfer.stories.tsx +4 -8
  266. package/src/components/transfer/transfer.tsx +28 -21
  267. package/src/components/tree/tree.meta.md +63 -5
  268. package/src/components/tree/tree.stories.tsx +31 -12
  269. package/src/components/tree/tree.tsx +9 -8
  270. package/src/components/tree-select/tree-select.meta.md +59 -8
  271. package/src/components/tree-select/tree-select.stories.tsx +3 -3
  272. package/src/components/tree-select/tree-select.tsx +42 -7
  273. package/src/components/typography/typography.meta.md +61 -14
  274. package/src/components/typography/typography.stories.tsx +12 -11
  275. package/src/components/typography/typography.tsx +43 -28
  276. package/src/components/upload/upload.meta.md +49 -4
  277. package/src/components/upload/upload.stories.tsx +72 -12
  278. package/src/components/upload/upload.tsx +170 -37
  279. package/src/components/watermark/watermark.meta.md +7 -2
  280. package/src/components/watermark/watermark.stories.tsx +101 -9
  281. package/src/components/watermark/watermark.tsx +1 -0
  282. package/src/hooks/use-breakpoint.ts +117 -0
  283. package/src/hooks/use-debounce-callback.ts +52 -0
  284. package/src/hooks/use-mobile.ts +23 -0
  285. package/src/stories/theme-tokens.stories.tsx +747 -0
  286. package/src/utils/trigger-input.ts +53 -0
  287. package/src/components/button-group/button-group.meta.md +0 -92
  288. package/src/components/button-group/button-group.stories.tsx +0 -90
  289. package/src/components/button-group/button-group.tsx +0 -75
  290. package/src/components/combobox/combobox.meta.md +0 -93
  291. package/src/components/combobox/combobox.stories.tsx +0 -55
  292. package/src/components/combobox/combobox.tsx +0 -130
  293. package/src/components/space/space.meta.md +0 -94
  294. package/src/components/space/space.stories.tsx +0 -94
  295. package/src/components/space/space.tsx +0 -106
@@ -1,94 +1,946 @@
1
1
  import * as React from 'react';
2
- import { Clock } from 'lucide-react';
2
+ import { Clock, X } from 'lucide-react';
3
3
 
4
4
  import { cn } from '@/utils/cn';
5
+ import {
6
+ inputElementClass,
7
+ triggerSizeClass,
8
+ triggerWrapperClass,
9
+ } from '@/utils/trigger-input';
10
+ import {
11
+ Popover,
12
+ PopoverAnchor,
13
+ PopoverContent,
14
+ } from '@/components/popover/popover';
15
+ import { Button } from '@/components/button/button';
5
16
 
6
- export interface TimePickerProps
7
- extends Omit<
8
- React.InputHTMLAttributes<HTMLInputElement>,
9
- 'onChange' | 'value' | 'defaultValue' | 'type' | 'size'
10
- > {
11
- /** 受控值(`HH:mm` `HH:mm:ss`) 空串表示未选。 */
17
+ // ─── 内部:TimeColumn(单列滚动选择器,h/m/s 共用) ────────────────────────
18
+
19
+ const ITEM_HEIGHT = 32; // 与 Tailwind `h-8` 对齐
20
+ const VISIBLE_COUNT = 7; // 可视行数(panel min-h-56 = 7 × 32)
21
+ const SELECTED_OFFSET_ROWS = 4; // selected 显示在 row 5(上方 4 行,下方 2 行 - 对齐 antd / next 视觉)
22
+ const PAD_BOTTOM = (VISIBLE_COUNT - SELECTED_OFFSET_ROWS - 1) * ITEM_HEIGHT; // 64px = 2 行,确保 max idx 也能滚到 row 5
23
+
24
+ const pad2 = (n: number) => String(n).padStart(2, '0');
25
+
26
+ const range = (start: number, end: number, step = 1): number[] => {
27
+ const out: number[] = [];
28
+ for (let v = start; v <= end; v += step) out.push(v);
29
+ return out;
30
+ };
31
+
32
+ interface TimeColumnProps {
33
+ values: number[];
34
+ value: number;
35
+ onChange: (v: number) => void;
36
+ disabledValues?: number[];
37
+ ariaLabel?: string;
38
+ /**
39
+ * 是否补零显示(`05` vs `5`)。DatePicker showTime 上下文一般传 `false`,
40
+ * 与 cloud-design DatePicker2 视觉对齐。
41
+ * @default true
42
+ */
43
+ padZero?: boolean;
44
+ /**
45
+ * 是否有真实选择(false 时不显示 selected 高亮,所有数字平等灰色字)。
46
+ * TimePicker / TimeRangePicker 未选时(`value === ''`)传 false,
47
+ * 避免 draft 默认 0 时 "00" 被误高亮 —— 对齐 antd TimePicker 默认空态视觉。
48
+ * @default true
49
+ */
50
+ hasSelection?: boolean;
51
+ }
52
+
53
+ /**
54
+ * TimeColumn — 单列滚动选择器(内部组件,不单独导出 entry)。
55
+ * 视觉:数字从顶部直接排列(对齐 antd `TimePicker2` / `@alifd/next` 风格),
56
+ * 选中项滚到顶部对应位置(`scrollTop = idx × ITEM_HEIGHT`),`bg-accent + font-medium` 高亮。
57
+ * 外层 `relative self-stretch min-h-56` 承担盒子高度(由 panel/flex stretch 决定),
58
+ * 内层 `absolute inset-0 overflow-y-auto` 承担内容滚动 —— 内容不撑开外层。
59
+ */
60
+ const TimeColumn: React.FC<TimeColumnProps> = ({
61
+ values,
62
+ value,
63
+ onChange,
64
+ disabledValues,
65
+ ariaLabel,
66
+ padZero = true,
67
+ hasSelection = true,
68
+ }) => {
69
+ const containerRef = React.useRef<HTMLDivElement>(null);
70
+ const initializedRef = React.useRef(false);
71
+
72
+ // 仅初次 mount 时同步 selected 到 visible row 5 位置(上 4 行可见、下 2 行 + bottom padding);
73
+ // 后续 value 变化不再 scroll,避免用户点选数字后列表突然跳动。
74
+ // Radix Popover 默认 mount/unmount,每次 open 重新 mount 自动重新初始化。
75
+ React.useLayoutEffect(() => {
76
+ if (initializedRef.current) return;
77
+ initializedRef.current = true;
78
+ if (!hasSelection) return; // 无选择时不滚动,从 0 顶对齐
79
+ const el = containerRef.current;
80
+ if (!el) return;
81
+ const idx = values.indexOf(value);
82
+ if (idx <= SELECTED_OFFSET_ROWS) return; // 前 4 个项保持自然位置(idx+1 行)
83
+ el.scrollTop = (idx - SELECTED_OFFSET_ROWS) * ITEM_HEIGHT;
84
+ }, [value, values, hasSelection]);
85
+
86
+ const disabledSet = React.useMemo(
87
+ () => new Set(disabledValues ?? []),
88
+ [disabledValues],
89
+ );
90
+
91
+ return (
92
+ <div className="relative w-14 shrink-0 self-stretch min-h-56">
93
+ <div
94
+ ref={containerRef}
95
+ role="listbox"
96
+ aria-label={ariaLabel}
97
+ className={cn(
98
+ 'absolute inset-0 overflow-y-auto [scrollbar-width:none]',
99
+ '[&::-webkit-scrollbar]:hidden',
100
+ )}
101
+ >
102
+ {values.map((v) => {
103
+ const isDisabled = disabledSet.has(v);
104
+ const isSelected = hasSelection && v === value;
105
+ return (
106
+ <button
107
+ key={v}
108
+ type="button"
109
+ role="option"
110
+ aria-selected={isSelected}
111
+ disabled={isDisabled}
112
+ onClick={() => {
113
+ if (!isDisabled) onChange(v);
114
+ }}
115
+ className={cn(
116
+ 'flex h-8 w-full shrink-0 cursor-pointer items-center justify-center text-xs tabular-nums transition-colors',
117
+ isSelected
118
+ ? 'bg-accent font-medium text-foreground'
119
+ : 'text-foreground hover:bg-accent/50',
120
+ isDisabled &&
121
+ 'cursor-not-allowed opacity-30 hover:bg-transparent',
122
+ )}
123
+ >
124
+ {padZero ? pad2(v) : String(v)}
125
+ </button>
126
+ );
127
+ })}
128
+ <div aria-hidden style={{ height: PAD_BOTTOM }} />
129
+ </div>
130
+ </div>
131
+ );
132
+ };
133
+
134
+ export { TimeColumn };
135
+ export type { TimeColumnProps };
136
+
137
+ // ─── 时间值 / 格式工具 ─────────────────────────────────────────────────────
138
+
139
+ export interface TimeParts {
140
+ h: number;
141
+ m: number;
142
+ s: number;
143
+ }
144
+
145
+ const ZERO_TIME: TimeParts = { h: 0, m: 0, s: 0 };
146
+
147
+ /**
148
+ * 解析时间字符串。容错策略:按非数字字符切分,依次取 h/m/s,缺失补 0。
149
+ * - "09" → {h:9, m:0, s:0}
150
+ * - "09:30" → {h:9, m:30, s:0}
151
+ * - "09:30:45" → {h:9, m:30, s:45}
152
+ */
153
+ export function parseTime(value: string | undefined): TimeParts | null {
154
+ if (!value) return null;
155
+ const parts = value
156
+ .split(/[^0-9]+/)
157
+ .filter(Boolean)
158
+ .map((s) => Number.parseInt(s, 10))
159
+ .filter((n) => !Number.isNaN(n));
160
+ if (parts.length === 0) return null;
161
+ return { h: parts[0] ?? 0, m: parts[1] ?? 0, s: parts[2] ?? 0 };
162
+ }
163
+
164
+ /** 按 format 模板拼接 h/m/s。支持 `HH/H/mm/m/ss/s` token。 */
165
+ export function formatTime(parts: TimeParts, format: string): string {
166
+ return format
167
+ .replace(/HH/g, pad2(parts.h))
168
+ .replace(/H/g, String(parts.h))
169
+ .replace(/mm/g, pad2(parts.m))
170
+ .replace(/m/g, String(parts.m))
171
+ .replace(/ss/g, pad2(parts.s))
172
+ .replace(/s/g, String(parts.s));
173
+ }
174
+
175
+ /** 检测 format 是否包含某 token,用于决定是否渲染对应的列。 */
176
+ export function inspectFormat(format: string) {
177
+ return {
178
+ showHour: /[Hh]/.test(format),
179
+ showMinute: /m/.test(format),
180
+ showSecond: /s/.test(format),
181
+ };
182
+ }
183
+
184
+ // trigger 样式迁移至 `@/utils/trigger-input`,4 个 picker 共享。
185
+
186
+ // ─── TimePickerPanel(共享面板,独立 TimePicker / DatePicker showTime 复用) ───
187
+
188
+ export interface TimePickerPanelProps {
189
+ /** 当前 h/m/s。 */
190
+ value: TimeParts;
191
+ /** 值变更回调。 */
192
+ onChange: (next: TimeParts) => void;
193
+ /** 时间格式,决定列的显隐。 @default "HH:mm:ss" */
194
+ format?: string;
195
+ /** 小时步长。 @default 1 */
196
+ hourStep?: number;
197
+ /** 分钟步长。 @default 1 */
198
+ minuteStep?: number;
199
+ /** 秒步长。 @default 1 */
200
+ secondStep?: number;
201
+ /** 禁用小时回调。返回禁用的小时数组。 */
202
+ disabledHours?: () => number[];
203
+ /** 禁用分钟回调。当前小时变化时重新计算。 */
204
+ disabledMinutes?: (hour: number) => number[];
205
+ /** 禁用秒回调。当前小时/分钟变化时重新计算。 */
206
+ disabledSeconds?: (hour: number, minute: number) => number[];
207
+ /**
208
+ * 是否补零显示(`05` vs `5`)。DatePicker showTime 内嵌使用时一般传 `false`。
209
+ * @default true
210
+ */
211
+ padZero?: boolean;
212
+ /**
213
+ * 是否有真实选择(false 时三列均不显示 selected 高亮,数字平等灰色字)。
214
+ * TimePicker / TimeRangePicker 在 value 为空字符串时传 false,避免 draft 默认 0
215
+ * 时 "00" 误高亮。对齐 antd TimePicker 空态视觉。
216
+ * @default true
217
+ */
218
+ hasSelection?: boolean;
219
+ /** 容器 className(可覆盖默认高度,如在 DatePicker 中传 `h-auto overflow-hidden`)。 */
220
+ className?: string;
221
+ }
222
+
223
+ /**
224
+ * TimePickerPanel — 3 列时间选择面板(可被 TimePicker / DatePicker showTime 复用)。
225
+ * 列之间有 `divide-x` 分隔,所有视觉走 OpenTrek tokens。
226
+ *
227
+ * 默认 `min-h-56`(= 7 × 32px)在独立 TimePicker 弹层中显示完整 7 行;
228
+ * 在 DatePicker showTime 中,外层 flex `items-stretch` 会把面板拉伸至日历等高,
229
+ * 内部 TimeColumn 用 `h-full overflow-y-auto` 跟随并 scroll。无需调用方传高度覆盖。
230
+ */
231
+ const TimePickerPanel: React.FC<TimePickerPanelProps> = ({
232
+ value,
233
+ onChange,
234
+ format = 'HH:mm:ss',
235
+ hourStep = 1,
236
+ minuteStep = 1,
237
+ secondStep = 1,
238
+ disabledHours,
239
+ disabledMinutes,
240
+ disabledSeconds,
241
+ padZero = true,
242
+ hasSelection = true,
243
+ className,
244
+ }) => {
245
+ const { showHour, showMinute, showSecond } = inspectFormat(format);
246
+ const hours = React.useMemo(() => range(0, 23, hourStep), [hourStep]);
247
+ const minutes = React.useMemo(() => range(0, 59, minuteStep), [minuteStep]);
248
+ const seconds = React.useMemo(() => range(0, 59, secondStep), [secondStep]);
249
+
250
+ return (
251
+ <div
252
+ className={cn(
253
+ 'flex min-h-56 divide-x divide-border overflow-hidden',
254
+ className,
255
+ )}
256
+ >
257
+ {showHour && (
258
+ <TimeColumn
259
+ values={hours}
260
+ value={value.h}
261
+ onChange={(h) => onChange({ ...value, h })}
262
+ disabledValues={disabledHours?.()}
263
+ ariaLabel="小时"
264
+ padZero={padZero}
265
+ hasSelection={hasSelection}
266
+ />
267
+ )}
268
+ {showMinute && (
269
+ <TimeColumn
270
+ values={minutes}
271
+ value={value.m}
272
+ onChange={(m) => onChange({ ...value, m })}
273
+ disabledValues={disabledMinutes?.(value.h)}
274
+ ariaLabel="分钟"
275
+ padZero={padZero}
276
+ hasSelection={hasSelection}
277
+ />
278
+ )}
279
+ {showSecond && (
280
+ <TimeColumn
281
+ values={seconds}
282
+ value={value.s}
283
+ onChange={(s) => onChange({ ...value, s })}
284
+ disabledValues={disabledSeconds?.(value.h, value.m)}
285
+ ariaLabel="秒"
286
+ padZero={padZero}
287
+ hasSelection={hasSelection}
288
+ />
289
+ )}
290
+ </div>
291
+ );
292
+ };
293
+
294
+ export { TimePickerPanel };
295
+
296
+ // ─── TimePicker(单值) ────────────────────────────────────────────────────
297
+
298
+ export interface TimePickerProps {
299
+ /** 受控值(由 `format` 决定字面)。空串表示未选。 */
12
300
  value?: string;
13
- /** uncontrolled 初值。 */
301
+ /** 非受控初值。 */
14
302
  defaultValue?: string;
15
- /** 值变化回调(实时,跟随原生 input)。 */
303
+ /** 值变化回调(选中或清除时触发)。 */
16
304
  onChange?: (value: string) => void;
17
305
  /**
18
- * 是否显示秒(antd `showSeconds` 并集) 关闭时原生 input 仅显示 HH:mm
306
+ * 时间格式(date-fns / antd 风格 token):支持 `HH/H/mm/m/ss/s`。
307
+ * 不含 `s` token 时不渲染秒列。
308
+ * @default "HH:mm:ss"
309
+ */
310
+ format?: string;
311
+ /**
312
+ * 小时步长(antd 并集)。
313
+ * @default 1
314
+ */
315
+ hourStep?: number;
316
+ /**
317
+ * 分钟步长。
318
+ * @default 1
319
+ */
320
+ minuteStep?: number;
321
+ /**
322
+ * 秒步长。
323
+ * @default 1
324
+ */
325
+ secondStep?: number;
326
+ /** 禁用小时回调,返回禁用的小时数组。 */
327
+ disabledHours?: () => number[];
328
+ /** 禁用分钟回调,接收当前选中小时。 */
329
+ disabledMinutes?: (hour: number) => number[];
330
+ /** 禁用秒回调,接收当前选中小时/分钟。 */
331
+ disabledSeconds?: (hour: number, minute: number) => number[];
332
+ /** 占位文本。 @default "请选择时间" */
333
+ placeholder?: string;
334
+ /** 是否禁用。 */
335
+ disabled?: boolean;
336
+ /**
337
+ * 触发器尺寸(对齐 Input)。
338
+ * @default "md"
339
+ */
340
+ size?: 'sm' | 'md' | 'lg';
341
+ /**
342
+ * 错误态(同时设置 `aria-invalid`)。
19
343
  * @default false
20
344
  */
21
- showSeconds?: boolean;
345
+ error?: boolean;
22
346
  /**
23
- * 步长(秒) 影响原生 picker 的滚动粒度,e.g. 60 = 一分钟一档。
24
- * @default 60
347
+ * 显示清除按钮(antd `allowClear` 并集)。有值时显示 X,空值还原为时钟 icon。
348
+ * @default true
25
349
  */
26
- step?: number;
350
+ hasClear?: boolean;
351
+ /** 触发器 className。 */
352
+ className?: string;
353
+ /** 触发器 id(用于 label htmlFor)。 */
354
+ id?: string;
27
355
  /**
28
- * 尺寸。
356
+ * 校验状态(对齐 antd `status`)— `'error'` / `'warning'` / `'default'`。
357
+ * 与 `error` 并存时,`status='error'` 优先。
29
358
  * @default "default"
30
359
  */
31
- size?: 'sm' | 'default' | 'lg';
360
+ status?: 'default' | 'error' | 'warning';
361
+ /**
362
+ * 受控弹层显隐(对齐 antd `open`)。
363
+ * 业务侧需要"程序化打开 / 关闭"时使用;不传则组件内部自管。
364
+ */
365
+ open?: boolean;
366
+ /** 弹层显隐变化回调。 */
367
+ onOpenChange?: (open: boolean) => void;
368
+ /**
369
+ * 输入框只读 — 仅能通过点击弹层选择,不能直接键入(对齐 antd `inputReadOnly`)。
370
+ * @default false
371
+ */
372
+ inputReadOnly?: boolean;
32
373
  }
33
374
 
34
- const sizeMap = {
35
- sm: 'h-8 text-xs',
36
- default: 'h-9 text-sm',
37
- lg: 'h-10 text-base',
38
- } as const;
39
-
40
- /**
41
- * 时间选择 — antd 独有补足。**等价 antd `TimePicker`** 基本能力。
42
- * 当前实现是**原生 `<input type="time">` 的样式封装**(零 JS,移动端友好);
43
- * 复杂场景(范围选择 / disabledTime / 自定义面板)可后续基于 Popover + Listbox 升级。
44
- */
45
375
  const TimePicker = React.forwardRef<HTMLInputElement, TimePickerProps>(
46
376
  (
47
377
  {
48
378
  value,
49
379
  defaultValue,
50
380
  onChange,
51
- showSeconds = false,
52
- step = 60,
53
- size = 'default',
54
- disabled,
381
+ format = 'HH:mm:ss',
382
+ hourStep = 1,
383
+ minuteStep = 1,
384
+ secondStep = 1,
385
+ disabledHours,
386
+ disabledMinutes,
387
+ disabledSeconds,
388
+ placeholder = '请选择时间',
389
+ disabled = false,
390
+ size = 'md',
391
+ error = false,
392
+ hasClear = true,
55
393
  className,
56
- ...props
394
+ id,
395
+ status = 'default',
396
+ open: openProp,
397
+ onOpenChange,
398
+ inputReadOnly = false,
57
399
  },
58
400
  ref,
59
401
  ) => {
402
+ const isControlled = value !== undefined;
403
+ const [internal, setInternal] = React.useState<string>(defaultValue ?? '');
404
+ const current = isControlled ? value ?? '' : internal;
405
+
406
+ // ─── input + draft state ───────────────────────────────────────
407
+ const isOpenControlled = openProp !== undefined;
408
+ const [openInternal, setOpenInternal] = React.useState(false);
409
+ const open = isOpenControlled ? openProp : openInternal;
410
+ const setOpen = (next: boolean) => {
411
+ if (!isOpenControlled) setOpenInternal(next);
412
+ onOpenChange?.(next);
413
+ };
414
+ // draft 是 popover 打开期间的中间态;关闭时若与 current 不同则 commit。
415
+ const [draft, setDraft] = React.useState<TimeParts>(
416
+ () => parseTime(current) ?? ZERO_TIME,
417
+ );
418
+ // touched:用户在 popover 内主动点过 / current 有值
419
+ const [touched, setTouched] = React.useState<boolean>(current.length > 0);
420
+ // input 文本(实时反映 draft 或用户键入)
421
+ const [inputText, setInputText] = React.useState<string>(current);
422
+
423
+ const inputRef = React.useRef<HTMLInputElement>(null);
424
+ const anchorRef = React.useRef<HTMLDivElement>(null);
425
+ React.useImperativeHandle(ref, () => inputRef.current!);
426
+
427
+ // 外部 current 变化时,非 focus 状态下同步 input text + draft
428
+ React.useEffect(() => {
429
+ if (document.activeElement !== inputRef.current) {
430
+ setInputText(current);
431
+ setDraft(parseTime(current) ?? ZERO_TIME);
432
+ setTouched(current.length > 0);
433
+ }
434
+ }, [current]);
435
+
436
+ // 打开 popover 时重置 draft / touched / hovered
437
+ React.useEffect(() => {
438
+ if (open) {
439
+ setDraft(parseTime(current) ?? ZERO_TIME);
440
+ setTouched(current.length > 0);
441
+ setInputText(current);
442
+ } else {
443
+ // popover 关闭时统一 commit(若 touched 且 draft 与 current 不同)
444
+ if (touched) {
445
+ const str = formatTime(draft, format);
446
+ if (str !== current) {
447
+ if (!isControlled) setInternal(str);
448
+ onChange?.(str);
449
+ }
450
+ }
451
+ }
452
+ // 仅在 popover 开关瞬间同步 draft / 兜底 commit,故意只依赖 open。
453
+ }, [open]);
454
+
455
+ // ─── handlers ───────────────────────────────────────────────────
456
+ const handlePanelChange = (next: TimeParts) => {
457
+ setDraft(next);
458
+ setTouched(true);
459
+ setInputText(formatTime(next, format));
460
+ };
461
+
462
+ const handleInputBlur = () => {
463
+ const trimmed = inputText.trim();
464
+ if (trimmed === '') {
465
+ // 清空 input → 标记未 touched,关闭时 commit "" 给外部
466
+ setTouched(false);
467
+ setDraft(ZERO_TIME);
468
+ return;
469
+ }
470
+ const parsed = parseTime(trimmed);
471
+ if (parsed) {
472
+ setDraft(parsed);
473
+ setTouched(true);
474
+ setInputText(formatTime(parsed, format));
475
+ } else {
476
+ // 非法 → 清空
477
+ setInputText('');
478
+ setTouched(false);
479
+ setDraft(ZERO_TIME);
480
+ }
481
+ };
482
+
483
+ const handleClear = (e: React.MouseEvent) => {
484
+ e.preventDefault();
485
+ setInputText('');
486
+ setDraft(ZERO_TIME);
487
+ setTouched(false);
488
+ if (!isControlled) setInternal('');
489
+ onChange?.('');
490
+ inputRef.current?.focus();
491
+ };
492
+
493
+ const hasValue = current.length > 0;
494
+ // hasClear:有值显示 X,空值还原为时钟 icon。默认 true,可传 hasClear={false} 关闭。
495
+ const showClear = hasClear && hasValue && !disabled;
496
+
60
497
  return (
61
- <div
62
- className={cn(
63
- 'relative inline-flex w-fit items-center',
64
- disabled && 'cursor-not-allowed opacity-50',
65
- )}
66
- >
67
- <Clock
68
- aria-hidden="true"
69
- className="pointer-events-none absolute left-3 size-4 text-muted-foreground"
70
- />
71
- <input
72
- ref={ref}
73
- type="time"
74
- value={value}
75
- defaultValue={defaultValue}
76
- step={showSeconds ? step : undefined}
77
- disabled={disabled}
78
- onChange={(e) => onChange?.(e.target.value)}
79
- className={cn(
80
- 'w-full rounded-md border border-input bg-background pl-9 pr-3 text-foreground shadow-sm',
81
- 'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring',
82
- 'disabled:cursor-not-allowed',
83
- sizeMap[size],
84
- className,
85
- )}
86
- {...props}
87
- />
88
- </div>
498
+ <Popover open={open} onOpenChange={setOpen}>
499
+ <PopoverAnchor asChild>
500
+ <div
501
+ ref={anchorRef}
502
+ aria-invalid={error || status === 'error' ? true : undefined}
503
+ className={cn(
504
+ triggerWrapperClass,
505
+ triggerSizeClass[size],
506
+ 'w-40',
507
+ (error || status === 'error') &&
508
+ 'border-destructive aria-invalid:border-destructive',
509
+ status === 'warning' && !error && 'border-warning',
510
+ className,
511
+ )}
512
+ >
513
+ <input
514
+ ref={inputRef}
515
+ id={id}
516
+ type="text"
517
+ value={inputText}
518
+ onChange={(e) => setInputText(e.target.value)}
519
+ onFocus={() => setOpen(true)}
520
+ onBlur={handleInputBlur}
521
+ placeholder={placeholder}
522
+ disabled={disabled}
523
+ readOnly={inputReadOnly}
524
+ aria-invalid={error || status === 'error' ? true : undefined}
525
+ className={inputElementClass}
526
+ />
527
+ {showClear ? (
528
+ <button
529
+ type="button"
530
+ aria-label="清除时间"
531
+ tabIndex={-1}
532
+ onMouseDown={handleClear}
533
+ className="flex shrink-0 cursor-pointer items-center text-muted-foreground transition-colors hover:text-foreground"
534
+ >
535
+ <X className="size-4" />
536
+ </button>
537
+ ) : (
538
+ <Clock
539
+ aria-hidden
540
+ className="size-4 shrink-0 text-muted-foreground"
541
+ />
542
+ )}
543
+ </div>
544
+ </PopoverAnchor>
545
+ <PopoverContent
546
+ align="start"
547
+ sideOffset={6}
548
+ className="w-auto rounded-lg border-border bg-popover p-0 shadow-md"
549
+ onOpenAutoFocus={(e) => e.preventDefault()}
550
+ onInteractOutside={(e) => {
551
+ const target = e.detail.originalEvent.target as Node | null;
552
+ if (target && anchorRef.current?.contains(target)) {
553
+ e.preventDefault();
554
+ }
555
+ }}
556
+ >
557
+ <TimePickerPanel
558
+ value={draft}
559
+ onChange={handlePanelChange}
560
+ format={format}
561
+ hourStep={hourStep}
562
+ minuteStep={minuteStep}
563
+ secondStep={secondStep}
564
+ disabledHours={disabledHours}
565
+ disabledMinutes={disabledMinutes}
566
+ disabledSeconds={disabledSeconds}
567
+ hasSelection={touched}
568
+ />
569
+ </PopoverContent>
570
+ </Popover>
89
571
  );
90
572
  },
91
573
  );
92
574
  TimePicker.displayName = 'TimePicker';
93
575
 
94
- export { TimePicker };
576
+ // ─── TimeRangePicker(双值) ──────────────────────────────────────────────
577
+
578
+ export type TimeRangeValue = [string, string];
579
+
580
+ export interface TimeRangePickerProps
581
+ extends Omit<TimePickerProps, 'value' | 'defaultValue' | 'onChange'> {
582
+ /** 受控值(`[起始, 结束]`)。空串表示对应端未选。 */
583
+ value?: TimeRangeValue;
584
+ /** 非受控初值。 */
585
+ defaultValue?: TimeRangeValue;
586
+ /** 值变化回调(点击「确定」时触发)。 */
587
+ onChange?: (value: TimeRangeValue) => void;
588
+ /**
589
+ * 起始 / 结束占位文本。
590
+ * @default ["请选择时间","请选择时间"]
591
+ */
592
+ placeholders?: [string, string];
593
+ }
594
+
595
+ const EMPTY_RANGE: TimeRangeValue = ['', ''];
596
+
597
+ const TimeRangePicker = React.forwardRef<
598
+ HTMLInputElement,
599
+ TimeRangePickerProps
600
+ >(
601
+ (
602
+ {
603
+ value,
604
+ defaultValue,
605
+ onChange,
606
+ format = 'HH:mm:ss',
607
+ hourStep = 1,
608
+ minuteStep = 1,
609
+ secondStep = 1,
610
+ disabledHours,
611
+ disabledMinutes,
612
+ disabledSeconds,
613
+ placeholders = ['请选择时间', '请选择时间'],
614
+ disabled = false,
615
+ size = 'md',
616
+ error = false,
617
+ hasClear = true,
618
+ className,
619
+ id,
620
+ status = 'default',
621
+ open: openProp,
622
+ onOpenChange,
623
+ inputReadOnly = false,
624
+ },
625
+ ref,
626
+ ) => {
627
+ const isControlled = value !== undefined;
628
+ const [internal, setInternal] = React.useState<TimeRangeValue>(
629
+ defaultValue ?? EMPTY_RANGE,
630
+ );
631
+ const current = (isControlled ? value : internal) ?? EMPTY_RANGE;
632
+
633
+ const isOpenControlled = openProp !== undefined;
634
+ const [openInternal, setOpenInternal] = React.useState(false);
635
+ const open = isOpenControlled ? openProp : openInternal;
636
+ const setOpen = (next: boolean) => {
637
+ if (!isOpenControlled) setOpenInternal(next);
638
+ onOpenChange?.(next);
639
+ };
640
+ const [draft, setDraft] = React.useState<[TimeParts, TimeParts]>(() => [
641
+ parseTime(current[0]) ?? ZERO_TIME,
642
+ parseTime(current[1]) ?? ZERO_TIME,
643
+ ]);
644
+ const [touched, setTouched] = React.useState<[boolean, boolean]>(() => [
645
+ current[0].length > 0,
646
+ current[1].length > 0,
647
+ ]);
648
+
649
+ // ─── 双 input refs + text state ───────────────────────────────
650
+ const fromInputRef = React.useRef<HTMLInputElement>(null);
651
+ const toInputRef = React.useRef<HTMLInputElement>(null);
652
+ const anchorRef = React.useRef<HTMLDivElement>(null);
653
+ React.useImperativeHandle(ref, () => fromInputRef.current!);
654
+ const [fromText, setFromText] = React.useState<string>(current[0]);
655
+ const [toText, setToText] = React.useState<string>(current[1]);
656
+ // confirm 标志:避免 popover 关闭 commit 与 confirm commit 重复触发
657
+ const confirmedRef = React.useRef(false);
658
+
659
+ // 外部 current 变化时,非 focus 同步 input text + draft
660
+ React.useEffect(() => {
661
+ const active = document.activeElement;
662
+ if (active !== fromInputRef.current) setFromText(current[0]);
663
+ if (active !== toInputRef.current) setToText(current[1]);
664
+ // 按元素值跟踪,故意不依赖整个 current 数组引用。
665
+ }, [current[0], current[1]]);
666
+
667
+ // 打开 popover 时重置 draft / touched / input text
668
+ React.useEffect(() => {
669
+ if (open) {
670
+ setDraft([
671
+ parseTime(current[0]) ?? ZERO_TIME,
672
+ parseTime(current[1]) ?? ZERO_TIME,
673
+ ]);
674
+ setTouched([current[0].length > 0, current[1].length > 0]);
675
+ setFromText(current[0]);
676
+ setToText(current[1]);
677
+ confirmedRef.current = false;
678
+ } else {
679
+ // popover 关闭兜底:若未通过 confirm 关闭 且任一段 touched → commit(未 touched 段输出空)
680
+ if (!confirmedRef.current && (touched[0] || touched[1])) {
681
+ const next: TimeRangeValue = [
682
+ touched[0] ? formatTime(draft[0], format) : '',
683
+ touched[1] ? formatTime(draft[1], format) : '',
684
+ ];
685
+ if (next[0] !== current[0] || next[1] !== current[1]) {
686
+ if (!isControlled) setInternal(next);
687
+ onChange?.(next);
688
+ }
689
+ }
690
+ }
691
+ // 仅在 popover 开关瞬间同步 draft / 兜底 commit,故意只依赖 open。
692
+ }, [open]);
693
+
694
+ const startHas = current[0].length > 0;
695
+ const endHas = current[1].length > 0;
696
+ const hasAny = startHas || endHas;
697
+
698
+ // ─── 双向 from/to 约束 ───────────────────────────────────────
699
+ // end disable < start,start disable > end(若 end touched)。
700
+ const startParts = draft[0];
701
+ const endParts = draft[1];
702
+
703
+ const endDisabledHours = () => {
704
+ const set = new Set(disabledHours?.() ?? []);
705
+ for (let h = 0; h < startParts.h; h++) set.add(h);
706
+ return [...set];
707
+ };
708
+ const endDisabledMinutes = (hour: number) => {
709
+ const set = new Set(disabledMinutes?.(hour) ?? []);
710
+ if (hour === startParts.h) {
711
+ for (let m = 0; m < startParts.m; m++) set.add(m);
712
+ }
713
+ return [...set];
714
+ };
715
+ const endDisabledSeconds = (hour: number, minute: number) => {
716
+ const set = new Set(disabledSeconds?.(hour, minute) ?? []);
717
+ if (hour === startParts.h && minute === startParts.m) {
718
+ for (let s = 0; s < startParts.s; s++) set.add(s);
719
+ }
720
+ return [...set];
721
+ };
722
+
723
+ const startDisabledHours = () => {
724
+ const set = new Set(disabledHours?.() ?? []);
725
+ if (touched[1]) {
726
+ for (let h = endParts.h + 1; h <= 23; h++) set.add(h);
727
+ }
728
+ return [...set];
729
+ };
730
+ const startDisabledMinutes = (hour: number) => {
731
+ const set = new Set(disabledMinutes?.(hour) ?? []);
732
+ if (touched[1] && hour === endParts.h) {
733
+ for (let m = endParts.m + 1; m <= 59; m++) set.add(m);
734
+ }
735
+ return [...set];
736
+ };
737
+ const startDisabledSeconds = (hour: number, minute: number) => {
738
+ const set = new Set(disabledSeconds?.(hour, minute) ?? []);
739
+ if (touched[1] && hour === endParts.h && minute === endParts.m) {
740
+ for (let s = endParts.s + 1; s <= 59; s++) set.add(s);
741
+ }
742
+ return [...set];
743
+ };
744
+
745
+ const partsToSec = (p: TimeParts) => p.h * 3600 + p.m * 60 + p.s;
746
+
747
+ const handleStartChange = (next: TimeParts) => {
748
+ const nextSec = partsToSec(next);
749
+ const endSec = partsToSec(draft[1]);
750
+ const endClamped = touched[1] && endSec < nextSec;
751
+ setDraft([next, endClamped ? next : draft[1]]);
752
+ setTouched([true, touched[1] || endClamped]);
753
+ setFromText(formatTime(next, format));
754
+ if (endClamped) setToText(formatTime(next, format));
755
+ };
756
+
757
+ const handleEndChange = (next: TimeParts) => {
758
+ const nextSec = partsToSec(next);
759
+ const startSec = partsToSec(draft[0]);
760
+ const startClamped = touched[0] && startSec > nextSec;
761
+ setDraft([startClamped ? next : draft[0], next]);
762
+ setTouched([touched[0] || startClamped, true]);
763
+ setToText(formatTime(next, format));
764
+ if (startClamped) setFromText(formatTime(next, format));
765
+ };
766
+
767
+ const handleFromInputBlur = () => {
768
+ const trimmed = fromText.trim();
769
+ if (trimmed === '') {
770
+ setTouched([false, touched[1]]);
771
+ setDraft([ZERO_TIME, draft[1]]);
772
+ return;
773
+ }
774
+ const parsed = parseTime(trimmed);
775
+ if (parsed) {
776
+ handleStartChange(parsed);
777
+ } else {
778
+ setFromText('');
779
+ setTouched([false, touched[1]]);
780
+ setDraft([ZERO_TIME, draft[1]]);
781
+ }
782
+ };
783
+
784
+ const handleToInputBlur = () => {
785
+ const trimmed = toText.trim();
786
+ if (trimmed === '') {
787
+ setTouched([touched[0], false]);
788
+ setDraft([draft[0], ZERO_TIME]);
789
+ return;
790
+ }
791
+ const parsed = parseTime(trimmed);
792
+ if (parsed) {
793
+ handleEndChange(parsed);
794
+ } else {
795
+ setToText('');
796
+ setTouched([touched[0], false]);
797
+ setDraft([draft[0], ZERO_TIME]);
798
+ }
799
+ };
800
+
801
+ const handleConfirm = () => {
802
+ const next: TimeRangeValue = [
803
+ touched[0] ? formatTime(draft[0], format) : '',
804
+ touched[1] ? formatTime(draft[1], format) : '',
805
+ ];
806
+ confirmedRef.current = true;
807
+ if (!isControlled) setInternal(next);
808
+ onChange?.(next);
809
+ setOpen(false);
810
+ };
811
+
812
+ const handleClear = (e: React.MouseEvent) => {
813
+ e.preventDefault();
814
+ setFromText('');
815
+ setToText('');
816
+ setDraft([ZERO_TIME, ZERO_TIME]);
817
+ setTouched([false, false]);
818
+ if (!isControlled) setInternal(EMPTY_RANGE);
819
+ onChange?.(EMPTY_RANGE);
820
+ fromInputRef.current?.focus();
821
+ };
822
+
823
+ // hasClear:有值显示 X,空值还原为时钟 icon。默认 true,可传 hasClear={false} 关闭。
824
+ const showClear = hasClear && hasAny && !disabled;
825
+
826
+ return (
827
+ <Popover open={open} onOpenChange={setOpen}>
828
+ <PopoverAnchor asChild>
829
+ <div
830
+ ref={anchorRef}
831
+ aria-invalid={error || status === 'error' ? true : undefined}
832
+ className={cn(
833
+ triggerWrapperClass,
834
+ triggerSizeClass[size],
835
+ 'w-72',
836
+ (error || status === 'error') &&
837
+ 'border-destructive aria-invalid:border-destructive',
838
+ status === 'warning' && !error && 'border-warning',
839
+ className,
840
+ )}
841
+ >
842
+ <input
843
+ ref={fromInputRef}
844
+ id={id}
845
+ type="text"
846
+ value={fromText}
847
+ onChange={(e) => setFromText(e.target.value)}
848
+ onFocus={() => setOpen(true)}
849
+ onBlur={handleFromInputBlur}
850
+ placeholder={placeholders[0]}
851
+ disabled={disabled}
852
+ readOnly={inputReadOnly}
853
+ aria-invalid={error || status === 'error' ? true : undefined}
854
+ className={cn(inputElementClass, 'text-center')}
855
+ />
856
+ <span aria-hidden className="shrink-0 text-muted-foreground">
857
+ -
858
+ </span>
859
+ <input
860
+ ref={toInputRef}
861
+ type="text"
862
+ value={toText}
863
+ onChange={(e) => setToText(e.target.value)}
864
+ onFocus={() => setOpen(true)}
865
+ onBlur={handleToInputBlur}
866
+ placeholder={placeholders[1]}
867
+ disabled={disabled}
868
+ readOnly={inputReadOnly}
869
+ aria-invalid={error || status === 'error' ? true : undefined}
870
+ className={cn(inputElementClass, 'text-center')}
871
+ />
872
+ {showClear ? (
873
+ <button
874
+ type="button"
875
+ aria-label="清除时间"
876
+ tabIndex={-1}
877
+ onMouseDown={handleClear}
878
+ className="flex shrink-0 cursor-pointer items-center text-muted-foreground transition-colors hover:text-foreground"
879
+ >
880
+ <X className="size-4" />
881
+ </button>
882
+ ) : (
883
+ <Clock
884
+ aria-hidden
885
+ className="size-4 shrink-0 text-muted-foreground"
886
+ />
887
+ )}
888
+ </div>
889
+ </PopoverAnchor>
890
+ <PopoverContent
891
+ align="start"
892
+ sideOffset={6}
893
+ className="w-auto rounded-lg border-border bg-popover p-0 shadow-md"
894
+ onOpenAutoFocus={(e) => e.preventDefault()}
895
+ onInteractOutside={(e) => {
896
+ const target = e.detail.originalEvent.target as Node | null;
897
+ if (target && anchorRef.current?.contains(target)) {
898
+ e.preventDefault();
899
+ }
900
+ }}
901
+ >
902
+ <div className="flex">
903
+ <TimePickerPanel
904
+ value={draft[0]}
905
+ onChange={handleStartChange}
906
+ format={format}
907
+ hourStep={hourStep}
908
+ minuteStep={minuteStep}
909
+ secondStep={secondStep}
910
+ disabledHours={startDisabledHours}
911
+ disabledMinutes={startDisabledMinutes}
912
+ disabledSeconds={startDisabledSeconds}
913
+ hasSelection={touched[0]}
914
+ className="px-2"
915
+ />
916
+ <TimePickerPanel
917
+ value={draft[1]}
918
+ onChange={handleEndChange}
919
+ format={format}
920
+ hourStep={hourStep}
921
+ minuteStep={minuteStep}
922
+ secondStep={secondStep}
923
+ disabledHours={endDisabledHours}
924
+ disabledMinutes={endDisabledMinutes}
925
+ disabledSeconds={endDisabledSeconds}
926
+ hasSelection={touched[1]}
927
+ className="px-2"
928
+ />
929
+ </div>
930
+ <div className="flex justify-end border-t border-border px-3 py-2">
931
+ <Button
932
+ size="sm"
933
+ onClick={handleConfirm}
934
+ disabled={!touched[0] && !touched[1]}
935
+ >
936
+ 确定
937
+ </Button>
938
+ </div>
939
+ </PopoverContent>
940
+ </Popover>
941
+ );
942
+ },
943
+ );
944
+ TimeRangePicker.displayName = 'TimeRangePicker';
945
+
946
+ export { TimePicker, TimeRangePicker };