@agile-plus/headless 0.1.1 → 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,342 @@
1
+ import { computed, toRef, toValue, useId } from 'vue';
2
+
3
+ import {
4
+ buildTreeIndex,
5
+ findTreeRowIndex,
6
+ flattenTreeRows,
7
+ getTreeItemAttrs,
8
+ getTreeRootAttrs,
9
+ resolveTreeCheckStates,
10
+ resolveTreeKeyIntent,
11
+ resolveTreeNavigation,
12
+ toggleTreeCheckedKeys,
13
+ toggleTreeKey,
14
+ } from '@agile-plus/primitives/tree';
15
+
16
+ import { useControllable } from '../utils/useControllable';
17
+ import { useVirtualList } from '../useVirtualList/useVirtualList';
18
+
19
+ import type { TreeDataNode, TreeKey, TreeRow } from '@agile-plus/primitives/tree';
20
+
21
+ import type { UseVirtualTreeOptions, UseVirtualTreeReturn } from './types';
22
+
23
+ /** 行内插槽允许放原生控件;它们的按键不应被树的导航处理器再次解释。 */
24
+ const INTERACTIVE_SELECTOR =
25
+ 'input, textarea, select, button, a[href], [contenteditable]:not([contenteditable="false"]), [role="button"], [role="checkbox"]';
26
+
27
+ /**
28
+ * 虚拟树形控件的 Vue Composable 适配。
29
+ *
30
+ * 组合而不是重写虚拟化:`data → 索引 → 展开后的可见行` 这一段是树独有的,行序列一旦算出来,
31
+ * 剩下的「按累计高度取窗口」与扁平列表是同一个问题,因此直接把 `rows.length` 交给
32
+ * useVirtualList,不复制它的树、测量与滚动补偿。
33
+ *
34
+ * 本层持有的状态:
35
+ *
36
+ * 1. 展开 / 选中 / 勾选 / 活动行四条通道,各自归一化成受控或非受控;
37
+ * 2. 勾选态由子节点回读,`checkedKeys` 只存级联后的最小集合,半选不落地;
38
+ * 3. 活动行记键不记下标——展开收起会在中间插入或删除整段行,下标会指到另一行上。
39
+ *
40
+ * 高度缓存按 `row.key` 记账:展开收起只重排当前可见行,同一个节点的实测高度不会转移到另一个
41
+ * 节点。折叠后暂时离开当前集合的测量会被释放,再次展开时按预估高度重新收敛。
42
+ *
43
+ * @param options 数据、字段名、四条状态通道与虚拟窗口参数。
44
+ * @returns 可见行、窗口几何、状态读写、键盘导航与属性绑定函数集合。
45
+ */
46
+ export function useVirtualTree<T extends TreeDataNode>(
47
+ options: UseVirtualTreeOptions<T>
48
+ ): UseVirtualTreeReturn<T> {
49
+ /**
50
+ * 实例级 id 前缀。
51
+ *
52
+ * 与 Tabs、RadioGroup 同样的原因:id 只由行下标拼成会让同页两棵树的
53
+ * `aria-activedescendant` 指向另一棵树的行。
54
+ */
55
+ const uid = useId();
56
+
57
+ /**
58
+ * 键到节点的索引。
59
+ *
60
+ * 每次读取都重算的话,键盘导航、勾选回读、属性表三处各要遍历一遍全部节点;索引一次构建
61
+ * 之后它们都只是查表。数据引用没变就不重建,因此异步加载某一层要传新数组。
62
+ */
63
+ const index = computed(() =>
64
+ buildTreeIndex(toValue(options.data) ?? [], toValue(options.fields))
65
+ );
66
+
67
+ const expandedState = useControllable<readonly TreeKey[]>({
68
+ value: options.expandedKeys,
69
+ defaultValue: options.defaultExpandedKeys ?? [],
70
+ onChange: (next) => options.onExpandedKeysChange?.([...next]),
71
+ });
72
+
73
+ const selectedState = useControllable<readonly TreeKey[]>({
74
+ value: options.selectedKeys,
75
+ defaultValue: options.defaultSelectedKeys ?? [],
76
+ onChange: (next) => options.onSelectedKeysChange?.([...next]),
77
+ });
78
+
79
+ const checkedState = useControllable<readonly TreeKey[]>({
80
+ value: options.checkedKeys,
81
+ defaultValue: options.defaultCheckedKeys ?? [],
82
+ onChange: (next) => options.onCheckedKeysChange?.([...next]),
83
+ });
84
+
85
+ const activeState = useControllable<TreeKey | null>({
86
+ value: options.activeKey,
87
+ defaultValue: options.defaultActiveKey ?? null,
88
+ onChange: options.onActiveKeyChange,
89
+ });
90
+
91
+ const expandedKeys = computed(() => expandedState.value.value);
92
+ const selectedKeys = computed(() => selectedState.value.value);
93
+ const checkedKeys = computed(() => checkedState.value.value);
94
+ const activeKey = computed(() => activeState.value.value);
95
+
96
+ /** 展开之后的可见行:虚拟列表的项序列,也是键盘导航的行序列。 */
97
+ const rows = computed(() => flattenTreeRows(index.value, expandedKeys.value));
98
+
99
+ const virtual = useVirtualList({
100
+ count: toRef(() => rows.value.length),
101
+ getItemKey: (index) => rows.value[index]?.key ?? index,
102
+ estimateSize: toRef(() => toValue(options.estimateSize)),
103
+ overscan: toRef(() => toValue(options.overscan)),
104
+ });
105
+
106
+ const selectionMode = computed(() => toValue(options.selection) ?? 'single');
107
+
108
+ const isCheckable = computed(() => toValue(options.checkable) === true);
109
+
110
+ const checkStates = computed(() => resolveTreeCheckStates(index.value, checkedKeys.value));
111
+
112
+ const activeIndex = computed(() => findTreeRowIndex(rows.value, activeKey.value));
113
+
114
+ /**
115
+ * 当前窗口内的行。
116
+ *
117
+ * 在下标越界时跳过而不是抛出:展开收起的同一 tick 里 `rows` 已经变短而窗口还是旧区间,
118
+ * 下一帧就会收敛,为这一帧的中间态报错没有意义。
119
+ */
120
+ const visibleRows = computed(() => {
121
+ const all = rows.value;
122
+ const rendered: TreeRow<T>[] = [];
123
+
124
+ for (const position of virtual.visibleIndices.value) {
125
+ const row = all[position];
126
+
127
+ if (row !== undefined) rendered.push(row);
128
+ }
129
+
130
+ return rendered;
131
+ });
132
+
133
+ function getItemId(row: TreeRow<T>): string {
134
+ return `tree-${uid}-item-${row.index}`;
135
+ }
136
+
137
+ function expand(key: TreeKey): void {
138
+ const node = index.value.nodes.get(key);
139
+
140
+ if (!node || !node.branch || expandedKeys.value.includes(key)) return;
141
+
142
+ expandedState.set([...expandedKeys.value, key]);
143
+ }
144
+
145
+ function collapse(key: TreeKey): void {
146
+ if (!expandedKeys.value.includes(key)) return;
147
+
148
+ expandedState.set(expandedKeys.value.filter((item) => item !== key));
149
+ }
150
+
151
+ function toggleExpand(row: TreeRow<T>): void {
152
+ if (!row.branch || row.disabled) return;
153
+
154
+ const nextExpanded = !expandedKeys.value.includes(row.key);
155
+
156
+ expandedState.set(toggleTreeKey(expandedKeys.value, row.key, nextExpanded));
157
+ }
158
+
159
+ /**
160
+ * 展开全部。
161
+ *
162
+ * 一次写入而不是逐行 `expand`:后者会让行序列在每次展开后重算一遍,十万节点的树点一次
163
+ * 「全部展开」就是十万次 flatten。
164
+ */
165
+ function expandAll(): void {
166
+ const next: TreeKey[] = [];
167
+
168
+ // 按 order(前序)而不是按 Map 的插入顺序收集:索引先登记全部顶层再下探,
169
+ // 前序才与行序列同序,emit 出去的展开键集合才有稳定可读的形态。
170
+ for (const key of index.value.order) {
171
+ if (index.value.nodes.get(key)?.branch) next.push(key);
172
+ }
173
+
174
+ expandedState.set(next);
175
+ }
176
+
177
+ function collapseAll(): void {
178
+ expandedState.set([]);
179
+ }
180
+
181
+ function selectRow(row: TreeRow<T>): void {
182
+ if (row.disabled || selectionMode.value === 'none') return;
183
+
184
+ const selected = selectedKeys.value.includes(row.key);
185
+
186
+ // 单选模式重复点击不取消:没有选中项的树在「点一行看详情」的场景里不是用户想要的状态,
187
+ // 清空另有 clearSelection。
188
+ if (selectionMode.value === 'single') {
189
+ if (!selected) selectedState.set([row.key]);
190
+
191
+ return;
192
+ }
193
+
194
+ selectedState.set(toggleTreeKey(selectedKeys.value, row.key, !selected));
195
+ }
196
+
197
+ function clearSelection(): void {
198
+ if (selectedKeys.value.length === 0) return;
199
+
200
+ selectedState.set([]);
201
+ }
202
+
203
+ /**
204
+ * 切换勾选并级联。
205
+ *
206
+ * 半选行按下去变成全选:`indeterminate` 是「下面有选中的东西」而不是「可以再点掉一半」,
207
+ * 从半选点成取消会让用户无法用一次点击得到整棵子树全选。
208
+ */
209
+ function toggleCheck(row: TreeRow<T>): void {
210
+ if (!isCheckable.value || row.disabled) return;
211
+
212
+ const state = checkStates.value.get(row.key) ?? 'unchecked';
213
+
214
+ checkedState.set(
215
+ toggleTreeCheckedKeys(index.value, checkedKeys.value, row.key, state !== 'checked')
216
+ );
217
+ }
218
+
219
+ function scrollToKey(key: TreeKey): void {
220
+ const position = findTreeRowIndex(rows.value, key);
221
+
222
+ if (position < 0) return;
223
+
224
+ virtual.scrollIntoView(position);
225
+ }
226
+
227
+ /**
228
+ * 设置活动行。
229
+ *
230
+ * 目标行存在时顺手带进视口:`aria-activedescendant` 指向一行不可见的元素,读屏会照常播报
231
+ * 而屏幕上什么都没有,用户只能靠方向键盲走。已经在视口内时不动。
232
+ */
233
+ function setActiveKey(key: TreeKey | null): void {
234
+ activeState.set(key);
235
+
236
+ if (key !== null) scrollToKey(key);
237
+ }
238
+
239
+ function onKeydown(event: KeyboardEvent): void {
240
+ if (event.defaultPrevented || event.isComposing) return;
241
+
242
+ const eventTarget = event.target;
243
+
244
+ if (eventTarget instanceof Element && eventTarget.closest(INTERACTIVE_SELECTOR)) return;
245
+
246
+ const intent = resolveTreeKeyIntent({
247
+ key: event.key,
248
+ hasModifierKey: event.ctrlKey || event.metaKey || event.altKey,
249
+ });
250
+
251
+ const result = resolveTreeNavigation({
252
+ intent,
253
+ rows: rows.value,
254
+ activeIndex: activeIndex.value,
255
+ });
256
+
257
+ if (!result.handled) return;
258
+
259
+ event.preventDefault();
260
+
261
+ // 先改展开再取目标行:导航结果的行下标相对的是当前行序列,而展开/收起只在活动行**之后**
262
+ // 插入或删除行,活动行自身的下标在其中不变,所以两个顺序都能取到同一行。
263
+ if (result.expandKey !== null) expand(result.expandKey);
264
+ if (result.collapseKey !== null) collapse(result.collapseKey);
265
+
266
+ const target = result.activeIndex >= 0 ? rows.value[result.activeIndex] : undefined;
267
+
268
+ if (target === undefined) return;
269
+
270
+ setActiveKey(target.key);
271
+
272
+ if (intent.kind !== 'select') return;
273
+
274
+ // Enter 与空格分开:Enter 选中当前行,Space 在勾选模式下切换勾选,两条通道都能独立用键盘操作。
275
+ if (event.key === ' ' && isCheckable.value) toggleCheck(target);
276
+ else selectRow(target);
277
+ }
278
+
279
+ function getRootAttrs(): Record<string, unknown> {
280
+ const position = activeIndex.value;
281
+
282
+ // 活动行必须落在**当前渲染窗口**里才输出 idref:`aria-activedescendant` 指向一个不在 DOM
283
+ // 里的 id 是无效引用,读屏会播报「未命名」而不是「这一行滚出去了」。
284
+ const target =
285
+ position >= virtual.startIndex.value && position <= virtual.endIndex.value
286
+ ? rows.value[position]
287
+ : undefined;
288
+
289
+ return getTreeRootAttrs({
290
+ multiselectable: selectionMode.value === 'multiple',
291
+ activeDescendantId: target === undefined ? undefined : getItemId(target),
292
+ });
293
+ }
294
+
295
+ function getItemAttrs(row: TreeRow<T>): Record<string, unknown> {
296
+ return getTreeItemAttrs({
297
+ id: getItemId(row),
298
+ level: row.level,
299
+ posinset: row.posinset,
300
+ setsize: row.setsize,
301
+ branch: row.branch,
302
+ expanded: row.expanded,
303
+ selectable: selectionMode.value !== 'none',
304
+ selected: selectedKeys.value.includes(row.key),
305
+ checked: isCheckable.value ? checkStates.value.get(row.key) : undefined,
306
+ active: row.index === activeIndex.value,
307
+ disabled: row.disabled,
308
+ });
309
+ }
310
+
311
+ return {
312
+ rows,
313
+ visibleRows,
314
+ expandedKeys,
315
+ selectedKeys,
316
+ checkedKeys,
317
+ checkStates,
318
+ activeKey,
319
+ activeIndex,
320
+ paddingTop: virtual.paddingTop,
321
+ paddingBottom: virtual.paddingBottom,
322
+ totalSize: virtual.totalSize,
323
+ scrollTop: virtual.scrollTop,
324
+ viewportSize: virtual.viewportSize,
325
+ setViewportElement: virtual.setViewportElement,
326
+ setContentElement: virtual.setContentElement,
327
+ invalidateMeasurements: virtual.invalidateMeasurements,
328
+ expand,
329
+ collapse,
330
+ toggleExpand,
331
+ expandAll,
332
+ collapseAll,
333
+ selectRow,
334
+ clearSelection,
335
+ toggleCheck,
336
+ setActiveKey,
337
+ scrollToKey,
338
+ onKeydown,
339
+ getRootAttrs,
340
+ getItemAttrs,
341
+ };
342
+ }
@@ -85,6 +85,8 @@ export function useFocusTrap(options: UseFocusTrapOptions): void {
85
85
  return;
86
86
  }
