@octanejs/aria 0.0.3 → 0.0.5
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/package.json +7 -5
- package/src/autocomplete/useAutocomplete.ts +656 -0
- package/src/collections/BaseCollection.ts +374 -0
- package/src/collections/CollectionBuilder.ts +343 -0
- package/src/collections/Document.ts +451 -0
- package/src/collections/Hidden.ts +84 -0
- package/src/collections/useCachedChildren.ts +103 -0
- package/src/components/Autocomplete.ts +124 -0
- package/src/components/Breadcrumbs.ts +170 -0
- package/src/components/Button.ts +209 -0
- package/src/components/Checkbox.ts +593 -0
- package/src/components/Collection.ts +301 -0
- package/src/components/ComboBox.ts +477 -0
- package/src/components/Dialog.ts +195 -0
- package/src/components/Disclosure.ts +322 -0
- package/src/components/DragAndDrop.ts +177 -0
- package/src/components/FieldError.ts +79 -0
- package/src/components/Form.ts +65 -0
- package/src/components/GridList.ts +1099 -0
- package/src/components/Group.ts +146 -0
- package/src/components/Header.ts +32 -0
- package/src/components/Heading.ts +45 -0
- package/src/components/Input.ts +139 -0
- package/src/components/Keyboard.ts +23 -0
- package/src/components/Label.ts +30 -0
- package/src/components/Link.ts +137 -0
- package/src/components/ListBox.ts +886 -0
- package/src/components/Menu.ts +685 -0
- package/src/components/Meter.ts +100 -0
- package/src/components/Modal.ts +352 -0
- package/src/components/NumberField.ts +221 -0
- package/src/components/OverlayArrow.ts +117 -0
- package/src/components/Popover.ts +385 -0
- package/src/components/ProgressBar.ts +112 -0
- package/src/components/RadioGroup.ts +542 -0
- package/src/components/SearchField.ts +208 -0
- package/src/components/Select.ts +505 -0
- package/src/components/SelectionIndicator.ts +52 -0
- package/src/components/Separator.ts +97 -0
- package/src/components/SharedElementTransition.ts +209 -0
- package/src/components/Slider.ts +472 -0
- package/src/components/Switch.ts +449 -0
- package/src/components/Tabs.ts +675 -0
- package/src/components/TagGroup.ts +443 -0
- package/src/components/Text.ts +27 -0
- package/src/components/TextArea.ts +92 -0
- package/src/components/TextField.ts +200 -0
- package/src/components/ToggleButton.ts +147 -0
- package/src/components/ToggleButtonGroup.ts +106 -0
- package/src/components/Toolbar.ts +74 -0
- package/src/components/Tooltip.ts +249 -0
- package/src/components/index.ts +302 -0
- package/src/components/useDragAndDrop.ts +164 -0
- package/src/components/utils.ts +521 -0
- package/src/intl/autocomplete/ar-AE.json +3 -0
- package/src/intl/autocomplete/bg-BG.json +3 -0
- package/src/intl/autocomplete/cs-CZ.json +3 -0
- package/src/intl/autocomplete/da-DK.json +3 -0
- package/src/intl/autocomplete/de-DE.json +3 -0
- package/src/intl/autocomplete/el-GR.json +3 -0
- package/src/intl/autocomplete/en-US.json +3 -0
- package/src/intl/autocomplete/es-ES.json +3 -0
- package/src/intl/autocomplete/et-EE.json +3 -0
- package/src/intl/autocomplete/fi-FI.json +3 -0
- package/src/intl/autocomplete/fr-FR.json +3 -0
- package/src/intl/autocomplete/he-IL.json +3 -0
- package/src/intl/autocomplete/hr-HR.json +3 -0
- package/src/intl/autocomplete/hu-HU.json +3 -0
- package/src/intl/autocomplete/index.ts +75 -0
- package/src/intl/autocomplete/it-IT.json +3 -0
- package/src/intl/autocomplete/ja-JP.json +3 -0
- package/src/intl/autocomplete/ko-KR.json +3 -0
- package/src/intl/autocomplete/lt-LT.json +3 -0
- package/src/intl/autocomplete/lv-LV.json +3 -0
- package/src/intl/autocomplete/nb-NO.json +3 -0
- package/src/intl/autocomplete/nl-NL.json +3 -0
- package/src/intl/autocomplete/pl-PL.json +3 -0
- package/src/intl/autocomplete/pt-BR.json +3 -0
- package/src/intl/autocomplete/pt-PT.json +3 -0
- package/src/intl/autocomplete/ro-RO.json +3 -0
- package/src/intl/autocomplete/ru-RU.json +3 -0
- package/src/intl/autocomplete/sk-SK.json +3 -0
- package/src/intl/autocomplete/sl-SI.json +3 -0
- package/src/intl/autocomplete/sr-SP.json +3 -0
- package/src/intl/autocomplete/sv-SE.json +3 -0
- package/src/intl/autocomplete/tr-TR.json +3 -0
- package/src/intl/autocomplete/uk-UA.json +3 -0
- package/src/intl/autocomplete/zh-CN.json +3 -0
- package/src/intl/autocomplete/zh-TW.json +3 -0
- package/src/stately/autocomplete/useAutocompleteState.ts +85 -0
- package/src/utils/animation.ts +149 -0
- package/src/utils/useLoadMoreSentinel.ts +78 -0
- package/src/utils/useViewportSize.ts +123 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
// Ported from react-aria (source: .react-spectrum/packages/react-aria/src/collections/BaseCollection.ts).
|
|
2
|
+
// Pure immutable data structures — ported near-verbatim. octane adaptations:
|
|
3
|
+
// React's ReactNode/ReactElement types → `any` (octane element descriptors).
|
|
4
|
+
import type { Collection as ICollection, Key, Node } from '@react-types/shared';
|
|
5
|
+
|
|
6
|
+
export type Mutable<T> = {
|
|
7
|
+
-readonly [P in keyof T]: T[P];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type FilterFn<T> = (textValue: string, node: Node<T>) => boolean;
|
|
11
|
+
|
|
12
|
+
/** An immutable object representing a Node in a Collection. */
|
|
13
|
+
export class CollectionNode<T> implements Node<T> {
|
|
14
|
+
static readonly type: string;
|
|
15
|
+
readonly type: string;
|
|
16
|
+
readonly key: Key;
|
|
17
|
+
readonly value: T | null = null;
|
|
18
|
+
readonly level: number = 0;
|
|
19
|
+
readonly hasChildNodes: boolean = false;
|
|
20
|
+
readonly rendered: any = null;
|
|
21
|
+
readonly textValue: string = '';
|
|
22
|
+
readonly 'aria-label'?: string = undefined;
|
|
23
|
+
readonly index: number = 0;
|
|
24
|
+
readonly parentKey: Key | null = null;
|
|
25
|
+
readonly prevKey: Key | null = null;
|
|
26
|
+
readonly nextKey: Key | null = null;
|
|
27
|
+
readonly firstChildKey: Key | null = null;
|
|
28
|
+
readonly lastChildKey: Key | null = null;
|
|
29
|
+
readonly props: any = {};
|
|
30
|
+
readonly render?: (node: Node<any>) => any;
|
|
31
|
+
readonly colSpan: number | null = null;
|
|
32
|
+
readonly colIndex: number | null = null;
|
|
33
|
+
|
|
34
|
+
constructor(key: Key) {
|
|
35
|
+
this.type = (this.constructor as typeof CollectionNode).type;
|
|
36
|
+
this.key = key;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get childNodes(): Iterable<Node<T>> {
|
|
40
|
+
throw new Error('childNodes is not supported');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
clone(): this {
|
|
44
|
+
let node: Mutable<this> = new (this.constructor as any)(this.key);
|
|
45
|
+
node.value = this.value;
|
|
46
|
+
node.level = this.level;
|
|
47
|
+
node.hasChildNodes = this.hasChildNodes;
|
|
48
|
+
node.rendered = this.rendered;
|
|
49
|
+
node.textValue = this.textValue;
|
|
50
|
+
node['aria-label'] = this['aria-label'];
|
|
51
|
+
node.index = this.index;
|
|
52
|
+
node.parentKey = this.parentKey;
|
|
53
|
+
node.prevKey = this.prevKey;
|
|
54
|
+
node.nextKey = this.nextKey;
|
|
55
|
+
node.firstChildKey = this.firstChildKey;
|
|
56
|
+
node.lastChildKey = this.lastChildKey;
|
|
57
|
+
node.props = this.props;
|
|
58
|
+
node.render = this.render;
|
|
59
|
+
node.colSpan = this.colSpan;
|
|
60
|
+
node.colIndex = this.colIndex;
|
|
61
|
+
return node;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
filter(
|
|
65
|
+
collection: BaseCollection<T>,
|
|
66
|
+
newCollection: BaseCollection<T>,
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
68
|
+
filterFn: FilterFn<T>,
|
|
69
|
+
): CollectionNode<T> | null {
|
|
70
|
+
let clone = this.clone();
|
|
71
|
+
newCollection.addDescendants(clone, collection);
|
|
72
|
+
return clone;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class FilterableNode<T> extends CollectionNode<T> {
|
|
77
|
+
filter(
|
|
78
|
+
collection: BaseCollection<T>,
|
|
79
|
+
newCollection: BaseCollection<T>,
|
|
80
|
+
filterFn: FilterFn<T>,
|
|
81
|
+
): CollectionNode<T> | null {
|
|
82
|
+
let [firstKey, lastKey] = filterChildren(
|
|
83
|
+
collection,
|
|
84
|
+
newCollection,
|
|
85
|
+
this.firstChildKey,
|
|
86
|
+
filterFn,
|
|
87
|
+
);
|
|
88
|
+
let newNode: Mutable<CollectionNode<T>> = this.clone();
|
|
89
|
+
newNode.firstChildKey = firstKey;
|
|
90
|
+
newNode.lastChildKey = lastKey;
|
|
91
|
+
return newNode;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class HeaderNode extends CollectionNode<unknown> {
|
|
96
|
+
static readonly type = 'header';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class LoaderNode extends CollectionNode<unknown> {
|
|
100
|
+
static readonly type = 'loader';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class ItemNode<T> extends FilterableNode<T> {
|
|
104
|
+
static readonly type = 'item';
|
|
105
|
+
|
|
106
|
+
filter(
|
|
107
|
+
collection: BaseCollection<T>,
|
|
108
|
+
newCollection: BaseCollection<T>,
|
|
109
|
+
filterFn: FilterFn<T>,
|
|
110
|
+
): ItemNode<T> | null {
|
|
111
|
+
if (filterFn(this.textValue, this)) {
|
|
112
|
+
let clone = this.clone();
|
|
113
|
+
newCollection.addDescendants(clone, collection);
|
|
114
|
+
return clone;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class SectionNode<T> extends FilterableNode<T> {
|
|
122
|
+
static readonly type = 'section';
|
|
123
|
+
|
|
124
|
+
filter(
|
|
125
|
+
collection: BaseCollection<T>,
|
|
126
|
+
newCollection: BaseCollection<T>,
|
|
127
|
+
filterFn: FilterFn<T>,
|
|
128
|
+
): SectionNode<T> | null {
|
|
129
|
+
let filteredSection = super.filter(collection, newCollection, filterFn);
|
|
130
|
+
if (filteredSection) {
|
|
131
|
+
if (filteredSection.lastChildKey !== null) {
|
|
132
|
+
let lastChild = collection.getItem(filteredSection.lastChildKey);
|
|
133
|
+
if (lastChild && lastChild.type !== 'header') {
|
|
134
|
+
return filteredSection;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* An immutable Collection implementation. Updates are only allowed
|
|
145
|
+
* when it is not marked as frozen. This can be subclassed to implement
|
|
146
|
+
* custom collection behaviors.
|
|
147
|
+
*/
|
|
148
|
+
export class BaseCollection<T> implements ICollection<Node<T>> {
|
|
149
|
+
protected keyMap: Map<Key, CollectionNode<T>> = new Map();
|
|
150
|
+
protected firstKey: Key | null = null;
|
|
151
|
+
protected lastKey: Key | null = null;
|
|
152
|
+
protected frozen = false;
|
|
153
|
+
protected itemCount: number = 0;
|
|
154
|
+
|
|
155
|
+
get size(): number {
|
|
156
|
+
return this.itemCount;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
getKeys(): IterableIterator<Key> {
|
|
160
|
+
return this.keyMap.keys();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
*[Symbol.iterator](): IterableIterator<Node<T>> {
|
|
164
|
+
let node: Node<T> | undefined =
|
|
165
|
+
this.firstKey != null ? this.keyMap.get(this.firstKey) : undefined;
|
|
166
|
+
while (node) {
|
|
167
|
+
yield node;
|
|
168
|
+
node = node.nextKey != null ? this.keyMap.get(node.nextKey) : undefined;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
getChildren(key: Key): Iterable<Node<T>> {
|
|
173
|
+
let keyMap = this.keyMap;
|
|
174
|
+
return {
|
|
175
|
+
*[Symbol.iterator]() {
|
|
176
|
+
let parent = keyMap.get(key);
|
|
177
|
+
let node = parent?.firstChildKey != null ? keyMap.get(parent.firstChildKey) : null;
|
|
178
|
+
while (node) {
|
|
179
|
+
yield node as Node<T>;
|
|
180
|
+
node = node.nextKey != null ? keyMap.get(node.nextKey) : undefined;
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
getKeyBefore(key: Key): Key | null {
|
|
187
|
+
let node = this.keyMap.get(key);
|
|
188
|
+
if (!node) {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (node.prevKey != null) {
|
|
193
|
+
node = this.keyMap.get(node.prevKey);
|
|
194
|
+
|
|
195
|
+
while (node && node.type !== 'item' && node.lastChildKey != null) {
|
|
196
|
+
node = this.keyMap.get(node.lastChildKey);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return node?.key ?? null;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return node.parentKey;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
getKeyAfter(key: Key): Key | null {
|
|
206
|
+
let node = this.keyMap.get(key);
|
|
207
|
+
if (!node) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (node.type !== 'item' && node.firstChildKey != null) {
|
|
212
|
+
return node.firstChildKey;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
while (node) {
|
|
216
|
+
if (node.nextKey != null) {
|
|
217
|
+
return node.nextKey;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (node.parentKey != null) {
|
|
221
|
+
node = this.keyMap.get(node.parentKey);
|
|
222
|
+
} else {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
getFirstKey(): Key | null {
|
|
231
|
+
return this.firstKey;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
getLastKey(): Key | null {
|
|
235
|
+
let node = this.lastKey != null ? this.keyMap.get(this.lastKey) : null;
|
|
236
|
+
while (node?.lastChildKey != null) {
|
|
237
|
+
node = this.keyMap.get(node.lastChildKey);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return node?.key ?? null;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
getItem(key: Key): Node<T> | null {
|
|
244
|
+
return this.keyMap.get(key) ?? null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
at(): Node<T> {
|
|
248
|
+
throw new Error('Not implemented');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
clone(): this {
|
|
252
|
+
// We need to clone using this.constructor so that subclasses have the right prototype.
|
|
253
|
+
// TypeScript isn't happy about this yet.
|
|
254
|
+
// https://github.com/microsoft/TypeScript/issues/3841
|
|
255
|
+
let Constructor: any = this.constructor;
|
|
256
|
+
let collection: this = new Constructor();
|
|
257
|
+
collection.keyMap = new Map(this.keyMap);
|
|
258
|
+
collection.firstKey = this.firstKey;
|
|
259
|
+
collection.lastKey = this.lastKey;
|
|
260
|
+
collection.itemCount = this.itemCount;
|
|
261
|
+
return collection;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
addNode(node: CollectionNode<T>): void {
|
|
265
|
+
if (this.frozen) {
|
|
266
|
+
throw new Error('Cannot add a node to a frozen collection');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (node.type === 'item' && this.keyMap.get(node.key) == null) {
|
|
270
|
+
this.itemCount++;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
this.keyMap.set(node.key, node);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Deeply add a node and its children to the collection from another collection, primarily used when filtering a collection
|
|
277
|
+
addDescendants(node: CollectionNode<T>, oldCollection: BaseCollection<T>): void {
|
|
278
|
+
this.addNode(node);
|
|
279
|
+
let children = oldCollection.getChildren(node.key);
|
|
280
|
+
for (let child of children) {
|
|
281
|
+
this.addDescendants(child as CollectionNode<T>, oldCollection);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
removeNode(key: Key): void {
|
|
286
|
+
if (this.frozen) {
|
|
287
|
+
throw new Error('Cannot remove a node to a frozen collection');
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
let node = this.keyMap.get(key);
|
|
291
|
+
if (node != null && node.type === 'item') {
|
|
292
|
+
this.itemCount--;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
this.keyMap.delete(key);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
commit(firstKey: Key | null, lastKey: Key | null, isSSR = false): void {
|
|
299
|
+
if (this.frozen) {
|
|
300
|
+
throw new Error('Cannot commit a frozen collection');
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
this.firstKey = firstKey;
|
|
304
|
+
this.lastKey = lastKey;
|
|
305
|
+
this.frozen = !isSSR;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
filter(filterFn: FilterFn<T>): this {
|
|
309
|
+
let newCollection = new (this.constructor as any)();
|
|
310
|
+
let [firstKey, lastKey] = filterChildren(this, newCollection, this.firstKey, filterFn);
|
|
311
|
+
newCollection?.commit(firstKey, lastKey);
|
|
312
|
+
return newCollection;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function filterChildren<T>(
|
|
317
|
+
collection: BaseCollection<T>,
|
|
318
|
+
newCollection: BaseCollection<T>,
|
|
319
|
+
firstChildKey: Key | null,
|
|
320
|
+
filterFn: FilterFn<T>,
|
|
321
|
+
): [Key | null, Key | null] {
|
|
322
|
+
// loop over the siblings for firstChildKey
|
|
323
|
+
// create new nodes based on calling node.filter for each child
|
|
324
|
+
// if it returns null then don't include it, otherwise update its prev/next keys
|
|
325
|
+
// add them to the newCollection
|
|
326
|
+
if (firstChildKey == null) {
|
|
327
|
+
return [null, null];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
let firstNode: Node<T> | null = null;
|
|
331
|
+
let lastNode: Node<T> | null = null;
|
|
332
|
+
let currentNode = collection.getItem(firstChildKey);
|
|
333
|
+
|
|
334
|
+
while (currentNode != null) {
|
|
335
|
+
let newNode: Mutable<CollectionNode<T>> | null = (currentNode as CollectionNode<T>).filter(
|
|
336
|
+
collection,
|
|
337
|
+
newCollection,
|
|
338
|
+
filterFn,
|
|
339
|
+
);
|
|
340
|
+
if (newNode != null) {
|
|
341
|
+
newNode.nextKey = null;
|
|
342
|
+
if (lastNode) {
|
|
343
|
+
newNode.prevKey = lastNode.key;
|
|
344
|
+
(lastNode as Mutable<Node<T>>).nextKey = newNode.key;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (firstNode == null) {
|
|
348
|
+
firstNode = newNode;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
newCollection.addNode(newNode);
|
|
352
|
+
lastNode = newNode;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
currentNode = currentNode.nextKey != null ? collection.getItem(currentNode.nextKey) : null;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// TODO: this is pretty specific to dividers but doesn't feel like there is a good way to get around it since we only can know
|
|
359
|
+
// to filter the last separator in a collection only after performing a filter for the rest of the contents after it
|
|
360
|
+
// Its gross that it needs to live here, might be nice if somehow we could have this live in the separator code
|
|
361
|
+
if (lastNode && lastNode.type === 'separator') {
|
|
362
|
+
let prevKey = lastNode.prevKey;
|
|
363
|
+
newCollection.removeNode(lastNode.key);
|
|
364
|
+
|
|
365
|
+
if (prevKey != null) {
|
|
366
|
+
lastNode = newCollection.getItem(prevKey) as Mutable<CollectionNode<T>>;
|
|
367
|
+
(lastNode as Mutable<Node<T>>).nextKey = null;
|
|
368
|
+
} else {
|
|
369
|
+
lastNode = null;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return [firstNode?.key ?? null, lastNode?.key ?? null];
|
|
374
|
+
}
|