@agile-plus/headless 0.1.2 → 0.2.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 (231) hide show
  1. package/README.md +24 -17
  2. package/dist/index.cjs +4163 -223
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +33 -0
  5. package/dist/index.d.ts +33 -0
  6. package/dist/index.js +4258 -216
  7. package/dist/index.js.map +1 -1
  8. package/dist/types-BxsQI7P5.d.cts +93 -0
  9. package/dist/types-BxsQI7P5.d.ts +93 -0
  10. package/dist/types-zpeY0yvD.d.cts +73 -0
  11. package/dist/types-zpeY0yvD.d.ts +73 -0
  12. package/dist/useAccordion/index.cjs +149 -0
  13. package/dist/useAccordion/index.cjs.map +1 -0
  14. package/dist/useAccordion/index.d.cts +119 -0
  15. package/dist/useAccordion/index.d.ts +119 -0
  16. package/dist/useAccordion/index.js +128 -0
  17. package/dist/useAccordion/index.js.map +1 -0
  18. package/dist/useAvatar/index.cjs +135 -0
  19. package/dist/useAvatar/index.cjs.map +1 -0
  20. package/dist/useAvatar/index.d.cts +120 -0
  21. package/dist/useAvatar/index.d.ts +120 -0
  22. package/dist/useAvatar/index.js +106 -0
  23. package/dist/useAvatar/index.js.map +1 -0
  24. package/dist/useCheckbox/index.cjs +3 -1
  25. package/dist/useCheckbox/index.cjs.map +1 -1
  26. package/dist/useCheckbox/index.js +3 -1
  27. package/dist/useCheckbox/index.js.map +1 -1
  28. package/dist/useCollapsible/index.cjs +10 -0
  29. package/dist/useCollapsible/index.cjs.map +1 -1
  30. package/dist/useCollapsible/index.js +11 -1
  31. package/dist/useCollapsible/index.js.map +1 -1
  32. package/dist/useDatePicker/index.cjs +684 -0
  33. package/dist/useDatePicker/index.cjs.map +1 -0
  34. package/dist/useDatePicker/index.d.cts +191 -0
  35. package/dist/useDatePicker/index.d.ts +191 -0
  36. package/dist/useDatePicker/index.js +671 -0
  37. package/dist/useDatePicker/index.js.map +1 -0
  38. package/dist/useDateRangePicker/index.cjs +614 -0
  39. package/dist/useDateRangePicker/index.cjs.map +1 -0
  40. package/dist/useDateRangePicker/index.d.cts +159 -0
  41. package/dist/useDateRangePicker/index.d.ts +159 -0
  42. package/dist/useDateRangePicker/index.js +601 -0
  43. package/dist/useDateRangePicker/index.js.map +1 -0
  44. package/dist/useDialog/index.cjs.map +1 -1
  45. package/dist/useDialog/index.js.map +1 -1
  46. package/dist/useDropdownMenu/index.cjs +468 -0
  47. package/dist/useDropdownMenu/index.cjs.map +1 -0
  48. package/dist/useDropdownMenu/index.d.cts +73 -0
  49. package/dist/useDropdownMenu/index.d.ts +73 -0
  50. package/dist/useDropdownMenu/index.js +441 -0
  51. package/dist/useDropdownMenu/index.js.map +1 -0
  52. package/dist/useEllipsis/index.cjs +181 -0
  53. package/dist/useEllipsis/index.cjs.map +1 -0
  54. package/dist/useEllipsis/index.d.cts +67 -0
  55. package/dist/useEllipsis/index.d.ts +67 -0
  56. package/dist/useEllipsis/index.js +162 -0
  57. package/dist/useEllipsis/index.js.map +1 -0
  58. package/dist/useFloating/index.cjs +177 -0
  59. package/dist/useFloating/index.cjs.map +1 -0
  60. package/dist/useFloating/index.d.cts +21 -0
  61. package/dist/useFloating/index.d.ts +21 -0
  62. package/dist/useFloating/index.js +150 -0
  63. package/dist/useFloating/index.js.map +1 -0
  64. package/dist/useFormControl/index.cjs +5 -0
  65. package/dist/useFormControl/index.cjs.map +1 -1
  66. package/dist/useFormControl/index.js +5 -0
  67. package/dist/useFormControl/index.js.map +1 -1
  68. package/dist/useInput/index.cjs +192 -0
  69. package/dist/useInput/index.cjs.map +1 -0
  70. package/dist/useInput/index.d.cts +41 -0
  71. package/dist/useInput/index.d.ts +41 -0
  72. package/dist/useInput/index.js +165 -0
  73. package/dist/useInput/index.js.map +1 -0
  74. package/dist/useKeepMounted/index.cjs +44 -0
  75. package/dist/useKeepMounted/index.cjs.map +1 -0
  76. package/dist/useKeepMounted/index.d.cts +45 -0
  77. package/dist/useKeepMounted/index.d.ts +45 -0
  78. package/dist/useKeepMounted/index.js +17 -0
  79. package/dist/useKeepMounted/index.js.map +1 -0
  80. package/dist/useMenu/index.cjs +438 -0
  81. package/dist/useMenu/index.cjs.map +1 -0
  82. package/dist/useMenu/index.d.cts +134 -0
  83. package/dist/useMenu/index.d.ts +134 -0
  84. package/dist/useMenu/index.js +419 -0
  85. package/dist/useMenu/index.js.map +1 -0
  86. package/dist/usePagination/index.cjs +113 -0
  87. package/dist/usePagination/index.cjs.map +1 -0
  88. package/dist/usePagination/index.d.cts +139 -0
  89. package/dist/usePagination/index.d.ts +139 -0
  90. package/dist/usePagination/index.js +95 -0
  91. package/dist/usePagination/index.js.map +1 -0
  92. package/dist/usePopover/index.cjs +312 -0
  93. package/dist/usePopover/index.cjs.map +1 -0
  94. package/dist/usePopover/index.d.cts +71 -0
  95. package/dist/usePopover/index.d.ts +71 -0
  96. package/dist/usePopover/index.js +285 -0
  97. package/dist/usePopover/index.js.map +1 -0
  98. package/dist/useSelect/index.cjs +729 -0
  99. package/dist/useSelect/index.cjs.map +1 -0
  100. package/dist/useSelect/index.d.cts +193 -0
  101. package/dist/useSelect/index.d.ts +193 -0
  102. package/dist/useSelect/index.js +710 -0
  103. package/dist/useSelect/index.js.map +1 -0
  104. package/dist/useSplitter/index.cjs +558 -0
  105. package/dist/useSplitter/index.cjs.map +1 -0
  106. package/dist/useSplitter/index.d.cts +307 -0
  107. package/dist/useSplitter/index.d.ts +307 -0
  108. package/dist/useSplitter/index.js +541 -0
  109. package/dist/useSplitter/index.js.map +1 -0
  110. package/dist/useSwitch/index.cjs +4 -1
  111. package/dist/useSwitch/index.cjs.map +1 -1
  112. package/dist/useSwitch/index.js +4 -1
  113. package/dist/useSwitch/index.js.map +1 -1
  114. package/dist/useTable/index.cjs +308 -0
  115. package/dist/useTable/index.cjs.map +1 -0
  116. package/dist/useTable/index.d.cts +242 -0
  117. package/dist/useTable/index.d.ts +242 -0
  118. package/dist/useTable/index.js +301 -0
  119. package/dist/useTable/index.js.map +1 -0
  120. package/dist/useTabs/index.cjs +13 -4
  121. package/dist/useTabs/index.cjs.map +1 -1
  122. package/dist/useTabs/index.d.cts +5 -1
  123. package/dist/useTabs/index.d.ts +5 -1
  124. package/dist/useTabs/index.js +13 -4
  125. package/dist/useTabs/index.js.map +1 -1
  126. package/dist/useTextControl-Dcq0GFmg.d.cts +63 -0
  127. package/dist/useTextControl-Dcq0GFmg.d.ts +63 -0
  128. package/dist/useTextarea/index.cjs +186 -0
  129. package/dist/useTextarea/index.cjs.map +1 -0
  130. package/dist/useTextarea/index.d.cts +30 -0
  131. package/dist/useTextarea/index.d.ts +30 -0
  132. package/dist/useTextarea/index.js +159 -0
  133. package/dist/useTextarea/index.js.map +1 -0
  134. package/dist/useToast/index.cjs +27 -20
  135. package/dist/useToast/index.cjs.map +1 -1
  136. package/dist/useToast/index.js +27 -20
  137. package/dist/useToast/index.js.map +1 -1
  138. package/dist/useToastViewport/index.cjs +1 -3
  139. package/dist/useToastViewport/index.cjs.map +1 -1
  140. package/dist/useToastViewport/index.js +1 -3
  141. package/dist/useToastViewport/index.js.map +1 -1
  142. package/dist/useTooltip/index.cjs +357 -0
  143. package/dist/useTooltip/index.cjs.map +1 -0
  144. package/dist/useTooltip/index.d.cts +81 -0
  145. package/dist/useTooltip/index.d.ts +81 -0
  146. package/dist/useTooltip/index.js +338 -0
  147. package/dist/useTooltip/index.js.map +1 -0
  148. package/dist/useVirtualList/index.cjs +308 -0
  149. package/dist/useVirtualList/index.cjs.map +1 -0
  150. package/dist/useVirtualList/index.d.cts +31 -0
  151. package/dist/useVirtualList/index.d.ts +31 -0
  152. package/dist/useVirtualList/index.js +288 -0
  153. package/dist/useVirtualList/index.js.map +1 -0
  154. package/dist/useVirtualTree/index.cjs +520 -0
  155. package/dist/useVirtualTree/index.cjs.map +1 -0
  156. package/dist/useVirtualTree/index.d.cts +146 -0
  157. package/dist/useVirtualTree/index.d.ts +146 -0
  158. package/dist/useVirtualTree/index.js +511 -0
  159. package/dist/useVirtualTree/index.js.map +1 -0
  160. package/package.json +121 -2
  161. package/src/index.ts +38 -0
  162. package/src/useAccordion/index.ts +7 -0
  163. package/src/useAccordion/types.ts +118 -0
  164. package/src/useAccordion/useAccordion.ts +130 -0
  165. package/src/useAvatar/index.ts +14 -0
  166. package/src/useAvatar/types.ts +90 -0
  167. package/src/useAvatar/useAvatar.ts +39 -0
  168. package/src/useAvatar/useAvatarFallback.ts +62 -0
  169. package/src/useAvatar/useAvatarImage.ts +78 -0
  170. package/src/useCheckbox/useCheckbox.ts +4 -1
  171. package/src/useCollapsible/useCollapsible.ts +22 -1
  172. package/src/useDatePicker/index.ts +9 -0
  173. package/src/useDatePicker/types.ts +177 -0
  174. package/src/useDatePicker/useDatePicker.ts +688 -0
  175. package/src/useDateRangePicker/index.ts +8 -0
  176. package/src/useDateRangePicker/types.ts +146 -0
  177. package/src/useDateRangePicker/useDateRangePicker.ts +618 -0
  178. package/src/useDropdownMenu/index.ts +3 -0
  179. package/src/useDropdownMenu/types.ts +61 -0
  180. package/src/useDropdownMenu/useDropdownMenu.ts +322 -0
  181. package/src/useEllipsis/index.ts +3 -0
  182. package/src/useEllipsis/types.ts +51 -0
  183. package/src/useEllipsis/useEllipsis.ts +169 -0
  184. package/src/useFloating/index.ts +3 -0
  185. package/src/useFloating/types.ts +79 -0
  186. package/src/useFloating/useFloating.ts +199 -0
  187. package/src/useFormControl/useFormControl.ts +13 -0
  188. package/src/useInput/index.ts +3 -0
  189. package/src/useInput/types.ts +29 -0
  190. package/src/useInput/useInput.ts +40 -0
  191. package/src/useKeepMounted/index.ts +3 -0
  192. package/src/useKeepMounted/types.ts +29 -0
  193. package/src/useKeepMounted/useKeepMounted.ts +34 -0
  194. package/src/useMenu/index.ts +9 -0
  195. package/src/useMenu/types.ts +126 -0
  196. package/src/useMenu/useMenu.ts +504 -0
  197. package/src/usePagination/index.ts +3 -0
  198. package/src/usePagination/types.ts +137 -0
  199. package/src/usePagination/usePagination.ts +109 -0
  200. package/src/usePopover/index.ts +3 -0
  201. package/src/usePopover/types.ts +65 -0
  202. package/src/usePopover/usePopover.ts +157 -0
  203. package/src/useSelect/index.ts +9 -0
  204. package/src/useSelect/types.ts +184 -0
  205. package/src/useSelect/useSelect.ts +713 -0
  206. package/src/useSplitter/index.ts +10 -0
  207. package/src/useSplitter/types.ts +336 -0
  208. package/src/useSplitter/useSplitter.ts +648 -0
  209. package/src/useSplitter/useSplitterPanel.ts +106 -0
  210. package/src/useSwitch/useSwitch.ts +6 -1
  211. package/src/useTable/index.ts +3 -0
  212. package/src/useTable/types.ts +282 -0
  213. package/src/useTable/useTable.ts +407 -0
  214. package/src/useTabs/types.ts +5 -1
  215. package/src/useTabs/useTabs.ts +30 -6
  216. package/src/useTextarea/index.ts +3 -0
  217. package/src/useTextarea/types.ts +17 -0
  218. package/src/useTextarea/useTextarea.ts +34 -0
  219. package/src/useToast/useToast.ts +43 -26
  220. package/src/useToastViewport/useToastViewport.ts +10 -9
  221. package/src/useTooltip/index.ts +3 -0
  222. package/src/useTooltip/types.ts +72 -0
  223. package/src/useTooltip/useTooltip.ts +240 -0
  224. package/src/useVirtualList/index.ts +8 -0
  225. package/src/useVirtualList/types.ts +95 -0
  226. package/src/useVirtualList/useVirtualList.ts +478 -0
  227. package/src/useVirtualTree/index.ts +3 -0
  228. package/src/useVirtualTree/types.ts +179 -0
  229. package/src/useVirtualTree/useVirtualTree.ts +342 -0
  230. package/src/utils/useFocusTrap.ts +2 -0
  231. package/src/utils/useTextControl.ts +191 -0
