@react-stately/selection 3.14.3 → 3.15.1

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 CHANGED
@@ -1,129 +1,5 @@
1
- import {useControlledState as $Qsto2$useControlledState} from "@react-stately/utils";
2
- import {useRef as $Qsto2$useRef, useState as $Qsto2$useState, useMemo as $Qsto2$useMemo, useEffect as $Qsto2$useEffect} from "react";
3
- import {compareNodeOrder as $Qsto2$compareNodeOrder, getFirstItem as $Qsto2$getFirstItem, getChildNodes as $Qsto2$getChildNodes} from "@react-stately/collections";
4
-
5
- /*
6
- * Copyright 2020 Adobe. All rights reserved.
7
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
8
- * you may not use this file except in compliance with the License. You may obtain a copy
9
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software distributed under
12
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
13
- * OF ANY KIND, either express or implied. See the License for the specific language
14
- * governing permissions and limitations under the License.
15
- */ /*
16
- * Copyright 2020 Adobe. All rights reserved.
17
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
18
- * you may not use this file except in compliance with the License. You may obtain a copy
19
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
20
- *
21
- * Unless required by applicable law or agreed to in writing, software distributed under
22
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
23
- * OF ANY KIND, either express or implied. See the License for the specific language
24
- * governing permissions and limitations under the License.
25
- */ /*
26
- * Copyright 2020 Adobe. All rights reserved.
27
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
28
- * you may not use this file except in compliance with the License. You may obtain a copy
29
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
30
- *
31
- * Unless required by applicable law or agreed to in writing, software distributed under
32
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
33
- * OF ANY KIND, either express or implied. See the License for the specific language
34
- * governing permissions and limitations under the License.
35
- */ class $e40ea825a81a3709$export$52baac22726c72bf extends Set {
36
- constructor(keys, anchorKey, currentKey){
37
- super(keys);
38
- if (keys instanceof $e40ea825a81a3709$export$52baac22726c72bf) {
39
- this.anchorKey = anchorKey || keys.anchorKey;
40
- this.currentKey = currentKey || keys.currentKey;
41
- } else {
42
- this.anchorKey = anchorKey;
43
- this.currentKey = currentKey;
44
- }
45
- }
46
- }
47
-
48
-
49
-
50
-
51
- function $7af3f5b51489e0b5$var$equalSets(setA, setB) {
52
- if (setA.size !== setB.size) return false;
53
- for (let item of setA){
54
- if (!setB.has(item)) return false;
55
- }
56
- return true;
57
- }
58
- function $7af3f5b51489e0b5$export$253fe78d46329472(props) {
59
- let { selectionMode: selectionMode = "none", disallowEmptySelection: disallowEmptySelection, allowDuplicateSelectionEvents: allowDuplicateSelectionEvents, selectionBehavior: selectionBehaviorProp = "toggle", disabledBehavior: disabledBehavior = "all" } = props;
60
- // We want synchronous updates to `isFocused` and `focusedKey` after their setters are called.
61
- // But we also need to trigger a react re-render. So, we have both a ref (sync) and state (async).
62
- let isFocusedRef = (0, $Qsto2$useRef)(false);
63
- let [, setFocused] = (0, $Qsto2$useState)(false);
64
- let focusedKeyRef = (0, $Qsto2$useRef)(null);
65
- let childFocusStrategyRef = (0, $Qsto2$useRef)(null);
66
- let [, setFocusedKey] = (0, $Qsto2$useState)(null);
67
- let selectedKeysProp = (0, $Qsto2$useMemo)(()=>$7af3f5b51489e0b5$var$convertSelection(props.selectedKeys), [
68
- props.selectedKeys
69
- ]);
70
- let defaultSelectedKeys = (0, $Qsto2$useMemo)(()=>$7af3f5b51489e0b5$var$convertSelection(props.defaultSelectedKeys, new (0, $e40ea825a81a3709$export$52baac22726c72bf)()), [
71
- props.defaultSelectedKeys
72
- ]);
73
- let [selectedKeys, setSelectedKeys] = (0, $Qsto2$useControlledState)(selectedKeysProp, defaultSelectedKeys, props.onSelectionChange);
74
- let disabledKeysProp = (0, $Qsto2$useMemo)(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [
75
- props.disabledKeys
76
- ]);
77
- let [selectionBehavior, setSelectionBehavior] = (0, $Qsto2$useState)(selectionBehaviorProp);
78
- // If the selectionBehavior prop is set to replace, but the current state is toggle (e.g. due to long press
79
- // to enter selection mode on touch), and the selection becomes empty, reset the selection behavior.
80
- if (selectionBehaviorProp === "replace" && selectionBehavior === "toggle" && typeof selectedKeys === "object" && selectedKeys.size === 0) setSelectionBehavior("replace");
81
- // If the selectionBehavior prop changes, update the state as well.
82
- let lastSelectionBehavior = (0, $Qsto2$useRef)(selectionBehaviorProp);
83
- (0, $Qsto2$useEffect)(()=>{
84
- if (selectionBehaviorProp !== lastSelectionBehavior.current) {
85
- setSelectionBehavior(selectionBehaviorProp);
86
- lastSelectionBehavior.current = selectionBehaviorProp;
87
- }
88
- }, [
89
- selectionBehaviorProp
90
- ]);
91
- return {
92
- selectionMode: selectionMode,
93
- disallowEmptySelection: disallowEmptySelection,
94
- selectionBehavior: selectionBehavior,
95
- setSelectionBehavior: setSelectionBehavior,
96
- get isFocused () {
97
- return isFocusedRef.current;
98
- },
99
- setFocused (f) {
100
- isFocusedRef.current = f;
101
- setFocused(f);
102
- },
103
- get focusedKey () {
104
- return focusedKeyRef.current;
105
- },
106
- get childFocusStrategy () {
107
- return childFocusStrategyRef.current;
108
- },
109
- setFocusedKey (k, childFocusStrategy = "first") {
110
- focusedKeyRef.current = k;
111
- childFocusStrategyRef.current = childFocusStrategy;
112
- setFocusedKey(k);
113
- },
114
- selectedKeys: selectedKeys,
115
- setSelectedKeys (keys) {
116
- if (allowDuplicateSelectionEvents || !$7af3f5b51489e0b5$var$equalSets(keys, selectedKeys)) setSelectedKeys(keys);
117
- },
118
- disabledKeys: disabledKeysProp,
119
- disabledBehavior: disabledBehavior
120
- };
121
- }
122
- function $7af3f5b51489e0b5$var$convertSelection(selection, defaultValue) {
123
- if (!selection) return defaultValue;
124
- return selection === "all" ? "all" : new (0, $e40ea825a81a3709$export$52baac22726c72bf)(selection);
125
- }
126
-
1
+ import {useMultipleSelectionState as $7af3f5b51489e0b5$export$253fe78d46329472} from "./useMultipleSelectionState.mjs";
2
+ import {SelectionManager as $d496c0a20b6e58ec$export$6c8a5aaad13c9852} from "./SelectionManager.mjs";
127
3
 