87
87
 
88
+ // Radix 同级的近似判断:容器/首项都算环的起点。焦点若停在 tabindex=-1 的
89
+ // 程序化元素上,反向 Tab 可能先短暂跳出容器,但紧接着的 focusin 会把它收回。
88
90
  const atStart = current === container || current === first;
89
91
 
90
92
  if (event.shiftKey && atStart) {
@@ -0,0 +1,191 @@
1
+ import { computed, ref, toValue } from 'vue';
2
+
3
+ import { getInputAttrs } from '@agile-plus/primitives/input';
4
+
5
+ import { useControllable } from './useControllable';
6
+ import { useFormResetListener } from './useFormResetListener';
7
+
8
+ import type { ComputedRef, MaybeRefOrGetter, Ref } from 'vue';
9
+
10
+ /**
11
+ * 文本输入控件(input / textarea)的共享配置。
12
+ *
13
+ * 两个组件的值语义完全一致:字符串受控 / 非受控、IME 组合输入、表单 reset 复原。
14
+ * 差异只在渲染目标与个别属性(input 有 type),因此值内核抽在这里共用一份,
15
+ * useInput / useTextarea 各自只做属性表的薄封装。
16
+ */
17
+ export interface UseTextControlOptions {
18
+ /**
19
+ * 受控值来源。
20
+ *
21
+ * **`undefined` 就是「未受控」的唯一判据**:封装层必须让 prop 缺省保持 undefined,
22
+ * 被 cast 成空串会让组件变成「受控且恒为空」,非受控用法静默失效。
23
+ */
24
+ modelValue?: MaybeRefOrGetter<string | undefined>;
25
+
26
+ /**
27
+ * 非受控模式的初始值。
28
+ *
29
+ * @default ''
30
+ */
31
+ defaultValue?: string;
32
+
33
+ /** 是否禁用 */
34
+ disabled?: MaybeRefOrGetter<boolean | undefined>;
35
+
36
+ /** 是否只读 */
37
+ readOnly?: MaybeRefOrGetter<boolean | undefined>;
38
+
39
+ /** 是否必填 */
40
+ required?: MaybeRefOrGetter<boolean | undefined>;
41
+
42
+ /** 显式指定所属表单的 id,用于控件放在 form 外部的场景 */
43
+ form?: MaybeRefOrGetter<string | undefined>;
44
+
45
+ /** 值变化回调,受控模式下同样触发,是新值唯一的落地路径 */
46
+ onModelValueChange?: (value: string) => void;
47
+ }
48
+
49
+ /**
50
+ * useTextControl 的返回值。
51
+ */
52
+ export interface UseTextControlReturn {
53
+ /** 当前值:受控时跟随外部来源,否则是内部状态 */
54
+ value: ComputedRef<string>;
55
+
56
+ /** 是否禁用 */
57
+ isDisabled: ComputedRef<boolean>;
58
+
59
+ /** 是否只读 */
60
+ isReadOnly: ComputedRef<boolean>;
61
+
62
+ /** 是否必填 */
63
+ isRequired: ComputedRef<boolean>;
64
+
65
+ /** 控件元素引用 */
66
+ control: Ref<HTMLInputElement | HTMLTextAreaElement | null>;
67
+
68
+ /** 绑定控件元素,传给模板的 `:ref` 或 Slot 的 elRef */
69
+ setControl(node: unknown): void;
70
+
71
+ /** 程序化写值,走与用户输入同一条受控 / 非受控判定 */
72
+ setValue(value: string): void;
73
+
74
+ /** 原生属性与状态钩子:disabled / readonly / required / data-* / aria-required */
75
+ getBaseAttrs(): Record<string, unknown>;
76
+
77
+ /** 原生 input 事件处理器;IME 组合期间不提交 */
78
+ onInput(event: Event): void;
79
+
80
+ /** 原生 compositionstart 处理器 */
81
+ onCompositionStart(): void;
82
+
83
+ /** 原生 compositionend 处理器:组合结束后一次性提交最终值 */
84
+ onCompositionEnd(event: Event): void;
85
+ }
86
+
87
+ /**
88
+ * 文本输入控件的值内核。
89
+ *
90
+ * IME 处理是这里存在的核心理由:中文输入法在 composition 期间逐键派发 input,
91
+ * 此时提交会把拼音碎片写进值真源;因此组合期间只记标记不提交,compositionend
92
+ * 一次性落最终值。受控模式下父级若拒绝新值,DOM 与真源会分叉,提交后立即把
93
+ * DOM 拉回受控值,保证「看到的」与「存下的」一致。
94
+ *
95
+ * 表单 reset 复原到**创建时捕获的初值**(与 useSwitch 的 initialChecked 同一约定):
96
+ * 原生 reset 只改 DOM 值,组件状态是另一套真源,不接 reset 事件两者就会各说各话。
97
+ *
98
+ * @param options 受控来源、默认值与状态配置。
99
+ * @returns 值、状态、属性表与三个原生事件处理器。
100
+ */
101
+ export function useTextControl(options: UseTextControlOptions = {}): UseTextControlReturn {
102
+ const state = useControllable<string>({
103
+ value: () => toValue(options.modelValue),
104
+ defaultValue: options.defaultValue ?? '',
105
+ onChange: (value) => options.onModelValueChange?.(value),
106
+ });
107
+
108
+ const isDisabled = computed(() => toValue(options.disabled) === true);
109
+
110
+ const isReadOnly = computed(() => toValue(options.readOnly) === true);
111
+
112
+ const isRequired = computed(() => toValue(options.required) === true);
113
+
114
+ const control = ref<HTMLInputElement | HTMLTextAreaElement | null>(null);
115
+
116
+ const isComposing = ref(false);
117
+
118
+ /** 表单 reset 要回到的值:在创建时捕获,运行期改 defaultValue 不影响它。 */
119
+ const initialValue = state.value.value;
120
+
121
+ useFormResetListener(
122
+ control,
123
+ () => state.set(initialValue),
124
+ () => toValue(options.form)
125
+ );
126
+
127
+ function setControl(node: unknown): void {
128
+ control.value =
129
+ node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement ? node : null;
130
+ }
131
+
132
+ function setValue(value: string): void {
133
+ state.set(value);
134
+ }
135
+
136
+ /** 从事件目标读值提交;受控被拒时把 DOM 拉回真源,避免显示与存储分叉。 */
137
+ function commitFromElement(event: Event): void {
138
+ const target = event.target;
139
+
140
+ if (!(target instanceof HTMLInputElement) && !(target instanceof HTMLTextAreaElement)) return;
141
+
142
+ state.set(target.value);
143
+
144
+ if (control.value && control.value.value !== state.value.value) {
145
+ control.value.value = state.value.value;
146
+ }
147
+ }
148
+
149
+ function onInput(event: Event): void {
150
+ if (isComposing.value) return;
151
+
152
+ commitFromElement(event);
153
+ }
154
+
155
+ function onCompositionStart(): void {
156
+ isComposing.value = true;
157
+ }
158
+
159
+ function onCompositionEnd(event: Event): void {
160
+ isComposing.value = false;
161
+
162
+ commitFromElement(event);
163
+ }
164
+
165
+ function getBaseAttrs(): Record<string, unknown> {
166
+ return {
167
+ disabled: isDisabled.value || undefined,
168
+ readonly: isReadOnly.value || undefined,
169
+ required: isRequired.value || undefined,
170
+ ...getInputAttrs({
171
+ disabled: isDisabled.value,
172
+ readOnly: isReadOnly.value,
173
+ required: isRequired.value,
174
+ }),
175
+ };
176
+ }
177
+
178
+ return {
179
+ value: state.value,
180
+ isDisabled,
181
+ isReadOnly,
182
+ isRequired,
183
+ control,
184
+ setControl,
185
+ setValue,
186
+ getBaseAttrs,
187
+ onInput,
188
+ onCompositionStart,
189
+ onCompositionEnd,
190
+ };
191
+ }