@fluixi-ui/listbox 0.2.0-alpha.18 → 0.2.0-alpha.19
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/dist/index.js +3 -11
- package/dist/listbox.js +338 -267
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/virtual-listbox.js +218 -166
- package/package.json +4 -4
package/dist/virtual-listbox.js
CHANGED
|
@@ -1,171 +1,223 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { createSelectionManager, createVirtualizer, createTypeahead, getNextEnabledIndex } from "@fluixi-ui/primitives";
|
|
1
|
+
import { createComponent, createNativeElement, delegateEvents, handleRef, insert, mergeProps, splitProps, spread, templateNode } from "@fluixi/dom";
|
|
2
|
+
import { createMemo, createSignal } from "@fluixi/reactive/signal";
|
|
3
|
+
import { composeEventHandlers, composeId, createId, dataAttr } from "@fluixi-ui/kit";
|
|
4
|
+
import { createSelectionManager, createTypeahead, createVirtualizer, getNextEnabledIndex } from "@fluixi-ui/primitives";
|
|
6
5
|
import { fxProps, mergeFx } from "@fluixi-ui/fx";
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
import { For } from "@fluixi/core";
|
|
7
|
+
//#region src/virtual-listbox.tsx
|
|
8
|
+
var _fxTmpl$0 = "<span data-select-item-indicator=\"\" aria-hidden=\"true\"></span>";
|
|
9
|
+
delegateEvents([
|
|
10
|
+
"click",
|
|
11
|
+
"keydown",
|
|
12
|
+
"focus"
|
|
13
|
+
]);
|
|
9
14
|
function toKeys(value) {
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
if (value == null) return [];
|
|
16
|
+
return Array.isArray(value) ? value : [value];
|
|
12
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* A listbox that virtualizes a large `options` array — only the rows in view are
|
|
20
|
+
* in the DOM, while keyboard navigation, typeahead and active-descendant operate
|
|
21
|
+
* over the full data (the active row is scrolled into view). Single or multiple
|
|
22
|
+
* selection. Uniform `rowHeight` is required so offsets are computable.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* <VirtualListbox
|
|
27
|
+
* options={items} // [{ value, label }, …] — thousands is fine
|
|
28
|
+
* selectionMode="multiple"
|
|
29
|
+
* onValuesChange={setSelected}
|
|
30
|
+
* />
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
13
33
|
function VirtualListbox(props) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
34
|
+
const [local, others] = splitProps(props, [
|
|
35
|
+
"options",
|
|
36
|
+
"selectionMode",
|
|
37
|
+
"value",
|
|
38
|
+
"defaultValue",
|
|
39
|
+
"onChange",
|
|
40
|
+
"onValuesChange",
|
|
41
|
+
"disabled",
|
|
42
|
+
"rowHeight",
|
|
43
|
+
"height",
|
|
44
|
+
"overscan",
|
|
45
|
+
"children",
|
|
46
|
+
"ref",
|
|
47
|
+
"onKeyDown",
|
|
48
|
+
"style",
|
|
49
|
+
"fx",
|
|
50
|
+
"style"
|
|
51
|
+
]);
|
|
52
|
+
const baseId = createId("vlistbox");
|
|
53
|
+
const selectionMode = local.selectionMode ?? "single";
|
|
54
|
+
const multiple = selectionMode === "multiple";
|
|
55
|
+
const rowHeight = () => local.rowHeight ?? 32;
|
|
56
|
+
const isDisabled = () => local.disabled ?? false;
|
|
57
|
+
const options = () => local.options;
|
|
58
|
+
const optionId = (index) => composeId(baseId, `option-${index}`);
|
|
59
|
+
const selection = createSelectionManager({
|
|
60
|
+
mode: selectionMode,
|
|
61
|
+
selectedKeys: local.value !== void 0 ? () => toKeys(local.value) : void 0,
|
|
62
|
+
defaultSelectedKeys: toKeys(local.defaultValue),
|
|
63
|
+
disallowEmptySelection: !multiple,
|
|
64
|
+
onSelectionChange: (keys) => {
|
|
65
|
+
const list = [...keys];
|
|
66
|
+
if (multiple) local.onValuesChange?.(list);
|
|
67
|
+
else local.onChange?.(list[0] ?? "");
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
const [activeIndex, setActiveIndex] = createSignal(-1);
|
|
71
|
+
const [scrollEl, setScrollEl] = createSignal(void 0);
|
|
72
|
+
const virtualizer = createVirtualizer({
|
|
73
|
+
count: () => options().length,
|
|
74
|
+
estimateSize: () => rowHeight(),
|
|
75
|
+
overscan: local.overscan ?? 6
|
|
76
|
+
});
|
|
77
|
+
const scrollIntoView = (index) => {
|
|
78
|
+
const el = scrollEl();
|
|
79
|
+
if (!el || index < 0) return;
|
|
80
|
+
const top = index * rowHeight();
|
|
81
|
+
const bottom = top + rowHeight();
|
|
82
|
+
if (top < el.scrollTop) el.scrollTop = top;
|
|
83
|
+
else if (bottom > el.scrollTop + el.clientHeight) el.scrollTop = bottom - el.clientHeight;
|
|
84
|
+
};
|
|
85
|
+
const move = (action) => {
|
|
86
|
+
const next = getNextEnabledIndex(options(), activeIndex(), action, true);
|
|
87
|
+
if (next !== -1) {
|
|
88
|
+
setActiveIndex(next);
|
|
89
|
+
scrollIntoView(next);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const typeahead = createTypeahead({
|
|
93
|
+
getItems: () => options().map((option) => ({
|
|
94
|
+
textValue: option.label ?? option.value,
|
|
95
|
+
disabled: option.disabled
|
|
96
|
+
})),
|
|
97
|
+
getActiveIndex: () => activeIndex(),
|
|
98
|
+
onMatch: (index) => {
|
|
99
|
+
setActiveIndex(index);
|
|
100
|
+
scrollIntoView(index);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
const pressOption = (index) => {
|
|
104
|
+
const option = options()[index];
|
|
105
|
+
if (!option || option.disabled || isDisabled()) return;
|
|
106
|
+
setActiveIndex(index);
|
|
107
|
+
if (multiple) selection.toggle(option.value);
|
|
108
|
+
else selection.select(option.value);
|
|
109
|
+
};
|
|
110
|
+
const onKeyDown = (event) => {
|
|
111
|
+
if (isDisabled()) return;
|
|
112
|
+
switch (event.key) {
|
|
113
|
+
case "ArrowDown":
|
|
114
|
+
event.preventDefault();
|
|
115
|
+
move(activeIndex() === -1 ? "first" : "next");
|
|
116
|
+
break;
|
|
117
|
+
case "ArrowUp":
|
|
118
|
+
event.preventDefault();
|
|
119
|
+
move(activeIndex() === -1 ? "last" : "previous");
|
|
120
|
+
break;
|
|
121
|
+
case "Home":
|
|
122
|
+
event.preventDefault();
|
|
123
|
+
move("first");
|
|
124
|
+
break;
|
|
125
|
+
case "End":
|
|
126
|
+
event.preventDefault();
|
|
127
|
+
move("last");
|
|
128
|
+
break;
|
|
129
|
+
case "Enter":
|
|
130
|
+
case " ":
|
|
131
|
+
if (event.key === " " && typeahead.buffer() !== "") {
|
|
132
|
+
typeahead.onKeyDown(event);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
event.preventDefault();
|
|
136
|
+
if (activeIndex() !== -1) pressOption(activeIndex());
|
|
137
|
+
break;
|
|
138
|
+
default: typeahead.onKeyDown(event);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const onFocus = () => {
|
|
142
|
+
if (activeIndex() === -1) move("first");
|
|
143
|
+
};
|
|
144
|
+
const setRef = (node) => {
|
|
145
|
+
virtualizer.scrollRef(node);
|
|
146
|
+
setScrollEl(node);
|
|
147
|
+
handleRef(local.ref, node);
|
|
148
|
+
};
|
|
149
|
+
const activeId = () => activeIndex() !== -1 ? optionId(activeIndex()) : void 0;
|
|
150
|
+
return (() => {
|
|
151
|
+
const _el$ = createNativeElement("div");
|
|
152
|
+
spread({
|
|
153
|
+
element: _el$,
|
|
154
|
+
props: mergeProps(fxProps(() => mergeFx({
|
|
155
|
+
overflow: "auto",
|
|
156
|
+
height: local.height ?? 240
|
|
157
|
+
}, local.fx), () => local.style), others, {
|
|
158
|
+
ref: setRef,
|
|
159
|
+
role: "listbox",
|
|
160
|
+
id: baseId,
|
|
161
|
+
tabindex: () => isDisabled() ? -1 : 0,
|
|
162
|
+
"aria-multiselectable": multiple ? "true" : void 0,
|
|
163
|
+
"aria-disabled": () => isDisabled() ? "true" : void 0,
|
|
164
|
+
"aria-activedescendant": activeId,
|
|
165
|
+
"data-virtual-listbox": "",
|
|
166
|
+
"data-mode": selectionMode,
|
|
167
|
+
onKeyDown: composeEventHandlers([local.onKeyDown, onKeyDown]),
|
|
168
|
+
onFocus
|
|
169
|
+
})
|
|
170
|
+
});
|
|
171
|
+
insert(_el$, (() => {
|
|
172
|
+
const _el$ = createNativeElement("div");
|
|
173
|
+
spread({
|
|
174
|
+
element: _el$,
|
|
175
|
+
props: { style: () => `position:relative;height:${virtualizer.getTotalSize()}px` }
|
|
176
|
+
});
|
|
177
|
+
insert(_el$, createMemo(() => createComponent(For, {
|
|
178
|
+
get each() {
|
|
179
|
+
return virtualizer.getVirtualItems;
|
|
180
|
+
},
|
|
181
|
+
get children() {
|
|
182
|
+
return (item) => {
|
|
183
|
+
const option = () => options()[item.index];
|
|
184
|
+
const active = () => activeIndex() === item.index;
|
|
185
|
+
const selected = () => selection.isSelected(option().value);
|
|
186
|
+
const disabled = () => option().disabled ?? false;
|
|
187
|
+
return (() => {
|
|
188
|
+
const _el$ = createNativeElement("div");
|
|
189
|
+
spread({
|
|
190
|
+
element: _el$,
|
|
191
|
+
props: {
|
|
192
|
+
role: "option",
|
|
193
|
+
id: () => optionId(item.index),
|
|
194
|
+
"aria-selected": () => selected() ? "true" : "false",
|
|
195
|
+
"aria-disabled": () => disabled() ? "true" : void 0,
|
|
196
|
+
"data-selected": () => dataAttr(selected()),
|
|
197
|
+
"data-active": () => dataAttr(active()),
|
|
198
|
+
"data-disabled": () => dataAttr(disabled()),
|
|
199
|
+
style: () => `position:absolute;top:${item.start}px;left:0;width:100%;height:${item.size}px;box-sizing:border-box`,
|
|
200
|
+
onClick: () => pressOption(item.index),
|
|
201
|
+
onPointerMove: () => {
|
|
202
|
+
if (!disabled()) setActiveIndex(item.index);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
insert(_el$, templateNode(_fxTmpl$0, "span"));
|
|
207
|
+
insert(_el$, () => local.children ? local.children(option(), {
|
|
208
|
+
active: active(),
|
|
209
|
+
selected: selected(),
|
|
210
|
+
disabled: disabled()
|
|
211
|
+
}) : option().label ?? option().value, null);
|
|
212
|
+
return _el$;
|
|
213
|
+
})();
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
})), null);
|
|
217
|
+
return _el$;
|
|
218
|
+
})());
|
|
219
|
+
return _el$;
|
|
220
|
+
})();
|
|
168
221
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
};
|
|
222
|
+
//#endregion
|
|
223
|
+
export { VirtualListbox };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluixi-ui/listbox",
|
|
3
|
-
"version": "0.2.0-alpha.
|
|
3
|
+
"version": "0.2.0-alpha.19",
|
|
4
4
|
"description": "Headless listbox for the Fluixi UI ecosystem",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"private": false,
|
|
@@ -41,17 +41,17 @@
|
|
|
41
41
|
"@fluixi/reactive": ">=1.0.0-alpha.41"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@fluixi-ui/fx": "0.1.0-alpha.
|
|
44
|
+
"@fluixi-ui/fx": "0.1.0-alpha.8",
|
|
45
45
|
"@fluixi-ui/kit": "0.2.0-alpha.3",
|
|
46
46
|
"@fluixi-ui/primitives": "0.2.0-alpha.9",
|
|
47
|
-
"@fluixi-ui/tokens": "0.4.0-alpha.
|
|
47
|
+
"@fluixi-ui/tokens": "0.4.0-alpha.18"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@fluixi/core": "alpha",
|
|
51
51
|
"@fluixi/jsx": "alpha",
|
|
52
52
|
"@fluixi/dom": "alpha",
|
|
53
53
|
"@fluixi/vite-plugin": "alpha",
|
|
54
|
-
"vite": "^
|
|
54
|
+
"vite": "^8.3.2",
|
|
55
55
|
"@fluixi/reactive": "alpha"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|