@@ -0,0 +1,618 @@
1
+ import { computed, onScopeDispose, ref, toValue, useId, watch } from 'vue';
2
+
3
+ import {
4
+ addMonths,
5
+ emptyRangeDraft,
6
+ formatDate,
7
+ getCalendarGrid,
8
+ getRangeHighlight,
9
+ getWeekdayLabels,
10
+ isSameDay,
11
+ isSameMonth,
12
+ parseDate,
13
+ resolveDateMove,
14
+ resolveDatePickerKeyIntent,
15
+ resolveRangePick,
16
+ startOfDay,
17
+ } from '@agile-plus/primitives/date';
18
+
19
+ import { useControllable } from '../utils/useControllable';
20
+ import { isBrowser } from '../utils/env';
21
+ import { useFloating } from '../useFloating';
22
+
23
+ import type { CSSProperties } from 'vue';
24
+
25
+ import type {
26
+ DateCell,
27
+ DateMove,
28
+ DateRangeCellState,
29
+ DateRangeDraft,
30
+ } from '@agile-plus/primitives/date';
31
+
32
+ import type {
33
+ DateRangeSide,
34
+ DateRangeViewUnit,
35
+ UseDateRangePickerOptions,
36
+ UseDateRangePickerReturn,
37
+ } from './types';
38
+
39
+ /** 面板与控件之间的缺省间距,与单选 DatePicker 一致。 */
40
+ const DEFAULT_OFFSET = 4;
41
+
42
+ /** 面板与视口边缘的缺省留白。 */
43
+ const DEFAULT_PADDING = 8;
44
+
45
+ const DEFAULT_VALUE_FORMAT = 'YYYY-MM-DD';
46
+
47
+ const DEFAULT_FIRST_DAY_OF_WEEK = 1;
48
+
49
+ const DAYS_PER_WEEK = 7;
50
+
51
+ /**
52
+ * DateRangePicker 的 Vue 适配。
53
+ *
54
+ * 与单选 DatePicker 共享同一套日历 / 格式 / 按键原语,差异集中在三处:
55
+ * **两个端点**(值与输入草稿都是二元组)、**活跃端**(点选与键盘落在哪一端)、
56
+ * **悬停预览**(等待终点时起点 + 悬停日期构成预览范围)。
57
+ *
58
+ * 焦点模型不变:焦点始终留在活跃输入框上,面板用 aria-activedescendant 指向
59
+ * 焦点格。左右两月网格含相同的补齐日期,格子 id 带 side 才唯一;键盘焦点恒在
60
+ * 左面板(焦点跨月时左面板跟着翻),右面板只做指针选择。
61
+ *
62
+ * 点选流程:起点端记录起点并切到终点端;终点端早于起点时交换两端;两端确定
63
+ * 即提交并关闭。未完成时外部点击 / Escape 丢弃草稿,已输入文本按 blur 规则提交。
64
+ */
65
+ export function useDateRangePicker(
66
+ options: UseDateRangePickerOptions = {}
67
+ ): UseDateRangePickerReturn {
68
+ const uid = useId();
69
+ const triggerIds = [`date-range-trigger-${uid}-0`, `date-range-trigger-${uid}-1`];
70
+ const contentId = `date-range-content-${uid}`;
71
+
72
+ const valueFormat = options.valueFormat ?? DEFAULT_VALUE_FORMAT;
73
+ const format = options.format ?? DEFAULT_VALUE_FORMAT;
74
+
75
+ const valueState = useControllable<[string, string] | null>({
76
+ value: () => toValue(options.modelValue),
77
+ defaultValue: options.defaultValue ?? null,
78
+ onChange: (next) => options.onValueChange?.(next),
79
+ });
80
+
81
+ const openState = useControllable<boolean>({
82
+ value: () => toValue(options.open),
83
+ defaultValue: options.defaultOpen ?? false,
84
+ onChange: (next) => options.onOpenChange?.(next),
85
+ });
86
+
87
+ const value = valueState.value;
88
+ const isOpen = openState.value;
89
+
90
+ const isDisabled = computed(() => toValue(options.disabled) === true);
91
+
92
+ const firstDayOfWeek = computed(() => {
93
+ const raw = toValue(options.firstDayOfWeek) ?? DEFAULT_FIRST_DAY_OF_WEEK;
94
+
95
+ return ((raw % DAYS_PER_WEEK) + DAYS_PER_WEEK) % DAYS_PER_WEEK;
96
+ });
97
+
98
+ const selectedRange = computed<[Date, Date] | null>(() => {
99
+ const current = value.value;
100
+
101
+ if (!current) return null;
102
+
103
+ const start = parseDate(current[0], valueFormat);
104
+ const end = parseDate(current[1], valueFormat);
105
+
106
+ return start && end ? [start, end] : null;
107
+ });
108
+
109
+ const activeIndex = ref<0 | 1>(0);
110
+ const draftRange = ref<DateRangeDraft>(emptyRangeDraft());
111
+ const inputDrafts = ref<[string | null, string | null]>([null, null]);
112
+ const hoverDate = ref<Date | null>(null);
113
+
114
+ // 草稿来源标记:输入提交的待定端点按 blur 语义在关闭后保留(等另一端补齐),
115
+ // 点选草稿在关闭时丢弃。Escape 强制丢弃两者。
116
+ let draftIsTyped = false;
117
+
118
+ const initialBase = selectedRange.value?.[0] ?? new Date();
119
+
120
+ const panelDate = ref(startOfMonth(initialBase));
121
+ const focusedDate = ref(startOfDay(initialBase));
122
+
123
+ const triggerElements: [HTMLElement | null, HTMLElement | null] = [null, null];
124
+
125
+ // close() 交还焦点会再触发一次 focus;不拦掉它,Escape 关闭会立刻重新打开。
126
+ let skipFocusOpen = false;
127
+
128
+ const floating = useFloating({
129
+ placement: options.placement ?? 'bottom-start',
130
+ offset: options.offset ?? DEFAULT_OFFSET,
131
+ flip: options.flip,
132
+ shift: options.shift,
133
+ padding: options.padding ?? DEFAULT_PADDING,
134
+ strategy: options.strategy,
135
+ open: isOpen,
136
+ });
137
+
138
+ const rightPanelDate = computed(() => addMonths(panelDate.value, 1));
139
+
140
+ const awaitingEnd = computed(() => draftRange.value[0] !== null && draftRange.value[1] === null);
141
+
142
+ const displayTexts = computed<[string, string]>(() => {
143
+ const picking = draftRange.value[0] !== null;
144
+ const committed = selectedRange.value;
145
+
146
+ function text(index: 0 | 1): string {
147
+ const draft = inputDrafts.value[index];
148
+
149
+ if (draft !== null) return draft;
150
+
151
+ // 选值进行中回显草稿而不是旧提交值:开始新一轮选择时终点输入框应当清空。
152
+ if (picking) {
153
+ const pending = draftRange.value[index];
154
+
155
+ return pending ? formatDate(pending, format) : '';
156
+ }
157
+
158
+ return committed ? formatDate(committed[index], format) : '';
159
+ }
160
+
161
+ return [text(0), text(1)];
162
+ });
163
+
164
+ const leftCells = computed<DateCell[]>(() =>
165
+ getCalendarGrid({
166
+ year: panelDate.value.getFullYear(),
167
+ month: panelDate.value.getMonth(),
168
+ firstDayOfWeek: firstDayOfWeek.value,
169
+ })
170
+ );
171
+
172
+ const rightCells = computed<DateCell[]>(() =>
173
+ getCalendarGrid({
174
+ year: rightPanelDate.value.getFullYear(),
175
+ month: rightPanelDate.value.getMonth(),
176
+ firstDayOfWeek: firstDayOfWeek.value,
177
+ })
178
+ );
179
+
180
+ const weekdayLabels = computed(() => getWeekdayLabels(firstDayOfWeek.value));
181
+
182
+ function startOfMonth(date: Date): Date {
183
+ return new Date(date.getFullYear(), date.getMonth(), 1);
184
+ }
185
+
186
+ function isDateDisabled(date: Date): boolean {
187
+ if (isDisabled.value) return true;
188
+
189
+ return options.disabledDate ? options.disabledDate(startOfDay(date)) : false;
190
+ }
191
+
192
+ function getCellState(date: Date, side: DateRangeSide): DateRangeCellState {
193
+ const picking = draftRange.value[0] !== null;
194
+ const committed = selectedRange.value;
195
+
196
+ const start = picking ? draftRange.value[0] : (committed?.[0] ?? null);
197
+ const end = picking ? draftRange.value[1] : (committed?.[1] ?? null);
198
+ const hover = awaitingEnd.value ? hoverDate.value : null;
199
+
200
+ const highlight = getRangeHighlight({ date, start, end, hover });
201
+ const monthAnchor = side === 'left' ? panelDate.value : rightPanelDate.value;
202
+
203
+ return {
204
+ selected: highlight.isStart || highlight.isEnd,
205
+ disabled: isDateDisabled(date),
206
+ today: isSameDay(new Date(), date),
207
+ inMonth: isSameMonth(monthAnchor, date),
208
+ ...highlight,
209
+ };
210
+ }
211
+
212
+ function getCellId(date: Date, side: DateRangeSide): string {
213
+ return `date-range-cell-${uid}-${side}-${formatDate(date, DEFAULT_VALUE_FORMAT)}`;
214
+ }
215
+
216
+ function open(initialActive: 0 | 1 = 0): void {
217
+ if (isDisabled.value) return;
218
+
219
+ // 输入提交的待定端点跨开合保留,用户重开面板可以接着补齐另一端。
220
+ if (!draftIsTyped) draftRange.value = emptyRangeDraft();
221
+
222
+ inputDrafts.value = [null, null];
223
+ hoverDate.value = null;
224
+
225
+ const base = draftRange.value[0] ?? selectedRange.value?.[0] ?? new Date();
226
+
227
+ panelDate.value = startOfMonth(base);
228
+ focusedDate.value = startOfDay(base);
229
+ activeIndex.value = initialActive;
230
+ openState.set(true);
231
+ }
232
+
233
+ function cleanupDrafts(): void {
234
+ if (!draftIsTyped) draftRange.value = emptyRangeDraft();
235
+
236
+ inputDrafts.value = [null, null];
237
+ hoverDate.value = null;
238
+ }
239
+
240
+ function close(): void {
241
+ const content = floating.floatingRef.value;
242
+ const active = isBrowser ? document.activeElement : null;
243
+
244
+ // 清理必须同步做:同一 tick 内 open→close 时 watch 的新旧值相同会被跳过。
245
+ cleanupDrafts();
246
+ openState.set(false);
247
+
248
+ if (content && active instanceof Node && content.contains(active)) {
249
+ skipFocusOpen = true;
250
+ triggerElements[activeIndex.value]?.focus();
251
+ }
252
+ }
253
+
254
+ function selectRange(start: Date, end: Date): void {
255
+ if (isDisabled.value) return;
256
+
257
+ const [orderedStart, orderedEnd] =
258
+ start.getTime() <= end.getTime() ? [start, end] : [end, start];
259
+
260
+ if (isDateDisabled(orderedStart) || isDateDisabled(orderedEnd)) return;
261
+
262
+ draftRange.value = emptyRangeDraft();
263
+ draftIsTyped = false;
264
+ valueState.set([formatDate(orderedStart, valueFormat), formatDate(orderedEnd, valueFormat)]);
265
+ close();
266
+ }
267
+
268
+ /** 预设快捷项入口:按 valueFormat 解析两端字符串,非法则静默忽略。 */
269
+ function selectRangeByValue(start: string, end: string): void {
270
+ const parsedStart = parseDate(start, valueFormat);
271
+ const parsedEnd = parseDate(end, valueFormat);
272
+
273
+ if (parsedStart && parsedEnd) selectRange(parsedStart, parsedEnd);
274
+ }
275
+
276
+ function pickDate(date: Date): void {
277
+ if (isDateDisabled(date)) return;
278
+
279
+ const result = resolveRangePick({ draft: draftRange.value, active: activeIndex.value, date });
280
+
281
+ if (result.done && result.draft[0] && result.draft[1]) {
282
+ selectRange(result.draft[0], result.draft[1]);
283
+
284
+ return;
285
+ }
286
+
287
+ draftRange.value = result.draft;
288
+ draftIsTyped = false;
289
+ hoverDate.value = null;
290
+ setActiveIndex(result.active);
291
+ }
292
+
293
+ function clear(): void {
294
+ draftRange.value = emptyRangeDraft();
295
+ draftIsTyped = false;
296
+ inputDrafts.value = [null, null];
297
+ hoverDate.value = null;
298
+ valueState.set(null);
299
+ }
300
+
301
+ function setActiveIndex(index: 0 | 1): void {
302
+ activeIndex.value = index;
303
+ triggerElements[index]?.focus();
304
+ }
305
+
306
+ function shiftView(unit: DateRangeViewUnit, delta: -1 | 1): void {
307
+ const months = unit === 'month' ? 1 : 12;
308
+
309
+ panelDate.value = addMonths(panelDate.value, delta * months);
310
+ focusedDate.value = addMonths(focusedDate.value, delta * months);
311
+ }
312
+
313
+ function moveFocus(move: DateMove): void {
314
+ const next = resolveDateMove(focusedDate.value, move);
315
+
316
+ focusedDate.value = next;
317
+ panelDate.value = startOfMonth(next);
318
+ }
319
+
320
+ /**
321
+ * 提交某一端的输入草稿。
322
+ *
323
+ * 另一端优先取草稿、其次取提交值;两端齐备即合成范围(自动排序)并关闭。
324
+ * 只有一端时保留为待定草稿,等另一端输入或点选。非法文本静默回退。
325
+ */
326
+ function commitInput(index: 0 | 1): void {
327
+ const text = inputDrafts.value[index];
328
+
329
+ if (text === null) return;
330
+
331
+ inputDrafts.value = index === 0 ? [null, inputDrafts.value[1]] : [inputDrafts.value[0], null];
332
+
333
+ const trimmed = text.trim();
334
+
335
+ if (trimmed === '') {
336
+ clear();
337
+
338
+ return;
339
+ }
340
+
341
+ const parsed = parseDate(trimmed, format);
342
+
343
+ if (!parsed || isDateDisabled(parsed)) return;
344
+
345
+ const other = draftRange.value[1 - index] ?? selectedRange.value?.[1 - index] ?? null;
346
+
347
+ if (other) {
348
+ selectRange(parsed, other);
349
+
350
+ return;
351
+ }
352
+
353
+ draftRange.value = index === 0 ? [parsed, null] : [null, parsed];
354
+ draftIsTyped = true;
355
+ }
356
+
357
+ function onInputInput(index: 0 | 1, event: Event): void {
358
+ if (isDisabled.value) return;
359
+
360
+ const target = event.target;
361
+
362
+ if (!(target instanceof HTMLInputElement)) return;
363
+
364
+ inputDrafts.value =
365
+ index === 0 ? [target.value, inputDrafts.value[1]] : [inputDrafts.value[0], target.value];
366
+ }
367
+
368
+ function onInputBlur(index: 0 | 1): void {
369
+ commitInput(index);
370
+ }
371
+
372
+ function onInputFocus(index: 0 | 1): void {
373
+ if (skipFocusOpen) {
374
+ skipFocusOpen = false;
375
+
376
+ return;
377
+ }
378
+
379
+ activeIndex.value = index;
380
+
381
+ if (!isDisabled.value && !isOpen.value) open(index);
382
+ }
383
+
384
+ function onInputClick(index: 0 | 1): void {
385
+ if (!isDisabled.value && !isOpen.value) open(index);
386
+ }
387
+
388
+ function onInputKeydown(index: 0 | 1, event: KeyboardEvent): void {
389
+ if (isDisabled.value) return;
390
+
391
+ // Tab 在打开态先换端:起点→终点、Shift+Tab 终点→起点;再按才走出组件。
392
+ if (event.key === 'Tab' && isOpen.value) {
393
+ if (index === 0 && !event.shiftKey) {
394
+ event.preventDefault();
395
+ setActiveIndex(1);
396
+
397
+ return;
398
+ }
399
+
400
+ if (index === 1 && event.shiftKey) {
401
+ event.preventDefault();
402
+ setActiveIndex(0);
403
+
404
+ return;
405
+ }
406
+
407
+ close();
408
+
409
+ return;
410
+ }
411
+
412
+ if (!isOpen.value && event.key === 'Enter' && inputDrafts.value[index] !== null) {
413
+ event.preventDefault();
414
+ commitInput(index);
415
+
416
+ return;
417
+ }
418
+
419
+ const intent = resolveDatePickerKeyIntent({
420
+ key: event.key,
421
+ open: isOpen.value,
422
+ panel: 'date',
423
+ focusedDate: focusedDate.value,
424
+ firstDayOfWeek: firstDayOfWeek.value,
425
+ hasModifierKey: event.ctrlKey || event.altKey || event.metaKey,
426
+ });
427
+
428
+ if (intent.kind === 'none') return;
429
+
430
+ if (intent.kind === 'tab-out') {
431
+ close();
432
+
433
+ return;
434
+ }
435
+
436
+ event.preventDefault();
437
+
438
+ if (intent.kind === 'open') open(index);
439
+ else if (intent.kind === 'close') {
440
+ // Escape 是显式取消:输入草稿也一并丢弃,与 blur / 外部点击的保留语义相反。
441
+ draftIsTyped = false;
442
+ close();
443
+ } else if (intent.kind === 'move') moveFocus(intent.move);
444
+ else if (intent.kind === 'commit') pickDate(focusedDate.value);
445
+ }
446
+
447
+ function onCellPointerDown(event: PointerEvent): void {
448
+ event.preventDefault();
449
+ }
450
+
451
+ function onCellPointerEnter(date: Date): void {
452
+ if (awaitingEnd.value) hoverDate.value = date;
453
+ }
454
+
455
+ function onCellPointerLeave(): void {
456
+ hoverDate.value = null;
457
+ }
458
+
459
+ function getTriggerProps(index: 0 | 1): Record<string, unknown> {
460
+ const labelledby = toValue(options.ariaLabelledby);
461
+ const describedby = toValue(options.ariaDescribedby);
462
+
463
+ return {
464
+ id: triggerIds[index],
465
+ role: 'combobox',
466
+ 'aria-haspopup': 'dialog',
467
+ 'aria-expanded': isOpen.value,
468
+ 'aria-controls': contentId,
469
+ 'aria-owns': isOpen.value ? contentId : undefined,
470
+ // 焦点格只在活跃输入框上声明,两个 combobox 同时指向一个格子会让读屏重复播报。
471
+ 'aria-activedescendant':
472
+ isOpen.value && activeIndex.value === index
473
+ ? getCellId(focusedDate.value, 'left')
474
+ : undefined,
475
+ 'aria-autocomplete': 'none',
476
+ 'aria-disabled': isDisabled.value || undefined,
477
+ 'aria-labelledby': labelledby || undefined,
478
+ 'aria-describedby': describedby || undefined,
479
+ 'data-state': isOpen.value ? 'open' : 'closed',
480
+ 'data-disabled': isDisabled.value ? '' : undefined,
481
+ 'data-active': isOpen.value && activeIndex.value === index ? '' : undefined,
482
+ disabled: isDisabled.value || undefined,
483
+ autocomplete: 'off',
484
+ };
485
+ }
486
+
487
+ function getContentProps(): Record<string, unknown> {
488
+ return {
489
+ id: contentId,
490
+ role: 'dialog',
491
+ 'aria-labelledby': toValue(options.ariaLabelledby) || triggerIds[0],
492
+ tabindex: -1,
493
+ 'data-state': isOpen.value ? 'open' : 'closed',
494
+ };
495
+ }
496
+
497
+ function getGridProps(side: DateRangeSide): Record<string, unknown> {
498
+ const anchor = side === 'left' ? panelDate.value : rightPanelDate.value;
499
+
500
+ return { role: 'grid', 'aria-label': formatDate(anchor, 'YYYY年 M月') };
501
+ }
502
+
503
+ function getCellProps(date: Date, side: DateRangeSide): Record<string, unknown> {
504
+ const state = getCellState(date, side);
505
+
506
+ return {
507
+ id: getCellId(date, side),
508
+ role: 'gridcell',
509
+ 'aria-selected': state.selected,
510
+ 'aria-disabled': state.disabled || undefined,
511
+ 'aria-label': formatDate(date, 'YYYY年M月D日'),
512
+ 'data-focused': isSameDay(focusedDate.value, date) ? '' : undefined,
513
+ 'data-selected': state.selected ? '' : undefined,
514
+ 'data-range-start': state.isStart ? '' : undefined,
515
+ 'data-range-end': state.isEnd ? '' : undefined,
516
+ 'data-in-range': state.inRange ? '' : undefined,
517
+ 'data-preview': state.inPreview ? '' : undefined,
518
+ 'data-hover-end': state.isHoverEnd ? '' : undefined,
519
+ 'data-disabled': state.disabled ? '' : undefined,
520
+ 'data-today': state.today ? '' : undefined,
521
+ 'data-outside': state.inMonth ? undefined : '',
522
+ 'data-value': formatDate(date, DEFAULT_VALUE_FORMAT),
523
+ };
524
+ }
525
+
526
+ function setTriggerElement(node: unknown, index: 0 | 1): void {
527
+ triggerElements[index] = node instanceof HTMLElement ? node : null;
528
+ }
529
+
530
+ function setControlElement(node: unknown): void {
531
+ floating.referenceRef.value = node instanceof HTMLElement ? node : null;
532
+ }
533
+
534
+ function setContentElement(node: unknown): void {
535
+ floating.floatingRef.value = node instanceof HTMLElement ? node : null;
536
+ }
537
+
538
+ function handleDocumentPointerDown(event: PointerEvent): void {
539
+ const target = event.target;
540
+
541
+ if (!(target instanceof Node)) return;
542
+
543
+ const control = floating.referenceRef.value;
544
+ const content = floating.floatingRef.value;
545
+
546
+ if (control?.contains(target) || content?.contains(target)) return;
547
+
548
+ // 外部点击先提交活跃端的文本,再丢弃未完成的点选草稿——与 blur 规则一致。
549
+ commitInput(activeIndex.value);
550
+ close();
551
+ }
552
+
553
+ watch(
554
+ isOpen,
555
+ (open) => {
556
+ if (!isBrowser) return;
557
+
558
+ document.removeEventListener('pointerdown', handleDocumentPointerDown, true);
559
+
560
+ if (open) {
561
+ document.addEventListener('pointerdown', handleDocumentPointerDown, true);
562
+ } else {
563
+ cleanupDrafts();
564
+ }
565
+ },
566
+ { immediate: true }
567
+ );
568
+
569
+ const contentStyle = computed<CSSProperties>(() => ({ ...floating.floatingStyle.value }));
570
+
571
+ onScopeDispose(() => {
572
+ if (isBrowser) document.removeEventListener('pointerdown', handleDocumentPointerDown, true);
573
+ });
574
+
575
+ return {
576
+ value,
577
+ setValue: valueState.set,
578
+ selectedRange,
579
+ isOpen,
580
+ open,
581
+ close,
582
+ activeIndex: computed(() => activeIndex.value),
583
+ setActiveIndex,
584
+ panelDate: computed(() => panelDate.value),
585
+ rightPanelDate,
586
+ leftCells,
587
+ rightCells,
588
+ weekdayLabels,
589
+ focusedDate: computed(() => focusedDate.value),
590
+ displayTexts,
591
+ isDateDisabled,
592
+ getCellState,
593
+ pickDate,
594
+ selectRange,
595
+ selectRangeByValue,
596
+ clear,
597
+ shiftView,
598
+ moveFocus,
599
+ onInputInput,
600
+ onInputKeydown,
601
+ onInputBlur,
602
+ onInputFocus,
603
+ onInputClick,
604
+ onCellPointerDown,
605
+ onCellPointerEnter,
606
+ onCellPointerLeave,
607
+ getTriggerProps,
608
+ getContentProps,
609
+ getGridProps,
610
+ getCellProps,
611
+ getCellId,
612
+ setTriggerElement,
613
+ setControlElement,
614
+ setContentElement,
615
+ floating,
616
+ contentStyle,
617
+ };
618
+ }
@@ -0,0 +1,3 @@
1
+ export type { DropdownMenuItemEntry, UseDropdownMenuOptions, UseDropdownMenuReturn } from './types';
2
+
3
+ export { useDropdownMenu } from './useDropdownMenu';
@@ -0,0 +1,61 @@
1
+ import type { ComputedRef, MaybeRefOrGetter, Ref } from 'vue';
2
+
3
+ import type { FloatingOffset, FloatingPlacement } from '@agile-plus/primitives/floating';
4
+
5
+ import type { FloatingStrategy, UseFloatingReturn } from '../useFloating';
6
+
7
+ /** 菜单项注册条目。以消费者语义 value 为键,顺序即导航顺序。 */
8
+ export interface DropdownMenuItemEntry {
9
+ /** 消费者语义 value,item-select 回调返回它 */
10
+ value: Ref<string>;
11
+ /** 是否禁用 */
12
+ disabled: Ref<boolean>;
13
+ /** 可读文本(typeahead / 读屏备用) */
14
+ textValue: Ref<string>;
15
+ /** 项元素,用于滚动跟随 */
16
+ element: Ref<HTMLElement | null>;
17
+ /** 项语义:普通 / 可勾选 / 单选 */
18
+ kind: 'item' | 'checkbox' | 'radio';
19
+ }
20
+
21
+ /** useDropdownMenu 的配置。 */
22
+ export interface UseDropdownMenuOptions {
23
+ open?: MaybeRefOrGetter<boolean | undefined>;
24
+ defaultOpen?: boolean;
25
+ onOpenChange?: (open: boolean) => void;
26
+ /** 选中某项回调(点击或 Enter/Space),返回消费者 value */
27
+ onSelect?: (value: string) => void;
28
+ placement?: FloatingPlacement;
29
+ offset?: number | FloatingOffset;
30
+ flip?: boolean;
31
+ shift?: boolean;
32
+ padding?: number;
33
+ strategy?: FloatingStrategy;
34
+ /** 方向键到边界是否循环,默认 true */
35
+ loop?: boolean;
36
+ }
37
+
38
+ /** useDropdownMenu 的返回值。 */
39
+ export interface UseDropdownMenuReturn {
40
+ isOpen: ComputedRef<boolean>;
41
+ open(): void;
42
+ close(): void;
43
+ toggle(): void;
44
+ triggerId: string;
45
+ contentId: string;
46
+ floating: UseFloatingReturn;
47
+ /** 当前高亮项 value */
48
+ highlightedValue: ComputedRef<string | null>;
49
+ registerItem(entry: DropdownMenuItemEntry): () => void;
50
+ getTriggerProps(): Record<string, unknown>;
51
+ getContentProps(): Record<string, unknown>;
52
+ getItemProps(value: string): Record<string, unknown>;
53
+ getLabelProps(): Record<string, unknown>;
54
+ getSeparatorProps(): Record<string, unknown>;
55
+ setTriggerElement(node: unknown): void;
56
+ setContentElement(node: unknown): void;
57
+ onTriggerKeydown(event: KeyboardEvent): void;
58
+ onContentKeydown(event: KeyboardEvent): void;
59
+ onItemPointerMove(value: string): void;
60
+ onItemClick(value: string): void;
61
+ }