128
4
  /*
129
5
  * Copyright 2020 Adobe. All rights reserved.
@@ -137,284 +13,6 @@ function $7af3f5b51489e0b5$var$convertSelection(selection, defaultValue) {
137
13
  * governing permissions and limitations under the License.
138
14
  */
139
15
 
140
- class $d496c0a20b6e58ec$export$6c8a5aaad13c9852 {
141
- /**
142
- * The type of selection that is allowed in the collection.
143
- */ get selectionMode() {
144
- return this.state.selectionMode;
145
- }
146
- /**
147
- * Whether the collection allows empty selection.
148
- */ get disallowEmptySelection() {
149
- return this.state.disallowEmptySelection;
150
- }
151
- /**
152
- * The selection behavior for the collection.
153
- */ get selectionBehavior() {
154
- return this.state.selectionBehavior;
155
- }
156
- /**
157
- * Sets the selection behavior for the collection.
158
- */ setSelectionBehavior(selectionBehavior) {
159
- this.state.setSelectionBehavior(selectionBehavior);
160
- }
161
- /**
162
- * Whether the collection is currently focused.
163
- */ get isFocused() {
164
- return this.state.isFocused;
165
- }
166
- /**
167
- * Sets whether the collection is focused.
168
- */ setFocused(isFocused) {
169
- this.state.setFocused(isFocused);
170
- }
171
- /**
172
- * The current focused key in the collection.
173
- */ get focusedKey() {
174
- return this.state.focusedKey;
175
- }
176
- /** Whether the first or last child of the focused key should receive focus. */ get childFocusStrategy() {
177
- return this.state.childFocusStrategy;
178
- }
179
- /**
180
- * Sets the focused key.
181
- */ setFocusedKey(key, childFocusStrategy) {
182
- if (key == null || this.collection.getItem(key)) this.state.setFocusedKey(key, childFocusStrategy);
183
- }
184
- /**
185
- * The currently selected keys in the collection.
186
- */ get selectedKeys() {
187
- return this.state.selectedKeys === "all" ? new Set(this.getSelectAllKeys()) : this.state.selectedKeys;
188
- }
189
- /**
190
- * The raw selection value for the collection.
191
- * Either 'all' for select all, or a set of keys.
192
- */ get rawSelection() {
193
- return this.state.selectedKeys;
194
- }
195
- /**
196
- * Returns whether a key is selected.
197
- */ isSelected(key) {
198
- if (this.state.selectionMode === "none") return false;
199
- key = this.getKey(key);
200
- return this.state.selectedKeys === "all" ? this.canSelectItem(key) : this.state.selectedKeys.has(key);
201
- }
202
- /**
203
- * Whether the selection is empty.
204
- */ get isEmpty() {
205
- return this.state.selectedKeys !== "all" && this.state.selectedKeys.size === 0;
206
- }
207
- /**
208
- * Whether all items in the collection are selected.
209
- */ get isSelectAll() {
210
- if (this.isEmpty) return false;
211
- if (this.state.selectedKeys === "all") return true;
212
- if (this._isSelectAll != null) return this._isSelectAll;
213
- let allKeys = this.getSelectAllKeys();
214
- let selectedKeys = this.state.selectedKeys;
215
- this._isSelectAll = allKeys.every((k)=>selectedKeys.has(k));
216
- return this._isSelectAll;
217
- }
218
- get firstSelectedKey() {
219
- let first = null;
220
- for (let key of this.state.selectedKeys){
221
- let item = this.collection.getItem(key);
222
- if (!first || item && (0, $Qsto2$compareNodeOrder)(this.collection, item, first) < 0) first = item;
223
- }
224
- return first === null || first === void 0 ? void 0 : first.key;
225
- }
226
- get lastSelectedKey() {
227
- let last = null;
228
- for (let key of this.state.selectedKeys){
229
- let item = this.collection.getItem(key);
230
- if (!last || item && (0, $Qsto2$compareNodeOrder)(this.collection, item, last) > 0) last = item;
231
- }
232
- return last === null || last === void 0 ? void 0 : last.key;
233
- }
234
- get disabledKeys() {
235
- return this.state.disabledKeys;
236
- }
237
- get disabledBehavior() {
238
- return this.state.disabledBehavior;
239
- }
240
- /**
241
- * Extends the selection to the given key.
242
- */ extendSelection(toKey) {
243
- if (this.selectionMode === "none") return;
244
- if (this.selectionMode === "single") {
245
- this.replaceSelection(toKey);
246
- return;
247
- }
248
- toKey = this.getKey(toKey);
249
- let selection;
250
- // Only select the one key if coming from a select all.
251
- if (this.state.selectedKeys === "all") selection = new (0, $e40ea825a81a3709$export$52baac22726c72bf)([
252
- toKey
253
- ], toKey, toKey);
254
- else {
255
- let selectedKeys = this.state.selectedKeys;
256
- let anchorKey = selectedKeys.anchorKey || toKey;
257
- selection = new (0, $e40ea825a81a3709$export$52baac22726c72bf)(selectedKeys, anchorKey, toKey);
258
- for (let key of this.getKeyRange(anchorKey, selectedKeys.currentKey || toKey))selection.delete(key);
259
- for (let key of this.getKeyRange(toKey, anchorKey))if (this.canSelectItem(key)) selection.add(key);
260
- }
261
- this.state.setSelectedKeys(selection);
262
- }
263
- getKeyRange(from, to) {
264
- let fromItem = this.collection.getItem(from);
265
- let toItem = this.collection.getItem(to);
266
- if (fromItem && toItem) {
267
- if ((0, $Qsto2$compareNodeOrder)(this.collection, fromItem, toItem) <= 0) return this.getKeyRangeInternal(from, to);
268
- return this.getKeyRangeInternal(to, from);
269
- }
270
- return [];
271
- }
272
- getKeyRangeInternal(from, to) {
273
- let keys = [];
274
- let key = from;
275
- while(key){
276
- let item = this.collection.getItem(key);
277
- if (item && item.type === "item" || item.type === "cell" && this.allowsCellSelection) keys.push(key);
278
- if (key === to) return keys;
279
- key = this.collection.getKeyAfter(key);
280
- }
281
- return [];
282
- }
283
- getKey(key) {
284
- let item = this.collection.getItem(key);
285
- if (!item) // ¯\_(ツ)_/¯
286
- return key;
287
- // If cell selection is allowed, just return the key.
288
- if (item.type === "cell" && this.allowsCellSelection) return key;
289
- // Find a parent item to select
290
- while(item.type !== "item" && item.parentKey != null)item = this.collection.getItem(item.parentKey);
291
- if (!item || item.type !== "item") return null;
292
- return item.key;
293
- }
294
- /**
295
- * Toggles whether the given key is selected.
296
- */ toggleSelection(key) {
297
- if (this.selectionMode === "none") return;
298
- if (this.selectionMode === "single" && !this.isSelected(key)) {
299
- this.replaceSelection(key);
300
- return;
301
- }
302
- key = this.getKey(key);
303
- if (key == null) return;
304
- let keys = new (0, $e40ea825a81a3709$export$52baac22726c72bf)(this.state.selectedKeys === "all" ? this.getSelectAllKeys() : this.state.selectedKeys);
305
- if (keys.has(key)) keys.delete(key);
306
- else if (this.canSelectItem(key)) {
307
- keys.add(key);
308
- keys.anchorKey = key;
309
- keys.currentKey = key;
310
- }
311
- if (this.disallowEmptySelection && keys.size === 0) return;
312
- this.state.setSelectedKeys(keys);
313
- }
314
- /**
315
- * Replaces the selection with only the given key.
316
- */ replaceSelection(key) {
317
- if (this.selectionMode === "none") return;
318
- key = this.getKey(key);
319
- if (key == null) return;
320
- let selection = this.canSelectItem(key) ? new (0, $e40ea825a81a3709$export$52baac22726c72bf)([
321
- key
322
- ], key, key) : new (0, $e40ea825a81a3709$export$52baac22726c72bf)();
323
- this.state.setSelectedKeys(selection);
324
- }
325
- /**
326
- * Replaces the selection with the given keys.
327
- */ setSelectedKeys(keys) {
328
- if (this.selectionMode === "none") return;
329
- let selection = new (0, $e40ea825a81a3709$export$52baac22726c72bf)();
330
- for (let key of keys){
331
- key = this.getKey(key);
332
- if (key != null) {
333
- selection.add(key);
334
- if (this.selectionMode === "single") break;
335
- }
336
- }
337
- this.state.setSelectedKeys(selection);
338
- }
339
- getSelectAllKeys() {
340
- let keys = [];
341
- let addKeys = (key)=>{
342
- while(key){
343
- if (this.canSelectItem(key)) {
344
- let item = this.collection.getItem(key);
345
- if (item.type === "item") keys.push(key);
346
- // Add child keys. If cell selection is allowed, then include item children too.
347
- if (item.hasChildNodes && (this.allowsCellSelection || item.type !== "item")) addKeys((0, $Qsto2$getFirstItem)((0, $Qsto2$getChildNodes)(item, this.collection)).key);
348
- }
349
- key = this.collection.getKeyAfter(key);
350
- }
351
- };
352
- addKeys(this.collection.getFirstKey());
353
- return keys;
354
- }
355
- /**
356
- * Selects all items in the collection.
357
- */ selectAll() {
358
- if (!this.isSelectAll && this.selectionMode === "multiple") this.state.setSelectedKeys("all");
359
- }
360
- /**
361
- * Removes all keys from the selection.
362
- */ clearSelection() {
363
- if (!this.disallowEmptySelection && (this.state.selectedKeys === "all" || this.state.selectedKeys.size > 0)) this.state.setSelectedKeys(new (0, $e40ea825a81a3709$export$52baac22726c72bf)());
364
- }
365
- /**
366
- * Toggles between select all and an empty selection.
367
- */ toggleSelectAll() {
368
- if (this.isSelectAll) this.clearSelection();
369
- else this.selectAll();
370
- }
371
- select(key, e) {
372
- if (this.selectionMode === "none") return;
373
- if (this.selectionMode === "single") {
374
- if (this.isSelected(key) && !this.disallowEmptySelection) this.toggleSelection(key);
375
- else this.replaceSelection(key);
376
- } else if (this.selectionBehavior === "toggle" || e && (e.pointerType === "touch" || e.pointerType === "virtual")) // 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
377
- this.toggleSelection(key);
378
- else this.replaceSelection(key);
379
- }
380
- /**
381
- * Returns whether the current selection is equal to the given selection.
382
- */ isSelectionEqual(selection) {
383
- if (selection === this.state.selectedKeys) return true;
384
- // Check if the set of keys match.
385
- let selectedKeys = this.selectedKeys;
386
- if (selection.size !== selectedKeys.size) return false;
387
- for (let key of selection){
388
- if (!selectedKeys.has(key)) return false;
389
- }
390
- for (let key of selectedKeys){
391
- if (!selection.has(key)) return false;
392
- }
393
- return true;
394
- }
395
- canSelectItem(key) {
396
- if (this.state.selectionMode === "none" || this.state.disabledKeys.has(key)) return false;
397
- let item = this.collection.getItem(key);
398
- if (!item || item.type === "cell" && !this.allowsCellSelection) return false;
399
- return true;
400
- }
401
- isDisabled(key) {
402
- return this.state.disabledKeys.has(key) && this.state.disabledBehavior === "all";
403
- }
404
- isLink(key) {
405
- var _this_collection_getItem_props, _this_collection_getItem;
406
- return !!((_this_collection_getItem = this.collection.getItem(key)) === null || _this_collection_getItem === void 0 ? void 0 : (_this_collection_getItem_props = _this_collection_getItem.props) === null || _this_collection_getItem_props === void 0 ? void 0 : _this_collection_getItem_props.href);
407
- }
408
- constructor(collection, state, options){
409
- this.collection = collection;
410
- this.state = state;
411
- var _options_allowsCellSelection;
412
- this.allowsCellSelection = (_options_allowsCellSelection = options === null || options === void 0 ? void 0 : options.allowsCellSelection) !== null && _options_allowsCellSelection !== void 0 ? _options_allowsCellSelection : false;
413
- this._isSelectAll = null;
414
- }
415
- }
416
-
417
-
418
16
 
419
17
 
420
18
  export {$7af3f5b51489e0b5$export$253fe78d46329472 as useMultipleSelectionState, $d496c0a20b6e58ec$export$6c8a5aaad13c9852 as SelectionManager};