@react-stately/selection 3.20.9 → 3.21.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/dist/import.mjs +3 -3
- package/dist/main.js +4 -4
- package/dist/main.js.map +1 -1
- package/dist/module.js +3 -3
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +5 -0
- package/package.json +17 -15
- package/src/index.ts +6 -4
- package/dist/Selection.main.js +0 -31
- package/dist/Selection.main.js.map +0 -1
- package/dist/Selection.mjs +0 -26
- package/dist/Selection.module.js +0 -26
- package/dist/Selection.module.js.map +0 -1
- package/dist/SelectionManager.main.js +0 -323
- package/dist/SelectionManager.main.js.map +0 -1
- package/dist/SelectionManager.mjs +0 -318
- package/dist/SelectionManager.module.js +0 -318
- package/dist/SelectionManager.module.js.map +0 -1
- package/dist/types.d.ts +0 -218
- package/dist/types.d.ts.map +0 -1
- package/dist/useMultipleSelectionState.main.js +0 -101
- package/dist/useMultipleSelectionState.main.js.map +0 -1
- package/dist/useMultipleSelectionState.mjs +0 -96
- package/dist/useMultipleSelectionState.module.js +0 -96
- package/dist/useMultipleSelectionState.module.js.map +0 -1
- package/src/Selection.ts +0 -33
- package/src/SelectionManager.ts +0 -522
- package/src/types.ts +0 -113
- package/src/useMultipleSelectionState.ts +0 -130
package/src/SelectionManager.ts
DELETED
|
@@ -1,522 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
Collection, DisabledBehavior,
|
|
15
|
-
FocusStrategy,
|
|
16
|
-
Selection as ISelection,
|
|
17
|
-
Key,
|
|
18
|
-
LayoutDelegate,
|
|
19
|
-
LongPressEvent,
|
|
20
|
-
Node,
|
|
21
|
-
PressEvent,
|
|
22
|
-
SelectionBehavior,
|
|
23
|
-
SelectionMode
|
|
24
|
-
} from '@react-types/shared';
|
|
25
|
-
import {compareNodeOrder, getChildNodes, getFirstItem} from '@react-stately/collections';
|
|
26
|
-
import {MultipleSelectionManager, MultipleSelectionState} from './types';
|
|
27
|
-
import {Selection} from './Selection';
|
|
28
|
-
|
|
29
|
-
interface SelectionManagerOptions {
|
|
30
|
-
allowsCellSelection?: boolean,
|
|
31
|
-
layoutDelegate?: LayoutDelegate
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* An interface for reading and updating multiple selection state.
|
|
36
|
-
*/
|
|
37
|
-
export class SelectionManager implements MultipleSelectionManager {
|
|
38
|
-
collection: Collection<Node<unknown>>;
|
|
39
|
-
private state: MultipleSelectionState;
|
|
40
|
-
private allowsCellSelection: boolean;
|
|
41
|
-
private _isSelectAll: boolean | null;
|
|
42
|
-
private layoutDelegate: LayoutDelegate | null;
|
|
43
|
-
|
|
44
|
-
constructor(collection: Collection<Node<unknown>>, state: MultipleSelectionState, options?: SelectionManagerOptions) {
|
|
45
|
-
this.collection = collection;
|
|
46
|
-
this.state = state;
|
|
47
|
-
this.allowsCellSelection = options?.allowsCellSelection ?? false;
|
|
48
|
-
this._isSelectAll = null;
|
|
49
|
-
this.layoutDelegate = options?.layoutDelegate || null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* The type of selection that is allowed in the collection.
|
|
54
|
-
*/
|
|
55
|
-
get selectionMode(): SelectionMode {
|
|
56
|
-
return this.state.selectionMode;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Whether the collection allows empty selection.
|
|
61
|
-
*/
|
|
62
|
-
get disallowEmptySelection(): boolean {
|
|
63
|
-
return this.state.disallowEmptySelection;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* The selection behavior for the collection.
|
|
68
|
-
*/
|
|
69
|
-
get selectionBehavior(): SelectionBehavior {
|
|
70
|
-
return this.state.selectionBehavior;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Sets the selection behavior for the collection.
|
|
75
|
-
*/
|
|
76
|
-
setSelectionBehavior(selectionBehavior: SelectionBehavior): void {
|
|
77
|
-
this.state.setSelectionBehavior(selectionBehavior);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Whether the collection is currently focused.
|
|
82
|
-
*/
|
|
83
|
-
get isFocused(): boolean {
|
|
84
|
-
return this.state.isFocused;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Sets whether the collection is focused.
|
|
89
|
-
*/
|
|
90
|
-
setFocused(isFocused: boolean): void {
|
|
91
|
-
this.state.setFocused(isFocused);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* The current focused key in the collection.
|
|
96
|
-
*/
|
|
97
|
-
get focusedKey(): Key | null {
|
|
98
|
-
return this.state.focusedKey;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** Whether the first or last child of the focused key should receive focus. */
|
|
102
|
-
get childFocusStrategy(): FocusStrategy | null {
|
|
103
|
-
return this.state.childFocusStrategy;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Sets the focused key.
|
|
108
|
-
*/
|
|
109
|
-
setFocusedKey(key: Key | null, childFocusStrategy?: FocusStrategy): void {
|
|
110
|
-
if (key == null || this.collection.getItem(key)) {
|
|
111
|
-
this.state.setFocusedKey(key, childFocusStrategy);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* The currently selected keys in the collection.
|
|
117
|
-
*/
|
|
118
|
-
get selectedKeys(): Set<Key> {
|
|
119
|
-
return this.state.selectedKeys === 'all'
|
|
120
|
-
? new Set(this.getSelectAllKeys())
|
|
121
|
-
: this.state.selectedKeys;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* The raw selection value for the collection.
|
|
126
|
-
* Either 'all' for select all, or a set of keys.
|
|
127
|
-
*/
|
|
128
|
-
get rawSelection(): ISelection {
|
|
129
|
-
return this.state.selectedKeys;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Returns whether a key is selected.
|
|
134
|
-
*/
|
|
135
|
-
isSelected(key: Key): boolean {
|
|
136
|
-
if (this.state.selectionMode === 'none') {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
let mappedKey = this.getKey(key);
|
|
141
|
-
if (mappedKey == null) {
|
|
142
|
-
return false;
|
|
143
|
-
}
|
|
144
|
-
return this.state.selectedKeys === 'all'
|
|
145
|
-
? this.canSelectItem(mappedKey)
|
|
146
|
-
: this.state.selectedKeys.has(mappedKey);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Whether the selection is empty.
|
|
151
|
-
*/
|
|
152
|
-
get isEmpty(): boolean {
|
|
153
|
-
return this.state.selectedKeys !== 'all' && this.state.selectedKeys.size === 0;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Whether all items in the collection are selected.
|
|
158
|
-
*/
|
|
159
|
-
get isSelectAll(): boolean {
|
|
160
|
-
if (this.isEmpty) {
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (this.state.selectedKeys === 'all') {
|
|
165
|
-
return true;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (this._isSelectAll != null) {
|
|
169
|
-
return this._isSelectAll;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
let allKeys = this.getSelectAllKeys();
|
|
173
|
-
let selectedKeys = this.state.selectedKeys;
|
|
174
|
-
this._isSelectAll = allKeys.every(k => selectedKeys.has(k));
|
|
175
|
-
return this._isSelectAll;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
get firstSelectedKey(): Key | null {
|
|
179
|
-
let first: Node<unknown> | null = null;
|
|
180
|
-
for (let key of this.state.selectedKeys) {
|
|
181
|
-
let item = this.collection.getItem(key);
|
|
182
|
-
if (!first || (item && compareNodeOrder(this.collection, item, first) < 0)) {
|
|
183
|
-
first = item;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return first?.key ?? null;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
get lastSelectedKey(): Key | null {
|
|
191
|
-
let last: Node<unknown> | null = null;
|
|
192
|
-
for (let key of this.state.selectedKeys) {
|
|
193
|
-
let item = this.collection.getItem(key);
|
|
194
|
-
if (!last || (item && compareNodeOrder(this.collection, item, last) > 0)) {
|
|
195
|
-
last = item;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return last?.key ?? null;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
get disabledKeys(): Set<Key> {
|
|
203
|
-
return this.state.disabledKeys;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
get disabledBehavior(): DisabledBehavior {
|
|
207
|
-
return this.state.disabledBehavior;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Extends the selection to the given key.
|
|
212
|
-
*/
|
|
213
|
-
extendSelection(toKey: Key): void {
|
|
214
|
-
if (this.selectionMode === 'none') {
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (this.selectionMode === 'single') {
|
|
219
|
-
this.replaceSelection(toKey);
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
let mappedToKey = this.getKey(toKey);
|
|
224
|
-
if (mappedToKey == null) {
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
let selection: Selection;
|
|
229
|
-
|
|
230
|
-
// Only select the one key if coming from a select all.
|
|
231
|
-
if (this.state.selectedKeys === 'all') {
|
|
232
|
-
selection = new Selection([mappedToKey], mappedToKey, mappedToKey);
|
|
233
|
-
} else {
|
|
234
|
-
let selectedKeys = this.state.selectedKeys as Selection;
|
|
235
|
-
let anchorKey = selectedKeys.anchorKey ?? mappedToKey;
|
|
236
|
-
selection = new Selection(selectedKeys, anchorKey, mappedToKey);
|
|
237
|
-
for (let key of this.getKeyRange(anchorKey, selectedKeys.currentKey ?? mappedToKey)) {
|
|
238
|
-
selection.delete(key);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
for (let key of this.getKeyRange(mappedToKey, anchorKey)) {
|
|
242
|
-
if (this.canSelectItem(key)) {
|
|
243
|
-
selection.add(key);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
this.state.setSelectedKeys(selection);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
private getKeyRange(from: Key, to: Key) {
|
|
252
|
-
let fromItem = this.collection.getItem(from);
|
|
253
|
-
let toItem = this.collection.getItem(to);
|
|
254
|
-
if (fromItem && toItem) {
|
|
255
|
-
if (compareNodeOrder(this.collection, fromItem, toItem) <= 0) {
|
|
256
|
-
return this.getKeyRangeInternal(from, to);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return this.getKeyRangeInternal(to, from);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return [];
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
private getKeyRangeInternal(from: Key, to: Key) {
|
|
266
|
-
if (this.layoutDelegate?.getKeyRange) {
|
|
267
|
-
return this.layoutDelegate.getKeyRange(from, to);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
let keys: Key[] = [];
|
|
271
|
-
let key: Key | null = from;
|
|
272
|
-
while (key != null) {
|
|
273
|
-
let item = this.collection.getItem(key);
|
|
274
|
-
if (item && (item.type === 'item' || (item.type === 'cell' && this.allowsCellSelection))) {
|
|
275
|
-
keys.push(key);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (key === to) {
|
|
279
|
-
return keys;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
key = this.collection.getKeyAfter(key);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
return [];
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
private getKey(key: Key) {
|
|
289
|
-
let item = this.collection.getItem(key);
|
|
290
|
-
if (!item) {
|
|
291
|
-
// ¯\_(ツ)_/¯
|
|
292
|
-
return key;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// If cell selection is allowed, just return the key.
|
|
296
|
-
if (item.type === 'cell' && this.allowsCellSelection) {
|
|
297
|
-
return key;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// Find a parent item to select
|
|
301
|
-
while (item && item.type !== 'item' && item.parentKey != null) {
|
|
302
|
-
item = this.collection.getItem(item.parentKey);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
if (!item || item.type !== 'item') {
|
|
306
|
-
return null;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
return item.key;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Toggles whether the given key is selected.
|
|
314
|
-
*/
|
|
315
|
-
toggleSelection(key: Key): void {
|
|
316
|
-
if (this.selectionMode === 'none') {
|
|
317
|
-
return;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
if (this.selectionMode === 'single' && !this.isSelected(key)) {
|
|
321
|
-
this.replaceSelection(key);
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
let mappedKey = this.getKey(key);
|
|
326
|
-
if (mappedKey == null) {
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
let keys = new Selection(this.state.selectedKeys === 'all' ? this.getSelectAllKeys() : this.state.selectedKeys);
|
|
331
|
-
if (keys.has(mappedKey)) {
|
|
332
|
-
keys.delete(mappedKey);
|
|
333
|
-
// TODO: move anchor to last selected key...
|
|
334
|
-
// Does `current` need to move here too?
|
|
335
|
-
} else if (this.canSelectItem(mappedKey)) {
|
|
336
|
-
keys.add(mappedKey);
|
|
337
|
-
keys.anchorKey = mappedKey;
|
|
338
|
-
keys.currentKey = mappedKey;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
if (this.disallowEmptySelection && keys.size === 0) {
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
this.state.setSelectedKeys(keys);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* Replaces the selection with only the given key.
|
|
350
|
-
*/
|
|
351
|
-
replaceSelection(key: Key): void {
|
|
352
|
-
if (this.selectionMode === 'none') {
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
let mappedKey = this.getKey(key);
|
|
357
|
-
if (mappedKey == null) {
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
let selection = this.canSelectItem(mappedKey)
|
|
362
|
-
? new Selection([mappedKey], mappedKey, mappedKey)
|
|
363
|
-
: new Selection();
|
|
364
|
-
|
|
365
|
-
this.state.setSelectedKeys(selection);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Replaces the selection with the given keys.
|
|
370
|
-
*/
|
|
371
|
-
setSelectedKeys(keys: Iterable<Key>): void {
|
|
372
|
-
if (this.selectionMode === 'none') {
|
|
373
|
-
return;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
let selection = new Selection();
|
|
377
|
-
for (let key of keys) {
|
|
378
|
-
let mappedKey = this.getKey(key);
|
|
379
|
-
if (mappedKey != null) {
|
|
380
|
-
selection.add(mappedKey);
|
|
381
|
-
if (this.selectionMode === 'single') {
|
|
382
|
-
break;
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
this.state.setSelectedKeys(selection);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
private getSelectAllKeys() {
|
|
391
|
-
let keys: Key[] = [];
|
|
392
|
-
let addKeys = (key: Key | null) => {
|
|
393
|
-
while (key != null) {
|
|
394
|
-
if (this.canSelectItem(key)) {
|
|
395
|
-
let item = this.collection.getItem(key);
|
|
396
|
-
if (item?.type === 'item') {
|
|
397
|
-
keys.push(key);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// Add child keys. If cell selection is allowed, then include item children too.
|
|
401
|
-
if (item?.hasChildNodes && (this.allowsCellSelection || item.type !== 'item')) {
|
|
402
|
-
addKeys(getFirstItem(getChildNodes(item, this.collection))?.key ?? null);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
key = this.collection.getKeyAfter(key);
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
|
|
410
|
-
addKeys(this.collection.getFirstKey());
|
|
411
|
-
return keys;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
/**
|
|
415
|
-
* Selects all items in the collection.
|
|
416
|
-
*/
|
|
417
|
-
selectAll(): void {
|
|
418
|
-
if (!this.isSelectAll && this.selectionMode === 'multiple') {
|
|
419
|
-
this.state.setSelectedKeys('all');
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
* Removes all keys from the selection.
|
|
425
|
-
*/
|
|
426
|
-
clearSelection(): void {
|
|
427
|
-
if (!this.disallowEmptySelection && (this.state.selectedKeys === 'all' || this.state.selectedKeys.size > 0)) {
|
|
428
|
-
this.state.setSelectedKeys(new Selection());
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* Toggles between select all and an empty selection.
|
|
434
|
-
*/
|
|
435
|
-
toggleSelectAll(): void {
|
|
436
|
-
if (this.isSelectAll) {
|
|
437
|
-
this.clearSelection();
|
|
438
|
-
} else {
|
|
439
|
-
this.selectAll();
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
select(key: Key, e?: PressEvent | LongPressEvent | PointerEvent): void {
|
|
444
|
-
if (this.selectionMode === 'none') {
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
if (this.selectionMode === 'single') {
|
|
449
|
-
if (this.isSelected(key) && !this.disallowEmptySelection) {
|
|
450
|
-
this.toggleSelection(key);
|
|
451
|
-
} else {
|
|
452
|
-
this.replaceSelection(key);
|
|
453
|
-
}
|
|
454
|
-
} else if (this.selectionBehavior === 'toggle' || (e && (e.pointerType === 'touch' || e.pointerType === 'virtual'))) {
|
|
455
|
-
// if touch or virtual (VO) then we just want to toggle, otherwise it's impossible to multi select because they don't have modifier keys
|
|
456
|
-
this.toggleSelection(key);
|
|
457
|
-
} else {
|
|
458
|
-
this.replaceSelection(key);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
* Returns whether the current selection is equal to the given selection.
|
|
464
|
-
*/
|
|
465
|
-
isSelectionEqual(selection: Set<Key>): boolean {
|
|
466
|
-
if (selection === this.state.selectedKeys) {
|
|
467
|
-
return true;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
// Check if the set of keys match.
|
|
471
|
-
let selectedKeys = this.selectedKeys;
|
|
472
|
-
if (selection.size !== selectedKeys.size) {
|
|
473
|
-
return false;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
for (let key of selection) {
|
|
477
|
-
if (!selectedKeys.has(key)) {
|
|
478
|
-
return false;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
for (let key of selectedKeys) {
|
|
483
|
-
if (!selection.has(key)) {
|
|
484
|
-
return false;
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
return true;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
canSelectItem(key: Key): boolean {
|
|
492
|
-
if (this.state.selectionMode === 'none' || this.state.disabledKeys.has(key)) {
|
|
493
|
-
return false;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
let item = this.collection.getItem(key);
|
|
497
|
-
if (!item || item?.props?.isDisabled || (item.type === 'cell' && !this.allowsCellSelection)) {
|
|
498
|
-
return false;
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
return true;
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
isDisabled(key: Key): boolean {
|
|
505
|
-
return this.state.disabledBehavior === 'all' && (this.state.disabledKeys.has(key) || !!this.collection.getItem(key)?.props?.isDisabled);
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
isLink(key: Key): boolean {
|
|
509
|
-
return !!this.collection.getItem(key)?.props?.href;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
getItemProps(key: Key): any {
|
|
513
|
-
return this.collection.getItem(key)?.props;
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
withCollection(collection: Collection<Node<unknown>>): SelectionManager {
|
|
517
|
-
return new SelectionManager(collection, this.state, {
|
|
518
|
-
allowsCellSelection: this.allowsCellSelection,
|
|
519
|
-
layoutDelegate: this.layoutDelegate || undefined
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {Collection, DisabledBehavior, FocusStrategy, Key, LongPressEvent, Node, PressEvent, Selection, SelectionBehavior, SelectionMode} from '@react-types/shared';
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export interface FocusState {
|
|
17
|
-
/** Whether the collection is currently focused. */
|
|
18
|
-
readonly isFocused: boolean,
|
|
19
|
-
/** Sets whether the collection is focused. */
|
|
20
|
-
setFocused(isFocused: boolean): void,
|
|
21
|
-
/** The current focused key in the collection. */
|
|
22
|
-
readonly focusedKey: Key | null,
|
|
23
|
-
/** Whether the first or last child of the focused key should receive focus. */
|
|
24
|
-
readonly childFocusStrategy: FocusStrategy | null,
|
|
25
|
-
/** Sets the focused key, and optionally, whether the first or last child of that key should receive focus. */
|
|
26
|
-
setFocusedKey(key: Key | null, child?: FocusStrategy): void
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface SingleSelectionState extends FocusState {
|
|
30
|
-
/** Whether the collection allows empty selection. */
|
|
31
|
-
readonly disallowEmptySelection?: boolean,
|
|
32
|
-
/** The currently selected key in the collection. */
|
|
33
|
-
readonly selectedKey: Key,
|
|
34
|
-
/** Sets the selected key in the collection. */
|
|
35
|
-
setSelectedKey(key: Key | null): void
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface MultipleSelectionState extends FocusState {
|
|
39
|
-
/** The type of selection that is allowed in the collection. */
|
|
40
|
-
readonly selectionMode: SelectionMode,
|
|
41
|
-
/** The selection behavior for the collection. */
|
|
42
|
-
readonly selectionBehavior: SelectionBehavior,
|
|
43
|
-
/** Sets the selection behavior for the collection. */
|
|
44
|
-
setSelectionBehavior(selectionBehavior: SelectionBehavior): void,
|
|
45
|
-
/** Whether the collection allows empty selection. */
|
|
46
|
-
readonly disallowEmptySelection: boolean,
|
|
47
|
-
/** The currently selected keys in the collection. */
|
|
48
|
-
readonly selectedKeys: Selection,
|
|
49
|
-
/** Sets the selected keys in the collection. */
|
|
50
|
-
setSelectedKeys(keys: Selection): void,
|
|
51
|
-
/** The currently disabled keys in the collection. */
|
|
52
|
-
readonly disabledKeys: Set<Key>,
|
|
53
|
-
/** Whether `disabledKeys` applies to selection, actions, or both. */
|
|
54
|
-
readonly disabledBehavior: DisabledBehavior
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export interface MultipleSelectionManager extends FocusState {
|
|
58
|
-
/** The type of selection that is allowed in the collection. */
|
|
59
|
-
readonly selectionMode: SelectionMode,
|
|
60
|
-
/** The selection behavior for the collection. */
|
|
61
|
-
readonly selectionBehavior: SelectionBehavior,
|
|
62
|
-
/** Whether the collection allows empty selection. */
|
|
63
|
-
readonly disallowEmptySelection?: boolean,
|
|
64
|
-
/** The currently selected keys in the collection. */
|
|
65
|
-
readonly selectedKeys: Set<Key>,
|
|
66
|
-
/** Whether the selection is empty. */
|
|
67
|
-
readonly isEmpty: boolean,
|
|
68
|
-
/** Whether all items in the collection are selected. */
|
|
69
|
-
readonly isSelectAll: boolean,
|
|
70
|
-
/** The first selected key in the collection. */
|
|
71
|
-
readonly firstSelectedKey: Key | null,
|
|
72
|
-
/** The last selected key in the collection. */
|
|
73
|
-
readonly lastSelectedKey: Key | null,
|
|
74
|
-
/** The currently disabled keys in the collection. */
|
|
75
|
-
readonly disabledKeys: Set<Key>,
|
|
76
|
-
/** Whether `disabledKeys` applies to selection, actions, or both. */
|
|
77
|
-
readonly disabledBehavior: DisabledBehavior,
|
|
78
|
-
/** Returns whether a key is selected. */
|
|
79
|
-
isSelected(key: Key): boolean,
|
|
80
|
-
/** Returns whether the current selection is equal to the given selection. */
|
|
81
|
-
isSelectionEqual(selection: Set<Key>): boolean,
|
|
82
|
-
/** Extends the selection to the given key. */
|
|
83
|
-
extendSelection(toKey: Key): void,
|
|
84
|
-
/** Toggles whether the given key is selected. */
|
|
85
|
-
toggleSelection(key: Key): void,
|
|
86
|
-
/** Replaces the selection with only the given key. */
|
|
87
|
-
replaceSelection(key: Key): void,
|
|
88
|
-
/** Replaces the selection with the given keys. */
|
|
89
|
-
setSelectedKeys(keys: Iterable<Key>): void,
|
|
90
|
-
/** Selects all items in the collection. */
|
|
91
|
-
selectAll(): void,
|
|
92
|
-
/** Removes all keys from the selection. */
|
|
93
|
-
clearSelection(): void,
|
|
94
|
-
/** Toggles between select all and an empty selection. */
|
|
95
|
-
toggleSelectAll(): void,
|
|
96
|
-
/**
|
|
97
|
-
* Toggles, replaces, or extends selection to the given key depending
|
|
98
|
-
* on the pointer event and collection's selection mode.
|
|
99
|
-
*/
|
|
100
|
-
select(key: Key, e?: PressEvent | LongPressEvent | PointerEvent): void,
|
|
101
|
-
/** Returns whether the given key can be selected. */
|
|
102
|
-
canSelectItem(key: Key): boolean,
|
|
103
|
-
/** Returns whether the given key is non-interactive, i.e. both selection and actions are disabled. */
|
|
104
|
-
isDisabled(key: Key): boolean,
|
|
105
|
-
/** Sets the selection behavior for the collection. */
|
|
106
|
-
setSelectionBehavior(selectionBehavior: SelectionBehavior): void,
|
|
107
|
-
/** Returns whether the given key is a hyperlink. */
|
|
108
|
-
isLink(key: Key): boolean,
|
|
109
|
-
/** Returns the props for the given item. */
|
|
110
|
-
getItemProps(key: Key): any,
|
|
111
|
-
/** The collection of nodes that the selection manager handles. */
|
|
112
|
-
collection: Collection<Node<unknown>>
|
|
113
|
-
}
|