@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.
- package/README.md +24 -17
- package/dist/index.cjs +4163 -223
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +4258 -216
- package/dist/index.js.map +1 -1
- package/dist/types-BxsQI7P5.d.cts +93 -0
- package/dist/types-BxsQI7P5.d.ts +93 -0
- package/dist/types-zpeY0yvD.d.cts +73 -0
- package/dist/types-zpeY0yvD.d.ts +73 -0
- package/dist/useAccordion/index.cjs +149 -0
- package/dist/useAccordion/index.cjs.map +1 -0
- package/dist/useAccordion/index.d.cts +119 -0
- package/dist/useAccordion/index.d.ts +119 -0
- package/dist/useAccordion/index.js +128 -0
- package/dist/useAccordion/index.js.map +1 -0
- package/dist/useAvatar/index.cjs +135 -0
- package/dist/useAvatar/index.cjs.map +1 -0
- package/dist/useAvatar/index.d.cts +120 -0
- package/dist/useAvatar/index.d.ts +120 -0
- package/dist/useAvatar/index.js +106 -0
- package/dist/useAvatar/index.js.map +1 -0
- package/dist/useCheckbox/index.cjs +3 -1
- package/dist/useCheckbox/index.cjs.map +1 -1
- package/dist/useCheckbox/index.js +3 -1
- package/dist/useCheckbox/index.js.map +1 -1
- package/dist/useCollapsible/index.cjs +10 -0
- package/dist/useCollapsible/index.cjs.map +1 -1
- package/dist/useCollapsible/index.js +11 -1
- package/dist/useCollapsible/index.js.map +1 -1
- package/dist/useDatePicker/index.cjs +684 -0
- package/dist/useDatePicker/index.cjs.map +1 -0
- package/dist/useDatePicker/index.d.cts +191 -0
- package/dist/useDatePicker/index.d.ts +191 -0
- package/dist/useDatePicker/index.js +671 -0
- package/dist/useDatePicker/index.js.map +1 -0
- package/dist/useDateRangePicker/index.cjs +614 -0
- package/dist/useDateRangePicker/index.cjs.map +1 -0
- package/dist/useDateRangePicker/index.d.cts +159 -0
- package/dist/useDateRangePicker/index.d.ts +159 -0
- package/dist/useDateRangePicker/index.js +601 -0
- package/dist/useDateRangePicker/index.js.map +1 -0
- package/dist/useDialog/index.cjs.map +1 -1
- package/dist/useDialog/index.js.map +1 -1
- package/dist/useDropdownMenu/index.cjs +468 -0
- package/dist/useDropdownMenu/index.cjs.map +1 -0
- package/dist/useDropdownMenu/index.d.cts +73 -0
- package/dist/useDropdownMenu/index.d.ts +73 -0
- package/dist/useDropdownMenu/index.js +441 -0
- package/dist/useDropdownMenu/index.js.map +1 -0
- package/dist/useEllipsis/index.cjs +181 -0
- package/dist/useEllipsis/index.cjs.map +1 -0
- package/dist/useEllipsis/index.d.cts +67 -0
- package/dist/useEllipsis/index.d.ts +67 -0
- package/dist/useEllipsis/index.js +162 -0
- package/dist/useEllipsis/index.js.map +1 -0
- package/dist/useFloating/index.cjs +177 -0
- package/dist/useFloating/index.cjs.map +1 -0
- package/dist/useFloating/index.d.cts +21 -0
- package/dist/useFloating/index.d.ts +21 -0
- package/dist/useFloating/index.js +150 -0
- package/dist/useFloating/index.js.map +1 -0
- package/dist/useFormControl/index.cjs +5 -0
- package/dist/useFormControl/index.cjs.map +1 -1
- package/dist/useFormControl/index.js +5 -0
- package/dist/useFormControl/index.js.map +1 -1
- package/dist/useInput/index.cjs +192 -0
- package/dist/useInput/index.cjs.map +1 -0
- package/dist/useInput/index.d.cts +41 -0
- package/dist/useInput/index.d.ts +41 -0
- package/dist/useInput/index.js +165 -0
- package/dist/useInput/index.js.map +1 -0
- package/dist/useKeepMounted/index.cjs +44 -0
- package/dist/useKeepMounted/index.cjs.map +1 -0
- package/dist/useKeepMounted/index.d.cts +45 -0
- package/dist/useKeepMounted/index.d.ts +45 -0
- package/dist/useKeepMounted/index.js +17 -0
- package/dist/useKeepMounted/index.js.map +1 -0
- package/dist/useMenu/index.cjs +438 -0
- package/dist/useMenu/index.cjs.map +1 -0
- package/dist/useMenu/index.d.cts +134 -0
- package/dist/useMenu/index.d.ts +134 -0
- package/dist/useMenu/index.js +419 -0
- package/dist/useMenu/index.js.map +1 -0
- package/dist/usePagination/index.cjs +113 -0
- package/dist/usePagination/index.cjs.map +1 -0
- package/dist/usePagination/index.d.cts +139 -0
- package/dist/usePagination/index.d.ts +139 -0
- package/dist/usePagination/index.js +95 -0
- package/dist/usePagination/index.js.map +1 -0
- package/dist/usePopover/index.cjs +312 -0
- package/dist/usePopover/index.cjs.map +1 -0
- package/dist/usePopover/index.d.cts +71 -0
- package/dist/usePopover/index.d.ts +71 -0
- package/dist/usePopover/index.js +285 -0
- package/dist/usePopover/index.js.map +1 -0
- package/dist/useSelect/index.cjs +729 -0
- package/dist/useSelect/index.cjs.map +1 -0
- package/dist/useSelect/index.d.cts +193 -0
- package/dist/useSelect/index.d.ts +193 -0
- package/dist/useSelect/index.js +710 -0
- package/dist/useSelect/index.js.map +1 -0
- package/dist/useSplitter/index.cjs +558 -0
- package/dist/useSplitter/index.cjs.map +1 -0
- package/dist/useSplitter/index.d.cts +307 -0
- package/dist/useSplitter/index.d.ts +307 -0
- package/dist/useSplitter/index.js +541 -0
- package/dist/useSplitter/index.js.map +1 -0
- package/dist/useSwitch/index.cjs +4 -1
- package/dist/useSwitch/index.cjs.map +1 -1
- package/dist/useSwitch/index.js +4 -1
- package/dist/useSwitch/index.js.map +1 -1
- package/dist/useTable/index.cjs +308 -0
- package/dist/useTable/index.cjs.map +1 -0
- package/dist/useTable/index.d.cts +242 -0
- package/dist/useTable/index.d.ts +242 -0
- package/dist/useTable/index.js +301 -0
- package/dist/useTable/index.js.map +1 -0
- package/dist/useTabs/index.cjs +13 -4
- package/dist/useTabs/index.cjs.map +1 -1
- package/dist/useTabs/index.d.cts +5 -1
- package/dist/useTabs/index.d.ts +5 -1
- package/dist/useTabs/index.js +13 -4
- package/dist/useTabs/index.js.map +1 -1
- package/dist/useTextControl-Dcq0GFmg.d.cts +63 -0
- package/dist/useTextControl-Dcq0GFmg.d.ts +63 -0
- package/dist/useTextarea/index.cjs +186 -0
- package/dist/useTextarea/index.cjs.map +1 -0
- package/dist/useTextarea/index.d.cts +30 -0
- package/dist/useTextarea/index.d.ts +30 -0
- package/dist/useTextarea/index.js +159 -0
- package/dist/useTextarea/index.js.map +1 -0
- package/dist/useToast/index.cjs +27 -20
- package/dist/useToast/index.cjs.map +1 -1
- package/dist/useToast/index.js +27 -20
- package/dist/useToast/index.js.map +1 -1
- package/dist/useToastViewport/index.cjs +1 -3
- package/dist/useToastViewport/index.cjs.map +1 -1
- package/dist/useToastViewport/index.js +1 -3
- package/dist/useToastViewport/index.js.map +1 -1
- package/dist/useTooltip/index.cjs +357 -0
- package/dist/useTooltip/index.cjs.map +1 -0
- package/dist/useTooltip/index.d.cts +81 -0
- package/dist/useTooltip/index.d.ts +81 -0
- package/dist/useTooltip/index.js +338 -0
- package/dist/useTooltip/index.js.map +1 -0
- package/dist/useVirtualList/index.cjs +308 -0
- package/dist/useVirtualList/index.cjs.map +1 -0
- package/dist/useVirtualList/index.d.cts +31 -0
- package/dist/useVirtualList/index.d.ts +31 -0
- package/dist/useVirtualList/index.js +288 -0
- package/dist/useVirtualList/index.js.map +1 -0
- package/dist/useVirtualTree/index.cjs +520 -0
- package/dist/useVirtualTree/index.cjs.map +1 -0
- package/dist/useVirtualTree/index.d.cts +146 -0
- package/dist/useVirtualTree/index.d.ts +146 -0
- package/dist/useVirtualTree/index.js +511 -0
- package/dist/useVirtualTree/index.js.map +1 -0
- package/package.json +121 -2
- package/src/index.ts +38 -0
- package/src/useAccordion/index.ts +7 -0
- package/src/useAccordion/types.ts +118 -0
- package/src/useAccordion/useAccordion.ts +130 -0
- package/src/useAvatar/index.ts +14 -0
- package/src/useAvatar/types.ts +90 -0
- package/src/useAvatar/useAvatar.ts +39 -0
- package/src/useAvatar/useAvatarFallback.ts +62 -0
- package/src/useAvatar/useAvatarImage.ts +78 -0
- package/src/useCheckbox/useCheckbox.ts +4 -1
- package/src/useCollapsible/useCollapsible.ts +22 -1
- package/src/useDatePicker/index.ts +9 -0
- package/src/useDatePicker/types.ts +177 -0
- package/src/useDatePicker/useDatePicker.ts +688 -0
- package/src/useDateRangePicker/index.ts +8 -0
- package/src/useDateRangePicker/types.ts +146 -0
- package/src/useDateRangePicker/useDateRangePicker.ts +618 -0
- package/src/useDropdownMenu/index.ts +3 -0
- package/src/useDropdownMenu/types.ts +61 -0
- package/src/useDropdownMenu/useDropdownMenu.ts +322 -0
- package/src/useEllipsis/index.ts +3 -0
- package/src/useEllipsis/types.ts +51 -0
- package/src/useEllipsis/useEllipsis.ts +169 -0
- package/src/useFloating/index.ts +3 -0
- package/src/useFloating/types.ts +79 -0
- package/src/useFloating/useFloating.ts +199 -0
- package/src/useFormControl/useFormControl.ts +13 -0
- package/src/useInput/index.ts +3 -0
- package/src/useInput/types.ts +29 -0
- package/src/useInput/useInput.ts +40 -0
- package/src/useKeepMounted/index.ts +3 -0
- package/src/useKeepMounted/types.ts +29 -0
- package/src/useKeepMounted/useKeepMounted.ts +34 -0
- package/src/useMenu/index.ts +9 -0
- package/src/useMenu/types.ts +126 -0
- package/src/useMenu/useMenu.ts +504 -0
- package/src/usePagination/index.ts +3 -0
- package/src/usePagination/types.ts +137 -0
- package/src/usePagination/usePagination.ts +109 -0
- package/src/usePopover/index.ts +3 -0
- package/src/usePopover/types.ts +65 -0
- package/src/usePopover/usePopover.ts +157 -0
- package/src/useSelect/index.ts +9 -0
- package/src/useSelect/types.ts +184 -0
- package/src/useSelect/useSelect.ts +713 -0
- package/src/useSplitter/index.ts +10 -0
- package/src/useSplitter/types.ts +336 -0
- package/src/useSplitter/useSplitter.ts +648 -0
- package/src/useSplitter/useSplitterPanel.ts +106 -0
- package/src/useSwitch/useSwitch.ts +6 -1
- package/src/useTable/index.ts +3 -0
- package/src/useTable/types.ts +282 -0
- package/src/useTable/useTable.ts +407 -0
- package/src/useTabs/types.ts +5 -1
- package/src/useTabs/useTabs.ts +30 -6
- package/src/useTextarea/index.ts +3 -0
- package/src/useTextarea/types.ts +17 -0
- package/src/useTextarea/useTextarea.ts +34 -0
- package/src/useToast/useToast.ts +43 -26
- package/src/useToastViewport/useToastViewport.ts +10 -9
- package/src/useTooltip/index.ts +3 -0
- package/src/useTooltip/types.ts +72 -0
- package/src/useTooltip/useTooltip.ts +240 -0
- package/src/useVirtualList/index.ts +8 -0
- package/src/useVirtualList/types.ts +95 -0
- package/src/useVirtualList/useVirtualList.ts +478 -0
- package/src/useVirtualTree/index.ts +3 -0
- package/src/useVirtualTree/types.ts +179 -0
- package/src/useVirtualTree/useVirtualTree.ts +342 -0
- package/src/utils/useFocusTrap.ts +2 -0
- package/src/utils/useTextControl.ts +191 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { computed, nextTick, onScopeDispose, ref, toValue, watch } from 'vue';
|
|
2
|
+
|
|
3
|
+
import { computeAvailableSpace, computeFloatingPosition } from '@agile-plus/primitives/floating';
|
|
4
|
+
|
|
5
|
+
import { isBrowser } from '../utils/env';
|
|
6
|
+
import { useResizeObserver } from '../utils/useResizeObserver';
|
|
7
|
+
|
|
8
|
+
import type { CSSProperties } from 'vue';
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
FloatingArrowPosition,
|
|
12
|
+
FloatingAvailableSpace,
|
|
13
|
+
FloatingPlacement,
|
|
14
|
+
FloatingSnapshot,
|
|
15
|
+
} from '@agile-plus/primitives/floating';
|
|
16
|
+
|
|
17
|
+
import type { UseFloatingOptions, UseFloatingReturn } from './types';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 浮层定位的 Vue 适配。
|
|
21
|
+
*
|
|
22
|
+
* 职责边界:几何计算全部委托 primitives,本层只做三件事——
|
|
23
|
+
*
|
|
24
|
+
* 1. **Measure**:一次性把锚点、浮层、视口、边界的几何读成快照。读操作集中在一个阶段,
|
|
25
|
+
* 之后不再碰布局,避免读写交错触发强制同步布局。
|
|
26
|
+
* 2. **Calculate**:把快照交给纯函数,得到坐标与实际 placement。
|
|
27
|
+
* 3. **Commit**:把结果写进响应式状态,样式由 computed 派生,写入交给 Vue 的渲染批次。
|
|
28
|
+
*
|
|
29
|
+
* scroll / resize 都是高频事件,统一经 rAF 合并成一次测量;同一帧内重复请求不会排多个任务。
|
|
30
|
+
* 关闭时不挂监听也不测量:不可见的浮层跟随滚动是纯浪费,且此时尺寸为 0,测了也没意义。
|
|
31
|
+
*/
|
|
32
|
+
export function useFloating(options: UseFloatingOptions = {}): UseFloatingReturn {
|
|
33
|
+
const referenceRef = ref<HTMLElement | null>(null);
|
|
34
|
+
const floatingRef = ref<HTMLElement | null>(null);
|
|
35
|
+
|
|
36
|
+
const x = ref(0);
|
|
37
|
+
const y = ref(0);
|
|
38
|
+
const placement = ref<FloatingPlacement>(toValue(options.placement) ?? 'bottom');
|
|
39
|
+
const arrow = ref<FloatingArrowPosition | null>(null);
|
|
40
|
+
const availableSpace = ref<FloatingAvailableSpace | null>(null);
|
|
41
|
+
|
|
42
|
+
const isOpen = computed(() => toValue(options.open) ?? true);
|
|
43
|
+
// 默认 absolute:portal 到 body 后浮层落在文档层,窗口滚动由合成器带着它一起移动,
|
|
44
|
+
// 主线程不必逐帧追。fixed 浮层停在视口层,合成器推进滚动的中间帧里它还在旧位置,
|
|
45
|
+
// 实测表现为滚动时浮层抖动、甚至压到触发元素上(间距在 -33…8px 间逐帧跳)。
|
|
46
|
+
const strategy = computed(() => options.strategy ?? 'absolute');
|
|
47
|
+
|
|
48
|
+
let frame: number | null = null;
|
|
49
|
+
|
|
50
|
+
const resize = useResizeObserver(requestUpdate);
|
|
51
|
+
|
|
52
|
+
/** 合并同一帧内的多次更新请求;没有浏览器环境时直接返回。 */
|
|
53
|
+
function requestUpdate(): void {
|
|
54
|
+
if (!isBrowser || frame !== null) return;
|
|
55
|
+
|
|
56
|
+
frame = window.requestAnimationFrame(() => {
|
|
57
|
+
frame = null;
|
|
58
|
+
|
|
59
|
+
measure();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 一次完整的 Measure → Calculate → Commit。 */
|
|
64
|
+
function measure(): void {
|
|
65
|
+
const reference = referenceRef.value;
|
|
66
|
+
const floating = floatingRef.value;
|
|
67
|
+
|
|
68
|
+
if (!reference || !floating) return;
|
|
69
|
+
|
|
70
|
+
// ---- Measure:本阶段之后不再读取任何布局信息 ----
|
|
71
|
+
const referenceRect = reference.getBoundingClientRect();
|
|
72
|
+
const floatingRect = floating.getBoundingClientRect();
|
|
73
|
+
const boundaryRect = toValue(options.boundary)?.getBoundingClientRect();
|
|
74
|
+
const viewport = { width: window.innerWidth, height: window.innerHeight };
|
|
75
|
+
const scrollX = window.scrollX;
|
|
76
|
+
const scrollY = window.scrollY;
|
|
77
|
+
|
|
78
|
+
const snapshot: FloatingSnapshot = {
|
|
79
|
+
reference: {
|
|
80
|
+
x: referenceRect.x,
|
|
81
|
+
y: referenceRect.y,
|
|
82
|
+
width: referenceRect.width,
|
|
83
|
+
height: referenceRect.height,
|
|
84
|
+
},
|
|
85
|
+
// 浮层自己的 x/y 是上一轮结果,传 0 以免本轮输出依赖历史状态。
|
|
86
|
+
floating: { x: 0, y: 0, width: floatingRect.width, height: floatingRect.height },
|
|
87
|
+
viewport,
|
|
88
|
+
...(boundaryRect
|
|
89
|
+
? {
|
|
90
|
+
boundary: {
|
|
91
|
+
x: boundaryRect.x,
|
|
92
|
+
y: boundaryRect.y,
|
|
93
|
+
width: boundaryRect.width,
|
|
94
|
+
height: boundaryRect.height,
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
: {}),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// ---- Calculate ----
|
|
101
|
+
const floatingOptions = {
|
|
102
|
+
placement: toValue(options.placement),
|
|
103
|
+
offset: options.offset,
|
|
104
|
+
flip: options.flip,
|
|
105
|
+
shift: options.shift,
|
|
106
|
+
padding: options.padding,
|
|
107
|
+
arrow: options.arrow,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const result = computeFloatingPosition(snapshot, floatingOptions);
|
|
111
|
+
|
|
112
|
+
// ---- Commit ----
|
|
113
|
+
// 默认 absolute 用页面坐标:浮层 portal 到 body 时 offsetParent 就是文档,见 types 里的前提说明。
|
|
114
|
+
x.value = strategy.value === 'absolute' ? result.x + scrollX : result.x;
|
|
115
|
+
y.value = strategy.value === 'absolute' ? result.y + scrollY : result.y;
|
|
116
|
+
placement.value = result.placement;
|
|
117
|
+
arrow.value = result.arrow;
|
|
118
|
+
// 可用空间与坐标出自同一份快照:消费者限制浮层最大高度时不必再量一次 DOM。
|
|
119
|
+
availableSpace.value = computeAvailableSpace(snapshot, result.placement, floatingOptions);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 位移用 transform 而不是 left/top 逐帧改:transform 不触发布局,
|
|
124
|
+
* 滚动跟随这种每帧写入的场景差别明显。left/top 固定为 0 提供定位原点。
|
|
125
|
+
*/
|
|
126
|
+
const floatingStyle = computed<CSSProperties>(() => ({
|
|
127
|
+
position: strategy.value,
|
|
128
|
+
left: 0,
|
|
129
|
+
top: 0,
|
|
130
|
+
transform: `translate(${x.value}px, ${y.value}px)`,
|
|
131
|
+
}));
|
|
132
|
+
|
|
133
|
+
const arrowStyle = computed<CSSProperties>(() => {
|
|
134
|
+
const offset = arrow.value;
|
|
135
|
+
|
|
136
|
+
if (!offset) return {};
|
|
137
|
+
|
|
138
|
+
return offset.x !== null
|
|
139
|
+
? { position: 'absolute', left: `${offset.x}px` }
|
|
140
|
+
: { position: 'absolute', top: `${offset.y}px` };
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
function attach(): void {
|
|
144
|
+
// capture 才能收到祖先滚动容器派发的 scroll(它不冒泡)。
|
|
145
|
+
window.addEventListener('scroll', requestUpdate, true);
|
|
146
|
+
window.addEventListener('resize', requestUpdate);
|
|
147
|
+
|
|
148
|
+
resize.observe(referenceRef.value);
|
|
149
|
+
resize.observe(floatingRef.value);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function detach(): void {
|
|
153
|
+
if (!isBrowser) return;
|
|
154
|
+
|
|
155
|
+
window.removeEventListener('scroll', requestUpdate, true);
|
|
156
|
+
window.removeEventListener('resize', requestUpdate);
|
|
157
|
+
|
|
158
|
+
resize.disconnect();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
watch(
|
|
162
|
+
[isOpen, referenceRef, floatingRef],
|
|
163
|
+
([open]) => {
|
|
164
|
+
detach();
|
|
165
|
+
|
|
166
|
+
if (!isBrowser || !open) return;
|
|
167
|
+
|
|
168
|
+
attach();
|
|
169
|
+
|
|
170
|
+
// 首帧测量必须在**绘制前**提交:rAF 会晚一帧,让浮层先以原点/旧坐标画一帧再
|
|
171
|
+
// 跳到锚点,表现为「打开后内容自动动一下」。nextTick 回调在本轮渲染补丁之后、
|
|
172
|
+
// 浏览器绘制之前执行,此时浮层已解除 hidden,量到的就是最终尺寸与位置。
|
|
173
|
+
// 滚动 / resize 等后续高频更新仍走 rAF 合并。
|
|
174
|
+
void nextTick(measure);
|
|
175
|
+
},
|
|
176
|
+
{ immediate: true }
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
onScopeDispose(() => {
|
|
180
|
+
detach();
|
|
181
|
+
|
|
182
|
+
if (frame !== null && isBrowser) {
|
|
183
|
+
window.cancelAnimationFrame(frame);
|
|
184
|
+
frame = null;
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
referenceRef,
|
|
190
|
+
floatingRef,
|
|
191
|
+
x: computed(() => x.value),
|
|
192
|
+
y: computed(() => y.value),
|
|
193
|
+
placement: computed(() => placement.value),
|
|
194
|
+
availableSpace: computed(() => availableSpace.value),
|
|
195
|
+
floatingStyle,
|
|
196
|
+
arrowStyle,
|
|
197
|
+
update: requestUpdate,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
@@ -61,6 +61,12 @@ export function useFormControl(
|
|
|
61
61
|
|
|
62
62
|
const controlElement = ref<ValidatableControl | null>(null);
|
|
63
63
|
|
|
64
|
+
/**
|
|
65
|
+
* 校验轮次。异步匹配器的 resolve 顺序不可控,旧一轮的结果若晚到会覆盖最新输入
|
|
66
|
+
* 的判定;每次 validate / resetValidation 都自增,只有最新一轮能写回状态。
|
|
67
|
+
*/
|
|
68
|
+
let validationGeneration = 0;
|
|
69
|
+
|
|
64
70
|
function setControlElement(node: unknown): void {
|
|
65
71
|
/**
|
|
66
72
|
* 只认领有约束校验 API 的元素。
|
|
@@ -96,6 +102,7 @@ export function useFormControl(
|
|
|
96
102
|
|
|
97
103
|
async function validate(): Promise<void> {
|
|
98
104
|
const control = controlElement.value;
|
|
105
|
+
const generation = ++validationGeneration;
|
|
99
106
|
|
|
100
107
|
if (!control) return;
|
|
101
108
|
|
|
@@ -122,6 +129,8 @@ export function useFormControl(
|
|
|
122
129
|
syncEntries.map((entry) => [entry.id, entry.match(control.value, formData)])
|
|
123
130
|
);
|
|
124
131
|
|
|
132
|
+
if (generation !== validationGeneration) return;
|
|
133
|
+
|
|
125
134
|
applyErrors(control, syncErrors);
|
|
126
135
|
|
|
127
136
|
if (Object.values(syncErrors).some(Boolean) || asyncEntries.length === 0) return;
|
|
@@ -134,6 +143,8 @@ export function useFormControl(
|
|
|
134
143
|
)
|
|
135
144
|
);
|
|
136
145
|
|
|
146
|
+
if (generation !== validationGeneration) return;
|
|
147
|
+
|
|
137
148
|
applyErrors(control, asyncErrors);
|
|
138
149
|
}
|
|
139
150
|
|
|
@@ -153,6 +164,8 @@ export function useFormControl(
|
|
|
153
164
|
}
|
|
154
165
|
|
|
155
166
|
function resetValidation(): void {
|
|
167
|
+
// 输入或 reset 会让在途的异步结果失效:即使它晚到,也不该把刚清空的状态写回去。
|
|
168
|
+
validationGeneration++;
|
|
156
169
|
controlElement.value?.setCustomValidity('');
|
|
157
170
|
form.clearFieldValidation(name);
|
|
158
171
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { UseTextControlOptions, UseTextControlReturn } from '../utils/useTextControl';
|
|
2
|
+
|
|
3
|
+
import type { MaybeRefOrGetter } from 'vue';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* useInput 的配置项。
|
|
7
|
+
*/
|
|
8
|
+
export interface UseInputOptions extends UseTextControlOptions {
|
|
9
|
+
/**
|
|
10
|
+
* 原生 input 的 type。
|
|
11
|
+
*
|
|
12
|
+
* 不做词表限制:type 的合法取值由浏览器定义且持续演进,校验规则(required /
|
|
13
|
+
* pattern / min 等)也随 type 变化,透传比枚举更诚实。
|
|
14
|
+
*
|
|
15
|
+
* @default 'text'
|
|
16
|
+
*/
|
|
17
|
+
type?: MaybeRefOrGetter<string | undefined>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* useInput 的返回值。
|
|
22
|
+
*
|
|
23
|
+
* 在文本值内核之上只多一个 getRootProps:把 type 并进属性表。
|
|
24
|
+
* getBaseAttrs 不对外暴露——调用方只需要一份完整的根属性表。
|
|
25
|
+
*/
|
|
26
|
+
export interface UseInputReturn extends Omit<UseTextControlReturn, 'getBaseAttrs'> {
|
|
27
|
+
/** 根元素属性表:type + 原生状态属性 + data-* / aria 钩子 */
|
|
28
|
+
getRootProps(): Record<string, unknown>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { toValue } from 'vue';
|
|
2
|
+
|
|
3
|
+
import { useTextControl } from '../utils/useTextControl';
|
|
4
|
+
|
|
5
|
+
import type { UseInputOptions, UseInputReturn } from './types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Input 的 Vue Composable 适配。
|
|
9
|
+
*
|
|
10
|
+
* 值真源、IME 组合输入与表单 reset 复原都在 useTextControl 里只有一份,
|
|
11
|
+
* 这里只补 input 独有的 type 属性。Textarea 走同一个内核,因此两个组件的
|
|
12
|
+
* 受控语义与组合输入行为不会各自漂移。
|
|
13
|
+
*
|
|
14
|
+
* @param options Composable 配置项。
|
|
15
|
+
* @returns 值、状态、根属性表与原生事件处理器。
|
|
16
|
+
*/
|
|
17
|
+
export function useInput(options: UseInputOptions = {}): UseInputReturn {
|
|
18
|
+
const text = useTextControl(options);
|
|
19
|
+
|
|
20
|
+
function getRootProps(): Record<string, unknown> {
|
|
21
|
+
return {
|
|
22
|
+
type: toValue(options.type) ?? 'text',
|
|
23
|
+
...text.getBaseAttrs(),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
value: text.value,
|
|
29
|
+
isDisabled: text.isDisabled,
|
|
30
|
+
isReadOnly: text.isReadOnly,
|
|
31
|
+
isRequired: text.isRequired,
|
|
32
|
+
control: text.control,
|
|
33
|
+
setControl: text.setControl,
|
|
34
|
+
setValue: text.setValue,
|
|
35
|
+
getRootProps,
|
|
36
|
+
onInput: text.onInput,
|
|
37
|
+
onCompositionStart: text.onCompositionStart,
|
|
38
|
+
onCompositionEnd: text.onCompositionEnd,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ComputedRef, MaybeRefOrGetter } from 'vue';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* useKeepMounted 的配置项。
|
|
5
|
+
*/
|
|
6
|
+
export interface UseKeepMountedOptions {
|
|
7
|
+
/**
|
|
8
|
+
* 浮层的开合状态,一旦观测到 `true` 就记住这件事。
|
|
9
|
+
*
|
|
10
|
+
* 用 MaybeRefOrGetter 而不是裸 boolean,理由与 `usePresence` 的 `present` 相同:
|
|
11
|
+
* 封装层传进来的是响应式 props / ref,裸值会在展开时被固化成快照。
|
|
12
|
+
*/
|
|
13
|
+
open: MaybeRefOrGetter<boolean>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 常驻策略开关,通常直接接组件的 `keepMounted` prop。
|
|
17
|
+
*
|
|
18
|
+
* 缺省视为开启:判定本身只在「已经打开过」之后才有输出,把默认值交给调用方更省样板。
|
|
19
|
+
*/
|
|
20
|
+
enabled?: MaybeRefOrGetter<boolean>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* useKeepMounted 的返回值。
|
|
25
|
+
*/
|
|
26
|
+
export interface UseKeepMountedReturn {
|
|
27
|
+
/** 是否应当把内容继续留在 DOM 里,直接并进 `usePresence` 的 `present`。 */
|
|
28
|
+
keepMounted: ComputedRef<boolean>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { computed, ref, toValue, watch } from 'vue';
|
|
2
|
+
|
|
3
|
+
import type { UseKeepMountedOptions, UseKeepMountedReturn } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 「打开过一次之后常驻」的判定。
|
|
7
|
+
*
|
|
8
|
+
* 懒渲染省的是首次渲染的成本,代价是每次关闭都要销毁、每次打开都要重建 DOM 并重新测量,
|
|
9
|
+
* 浮层内部的滚动位置与临时状态也留不住。本 hook 把两种诉求合成一条规则:**打开之前不
|
|
10
|
+
* 渲染,打开过之后不销毁**,关闭时看得见与否交给组件自己的 `data-state`。
|
|
11
|
+
*
|
|
12
|
+
* 判定只认一个真源——浮层的开合状态。封装层若同时看 prop 和 `update:open` 事件,就会
|
|
13
|
+
* 得到两套缺一不可的分支(受控写法翻 prop 不派发事件,非受控的内部开合又不经过 prop),
|
|
14
|
+
* 所以这条 latch 只应该待在能直接读到开合状态的地方,由调用方调一次。
|
|
15
|
+
*
|
|
16
|
+
* @param options 开合状态与常驻策略。
|
|
17
|
+
* @returns 可直接并进 `usePresence` 的 `present` 的判定。
|
|
18
|
+
*/
|
|
19
|
+
export function useKeepMounted(options: UseKeepMountedOptions): UseKeepMountedReturn {
|
|
20
|
+
// 初值直接读一次:defaultOpen / 受控 open=true 这种「生下来就是打开」的形态,
|
|
21
|
+
// 等 watch 的首个变化才是多余的,读初值一步到位。
|
|
22
|
+
const hasOpened = ref(toValue(options.open));
|
|
23
|
+
|
|
24
|
+
watch(
|
|
25
|
+
() => toValue(options.open),
|
|
26
|
+
(open) => {
|
|
27
|
+
if (open) hasOpened.value = true;
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const keepMounted = computed(() => toValue(options.enabled) !== false && hasOpened.value);
|
|
32
|
+
|
|
33
|
+
return { keepMounted };
|
|
34
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { ComputedRef, MaybeRefOrGetter, Ref } from 'vue';
|
|
2
|
+
|
|
3
|
+
import type { MenuKey, MenuMode } from '@agile-plus/primitives/menu';
|
|
4
|
+
|
|
5
|
+
/** 菜单注册项类型。 */
|
|
6
|
+
export type MenuItemKind = 'item' | 'sub';
|
|
7
|
+
|
|
8
|
+
/** 菜单项或子菜单触发器的注册条目。 */
|
|
9
|
+
export interface MenuItemEntry {
|
|
10
|
+
/** 组件实例级稳定 id,用于 DOM id 与焦点定位。 */
|
|
11
|
+
id: Ref<string>;
|
|
12
|
+
/** 消费者语义键。 */
|
|
13
|
+
value: Ref<MenuKey>;
|
|
14
|
+
/** 父级子菜单键;顶层为 null。 */
|
|
15
|
+
parentValue: Ref<MenuKey | null>;
|
|
16
|
+
/** typeahead 与折叠提示使用的可读文本。 */
|
|
17
|
+
textValue: Ref<string>;
|
|
18
|
+
/** 实际交互元素。 */
|
|
19
|
+
element: Ref<HTMLElement | null>;
|
|
20
|
+
/** 项自身是否禁用。 */
|
|
21
|
+
disabled: Ref<boolean>;
|
|
22
|
+
/** 普通菜单项或子菜单触发器。 */
|
|
23
|
+
kind: MenuItemKind;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 菜单选中事件负载。 */
|
|
27
|
+
export interface MenuSelectInfo {
|
|
28
|
+
/** 本次激活项的原始键。 */
|
|
29
|
+
key: MenuKey;
|
|
30
|
+
/** 从顶层到当前项的键路径。 */
|
|
31
|
+
keyPath: MenuKey[];
|
|
32
|
+
/** 激活后的完整选中键集合。 */
|
|
33
|
+
selectedKeys: MenuKey[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** useMenu 配置。 */
|
|
37
|
+
export interface UseMenuOptions {
|
|
38
|
+
/** 布局模式,默认 vertical。 */
|
|
39
|
+
mode?: MaybeRefOrGetter<MenuMode>;
|
|
40
|
+
/** 受控选中键;undefined 表示非受控。 */
|
|
41
|
+
selectedKeys?: MaybeRefOrGetter<MenuKey[] | undefined>;
|
|
42
|
+
/** 非受控选中键初值。 */
|
|
43
|
+
defaultSelectedKeys?: MenuKey[];
|
|
44
|
+
/** 受控展开键;undefined 表示非受控。 */
|
|
45
|
+
openKeys?: MaybeRefOrGetter<MenuKey[] | undefined>;
|
|
46
|
+
/** 非受控展开键初值。 */
|
|
47
|
+
defaultOpenKeys?: MenuKey[];
|
|
48
|
+
/** 是否允许多选,默认 false。 */
|
|
49
|
+
multiple?: MaybeRefOrGetter<boolean>;
|
|
50
|
+
/** 是否允许选中,默认 true。 */
|
|
51
|
+
selectable?: MaybeRefOrGetter<boolean>;
|
|
52
|
+
/** 是否禁用整棵菜单,默认 false。 */
|
|
53
|
+
disabled?: MaybeRefOrGetter<boolean>;
|
|
54
|
+
/** 同级子菜单是否手风琴展开,默认 false。 */
|
|
55
|
+
accordion?: MaybeRefOrGetter<boolean>;
|
|
56
|
+
/** inline 模式是否折叠,默认 false。 */
|
|
57
|
+
inlineCollapsed?: MaybeRefOrGetter<boolean>;
|
|
58
|
+
/** 弹出子菜单的传送目标,默认 body。 */
|
|
59
|
+
container?: MaybeRefOrGetter<string | HTMLElement | undefined>;
|
|
60
|
+
/** 选中键变化回调。 */
|
|
61
|
+
onSelectedKeysChange?: (keys: MenuKey[]) => void;
|
|
62
|
+
/** 选中事件回调。 */
|
|
63
|
+
onSelect?: (info: MenuSelectInfo) => void;
|
|
64
|
+
/** 展开键变化回调。 */
|
|
65
|
+
onOpenKeysChange?: (keys: MenuKey[]) => void;
|
|
66
|
+
/** 展开事件回调。 */
|
|
67
|
+
onOpenChange?: (keys: MenuKey[]) => void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** useMenu 返回值。 */
|
|
71
|
+
export interface UseMenuReturn {
|
|
72
|
+
/** 当前布局模式。 */
|
|
73
|
+
mode: ComputedRef<MenuMode>;
|
|
74
|
+
/** 当前选中键集合。 */
|
|
75
|
+
selectedKeys: ComputedRef<MenuKey[]>;
|
|
76
|
+
/** 当前展开键集合。 */
|
|
77
|
+
openKeys: ComputedRef<MenuKey[]>;
|
|
78
|
+
/** 是否禁用整棵菜单。 */
|
|
79
|
+
isDisabled: ComputedRef<boolean>;
|
|
80
|
+
/** inline 模式是否处于折叠态。 */
|
|
81
|
+
isCollapsed: ComputedRef<boolean>;
|
|
82
|
+
/** 子菜单是否使用当前层内展开;false 表示弹出。 */
|
|
83
|
+
isInline: ComputedRef<boolean>;
|
|
84
|
+
/** 弹出层传送目标。 */
|
|
85
|
+
container: ComputedRef<string | HTMLElement>;
|
|
86
|
+
/** 当前焦点所在项的语义键。 */
|
|
87
|
+
focusedValue: ComputedRef<MenuKey | null>;
|
|
88
|
+
/** 判断项是否选中。 */
|
|
89
|
+
isSelected(value: MenuKey): boolean;
|
|
90
|
+
/** 判断子菜单是否展开。 */
|
|
91
|
+
isSubOpen(value: MenuKey): boolean;
|
|
92
|
+
/** 登记项并返回注销函数。 */
|
|
93
|
+
registerItem(entry: MenuItemEntry): () => void;
|
|
94
|
+
/** 回填菜单根元素。 */
|
|
95
|
+
setRootElement(node: unknown): void;
|
|
96
|
+
/** 登记或移除弹出子菜单元素。 */
|
|
97
|
+
setPopupElement(entryId: string, node: HTMLElement | null): void;
|
|
98
|
+
/** 根元素属性表。 */
|
|
99
|
+
getRootAttrs(): Record<string, unknown>;
|
|
100
|
+
/** 菜单项或子菜单触发器属性表。 */
|
|
101
|
+
getItemAttrs(entryId: string): Record<string, unknown>;
|
|
102
|
+
/** 子菜单内容属性表。 */
|
|
103
|
+
getSubContentAttrs(entryId: string): Record<string, unknown>;
|
|
104
|
+
/** 激活普通菜单项。 */
|
|
105
|
+
selectItem(value: MenuKey): void;
|
|
106
|
+
/** 切换子菜单展开状态。 */
|
|
107
|
+
toggleSub(value: MenuKey): void;
|
|
108
|
+
/** 展开子菜单。 */
|
|
109
|
+
openSub(value: MenuKey): void;
|
|
110
|
+
/** 关闭子菜单及其后代。 */
|
|
111
|
+
closeSub(value: MenuKey): void;
|
|
112
|
+
/** 延迟关闭子菜单,供指针在触发器与弹出层之间移动。 */
|
|
113
|
+
scheduleSubClose(value: MenuKey): void;
|
|
114
|
+
/** 取消待关闭的子菜单。 */
|
|
115
|
+
cancelSubClose(value: MenuKey): void;
|
|
116
|
+
/** 关闭全部弹出子菜单。 */
|
|
117
|
+
closePopups(): void;
|
|
118
|
+
/** 聚焦指定注册项。 */
|
|
119
|
+
focusItem(entryId: string): void;
|
|
120
|
+
/** 项获得焦点时更新 roving tabindex 真源。 */
|
|
121
|
+
onItemFocus(entryId: string): void;
|
|
122
|
+
/** 指针进入项时同步焦点与高亮。 */
|
|
123
|
+
onItemPointerEnter(entryId: string): void;
|
|
124
|
+
/** 菜单项键盘交互。 */
|
|
125
|
+
onItemKeydown(event: KeyboardEvent, entryId: string): void;
|
|
126
|
+
}
|