@react-stately/selection 3.14.2 → 3.15.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/Selection.main.js +31 -0
- package/dist/Selection.main.js.map +1 -0
- package/dist/Selection.mjs +26 -0
- package/dist/Selection.module.js +26 -0
- package/dist/Selection.module.js.map +1 -0
- package/dist/SelectionManager.main.js +306 -0
- package/dist/SelectionManager.main.js.map +1 -0
- package/dist/SelectionManager.mjs +301 -0
- package/dist/SelectionManager.module.js +301 -0
- package/dist/SelectionManager.module.js.map +1 -0
- package/dist/import.mjs +2 -404
- package/dist/main.js +4 -406
- package/dist/main.js.map +1 -1
- package/dist/module.js +2 -404
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/useMultipleSelectionState.main.js +101 -0
- package/dist/useMultipleSelectionState.main.js.map +1 -0
- package/dist/useMultipleSelectionState.mjs +96 -0
- package/dist/useMultipleSelectionState.module.js +96 -0
- package/dist/useMultipleSelectionState.module.js.map +1 -0
- package/package.json +5 -5
- package/src/SelectionManager.ts +6 -2
- package/src/types.ts +3 -1
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
function $parcel$export(e, n, v, s) {
|
|
3
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
$parcel$export(module.exports, "Selection", () => $21c847070f1f9569$export$52baac22726c72bf);
|
|
7
|
+
/*
|
|
8
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
9
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
11
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
14
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
15
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
16
|
+
* governing permissions and limitations under the License.
|
|
17
|
+
*/ class $21c847070f1f9569$export$52baac22726c72bf extends Set {
|
|
18
|
+
constructor(keys, anchorKey, currentKey){
|
|
19
|
+
super(keys);
|
|
20
|
+
if (keys instanceof $21c847070f1f9569$export$52baac22726c72bf) {
|
|
21
|
+
this.anchorKey = anchorKey || keys.anchorKey;
|
|
22
|
+
this.currentKey = currentKey || keys.currentKey;
|
|
23
|
+
} else {
|
|
24
|
+
this.anchorKey = anchorKey;
|
|
25
|
+
this.currentKey = currentKey;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=Selection.main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;AAAA;;;;;;;;;;CAUC,GAQM,MAAM,kDAAkB;IAI7B,YAAY,IAAgC,EAAE,SAAe,EAAE,UAAgB,CAAE;QAC/E,KAAK,CAAC;QACN,IAAI,gBAAgB,2CAAW;YAC7B,IAAI,CAAC,SAAS,GAAG,aAAa,KAAK,SAAS;YAC5C,IAAI,CAAC,UAAU,GAAG,cAAc,KAAK,UAAU;QACjD,OAAO;YACL,IAAI,CAAC,SAAS,GAAG;YACjB,IAAI,CAAC,UAAU,GAAG;QACpB;IACF;AACF","sources":["packages/@react-stately/selection/src/Selection.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Key} from '@react-types/shared';\n\n/**\n * A Selection is a special Set containing Keys, which also has an anchor\n * and current selected key for use when range selecting.\n */\nexport class Selection extends Set<Key> {\n anchorKey: Key;\n currentKey: Key;\n\n constructor(keys?: Iterable<Key> | Selection, anchorKey?: Key, currentKey?: Key) {\n super(keys);\n if (keys instanceof Selection) {\n this.anchorKey = anchorKey || keys.anchorKey;\n this.currentKey = currentKey || keys.currentKey;\n } else {\n this.anchorKey = anchorKey;\n this.currentKey = currentKey;\n }\n }\n}\n"],"names":[],"version":3,"file":"Selection.main.js.map"}
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
*/ class $e40ea825a81a3709$export$52baac22726c72bf extends Set {
|
|
12
|
+
constructor(keys, anchorKey, currentKey){
|
|
13
|
+
super(keys);
|
|
14
|
+
if (keys instanceof $e40ea825a81a3709$export$52baac22726c72bf) {
|
|
15
|
+
this.anchorKey = anchorKey || keys.anchorKey;
|
|
16
|
+
this.currentKey = currentKey || keys.currentKey;
|
|
17
|
+
} else {
|
|
18
|
+
this.anchorKey = anchorKey;
|
|
19
|
+
this.currentKey = currentKey;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export {$e40ea825a81a3709$export$52baac22726c72bf as Selection};
|
|
26
|
+
//# sourceMappingURL=Selection.mjs.map
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
*/ class $e40ea825a81a3709$export$52baac22726c72bf extends Set {
|
|
12
|
+
constructor(keys, anchorKey, currentKey){
|
|
13
|
+
super(keys);
|
|
14
|
+
if (keys instanceof $e40ea825a81a3709$export$52baac22726c72bf) {
|
|
15
|
+
this.anchorKey = anchorKey || keys.anchorKey;
|
|
16
|
+
this.currentKey = currentKey || keys.currentKey;
|
|
17
|
+
} else {
|
|
18
|
+
this.anchorKey = anchorKey;
|
|
19
|
+
this.currentKey = currentKey;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export {$e40ea825a81a3709$export$52baac22726c72bf as Selection};
|
|
26
|
+
//# sourceMappingURL=Selection.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAAA;;;;;;;;;;CAUC,GAQM,MAAM,kDAAkB;IAI7B,YAAY,IAAgC,EAAE,SAAe,EAAE,UAAgB,CAAE;QAC/E,KAAK,CAAC;QACN,IAAI,gBAAgB,2CAAW;YAC7B,IAAI,CAAC,SAAS,GAAG,aAAa,KAAK,SAAS;YAC5C,IAAI,CAAC,UAAU,GAAG,cAAc,KAAK,UAAU;QACjD,OAAO;YACL,IAAI,CAAC,SAAS,GAAG;YACjB,IAAI,CAAC,UAAU,GAAG;QACpB;IACF;AACF","sources":["packages/@react-stately/selection/src/Selection.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Key} from '@react-types/shared';\n\n/**\n * A Selection is a special Set containing Keys, which also has an anchor\n * and current selected key for use when range selecting.\n */\nexport class Selection extends Set<Key> {\n anchorKey: Key;\n currentKey: Key;\n\n constructor(keys?: Iterable<Key> | Selection, anchorKey?: Key, currentKey?: Key) {\n super(keys);\n if (keys instanceof Selection) {\n this.anchorKey = anchorKey || keys.anchorKey;\n this.currentKey = currentKey || keys.currentKey;\n } else {\n this.anchorKey = anchorKey;\n this.currentKey = currentKey;\n }\n }\n}\n"],"names":[],"version":3,"file":"Selection.module.js.map"}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
var $21c847070f1f9569$exports = require("./Selection.main.js");
|
|
2
|
+
var $ia6MY$reactstatelycollections = require("@react-stately/collections");
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
function $parcel$export(e, n, v, s) {
|
|
6
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
$parcel$export(module.exports, "SelectionManager", () => $8112da6fa5bbc322$export$6c8a5aaad13c9852);
|
|
10
|
+
/*
|
|
11
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
12
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
13
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
14
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
17
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
18
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
19
|
+
* governing permissions and limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
class $8112da6fa5bbc322$export$6c8a5aaad13c9852 {
|
|
23
|
+
/**
|
|
24
|
+
* The type of selection that is allowed in the collection.
|
|
25
|
+
*/ get selectionMode() {
|
|
26
|
+
return this.state.selectionMode;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Whether the collection allows empty selection.
|
|
30
|
+
*/ get disallowEmptySelection() {
|
|
31
|
+
return this.state.disallowEmptySelection;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The selection behavior for the collection.
|
|
35
|
+
*/ get selectionBehavior() {
|
|
36
|
+
return this.state.selectionBehavior;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sets the selection behavior for the collection.
|
|
40
|
+
*/ setSelectionBehavior(selectionBehavior) {
|
|
41
|
+
this.state.setSelectionBehavior(selectionBehavior);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Whether the collection is currently focused.
|
|
45
|
+
*/ get isFocused() {
|
|
46
|
+
return this.state.isFocused;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sets whether the collection is focused.
|
|
50
|
+
*/ setFocused(isFocused) {
|
|
51
|
+
this.state.setFocused(isFocused);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The current focused key in the collection.
|
|
55
|
+
*/ get focusedKey() {
|
|
56
|
+
return this.state.focusedKey;
|
|
57
|
+
}
|
|
58
|
+
/** Whether the first or last child of the focused key should receive focus. */ get childFocusStrategy() {
|
|
59
|
+
return this.state.childFocusStrategy;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Sets the focused key.
|
|
63
|
+
*/ setFocusedKey(key, childFocusStrategy) {
|
|
64
|
+
if (key == null || this.collection.getItem(key)) this.state.setFocusedKey(key, childFocusStrategy);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The currently selected keys in the collection.
|
|
68
|
+
*/ get selectedKeys() {
|
|
69
|
+
return this.state.selectedKeys === "all" ? new Set(this.getSelectAllKeys()) : this.state.selectedKeys;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The raw selection value for the collection.
|
|
73
|
+
* Either 'all' for select all, or a set of keys.
|
|
74
|
+
*/ get rawSelection() {
|
|
75
|
+
return this.state.selectedKeys;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns whether a key is selected.
|
|
79
|
+
*/ isSelected(key) {
|
|
80
|
+
if (this.state.selectionMode === "none") return false;
|
|
81
|
+
key = this.getKey(key);
|
|
82
|
+
return this.state.selectedKeys === "all" ? this.canSelectItem(key) : this.state.selectedKeys.has(key);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Whether the selection is empty.
|
|
86
|
+
*/ get isEmpty() {
|
|
87
|
+
return this.state.selectedKeys !== "all" && this.state.selectedKeys.size === 0;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Whether all items in the collection are selected.
|
|
91
|
+
*/ get isSelectAll() {
|
|
92
|
+
if (this.isEmpty) return false;
|
|
93
|
+
if (this.state.selectedKeys === "all") return true;
|
|
94
|
+
if (this._isSelectAll != null) return this._isSelectAll;
|
|
95
|
+
let allKeys = this.getSelectAllKeys();
|
|
96
|
+
let selectedKeys = this.state.selectedKeys;
|
|
97
|
+
this._isSelectAll = allKeys.every((k)=>selectedKeys.has(k));
|
|
98
|
+
return this._isSelectAll;
|
|
99
|
+
}
|
|
100
|
+
get firstSelectedKey() {
|
|
101
|
+
let first = null;
|
|
102
|
+
for (let key of this.state.selectedKeys){
|
|
103
|
+
let item = this.collection.getItem(key);
|
|
104
|
+
if (!first || item && (0, $ia6MY$reactstatelycollections.compareNodeOrder)(this.collection, item, first) < 0) first = item;
|
|
105
|
+
}
|
|
106
|
+
return first === null || first === void 0 ? void 0 : first.key;
|
|
107
|
+
}
|
|
108
|
+
get lastSelectedKey() {
|
|
109
|
+
let last = null;
|
|
110
|
+
for (let key of this.state.selectedKeys){
|
|
111
|
+
let item = this.collection.getItem(key);
|
|
112
|
+
if (!last || item && (0, $ia6MY$reactstatelycollections.compareNodeOrder)(this.collection, item, last) > 0) last = item;
|
|
113
|
+
}
|
|
114
|
+
return last === null || last === void 0 ? void 0 : last.key;
|
|
115
|
+
}
|
|
116
|
+
get disabledKeys() {
|
|
117
|
+
return this.state.disabledKeys;
|
|
118
|
+
}
|
|
119
|
+
get disabledBehavior() {
|
|
120
|
+
return this.state.disabledBehavior;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Extends the selection to the given key.
|
|
124
|
+
*/ extendSelection(toKey) {
|
|
125
|
+
if (this.selectionMode === "none") return;
|
|
126
|
+
if (this.selectionMode === "single") {
|
|
127
|
+
this.replaceSelection(toKey);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
toKey = this.getKey(toKey);
|
|
131
|
+
let selection;
|
|
132
|
+
// Only select the one key if coming from a select all.
|
|
133
|
+
if (this.state.selectedKeys === "all") selection = new (0, $21c847070f1f9569$exports.Selection)([
|
|
134
|
+
toKey
|
|
135
|
+
], toKey, toKey);
|
|
136
|
+
else {
|
|
137
|
+
let selectedKeys = this.state.selectedKeys;
|
|
138
|
+
let anchorKey = selectedKeys.anchorKey || toKey;
|
|
139
|
+
selection = new (0, $21c847070f1f9569$exports.Selection)(selectedKeys, anchorKey, toKey);
|
|
140
|
+
for (let key of this.getKeyRange(anchorKey, selectedKeys.currentKey || toKey))selection.delete(key);
|
|
141
|
+
for (let key of this.getKeyRange(toKey, anchorKey))if (this.canSelectItem(key)) selection.add(key);
|
|
142
|
+
}
|
|
143
|
+
this.state.setSelectedKeys(selection);
|
|
144
|
+
}
|
|
145
|
+
getKeyRange(from, to) {
|
|
146
|
+
let fromItem = this.collection.getItem(from);
|
|
147
|
+
let toItem = this.collection.getItem(to);
|
|
148
|
+
if (fromItem && toItem) {
|
|
149
|
+
if ((0, $ia6MY$reactstatelycollections.compareNodeOrder)(this.collection, fromItem, toItem) <= 0) return this.getKeyRangeInternal(from, to);
|
|
150
|
+
return this.getKeyRangeInternal(to, from);
|
|
151
|
+
}
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
getKeyRangeInternal(from, to) {
|
|
155
|
+
let keys = [];
|
|
156
|
+
let key = from;
|
|
157
|
+
while(key){
|
|
158
|
+
let item = this.collection.getItem(key);
|
|
159
|
+
if (item && item.type === "item" || item.type === "cell" && this.allowsCellSelection) keys.push(key);
|
|
160
|
+
if (key === to) return keys;
|
|
161
|
+
key = this.collection.getKeyAfter(key);
|
|
162
|
+
}
|
|
163
|
+
return [];
|
|
164
|
+
}
|
|
165
|
+
getKey(key) {
|
|
166
|
+
let item = this.collection.getItem(key);
|
|
167
|
+
if (!item) // ¯\_(ツ)_/¯
|
|
168
|
+
return key;
|
|
169
|
+
// If cell selection is allowed, just return the key.
|
|
170
|
+
if (item.type === "cell" && this.allowsCellSelection) return key;
|
|
171
|
+
// Find a parent item to select
|
|
172
|
+
while(item.type !== "item" && item.parentKey != null)item = this.collection.getItem(item.parentKey);
|
|
173
|
+
if (!item || item.type !== "item") return null;
|
|
174
|
+
return item.key;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Toggles whether the given key is selected.
|
|
178
|
+
*/ toggleSelection(key) {
|
|
179
|
+
if (this.selectionMode === "none") return;
|
|
180
|
+
if (this.selectionMode === "single" && !this.isSelected(key)) {
|
|
181
|
+
this.replaceSelection(key);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
key = this.getKey(key);
|
|
185
|
+
if (key == null) return;
|
|
186
|
+
let keys = new (0, $21c847070f1f9569$exports.Selection)(this.state.selectedKeys === "all" ? this.getSelectAllKeys() : this.state.selectedKeys);
|
|
187
|
+
if (keys.has(key)) keys.delete(key);
|
|
188
|
+
else if (this.canSelectItem(key)) {
|
|
189
|
+
keys.add(key);
|
|
190
|
+
keys.anchorKey = key;
|
|
191
|
+
keys.currentKey = key;
|
|
192
|
+
}
|
|
193
|
+
if (this.disallowEmptySelection && keys.size === 0) return;
|
|
194
|
+
this.state.setSelectedKeys(keys);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Replaces the selection with only the given key.
|
|
198
|
+
*/ replaceSelection(key) {
|
|
199
|
+
if (this.selectionMode === "none") return;
|
|
200
|
+
key = this.getKey(key);
|
|
201
|
+
if (key == null) return;
|
|
202
|
+
let selection = this.canSelectItem(key) ? new (0, $21c847070f1f9569$exports.Selection)([
|
|
203
|
+
key
|
|
204
|
+
], key, key) : new (0, $21c847070f1f9569$exports.Selection)();
|
|
205
|
+
this.state.setSelectedKeys(selection);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Replaces the selection with the given keys.
|
|
209
|
+
*/ setSelectedKeys(keys) {
|
|
210
|
+
if (this.selectionMode === "none") return;
|
|
211
|
+
let selection = new (0, $21c847070f1f9569$exports.Selection)();
|
|
212
|
+
for (let key of keys){
|
|
213
|
+
key = this.getKey(key);
|
|
214
|
+
if (key != null) {
|
|
215
|
+
selection.add(key);
|
|
216
|
+
if (this.selectionMode === "single") break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
this.state.setSelectedKeys(selection);
|
|
220
|
+
}
|
|
221
|
+
getSelectAllKeys() {
|
|
222
|
+
let keys = [];
|
|
223
|
+
let addKeys = (key)=>{
|
|
224
|
+
while(key){
|
|
225
|
+
if (this.canSelectItem(key)) {
|
|
226
|
+
let item = this.collection.getItem(key);
|
|
227
|
+
if (item.type === "item") keys.push(key);
|
|
228
|
+
// Add child keys. If cell selection is allowed, then include item children too.
|
|
229
|
+
if (item.hasChildNodes && (this.allowsCellSelection || item.type !== "item")) addKeys((0, $ia6MY$reactstatelycollections.getFirstItem)((0, $ia6MY$reactstatelycollections.getChildNodes)(item, this.collection)).key);
|
|
230
|
+
}
|
|
231
|
+
key = this.collection.getKeyAfter(key);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
addKeys(this.collection.getFirstKey());
|
|
235
|
+
return keys;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Selects all items in the collection.
|
|
239
|
+
*/ selectAll() {
|
|
240
|
+
if (!this.isSelectAll && this.selectionMode === "multiple") this.state.setSelectedKeys("all");
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Removes all keys from the selection.
|
|
244
|
+
*/ clearSelection() {
|
|
245
|
+
if (!this.disallowEmptySelection && (this.state.selectedKeys === "all" || this.state.selectedKeys.size > 0)) this.state.setSelectedKeys(new (0, $21c847070f1f9569$exports.Selection)());
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Toggles between select all and an empty selection.
|
|
249
|
+
*/ toggleSelectAll() {
|
|
250
|
+
if (this.isSelectAll) this.clearSelection();
|
|
251
|
+
else this.selectAll();
|
|
252
|
+
}
|
|
253
|
+
select(key, e) {
|
|
254
|
+
if (this.selectionMode === "none") return;
|
|
255
|
+
if (this.selectionMode === "single") {
|
|
256
|
+
if (this.isSelected(key) && !this.disallowEmptySelection) this.toggleSelection(key);
|
|
257
|
+
else this.replaceSelection(key);
|
|
258
|
+
} 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
|
|
259
|
+
this.toggleSelection(key);
|
|
260
|
+
else this.replaceSelection(key);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Returns whether the current selection is equal to the given selection.
|
|
264
|
+
*/ isSelectionEqual(selection) {
|
|
265
|
+
if (selection === this.state.selectedKeys) return true;
|
|
266
|
+
// Check if the set of keys match.
|
|
267
|
+
let selectedKeys = this.selectedKeys;
|
|
268
|
+
if (selection.size !== selectedKeys.size) return false;
|
|
269
|
+
for (let key of selection){
|
|
270
|
+
if (!selectedKeys.has(key)) return false;
|
|
271
|
+
}
|
|
272
|
+
for (let key of selectedKeys){
|
|
273
|
+
if (!selection.has(key)) return false;
|
|
274
|
+
}
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
canSelectItem(key) {
|
|
278
|
+
var _item_props;
|
|
279
|
+
if (this.state.selectionMode === "none" || this.state.disabledKeys.has(key)) return false;
|
|
280
|
+
let item = this.collection.getItem(key);
|
|
281
|
+
if (!item || (item === null || item === void 0 ? void 0 : (_item_props = item.props) === null || _item_props === void 0 ? void 0 : _item_props.isDisabled) || item.type === "cell" && !this.allowsCellSelection) return false;
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
isDisabled(key) {
|
|
285
|
+
var _this_collection_getItem_props, _this_collection_getItem;
|
|
286
|
+
return this.state.disabledBehavior === "all" && (this.state.disabledKeys.has(key) || !!((_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.isDisabled));
|
|
287
|
+
}
|
|
288
|
+
isLink(key) {
|
|
289
|
+
var _this_collection_getItem_props, _this_collection_getItem;
|
|
290
|
+
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);
|
|
291
|
+
}
|
|
292
|
+
getItemProps(key) {
|
|
293
|
+
var _this_collection_getItem;
|
|
294
|
+
return (_this_collection_getItem = this.collection.getItem(key)) === null || _this_collection_getItem === void 0 ? void 0 : _this_collection_getItem.props;
|
|
295
|
+
}
|
|
296
|
+
constructor(collection, state, options){
|
|
297
|
+
this.collection = collection;
|
|
298
|
+
this.state = state;
|
|
299
|
+
var _options_allowsCellSelection;
|
|
300
|
+
this.allowsCellSelection = (_options_allowsCellSelection = options === null || options === void 0 ? void 0 : options.allowsCellSelection) !== null && _options_allowsCellSelection !== void 0 ? _options_allowsCellSelection : false;
|
|
301
|
+
this._isSelectAll = null;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
//# sourceMappingURL=SelectionManager.main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;AAAA;;;;;;;;;;CAUC;;AAwBM,MAAM;IAaX;;GAEC,GACD,IAAI,gBAA+B;QACjC,OAAO,IAAI,CAAC,KAAK,CAAC,aAAa;IACjC;IAEA;;GAEC,GACD,IAAI,yBAAkC;QACpC,OAAO,IAAI,CAAC,KAAK,CAAC,sBAAsB;IAC1C;IAEA;;GAEC,GACD,IAAI,oBAAuC;QACzC,OAAO,IAAI,CAAC,KAAK,CAAC,iBAAiB;IACrC;IAEA;;GAEC,GACD,qBAAqB,iBAAoC,EAAE;QACzD,IAAI,CAAC,KAAK,CAAC,oBAAoB,CAAC;IAClC;IAEA;;GAEC,GACD,IAAI,YAAqB;QACvB,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS;IAC7B;IAEA;;GAEC,GACD,WAAW,SAAkB,EAAE;QAC7B,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC;IACxB;IAEA;;GAEC,GACD,IAAI,aAAkB;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC,UAAU;IAC9B;IAEA,6EAA6E,GAC7E,IAAI,qBAAoC;QACtC,OAAO,IAAI,CAAC,KAAK,CAAC,kBAAkB;IACtC;IAEA;;GAEC,GACD,cAAc,GAAe,EAAE,kBAAkC,EAAE;QACjE,IAAI,OAAO,QAAQ,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,MACzC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,KAAK;IAElC;IAEA;;GAEC,GACD,IAAI,eAAy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AE;YACoE,gCAAA;QAAvF,OAAO,IAAI,CAAC,KAAK,CAAC,gBAAgB,KAAK,SAAU,CAAA,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAC,2BAAA,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,kBAAxB,gDAAA,iCAAA,yBAA8B,KAAK,cAAnC,qDAAA,+BAAqC,UAAU,CAAD;IACvI;IAEA,OAAO,GAAQ,EAAE;YACN,gCAAA;QAAT,OAAO,CAAC,GAAC,2BAAA,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,kBAAxB,gDAAA,iCAAA,yBAA8B,KAAK,cAAnC,qDAAA,+BAAqC,IAAI;IACpD;IAEA,aAAa,GAAQ,EAAE;YACd;QAAP,QAAO,2BAAA,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,kBAAxB,+CAAA,yBAA8B,KAAK;IAC5C;IA3cA,YAAY,UAAqC,EAAE,KAA6B,EAAE,OAAiC,CAAE;QACnH,IAAI,CAAC,UAAU,GAAG;QAClB,IAAI,CAAC,KAAK,GAAG;YACc;QAA3B,IAAI,CAAC,mBAAmB,GAAG,CAAA,+BAAA,oBAAA,8BAAA,QAAS,mBAAmB,cAA5B,0CAAA,+BAAgC;QAC3D,IAAI,CAAC,YAAY,GAAG;IACtB;AAucF","sources":["packages/@react-stately/selection/src/SelectionManager.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {\n Collection, DisabledBehavior,\n FocusStrategy,\n Selection as ISelection,\n Key,\n LongPressEvent,\n Node,\n PressEvent,\n SelectionBehavior,\n SelectionMode\n} from '@react-types/shared';\nimport {compareNodeOrder, getChildNodes, getFirstItem} from '@react-stately/collections';\nimport {MultipleSelectionManager, MultipleSelectionState} from './types';\nimport {Selection} from './Selection';\n\ninterface SelectionManagerOptions {\n allowsCellSelection?: boolean\n}\n\n/**\n * An interface for reading and updating multiple selection state.\n */\nexport class SelectionManager implements MultipleSelectionManager {\n private collection: Collection<Node<unknown>>;\n private state: MultipleSelectionState;\n private allowsCellSelection: boolean;\n private _isSelectAll: boolean;\n\n constructor(collection: Collection<Node<unknown>>, state: MultipleSelectionState, options?: SelectionManagerOptions) {\n this.collection = collection;\n this.state = state;\n this.allowsCellSelection = options?.allowsCellSelection ?? false;\n this._isSelectAll = null;\n }\n\n /**\n * The type of selection that is allowed in the collection.\n */\n get selectionMode(): SelectionMode {\n return this.state.selectionMode;\n }\n\n /**\n * Whether the collection allows empty selection.\n */\n get disallowEmptySelection(): boolean {\n return this.state.disallowEmptySelection;\n }\n\n /**\n * The selection behavior for the collection.\n */\n get selectionBehavior(): SelectionBehavior {\n return this.state.selectionBehavior;\n }\n\n /**\n * Sets the selection behavior for the collection.\n */\n setSelectionBehavior(selectionBehavior: SelectionBehavior) {\n this.state.setSelectionBehavior(selectionBehavior);\n }\n\n /**\n * Whether the collection is currently focused.\n */\n get isFocused(): boolean {\n return this.state.isFocused;\n }\n\n /**\n * Sets whether the collection is focused.\n */\n setFocused(isFocused: boolean) {\n this.state.setFocused(isFocused);\n }\n\n /**\n * The current focused key in the collection.\n */\n get focusedKey(): Key {\n return this.state.focusedKey;\n }\n\n /** Whether the first or last child of the focused key should receive focus. */\n get childFocusStrategy(): FocusStrategy {\n return this.state.childFocusStrategy;\n }\n\n /**\n * Sets the focused key.\n */\n setFocusedKey(key: Key | null, childFocusStrategy?: FocusStrategy) {\n if (key == null || this.collection.getItem(key)) {\n this.state.setFocusedKey(key, childFocusStrategy);\n }\n }\n\n /**\n * The currently selected keys in the collection.\n */\n get selectedKeys(): Set<Key> {\n return this.state.selectedKeys === 'all'\n ? new Set(this.getSelectAllKeys())\n : this.state.selectedKeys;\n }\n\n /**\n * The raw selection value for the collection.\n * Either 'all' for select all, or a set of keys.\n */\n get rawSelection(): ISelection {\n return this.state.selectedKeys;\n }\n\n /**\n * Returns whether a key is selected.\n */\n isSelected(key: Key) {\n if (this.state.selectionMode === 'none') {\n return false;\n }\n\n key = this.getKey(key);\n return this.state.selectedKeys === 'all'\n ? this.canSelectItem(key)\n : this.state.selectedKeys.has(key);\n }\n\n /**\n * Whether the selection is empty.\n */\n get isEmpty(): boolean {\n return this.state.selectedKeys !== 'all' && this.state.selectedKeys.size === 0;\n }\n\n /**\n * Whether all items in the collection are selected.\n */\n get isSelectAll(): boolean {\n if (this.isEmpty) {\n return false;\n }\n\n if (this.state.selectedKeys === 'all') {\n return true;\n }\n\n if (this._isSelectAll != null) {\n return this._isSelectAll;\n }\n\n let allKeys = this.getSelectAllKeys();\n let selectedKeys = this.state.selectedKeys;\n this._isSelectAll = allKeys.every(k => selectedKeys.has(k));\n return this._isSelectAll;\n }\n\n get firstSelectedKey(): Key | null {\n let first: Node<unknown> | null = null;\n for (let key of this.state.selectedKeys) {\n let item = this.collection.getItem(key);\n if (!first || (item && compareNodeOrder(this.collection, item, first) < 0)) {\n first = item;\n }\n }\n\n return first?.key;\n }\n\n get lastSelectedKey(): Key | null {\n let last: Node<unknown> | null = null;\n for (let key of this.state.selectedKeys) {\n let item = this.collection.getItem(key);\n if (!last || (item && compareNodeOrder(this.collection, item, last) > 0)) {\n last = item;\n }\n }\n\n return last?.key;\n }\n\n get disabledKeys(): Set<Key> {\n return this.state.disabledKeys;\n }\n\n get disabledBehavior(): DisabledBehavior {\n return this.state.disabledBehavior;\n }\n\n /**\n * Extends the selection to the given key.\n */\n extendSelection(toKey: Key) {\n if (this.selectionMode === 'none') {\n return;\n }\n\n if (this.selectionMode === 'single') {\n this.replaceSelection(toKey);\n return;\n }\n\n toKey = this.getKey(toKey);\n\n let selection: Selection;\n\n // Only select the one key if coming from a select all.\n if (this.state.selectedKeys === 'all') {\n selection = new Selection([toKey], toKey, toKey);\n } else {\n let selectedKeys = this.state.selectedKeys as Selection;\n let anchorKey = selectedKeys.anchorKey || toKey;\n selection = new Selection(selectedKeys, anchorKey, toKey);\n for (let key of this.getKeyRange(anchorKey, selectedKeys.currentKey || toKey)) {\n selection.delete(key);\n }\n\n for (let key of this.getKeyRange(toKey, anchorKey)) {\n if (this.canSelectItem(key)) {\n selection.add(key);\n }\n }\n }\n\n this.state.setSelectedKeys(selection);\n }\n\n private getKeyRange(from: Key, to: Key) {\n let fromItem = this.collection.getItem(from);\n let toItem = this.collection.getItem(to);\n if (fromItem && toItem) {\n if (compareNodeOrder(this.collection, fromItem, toItem) <= 0) {\n return this.getKeyRangeInternal(from, to);\n }\n\n return this.getKeyRangeInternal(to, from);\n }\n\n return [];\n }\n\n private getKeyRangeInternal(from: Key, to: Key) {\n let keys: Key[] = [];\n let key = from;\n while (key) {\n let item = this.collection.getItem(key);\n if (item && item.type === 'item' || (item.type === 'cell' && this.allowsCellSelection)) {\n keys.push(key);\n }\n\n if (key === to) {\n return keys;\n }\n\n key = this.collection.getKeyAfter(key);\n }\n\n return [];\n }\n\n private getKey(key: Key) {\n let item = this.collection.getItem(key);\n if (!item) {\n // ¯\\_(ツ)_/¯\n return key;\n }\n\n // If cell selection is allowed, just return the key.\n if (item.type === 'cell' && this.allowsCellSelection) {\n return key;\n }\n\n // Find a parent item to select\n while (item.type !== 'item' && item.parentKey != null) {\n item = this.collection.getItem(item.parentKey);\n }\n\n if (!item || item.type !== 'item') {\n return null;\n }\n\n return item.key;\n }\n\n /**\n * Toggles whether the given key is selected.\n */\n toggleSelection(key: Key) {\n if (this.selectionMode === 'none') {\n return;\n }\n\n if (this.selectionMode === 'single' && !this.isSelected(key)) {\n this.replaceSelection(key);\n return;\n }\n\n key = this.getKey(key);\n if (key == null) {\n return;\n }\n\n let keys = new Selection(this.state.selectedKeys === 'all' ? this.getSelectAllKeys() : this.state.selectedKeys);\n if (keys.has(key)) {\n keys.delete(key);\n // TODO: move anchor to last selected key...\n // Does `current` need to move here too?\n } else if (this.canSelectItem(key)) {\n keys.add(key);\n keys.anchorKey = key;\n keys.currentKey = key;\n }\n\n if (this.disallowEmptySelection && keys.size === 0) {\n return;\n }\n\n this.state.setSelectedKeys(keys);\n }\n\n /**\n * Replaces the selection with only the given key.\n */\n replaceSelection(key: Key) {\n if (this.selectionMode === 'none') {\n return;\n }\n\n key = this.getKey(key);\n if (key == null) {\n return;\n }\n\n let selection = this.canSelectItem(key)\n ? new Selection([key], key, key)\n : new Selection();\n\n this.state.setSelectedKeys(selection);\n }\n\n /**\n * Replaces the selection with the given keys.\n */\n setSelectedKeys(keys: Iterable<Key>) {\n if (this.selectionMode === 'none') {\n return;\n }\n\n let selection = new Selection();\n for (let key of keys) {\n key = this.getKey(key);\n if (key != null) {\n selection.add(key);\n if (this.selectionMode === 'single') {\n break;\n }\n }\n }\n\n this.state.setSelectedKeys(selection);\n }\n\n private getSelectAllKeys() {\n let keys: Key[] = [];\n let addKeys = (key: Key) => {\n while (key) {\n if (this.canSelectItem(key)) {\n let item = this.collection.getItem(key);\n if (item.type === 'item') {\n keys.push(key);\n }\n\n // Add child keys. If cell selection is allowed, then include item children too.\n if (item.hasChildNodes && (this.allowsCellSelection || item.type !== 'item')) {\n addKeys(getFirstItem(getChildNodes(item, this.collection)).key);\n }\n }\n\n key = this.collection.getKeyAfter(key);\n }\n };\n\n addKeys(this.collection.getFirstKey());\n return keys;\n }\n\n /**\n * Selects all items in the collection.\n */\n selectAll() {\n if (!this.isSelectAll && this.selectionMode === 'multiple') {\n this.state.setSelectedKeys('all');\n }\n }\n\n /**\n * Removes all keys from the selection.\n */\n clearSelection() {\n if (!this.disallowEmptySelection && (this.state.selectedKeys === 'all' || this.state.selectedKeys.size > 0)) {\n this.state.setSelectedKeys(new Selection());\n }\n }\n\n /**\n * Toggles between select all and an empty selection.\n */\n toggleSelectAll() {\n if (this.isSelectAll) {\n this.clearSelection();\n } else {\n this.selectAll();\n }\n }\n\n select(key: Key, e?: PressEvent | LongPressEvent | PointerEvent) {\n if (this.selectionMode === 'none') {\n return;\n }\n\n if (this.selectionMode === 'single') {\n if (this.isSelected(key) && !this.disallowEmptySelection) {\n this.toggleSelection(key);\n } else {\n this.replaceSelection(key);\n }\n } else if (this.selectionBehavior === 'toggle' || (e && (e.pointerType === 'touch' || e.pointerType === 'virtual'))) {\n // 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\n this.toggleSelection(key);\n } else {\n this.replaceSelection(key);\n }\n }\n\n /**\n * Returns whether the current selection is equal to the given selection.\n */\n isSelectionEqual(selection: Set<Key>) {\n if (selection === this.state.selectedKeys) {\n return true;\n }\n\n // Check if the set of keys match.\n let selectedKeys = this.selectedKeys;\n if (selection.size !== selectedKeys.size) {\n return false;\n }\n\n for (let key of selection) {\n if (!selectedKeys.has(key)) {\n return false;\n }\n }\n\n for (let key of selectedKeys) {\n if (!selection.has(key)) {\n return false;\n }\n }\n\n return true;\n }\n\n canSelectItem(key: Key) {\n if (this.state.selectionMode === 'none' || this.state.disabledKeys.has(key)) {\n return false;\n }\n\n let item = this.collection.getItem(key);\n if (!item || item?.props?.isDisabled || (item.type === 'cell' && !this.allowsCellSelection)) {\n return false;\n }\n\n return true;\n }\n\n isDisabled(key: Key) {\n return this.state.disabledBehavior === 'all' && (this.state.disabledKeys.has(key) || !!this.collection.getItem(key)?.props?.isDisabled);\n }\n\n isLink(key: Key) {\n return !!this.collection.getItem(key)?.props?.href;\n }\n\n getItemProps(key: Key) {\n return this.collection.getItem(key)?.props;\n }\n}\n"],"names":[],"version":3,"file":"SelectionManager.main.js.map"}
|