@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,407 @@
1
+ import { computed, ref, shallowRef, toValue, useId } from 'vue';
2
+
3
+ import {
4
+ TABLE_DEFAULT_MIN_WIDTH,
5
+ TABLE_EXPANDED_KEY_SUFFIX,
6
+ getTableBodyRows,
7
+ getTableCellAttrs,
8
+ getTableColSpan,
9
+ getTableExpandTriggerAttrs,
10
+ getTableFixedOffsets,
11
+ getTableHeadAttrs,
12
+ getNextTableSortState,
13
+ getTableRowAttrs,
14
+ getTableSelectionState,
15
+ getTableSortTriggerAttrs,
16
+ getTableTotalWidth,
17
+ resolveTableCellSpan,
18
+ resolveTableCssSize,
19
+ resolveTableLayout,
20
+ sortTableRows,
21
+ toggleTableAllKeys,
22
+ toggleTableKey,
23
+ } from '@agile-plus/primitives/table';
24
+
25
+ import { useControllable } from '../utils/useControllable';
26
+ import { usePointerDrag } from '../utils/usePointerDrag';
27
+
28
+ import type {
29
+ TableBodyRow,
30
+ TableCellSpan,
31
+ TableColumn,
32
+ TableFixedOffset,
33
+ TableRowData,
34
+ TableSortState,
35
+ } from '@agile-plus/primitives/table';
36
+
37
+ import type { UseTableOptions, UseTableReturn } from './types';
38
+
39
+ /**
40
+ * Table 的 Vue Composable 适配。
41
+ *
42
+ * 排序、选择、展开的转换规则全部委托给 primitives;本层只做几件事:
43
+ * 1. 把三条状态通道各自归一化成受控 / 非受控状态;
44
+ * 2. 把「数据 + 列模型 + 排序」合成排序后的行,再按展开键扁平化成表体序列;
45
+ * 3. 派生全选三态与可选键集合——它们只作用域于当前页;
46
+ * 4. 持有列宽覆盖态,派生固定列几何与布局,并把拖拽改宽收口到 usePointerDrag;
47
+ * 5. 暴露部件可以直接 v-bind 的属性表与单元格合并跨度。
48
+ *
49
+ * 除拖拽监听(由 usePointerDrag 在作用域销毁时自动释放)外没有需要手动清理的资源:
50
+ * 派生值都是 computed,数据变化在读的那一刻反映,不需要 watch 去补救。
51
+ *
52
+ * @param options 表格配置。
53
+ * @returns 派生行数据、状态通道、列宽控制与属性绑定函数集合。
54
+ */
55
+ export function useTable(options: UseTableOptions = {}): UseTableReturn {
56
+ const sortState = useControllable<TableSortState | null>({
57
+ value: options.sort,
58
+ defaultValue: options.defaultSort ?? null,
59
+ onChange: (next) => options.onSortChange?.(next),
60
+ });
61
+
62
+ const selectedState = useControllable<readonly string[]>({
63
+ value: options.selectedKeys,
64
+ defaultValue: options.defaultSelectedKeys ?? [],
65
+ onChange: (next) => options.onSelectedKeysChange?.([...next]),
66
+ });
67
+
68
+ const expandedState = useControllable<readonly string[]>({
69
+ value: options.expandedKeys,
70
+ defaultValue: options.defaultExpandedKeys ?? [],
71
+ onChange: (next) => options.onExpandedKeysChange?.([...next]),
72
+ });
73
+
74
+ const columns = computed<readonly TableColumn[]>(() => toValue(options.columns) ?? []);
75
+
76
+ const isSelectionEnabled = computed(() => toValue(options.selection) === true);
77
+
78
+ const isResizable = computed(() => toValue(options.resizable) === true);
79
+
80
+ /**
81
+ * 用户拖拽产生的列宽覆盖,只记被改过的列。
82
+ *
83
+ * 基础宽度始终来自当前列模型,覆盖只叠在其上:列模型动态变化(增删列、改宽)
84
+ * 时不必重新播种,未被拖拽的列自动跟随新配置,被拖拽的列保留用户意图。
85
+ */
86
+ const widthOverrides = shallowRef<Record<string, number>>({});
87
+
88
+ const effectiveColumns = computed<readonly TableColumn[]>(() =>
89
+ columns.value.map((column) => {
90
+ const override = widthOverrides.value[column.key];
91
+
92
+ return override === undefined ? column : { ...column, width: override };
93
+ })
94
+ );
95
+
96
+ const sortedRows = computed(() =>
97
+ sortTableRows(toValue(options.data) ?? [], columns.value, sortState.value.value)
98
+ );
99
+
100
+ const rows = computed<TableBodyRow[]>(() =>
101
+ getTableBodyRows(sortedRows.value, options.rowKey, expandedState.value.value)
102
+ );
103
+
104
+ /**
105
+ * 键到行数据的索引,选择与展开的守卫都查它。
106
+ *
107
+ * 从渲染序列(而不是原始 data)建索引:键的兜底值是行下标,排序后下标会变,
108
+ * 守卫必须与屏幕上实际渲染的那一行同源,否则禁用判定会落到别的行上。
109
+ */
110
+ const rowMap = computed(() => {
111
+ const map = new Map<string, TableRowData>();
112
+
113
+ for (const item of rows.value) {
114
+ if (item.type === 'data') map.set(item.key, item.row);
115
+ }
116
+
117
+ return map;
118
+ });
119
+
120
+ function canSelectRow(row: TableRowData): boolean {
121
+ return isSelectionEnabled.value && (options.isRowSelectable?.(row) ?? true);
122
+ }
123
+
124
+ const selectableKeys = computed(() =>
125
+ rows.value
126
+ .filter((item) => item.type === 'data' && canSelectRow(item.row))
127
+ .map((item) => item.key)
128
+ );
129
+
130
+ const selectionState = computed(() =>
131
+ getTableSelectionState(selectedState.value.value, selectableKeys.value)
132
+ );
133
+
134
+ // 固定列几何与布局都从「生效列」派生:拖拽改了固定列宽度,后续固定列的
135
+ // sticky 偏移必须随之重算,否则贴边位置会与实际列宽错位。
136
+ const fixedOffsets = computed(() => getTableFixedOffsets(effectiveColumns.value));
137
+
138
+ const layout = computed(() => resolveTableLayout(effectiveColumns.value));
139
+
140
+ const columnWidths = computed<Record<string, string>>(() => {
141
+ const result: Record<string, string> = {};
142
+
143
+ for (const column of effectiveColumns.value) {
144
+ const width = resolveTableCssSize(column.width);
145
+
146
+ if (width !== undefined) result[column.key] = width;
147
+ }
148
+
149
+ return result;
150
+ });
151
+
152
+ const colSpan = computed(() =>
153
+ getTableColSpan(columns.value, isSelectionEnabled.value, toValue(options.expandable) === true)
154
+ );
155
+
156
+ // 最小像素宽度从生效列派生:拖拽改宽后总宽随之更新,横向滚动范围跟着变。
157
+ const totalColumnWidth = computed(() =>
158
+ getTableTotalWidth(effectiveColumns.value, isSelectionEnabled.value, toValue(options.expandable) === true)
159
+ );
160
+
161
+ const uid = useId();
162
+
163
+ const drag = usePointerDrag();
164
+
165
+ const resizingKey = ref<string | null>(null);
166
+
167
+ function setSort(next: TableSortState | null): void {
168
+ const current = sortState.value.value;
169
+ const unchanged =
170
+ current === next ||
171
+ (current !== null && next !== null && current.key === next.key && current.order === next.order);
172
+
173
+ if (unchanged) return;
174
+
175
+ sortState.set(next);
176
+ }
177
+
178
+ function toggleSort(key: string): void {
179
+ const column = columns.value.find((item) => item.key === key);
180
+
181
+ if (!column?.sortable) return;
182
+
183
+ setSort(getNextTableSortState(sortState.value.value, key));
184
+ }
185
+
186
+ function isRowSelected(key: string): boolean {
187
+ return selectedState.value.value.includes(key);
188
+ }
189
+
190
+ function isRowSelectable(key: string): boolean {
191
+ const row = rowMap.value.get(key);
192
+
193
+ return row !== undefined && canSelectRow(row);
194
+ }
195
+
196
+ function toggleRowSelection(key: string, checked?: boolean): void {
197
+ if (!isRowSelectable(key)) return;
198
+
199
+ const next = checked ?? !isRowSelected(key);
200
+
201
+ if (next === isRowSelected(key)) return;
202
+
203
+ selectedState.set(toggleTableKey(selectedState.value.value, key, next));
204
+ }
205
+
206
+ function toggleAllSelection(checked?: boolean): void {
207
+ if (!isSelectionEnabled.value || selectableKeys.value.length === 0) return;
208
+
209
+ const next = checked ?? selectionState.value !== 'checked';
210
+
211
+ // 已经站在目标态上就不算一次操作:全选框 checked 时再点「全选」、
212
+ // 没有任何可选行时点「取消」,都不该冒出 update:selectedKeys。
213
+ if (next && selectionState.value === 'checked') return;
214
+ if (!next && selectionState.value === 'unchecked') return;
215
+
216
+ selectedState.set(toggleTableAllKeys(selectedState.value.value, selectableKeys.value, next));
217
+ }
218
+
219
+ function clearSelection(): void {
220
+ if (selectedState.value.value.length === 0) return;
221
+
222
+ selectedState.set([]);
223
+ }
224
+
225
+ function isRowExpanded(key: string): boolean {
226
+ return expandedState.value.value.includes(key);
227
+ }
228
+
229
+ function toggleRowExpanded(key: string): void {
230
+ if (!rowMap.value.has(key)) return;
231
+
232
+ expandedState.set(toggleTableKey(expandedState.value.value, key, !isRowExpanded(key)));
233
+ }
234
+
235
+ function getExpandedRowId(key: string): string {
236
+ // 触发按钮拿数据行键、展开内容行拿带后缀的派生键,两者必须收敛到同一个 id,
237
+ // 否则 aria-controls 失配。后缀只用于 v-for 键区分,在这里剥掉。
238
+ const dataKey = key.endsWith(TABLE_EXPANDED_KEY_SUFFIX)
239
+ ? key.slice(0, -TABLE_EXPANDED_KEY_SUFFIX.length)
240
+ : key;
241
+
242
+ // 行键可能含空格等 id 非法字符,编码后仍是稳定双射,aria-controls 不会失配。
243
+ return `table-${uid}-expanded-${encodeURIComponent(dataKey)}`;
244
+ }
245
+
246
+ /** 取某列的最小宽度:列模型 minWidth 优先,缺省回退全局默认。 */
247
+ function resolveMinWidth(key: string): number {
248
+ const column = columns.value.find((item) => item.key === key);
249
+ const min = column?.minWidth;
250
+
251
+ return typeof min === 'number' && Number.isFinite(min) && min > 0 ? min : TABLE_DEFAULT_MIN_WIDTH;
252
+ }
253
+
254
+ function setColumnWidth(key: string, width: number): void {
255
+ if (!columns.value.some((item) => item.key === key)) return;
256
+
257
+ const next = Math.max(Math.round(width), resolveMinWidth(key));
258
+
259
+ if (!Number.isFinite(next)) return;
260
+
261
+ widthOverrides.value = { ...widthOverrides.value, [key]: next };
262
+ }
263
+
264
+ function resetColumnWidths(): void {
265
+ widthOverrides.value = {};
266
+ }
267
+
268
+ /**
269
+ * 开始一次列宽拖拽。
270
+ *
271
+ * 起点像素宽度由调用方(ui 层)从 th 的 offsetWidth 量好传入,本层不读 DOM;
272
+ * 移动量换算成新宽度并夹到最小宽度,监听的生命周期由 usePointerDrag 收口。
273
+ * 抬起时派发 onColumnResize,系统中断(cancel)不派发——那不算一次用户提交。
274
+ *
275
+ * @param key 被拖拽的列键。
276
+ * @param startX 按下时的指针横坐标。
277
+ * @param startWidth 按下时该列的像素宽度。
278
+ */
279
+ function startColumnResize(key: string, startX: number, startWidth: number): void {
280
+ if (!isResizable.value) return;
281
+
282
+ resizingKey.value = key;
283
+
284
+ drag.start({
285
+ onMove(event) {
286
+ setColumnWidth(key, startWidth + (event.clientX - startX));
287
+ },
288
+ onEnd(reason) {
289
+ resizingKey.value = null;
290
+
291
+ if (reason === 'up') options.onColumnResize?.(key, widthOverrides.value[key] ?? startWidth);
292
+ },
293
+ });
294
+ }
295
+
296
+ /**
297
+ * 单元格的合并跨度。
298
+ *
299
+ * 未配置 spanMethod 或列不存在时返回 1×1(不合并)。columnIndex 取数据列下标,
300
+ * 不含选择列 / 展开列——合并只作用在数据格上。removed 供渲染层决定要不要画这个 td。
301
+ *
302
+ * @param row 行数据。
303
+ * @param columnKey 列键。
304
+ * @param rowIndex 数据行下标。
305
+ */
306
+ function getCellSpan(
307
+ row: TableRowData,
308
+ columnKey: string,
309
+ rowIndex: number
310
+ ): TableCellSpan & { removed: boolean } {
311
+ const columnIndex = columns.value.findIndex((item) => item.key === columnKey);
312
+ const column = columns.value[columnIndex];
313
+
314
+ if (!options.spanMethod || !column) return { rowspan: 1, colspan: 1, removed: false };
315
+
316
+ const span = resolveTableCellSpan(options.spanMethod({ row, column, rowIndex, columnIndex }));
317
+
318
+ return { ...span, removed: span.rowspan === 0 || span.colspan === 0 };
319
+ }
320
+
321
+ function getRowProps(item: TableBodyRow): Record<string, unknown> {
322
+ return getTableRowAttrs({
323
+ selected: isRowSelected(item.key),
324
+ expanded: isRowExpanded(item.key),
325
+ disabled: isSelectionEnabled.value && !canSelectRow(item.row),
326
+ });
327
+ }
328
+
329
+ /**
330
+ * 固定列几何并入属性表。
331
+ *
332
+ * 没有生效偏移的固定列(缺数字宽度)必须抹掉 data-fixed:样式层见属性就贴
333
+ * sticky,留着它会让该列以偏移 0 贴在边上,压住真正的第一个固定列。
334
+ */
335
+ function withFixedGeometry(
336
+ attrs: Record<string, unknown>,
337
+ key: string
338
+ ): Record<string, unknown> {
339
+ const fixed: TableFixedOffset | undefined = fixedOffsets.value[key];
340
+
341
+ if (!fixed) return { ...attrs, 'data-fixed': undefined };
342
+
343
+ return {
344
+ ...attrs,
345
+ 'data-fixed': fixed.side,
346
+ 'data-fixed-edge': fixed.edge ? fixed.side : undefined,
347
+ style: { '--y-table-fixed-offset': `${fixed.offset}px` },
348
+ };
349
+ }
350
+
351
+ function getHeadProps(key: string): Record<string, unknown> {
352
+ const column = effectiveColumns.value.find((item) => item.key === key);
353
+
354
+ return withFixedGeometry(getTableHeadAttrs(column, sortState.value.value), key);
355
+ }
356
+
357
+ function getCellProps(key: string): Record<string, unknown> {
358
+ const column = effectiveColumns.value.find((item) => item.key === key);
359
+
360
+ return withFixedGeometry(getTableCellAttrs(column), key);
361
+ }
362
+
363
+ function getSortTriggerProps(key: string): Record<string, unknown> {
364
+ return getTableSortTriggerAttrs(key, sortState.value.value);
365
+ }
366
+
367
+ function getExpandTriggerProps(key: string): Record<string, unknown> {
368
+ return getTableExpandTriggerAttrs(isRowExpanded(key), getExpandedRowId(key));
369
+ }
370
+
371
+ return {
372
+ columns,
373
+ sortedRows,
374
+ rows,
375
+ sort: sortState.value,
376
+ toggleSort,
377
+ setSort,
378
+ selectedKeys: selectedState.value,
379
+ selectionState,
380
+ selectableKeys,
381
+ isRowSelected,
382
+ isRowSelectable,
383
+ toggleRowSelection,
384
+ toggleAllSelection,
385
+ clearSelection,
386
+ expandedKeys: expandedState.value,
387
+ isRowExpanded,
388
+ toggleRowExpanded,
389
+ fixedOffsets,
390
+ layout,
391
+ colSpan,
392
+ totalColumnWidth,
393
+ resizable: isResizable,
394
+ columnWidths,
395
+ resizingKey,
396
+ setColumnWidth,
397
+ resetColumnWidths,
398
+ startColumnResize,
399
+ getCellSpan,
400
+ getRowProps,
401
+ getHeadProps,
402
+ getCellProps,
403
+ getSortTriggerProps,
404
+ getExpandTriggerProps,
405
+ getExpandedRowId,
406
+ };
407
+ }
@@ -49,8 +49,12 @@ export interface UseTabsReturn {
49
49
  *
50
50
  * 由 TabsTrigger 在创建时调用,value 重复注册时幂等忽略。
51
51
  * 注册顺序决定键盘导航顺序,因此必须与 DOM 顺序一致。
52
+ *
53
+ * @param value tab 的唯一 value。
54
+ * @param element 对应的 trigger 元素。键盘导航直接聚焦这里登记的元素,
55
+ * 不做 `document.getElementById` 全局查询,因此 Shadow DOM 与多实例重名都安全。
52
56
  */
53
- registerTab(value: string): void;
57
+ registerTab(value: string, element?: HTMLElement | null): void;
54
58
 
55
59
  /** 注销一个 tab,由 TabsTrigger 卸载时调用 */
56
60
  unregisterTab(value: string): void;
@@ -25,6 +25,14 @@ export function useTabs(options: UseTabsOptions = {}): UseTabsReturn {
25
25
  */
26
26
  const uid = useId();
27
27
 
28
+ /**
29
+ * trigger 元素登记表。
30
+ *
31
+ * 键盘导航必须用这里登记的元素入口,而不是 `document.getElementById`:
32
+ * 后者拿不到 Shadow DOM 内的节点,多实例 value 重名时还会聚焦到别的标签页。
33
+ */
34
+ const triggerElements = new Map<string, HTMLElement>();
35
+
28
36
  function getTriggerId(tabValue: string): string {
29
37
  return `${uid}-tab-${tabValue}`;
30
38
  }
@@ -44,6 +52,7 @@ export function useTabs(options: UseTabsOptions = {}): UseTabsReturn {
44
52
  });
45
53
 
46
54
  const internalActive = ref(machine.getState().activeValue);
55
+ const internalFocused = ref<string | null>(machine.getState().focusedValue);
47
56
 
48
57
  const activeValue = computed(() =>
49
58
  isControlled.value ? controlledActive.value! : internalActive.value
@@ -52,8 +61,8 @@ export function useTabs(options: UseTabsOptions = {}): UseTabsReturn {
52
61
  /**
53
62
  * 状态机是唯一的状态来源,向外部广播变化。
54
63
  *
55
- * 键盘导航(方向键 / Home / End)通过 FOCUS_TAB 改变激活值而非 setActive,
56
- * 因此必须在订阅回调中上报 onValueChange,否则受控模式下方向键无效。
64
+ * 键盘导航只通过 FOCUS_TAB 移动焦点;激活统一走 SET_ACTIVE(见 onTriggerKeydown),
65
+ * 所以这里只在激活值真的变化时上报,受控值不会被焦点态悄悄改写。
57
66
  * changed 需在写入 internalActive 之前捕获,否则比较恒等。
58
67
  */
59
68
  const unsubscribe = machine.subscribe((state) => {
@@ -63,6 +72,10 @@ export function useTabs(options: UseTabsOptions = {}): UseTabsReturn {
63
72
  internalActive.value = state.activeValue;
64
73
  }
65
74
 
75
+ // 焦点是独立的响应式状态:manual 模式下它变化而 activeValue 不变,
76
+ // 依赖 getTriggerProps 的 computed 也必须跟着重算 roving tabindex。
77
+ internalFocused.value = state.focusedValue;
78
+
66
79
  if (changed) {
67
80
  options.onValueChange?.(state.activeValue);
68
81
  }
@@ -99,12 +112,15 @@ export function useTabs(options: UseTabsOptions = {}): UseTabsReturn {
99
112
  machine.send({ type: 'SET_ACTIVE', payload: value });
100
113
  }
101
114
 
102
- function registerTab(value: string): void {
115
+ function registerTab(value: string, element?: HTMLElement | null): void {
103
116
  machine.send({ type: 'REGISTER_TAB', payload: value });
117
+
118
+ if (element) triggerElements.set(value, element);
104
119
  }
105
120
 
106
121
  function unregisterTab(value: string): void {
107
122
  machine.send({ type: 'UNREGISTER_TAB', payload: value });
123
+ triggerElements.delete(value);
108
124
  }
109
125
 
110
126
  /**
@@ -128,9 +144,13 @@ export function useTabs(options: UseTabsOptions = {}): UseTabsReturn {
128
144
  }
129
145
 
130
146
  if (result.nextFocusedValue !== null) {
131
- machine.send({ type: 'FOCUS_TAB', payload: result.nextFocusedValue });
147
+ const nextValue = result.nextFocusedValue;
148
+
149
+ // 焦点态与激活态解耦:先只移动焦点,automatic 再显式走 setActive 触发激活。
150
+ machine.send({ type: 'FOCUS_TAB', payload: nextValue });
151
+ triggerElements.get(nextValue)?.focus();
132
152
 
133
- document.getElementById(getTriggerId(result.nextFocusedValue))?.focus();
153
+ if (activationMode === 'automatic' && result.shouldActivate) setActive(nextValue);
134
154
  }
135
155
 
136
156
  if (event.key === 'Enter' || event.key === ' ') {
@@ -157,13 +177,17 @@ export function useTabs(options: UseTabsOptions = {}): UseTabsReturn {
157
177
 
158
178
  function getTriggerProps(value: string): Record<string, unknown> {
159
179
  const isActive = activeValue.value === value;
180
+ // roving tabindex 跟的是真实焦点而非激活值:manual 模式下二者分离,
181
+ // 若用 activeValue 判定,方向键移过去的按钮仍不可 Tab 访问。
182
+ const focused = internalFocused.value ?? activeValue.value;
183
+ const isFocused = focused === value;
160
184
 
161
185
  return {
162
186
  role: 'tab' as const,
163
187
  'aria-selected': isActive,
164
188
  'aria-controls': getPanelId(value),
165
189
  id: getTriggerId(value),
166
- tabindex: isActive ? 0 : -1,
190
+ tabindex: isFocused ? 0 : -1,
167
191
  'data-state': isActive ? 'active' : 'inactive',
168
192
  'data-value': value,
169
193
  };
@@ -0,0 +1,3 @@
1
+ export { useTextarea } from './useTextarea';
2
+
3
+ export type { UseTextareaOptions, UseTextareaReturn } from './types';
@@ -0,0 +1,17 @@
1
+ import type { UseTextControlOptions, UseTextControlReturn } from '../utils/useTextControl';
2
+
3
+ /**
4
+ * useTextarea 的配置项。
5
+ *
6
+ * 与 useInput 相比没有 type:textarea 不接受该属性。rows / wrap 等原生属性
7
+ * 由封装层透传,不进入值内核。
8
+ */
9
+ export type UseTextareaOptions = UseTextControlOptions;
10
+
11
+ /**
12
+ * useTextarea 的返回值。
13
+ */
14
+ export interface UseTextareaReturn extends Omit<UseTextControlReturn, 'getBaseAttrs'> {
15
+ /** 根元素属性表:原生状态属性 + data-* / aria 钩子 */
16
+ getRootProps(): Record<string, unknown>;
17
+ }
@@ -0,0 +1,34 @@
1
+ import { useTextControl } from '../utils/useTextControl';
2
+
3
+ import type { UseTextareaOptions, UseTextareaReturn } from './types';
4
+
5
+ /**
6
+ * Textarea 的 Vue Composable 适配。
7
+ *
8
+ * 与 useInput 共用 useTextControl 值内核,受控语义、IME 组合输入与表单 reset
9
+ * 复原完全一致;差异只有渲染目标(textarea 没有 type 属性)。
10
+ *
11
+ * @param options Composable 配置项。
12
+ * @returns 值、状态、根属性表与原生事件处理器。
13
+ */
14
+ export function useTextarea(options: UseTextareaOptions = {}): UseTextareaReturn {
15
+ const text = useTextControl(options);
16
+
17
+ function getRootProps(): Record<string, unknown> {
18
+ return text.getBaseAttrs();
19
+ }
20
+
21
+ return {
22
+ value: text.value,
23
+ isDisabled: text.isDisabled,
24
+ isReadOnly: text.isReadOnly,
25
+ isRequired: text.isRequired,
26
+ control: text.control,
27
+ setControl: text.setControl,
28
+ setValue: text.setValue,
29
+ getRootProps,
30
+ onInput: text.onInput,
31
+ onCompositionStart: text.onCompositionStart,
32
+ onCompositionEnd: text.onCompositionEnd,
33
+ };
34
+ }
@@ -76,6 +76,8 @@ export function useToast(options: UseToastOptions): UseToastReturn {
76
76
 
77
77
  let timerId = 0;
78
78
  let snapshot = createToastTimerSnapshot(duration.value, isBrowser ? Date.now() : 0);
79
+ let timerKey = '';
80
+ let isCounting = false;
79
81
 
80
82
  function setOpen(value: boolean): void {
81
83
  state.set(value);
@@ -100,49 +102,64 @@ export function useToast(options: UseToastOptions): UseToastReturn {
100
102
  }
101
103
 
102
104
  /**
103
- * 启动(或重启)一段计时。
105
+ * 计时的唯一入口:watch 与 Provider 的 pause/resume 广播都经过这里。
104
106
  *
105
- * 快照与句柄必须一起更新:暂停时要从快照扣时间,而重入时旧的 setTimeout 已经无效。
107
+ * 之前两条路径各自维护快照,初始化时订阅先于 watch 注册,暂停广播会先扣一份快照、
108
+ * 紧接着 watch 又重建一份,恢复时按错误剩余时间续期。现在用 `(open, duration)` 作为
109
+ * 重建键:只有这两个输入变化才重置快照,暂停/恢复只在同一份快照上扣减或续期。
110
+ *
111
+ * @param open 通知是否打开。
112
+ * @param paused Provider 当前是否暂停关闭。
113
+ * @param nextDuration 本条通知的有效时长。
106
114
  */
107
- function startTimer(delay: number): void {
108
- if (isToastTimerDisabled(delay)) return;
115
+ function syncTimer(open: boolean, paused: boolean, nextDuration: number): void {
116
+ if (!isBrowser) return;
109
117
 
110
- window.clearTimeout(timerId);
111
- snapshot = createToastTimerSnapshot(delay, Date.now());
112
- timerId = window.setTimeout(handleClose, delay);
113
- }
118
+ const key = `${open ? 'open' : 'closed'}|${String(nextDuration)}`;
114
119
 
115
- function pauseTimer(): void {
116
- snapshot = pauseToastTimer(snapshot, Date.now());
117
- window.clearTimeout(timerId);
118
- options.onPause?.();
119
- }
120
+ if (key !== timerKey) {
121
+ timerKey = key;
122
+ window.clearTimeout(timerId);
123
+ timerId = 0;
124
+ isCounting = false;
125
+ // open 或 duration 变化都重置剩余时间:常驻通知改成有限时长后,
126
+ // 若沿用 Infinity 的剩余值就永远不会关闭(Radix issue #2233)。
127
+ snapshot = createToastTimerSnapshot(nextDuration, Date.now());
128
+ }
129
+
130
+ if (!open) return;
131
+
132
+ if (paused || isToastTimerDisabled(nextDuration)) {
133
+ if (isCounting) snapshot = pauseToastTimer(snapshot, Date.now());
134
+
135
+ isCounting = false;
136
+ window.clearTimeout(timerId);
137
+ timerId = 0;
138
+
139
+ return;
140
+ }
141
+
142
+ if (isCounting) return;
120
143
 
121
- function resumeTimer(): void {
122
144
  snapshot = resumeToastTimer(snapshot, Date.now());
123
- startTimer(snapshot.remaining);
124
- options.onResume?.();
145
+ isCounting = true;
146
+ timerId = window.setTimeout(handleClose, snapshot.remaining);
125
147
  }
126
148
 
127
149
  // 正在关闭的通知不再响应暂停/恢复:动画期间重设计时只会留下一个无意义的定时器。
128
150
  const unsubscribeClosing = provider.subscribeClosing((paused) => {
129
151
  if (!isOpen.value) return;
130
152
 
131
- if (paused) pauseTimer();
132
- else resumeTimer();
153
+ syncTimer(true, paused, duration.value);
154
+
155
+ if (paused) options.onPause?.();
156
+ else options.onResume?.();
133
157
  });
134
158
 
135
159
  watch(
136
160
  [isOpen, duration],
137
161
  ([open, nextDuration]) => {
138
- // 每次 open/duration 变化都重置剩余时间:常驻加载通知改成有限时长后,
139
- // 若沿用 Infinity 的剩余值就永远不会关闭(Radix issue #2233)。
140
- snapshot = createToastTimerSnapshot(nextDuration, Date.now());
141
-
142
- if (!isBrowser) return;
143
-
144
- if (open && !provider.isClosePaused.value) startTimer(nextDuration);
145
- else window.clearTimeout(timerId);
162
+ syncTimer(open, provider.isClosePaused.value, nextDuration);
146
163
  },
147
164
  { immediate: true }
148
165
  );