@react-stately/tree 3.1.2 → 3.2.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/main.js +122 -150
- package/dist/main.js.map +1 -1
- package/dist/module.js +108 -136
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/TreeCollection.ts +5 -0
- package/src/useTreeState.ts +3 -3
package/dist/main.js
CHANGED
|
@@ -1,166 +1,138 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
SelectionManager,
|
|
11
|
-
useMultipleSelectionState
|
|
12
|
-
} = require("@react-stately/selection");
|
|
13
|
-
|
|
14
|
-
var {
|
|
15
|
-
useEffect,
|
|
16
|
-
useMemo
|
|
17
|
-
} = require("react");
|
|
18
|
-
|
|
19
|
-
let $f4c7caecb598119f63e2918a55ec91a9$var$_Symbol$iterator;
|
|
20
|
-
$f4c7caecb598119f63e2918a55ec91a9$var$_Symbol$iterator = Symbol.iterator;
|
|
21
|
-
|
|
22
|
-
/*
|
|
23
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
24
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
25
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
26
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
27
|
-
*
|
|
28
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
29
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
30
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
31
|
-
* governing permissions and limitations under the License.
|
|
32
|
-
*/
|
|
33
|
-
class $f4c7caecb598119f63e2918a55ec91a9$export$TreeCollection {
|
|
34
|
-
constructor(nodes, _temp) {
|
|
35
|
-
var _last;
|
|
36
|
-
|
|
37
|
-
let {
|
|
38
|
-
expandedKeys
|
|
39
|
-
} = _temp === void 0 ? {} : _temp;
|
|
40
|
-
this.keyMap = new Map();
|
|
41
|
-
this.iterable = void 0;
|
|
42
|
-
this.firstKey = void 0;
|
|
43
|
-
this.lastKey = void 0;
|
|
44
|
-
this.iterable = nodes;
|
|
45
|
-
expandedKeys = expandedKeys || new Set();
|
|
46
|
-
|
|
47
|
-
let visit = node => {
|
|
48
|
-
this.keyMap.set(node.key, node);
|
|
49
|
-
|
|
50
|
-
if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) {
|
|
51
|
-
for (let child of node.childNodes) {
|
|
52
|
-
visit(child);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
for (let node of nodes) {
|
|
58
|
-
visit(node);
|
|
1
|
+
var $g0QeW$react = require("react");
|
|
2
|
+
var $g0QeW$reactstatelyselection = require("@react-stately/selection");
|
|
3
|
+
var $g0QeW$reactstatelycollections = require("@react-stately/collections");
|
|
4
|
+
var $g0QeW$reactstatelyutils = require("@react-stately/utils");
|
|
5
|
+
|
|
6
|
+
function $parcel$exportWildcard(dest, source) {
|
|
7
|
+
Object.keys(source).forEach(function(key) {
|
|
8
|
+
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
9
|
+
return;
|
|
59
10
|
}
|
|
60
11
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (last) {
|
|
66
|
-
last.nextKey = key;
|
|
67
|
-
node.prevKey = last.key;
|
|
68
|
-
} else {
|
|
69
|
-
this.firstKey = key;
|
|
70
|
-
node.prevKey = undefined;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (node.type === 'item') {
|
|
74
|
-
node.index = index++;
|
|
12
|
+
Object.defineProperty(dest, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return source[key];
|
|
75
16
|
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
76
19
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
20
|
+
return dest;
|
|
21
|
+
}
|
|
22
|
+
function $parcel$export(e, n, v, s) {
|
|
23
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
24
|
+
}
|
|
25
|
+
var $89057d8278b2b959$exports = {};
|
|
26
|
+
|
|
27
|
+
$parcel$export($89057d8278b2b959$exports, "useTreeState", () => $89057d8278b2b959$export$728d6ba534403756);
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class $ed9110bedb94f15e$export$863faf230ee2118a {
|
|
31
|
+
constructor(nodes, { expandedKeys: expandedKeys } = {
|
|
32
|
+
}){
|
|
33
|
+
this.keyMap = new Map();
|
|
34
|
+
this.iterable = nodes;
|
|
35
|
+
expandedKeys = expandedKeys || new Set();
|
|
36
|
+
let visit = (node)=>{
|
|
37
|
+
this.keyMap.set(node.key, node);
|
|
38
|
+
if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) for (let child of node.childNodes)visit(child);
|
|
39
|
+
};
|
|
40
|
+
for (let node2 of nodes)visit(node2);
|
|
41
|
+
let last;
|
|
42
|
+
let index = 0;
|
|
43
|
+
for (let [key, node1] of this.keyMap){
|
|
44
|
+
if (last) {
|
|
45
|
+
last.nextKey = key;
|
|
46
|
+
node1.prevKey = last.key;
|
|
47
|
+
} else {
|
|
48
|
+
this.firstKey = key;
|
|
49
|
+
node1.prevKey = undefined;
|
|
50
|
+
}
|
|
51
|
+
if (node1.type === 'item') node1.index = index++;
|
|
52
|
+
last = node1;
|
|
53
|
+
// Set nextKey as undefined since this might be the last node
|
|
54
|
+
// If it isn't the last node, last.nextKey will properly set at start of new loop
|
|
55
|
+
last.nextKey = undefined;
|
|
56
|
+
}
|
|
57
|
+
this.lastKey = last?.key;
|
|
58
|
+
}
|
|
59
|
+
*[Symbol.iterator]() {
|
|
60
|
+
yield* this.iterable;
|
|
61
|
+
}
|
|
62
|
+
get size() {
|
|
63
|
+
return this.keyMap.size;
|
|
64
|
+
}
|
|
65
|
+
getKeys() {
|
|
66
|
+
return this.keyMap.keys();
|
|
67
|
+
}
|
|
68
|
+
getKeyBefore(key) {
|
|
69
|
+
let node = this.keyMap.get(key);
|
|
70
|
+
return node ? node.prevKey : null;
|
|
71
|
+
}
|
|
72
|
+
getKeyAfter(key) {
|
|
73
|
+
let node = this.keyMap.get(key);
|
|
74
|
+
return node ? node.nextKey : null;
|
|
75
|
+
}
|
|
76
|
+
getFirstKey() {
|
|
77
|
+
return this.firstKey;
|
|
78
|
+
}
|
|
79
|
+
getLastKey() {
|
|
80
|
+
return this.lastKey;
|
|
81
|
+
}
|
|
82
|
+
getItem(key) {
|
|
83
|
+
return this.keyMap.get(key);
|
|
84
|
+
}
|
|
85
|
+
at(idx) {
|
|
86
|
+
const keys = [
|
|
87
|
+
...this.getKeys()
|
|
88
|
+
];
|
|
89
|
+
return this.getItem(keys[idx]);
|
|
81
90
|
}
|
|
82
|
-
|
|
83
|
-
this.lastKey = (_last = last) == null ? void 0 : _last.key;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
*[$f4c7caecb598119f63e2918a55ec91a9$var$_Symbol$iterator]() {
|
|
87
|
-
yield* this.iterable;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
get size() {
|
|
91
|
-
return this.keyMap.size;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
getKeys() {
|
|
95
|
-
return this.keyMap.keys();
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
getKeyBefore(key) {
|
|
99
|
-
let node = this.keyMap.get(key);
|
|
100
|
-
return node ? node.prevKey : null;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
getKeyAfter(key) {
|
|
104
|
-
let node = this.keyMap.get(key);
|
|
105
|
-
return node ? node.nextKey : null;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
getFirstKey() {
|
|
109
|
-
return this.firstKey;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
getLastKey() {
|
|
113
|
-
return this.lastKey;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
getItem(key) {
|
|
117
|
-
return this.keyMap.get(key);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
91
|
}
|
|
121
92
|
|
|
122
|
-
/**
|
|
123
|
-
* Provides state management for tree-like components. Handles building a collection
|
|
124
|
-
* of items from props, item expanded state, and manages multiple selection state.
|
|
125
|
-
*/
|
|
126
|
-
function useTreeState(props) {
|
|
127
|
-
let [expandedKeys, setExpandedKeys] = useControlledState(props.expandedKeys ? new Set(props.expandedKeys) : undefined, props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), props.onExpandedChange);
|
|
128
|
-
let selectionState = useMultipleSelectionState(props);
|
|
129
|
-
let disabledKeys = useMemo(() => props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [props.disabledKeys]);
|
|
130
|
-
let tree = useCollection(props, nodes => new $f4c7caecb598119f63e2918a55ec91a9$export$TreeCollection(nodes, {
|
|
131
|
-
expandedKeys
|
|
132
|
-
})); // Reset focused key if that item is deleted from the collection.
|
|
133
93
|
|
|
134
|
-
useEffect(() => {
|
|
135
|
-
if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) {
|
|
136
|
-
selectionState.setFocusedKey(null);
|
|
137
|
-
}
|
|
138
|
-
}, [tree, selectionState.focusedKey]);
|
|
139
94
|
|
|
140
|
-
let onToggle = key => {
|
|
141
|
-
setExpandedKeys(expandedKeys => $f01b8043d944d5f04fffbeb1df9fd6cb$var$toggleKey(expandedKeys, key));
|
|
142
|
-
};
|
|
143
95
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
disabledKeys
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
96
|
+
function $89057d8278b2b959$export$728d6ba534403756(props) {
|
|
97
|
+
let [expandedKeys, setExpandedKeys] = $g0QeW$reactstatelyutils.useControlledState(props.expandedKeys ? new Set(props.expandedKeys) : undefined, props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), props.onExpandedChange);
|
|
98
|
+
let selectionState = $g0QeW$reactstatelyselection.useMultipleSelectionState(props);
|
|
99
|
+
let disabledKeys = $g0QeW$react.useMemo(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set()
|
|
100
|
+
, [
|
|
101
|
+
props.disabledKeys
|
|
102
|
+
]);
|
|
103
|
+
let tree = $g0QeW$reactstatelycollections.useCollection(props, (nodes)=>new $ed9110bedb94f15e$export$863faf230ee2118a(nodes, {
|
|
104
|
+
expandedKeys: expandedKeys
|
|
105
|
+
})
|
|
106
|
+
, null, [
|
|
107
|
+
expandedKeys
|
|
108
|
+
]);
|
|
109
|
+
// Reset focused key if that item is deleted from the collection.
|
|
110
|
+
$g0QeW$react.useEffect(()=>{
|
|
111
|
+
if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) selectionState.setFocusedKey(null);
|
|
112
|
+
}, [
|
|
113
|
+
tree,
|
|
114
|
+
selectionState.focusedKey
|
|
115
|
+
]);
|
|
116
|
+
let onToggle = (key)=>{
|
|
117
|
+
setExpandedKeys($89057d8278b2b959$var$toggleKey(expandedKeys, key));
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
collection: tree,
|
|
121
|
+
expandedKeys: expandedKeys,
|
|
122
|
+
disabledKeys: disabledKeys,
|
|
123
|
+
toggleKey: onToggle,
|
|
124
|
+
selectionManager: new $g0QeW$reactstatelyselection.SelectionManager(tree, selectionState)
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function $89057d8278b2b959$var$toggleKey(set, key) {
|
|
128
|
+
let res = new Set(set);
|
|
129
|
+
if (res.has(key)) res.delete(key);
|
|
130
|
+
else res.add(key);
|
|
131
|
+
return res;
|
|
151
132
|
}
|
|
152
133
|
|
|
153
|
-
exports.useTreeState = useTreeState;
|
|
154
134
|
|
|
155
|
-
|
|
156
|
-
let res = new Set(set);
|
|
135
|
+
$parcel$exportWildcard(module.exports, $89057d8278b2b959$exports);
|
|
157
136
|
|
|
158
|
-
if (res.has(key)) {
|
|
159
|
-
res.delete(key);
|
|
160
|
-
} else {
|
|
161
|
-
res.add(key);
|
|
162
|
-
}
|
|
163
137
|
|
|
164
|
-
return res;
|
|
165
|
-
}
|
|
166
138
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;;yDAgEIA,MAAM,CAACC,Q;;AAhEX;;;;;;;;;;;AAeO,MAAMC,uDAAN,CAAuD;AAM5DC,EAAAA,WAAW,CAACC,KAAD,SAA2E;AAAA;;AAAA,QAAhD;AAACC,MAAAA;AAAD,KAAgD,sBAAJ,EAAI;AAAA,SAL9EC,MAK8E,GALlD,IAAIC,GAAJ,EAKkD;AAAA,SAJ9EC,QAI8E;AAAA,SAH9EC,QAG8E;AAAA,SAF9EC,OAE8E;AACpF,SAAKF,QAAL,GAAgBJ,KAAhB;AACAC,IAAAA,YAAY,GAAGA,YAAY,IAAI,IAAIM,GAAJ,EAA/B;;AAEA,QAAIC,KAAK,GAAIC,IAAD,IAAmB;AAC7B,WAAKP,MAAL,CAAYQ,GAAZ,CAAgBD,IAAI,CAACE,GAArB,EAA0BF,IAA1B;;AAEA,UAAIA,IAAI,CAACG,UAAL,KAAoBH,IAAI,CAACI,IAAL,KAAc,SAAd,IAA2BZ,YAAY,CAACa,GAAb,CAAiBL,IAAI,CAACE,GAAtB,CAA/C,CAAJ,EAAgF;AAC9E,aAAK,IAAII,KAAT,IAAkBN,IAAI,CAACG,UAAvB,EAAmC;AACjCJ,UAAAA,KAAK,CAACO,KAAD,CAAL;AACD;AACF;AACF,KARD;;AAUA,SAAK,IAAIN,IAAT,IAAiBT,KAAjB,EAAwB;AACtBQ,MAAAA,KAAK,CAACC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,CAAT;;AAMA,MAAIE,QAAQ,GAAIlC,GAAD,IAAc;AAC3BqB,IAAAA,eAAe,CAAC/B,YAAY,IAAI6C,+CAAS,CAAC7C,YAAD,EAAeU,GAAf,CAA1B,CAAf;AACD,GAFD;;AAIA,SAAO;AACLoC,IAAAA,UAAU,EAAEP,IADP;AAELvC,IAAAA,YAFK;AAGLqC,IAAAA,YAHK;AAILQ,IAAAA,SAAS,EAAED,QAJN;AAKLG,IAAAA,gBAAgB,EAAE,IAAIC,gBAAJ,CAAqBT,IAArB,EAA2BJ,cAA3B;AALb,GAAP;AAOD;;;;AAED,SAASU,+CAAT,CAAmBpC,GAAnB,EAAkCC,GAAlC,EAAsD;AACpD,MAAIuC,GAAG,GAAG,IAAI3C,GAAJ,CAAQG,GAAR,CAAV;;AACA,MAAIwC,GAAG,CAACpC,GAAJ,CAAQH,GAAR,CAAJ,EAAkB;AAChBuC,IAAAA,GAAG,CAACC,MAAJ,CAAWxC,GAAX;AACD,GAFD,MAEO;AACLuC,IAAAA,GAAG,CAACE,GAAJ,CAAQzC,GAAR;AACD;;AAED,SAAOuC,GAAP;AACD","sources":["./packages/@react-stately/tree/src/TreeCollection.ts","./packages/@react-stately/tree/src/useTreeState.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 {Collection, Node} from '@react-types/shared';\nimport {Key} from 'react';\n\nexport class TreeCollection<T> implements Collection<Node<T>> {\n private keyMap: Map<Key, Node<T>> = new Map();\n private iterable: Iterable<Node<T>>;\n private firstKey: Key;\n private lastKey: Key;\n\n constructor(nodes: Iterable<Node<T>>, {expandedKeys}: {expandedKeys?: Set<Key>} = {}) {\n this.iterable = nodes;\n expandedKeys = expandedKeys || new Set();\n\n let visit = (node: Node<T>) => {\n this.keyMap.set(node.key, node);\n\n if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) {\n for (let child of node.childNodes) {\n visit(child);\n }\n }\n };\n\n for (let node of nodes) {\n visit(node);\n }\n\n let last: Node<T>;\n let index = 0;\n for (let [key, node] of this.keyMap) {\n if (last) {\n last.nextKey = key;\n node.prevKey = last.key;\n } else {\n this.firstKey = key;\n node.prevKey = undefined;\n }\n\n if (node.type === 'item') {\n node.index = index++;\n }\n\n last = node;\n\n // Set nextKey as undefined since this might be the last node\n // If it isn't the last node, last.nextKey will properly set at start of new loop\n last.nextKey = undefined;\n }\n\n this.lastKey = last?.key;\n }\n\n *[Symbol.iterator]() {\n yield* this.iterable;\n }\n\n get size() {\n return this.keyMap.size;\n }\n\n getKeys() {\n return this.keyMap.keys();\n }\n\n getKeyBefore(key: Key) {\n let node = this.keyMap.get(key);\n return node ? node.prevKey : null;\n }\n\n getKeyAfter(key: Key) {\n let node = this.keyMap.get(key);\n return node ? node.nextKey : null;\n }\n\n getFirstKey() {\n return this.firstKey;\n }\n\n getLastKey() {\n return this.lastKey;\n }\n\n getItem(key: Key) {\n return this.keyMap.get(key);\n }\n}\n","/*\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 {Collection, CollectionBase, Expandable, MultipleSelection, Node} from '@react-types/shared';\nimport {Key, useEffect, useMemo} from 'react';\nimport {SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {TreeCollection} from './TreeCollection';\nimport {useCollection} from '@react-stately/collections';\nimport {useControlledState} from '@react-stately/utils';\n\ninterface TreeProps<T> extends CollectionBase<T>, Expandable, MultipleSelection {}\nexport interface TreeState<T> {\n /** A collection of items in the tree. */\n readonly collection: Collection<Node<T>>,\n\n /** A set of keys for items that are disabled. */\n readonly disabledKeys: Set<Key>,\n\n /** A set of keys for items that are expanded. */\n readonly expandedKeys: Set<Key>,\n\n /** Toggles the expanded state for an item by its key. */\n toggleKey(key: Key): void,\n\n /** A selection manager to read and update multiple selection state. */\n readonly selectionManager: SelectionManager\n}\n\n/**\n * Provides state management for tree-like components. Handles building a collection\n * of items from props, item expanded state, and manages multiple selection state.\n */\nexport function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T> {\n let [expandedKeys, setExpandedKeys] = useControlledState(\n props.expandedKeys ? new Set(props.expandedKeys) : undefined,\n props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(),\n props.onExpandedChange\n );\n\n let selectionState = useMultipleSelectionState(props);\n let disabledKeys = useMemo(() =>\n props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n , [props.disabledKeys]);\n\n let tree = useCollection(props, nodes => new TreeCollection(nodes, {expandedKeys}));\n\n // Reset focused key if that item is deleted from the collection.\n useEffect(() => {\n if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) {\n selectionState.setFocusedKey(null);\n }\n }, [tree, selectionState.focusedKey]);\n\n let onToggle = (key: Key) => {\n setExpandedKeys(expandedKeys => toggleKey(expandedKeys, key));\n };\n\n return {\n collection: tree,\n expandedKeys,\n disabledKeys,\n toggleKey: onToggle,\n selectionManager: new SelectionManager(tree, selectionState)\n };\n}\n\nfunction toggleKey(set: Set<Key>, key: Key): Set<Key> {\n let res = new Set(set);\n if (res.has(key)) {\n res.delete(key);\n } else {\n res.add(key);\n }\n\n return res;\n}\n"],"names":["Symbol","iterator","TreeCollection","constructor","nodes","expandedKeys","keyMap","Map","iterable","firstKey","lastKey","Set","visit","node","set","key","childNodes","type","has","child","last","index","nextKey","prevKey","undefined","size","getKeys","keys","getKeyBefore","get","getKeyAfter","getFirstKey","getLastKey","getItem","useTreeState","props","setExpandedKeys","useControlledState","defaultExpandedKeys","onExpandedChange","selectionState","useMultipleSelectionState","disabledKeys","useMemo","tree","useCollection","useEffect","focusedKey","setFocusedKey","onToggle","toggleKey","collection","selectionManager","SelectionManager","res","delete","add"],"version":3,"file":"main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;MEea,yCAAc;gBAMb,KAAwB,EAAE,CAAC,eAAA,YAAY,EAA2B,CAAC,GAAG,CAAC;IAAA,CAAC,CAAE,CAAC;QANlF,IAuFN,CAtFS,MAAM,GAAsB,GAAG,CAAC,GAAG;QAMzC,IAAI,CAAC,QAAQ,GAAG,KAAK;QACrB,YAAY,GAAG,YAAY,IAAI,GAAG,CAAC,GAAG;QAEtC,GAAG,CAAC,KAAK,IAAI,IAAa,GAAK,CAAC;YAC9B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI;YAE9B,EAAE,EAAE,IAAI,CAAC,UAAU,KAAK,IAAI,CAAC,IAAI,KAAK,CAAS,YAAI,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,IAC1E,GAAG,EAAE,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,UAAU,CAC/B,KAAK,CAAC,KAAK;QAGjB,CAAC;QAED,GAAG,EAAE,GAAG,CAAC,KAAI,IAAI,KAAK,CACpB,KAAK,CAAC,KAAI;QAGZ,GAAG,CAAC,IAAI;QACR,GAAG,CAAC,KAAK,GAAG,CAAC;QACb,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,KAAI,KAAK,IAAI,CAAC,MAAM,CAAE,CAAC;YACpC,EAAE,EAAE,IAAI,EAAE,CAAC;gBACT,IAAI,CAAC,OAAO,GAAG,GAAG;gBAClB,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G,CAAC,QAAQ,IAAI,GAAQ,GAAK,CAAC;QAC5B,eAAe,CAAC,+BAAS,CAAC,YAAY,EAAE,GAAG;IAC7C,CAAC;IAED,MAAM,CAAC,CAAC;QACN,UAAU,EAAE,IAAI;sBAChB,YAAY;sBACZ,YAAY;QACZ,SAAS,EAAE,QAAQ;QACnB,gBAAgB,EAAE,GAAG,CAAC,6CAAgB,CAAC,IAAI,EAAE,cAAc;IAC7D,CAAC;AACH,CAAC;SAEQ,+BAAS,CAAC,GAAa,EAAE,GAAQ,EAAY,CAAC;IACrD,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG;IACrB,EAAE,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,GACb,GAAG,CAAC,MAAM,CAAC,GAAG;SAEd,GAAG,CAAC,GAAG,CAAC,GAAG;IAGb,MAAM,CAAC,GAAG;AACZ,CAAC","sources":["packages/@react-stately/tree/src/index.ts","packages/@react-stately/tree/src/useTreeState.ts","packages/@react-stately/tree/src/TreeCollection.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\nexport * from './useTreeState';\n","/*\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 {Collection, CollectionBase, Expandable, MultipleSelection, Node} from '@react-types/shared';\nimport {Key, useEffect, useMemo} from 'react';\nimport {SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {TreeCollection} from './TreeCollection';\nimport {useCollection} from '@react-stately/collections';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface TreeProps<T> extends CollectionBase<T>, Expandable, MultipleSelection {}\nexport interface TreeState<T> {\n /** A collection of items in the tree. */\n readonly collection: Collection<Node<T>>,\n\n /** A set of keys for items that are disabled. */\n readonly disabledKeys: Set<Key>,\n\n /** A set of keys for items that are expanded. */\n readonly expandedKeys: Set<Key>,\n\n /** Toggles the expanded state for an item by its key. */\n toggleKey(key: Key): void,\n\n /** A selection manager to read and update multiple selection state. */\n readonly selectionManager: SelectionManager\n}\n\n/**\n * Provides state management for tree-like components. Handles building a collection\n * of items from props, item expanded state, and manages multiple selection state.\n */\nexport function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T> {\n let [expandedKeys, setExpandedKeys] = useControlledState(\n props.expandedKeys ? new Set(props.expandedKeys) : undefined,\n props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(),\n props.onExpandedChange\n );\n\n let selectionState = useMultipleSelectionState(props);\n let disabledKeys = useMemo(() =>\n props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n , [props.disabledKeys]);\n\n let tree = useCollection(props, nodes => new TreeCollection(nodes, {expandedKeys}), null, [expandedKeys]);\n\n // Reset focused key if that item is deleted from the collection.\n useEffect(() => {\n if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) {\n selectionState.setFocusedKey(null);\n }\n }, [tree, selectionState.focusedKey]);\n\n let onToggle = (key: Key) => {\n setExpandedKeys(toggleKey(expandedKeys, key));\n };\n\n return {\n collection: tree,\n expandedKeys,\n disabledKeys,\n toggleKey: onToggle,\n selectionManager: new SelectionManager(tree, selectionState)\n };\n}\n\nfunction toggleKey(set: Set<Key>, key: Key): Set<Key> {\n let res = new Set(set);\n if (res.has(key)) {\n res.delete(key);\n } else {\n res.add(key);\n }\n\n return res;\n}\n","/*\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 {Collection, Node} from '@react-types/shared';\nimport {Key} from 'react';\n\nexport class TreeCollection<T> implements Collection<Node<T>> {\n private keyMap: Map<Key, Node<T>> = new Map();\n private iterable: Iterable<Node<T>>;\n private firstKey: Key;\n private lastKey: Key;\n\n constructor(nodes: Iterable<Node<T>>, {expandedKeys}: {expandedKeys?: Set<Key>} = {}) {\n this.iterable = nodes;\n expandedKeys = expandedKeys || new Set();\n\n let visit = (node: Node<T>) => {\n this.keyMap.set(node.key, node);\n\n if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) {\n for (let child of node.childNodes) {\n visit(child);\n }\n }\n };\n\n for (let node of nodes) {\n visit(node);\n }\n\n let last: Node<T>;\n let index = 0;\n for (let [key, node] of this.keyMap) {\n if (last) {\n last.nextKey = key;\n node.prevKey = last.key;\n } else {\n this.firstKey = key;\n node.prevKey = undefined;\n }\n\n if (node.type === 'item') {\n node.index = index++;\n }\n\n last = node;\n\n // Set nextKey as undefined since this might be the last node\n // If it isn't the last node, last.nextKey will properly set at start of new loop\n last.nextKey = undefined;\n }\n\n this.lastKey = last?.key;\n }\n\n *[Symbol.iterator]() {\n yield* this.iterable;\n }\n\n get size() {\n return this.keyMap.size;\n }\n\n getKeys() {\n return this.keyMap.keys();\n }\n\n getKeyBefore(key: Key) {\n let node = this.keyMap.get(key);\n return node ? node.prevKey : null;\n }\n\n getKeyAfter(key: Key) {\n let node = this.keyMap.get(key);\n return node ? node.nextKey : null;\n }\n\n getFirstKey() {\n return this.firstKey;\n }\n\n getLastKey() {\n return this.lastKey;\n }\n\n getItem(key: Key) {\n return this.keyMap.get(key);\n }\n\n at(idx: number) {\n const keys = [...this.getKeys()];\n return this.getItem(keys[idx]);\n }\n}\n"],"names":[],"version":3,"file":"main.js.map"}
|
package/dist/module.js
CHANGED
|
@@ -1,150 +1,122 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
let $afa6f708e32ecf7f97d9a58dfd59c$var$_Symbol$iterator;
|
|
6
|
-
$afa6f708e32ecf7f97d9a58dfd59c$var$_Symbol$iterator = Symbol.iterator;
|
|
1
|
+
import {useMemo as $l43NX$useMemo, useEffect as $l43NX$useEffect} from "react";
|
|
2
|
+
import {useMultipleSelectionState as $l43NX$useMultipleSelectionState, SelectionManager as $l43NX$SelectionManager} from "@react-stately/selection";
|
|
3
|
+
import {useCollection as $l43NX$useCollection} from "@react-stately/collections";
|
|
4
|
+
import {useControlledState as $l43NX$useControlledState} from "@react-stately/utils";
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
6
|
+
function $parcel$export(e, n, v, s) {
|
|
7
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
8
|
+
}
|
|
9
|
+
var $443b2488e6fa10fb$exports = {};
|
|
10
|
+
|
|
11
|
+
$parcel$export($443b2488e6fa10fb$exports, "useTreeState", () => $443b2488e6fa10fb$export$728d6ba534403756);
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class $62dcb9c0c42c454a$export$863faf230ee2118a {
|
|
15
|
+
constructor(nodes, { expandedKeys: expandedKeys } = {
|
|
16
|
+
}){
|
|
17
|
+
this.keyMap = new Map();
|
|
18
|
+
this.iterable = nodes;
|
|
19
|
+
expandedKeys = expandedKeys || new Set();
|
|
20
|
+
let visit = (node)=>{
|
|
21
|
+
this.keyMap.set(node.key, node);
|
|
22
|
+
if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) for (let child of node.childNodes)visit(child);
|
|
23
|
+
};
|
|
24
|
+
for (let node2 of nodes)visit(node2);
|
|
25
|
+
let last;
|
|
26
|
+
let index = 0;
|
|
27
|
+
for (let [key, node1] of this.keyMap){
|
|
28
|
+
if (last) {
|
|
29
|
+
last.nextKey = key;
|
|
30
|
+
node1.prevKey = last.key;
|
|
31
|
+
} else {
|
|
32
|
+
this.firstKey = key;
|
|
33
|
+
node1.prevKey = undefined;
|
|
34
|
+
}
|
|
35
|
+
if (node1.type === 'item') node1.index = index++;
|
|
36
|
+
last = node1;
|
|
37
|
+
// Set nextKey as undefined since this might be the last node
|
|
38
|
+
// If it isn't the last node, last.nextKey will properly set at start of new loop
|
|
39
|
+
last.nextKey = undefined;
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
for (let node of nodes) {
|
|
44
|
-
visit(node);
|
|
41
|
+
this.lastKey = last?.key;
|
|
45
42
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
node
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
node
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
43
|
+
*[Symbol.iterator]() {
|
|
44
|
+
yield* this.iterable;
|
|
45
|
+
}
|
|
46
|
+
get size() {
|
|
47
|
+
return this.keyMap.size;
|
|
48
|
+
}
|
|
49
|
+
getKeys() {
|
|
50
|
+
return this.keyMap.keys();
|
|
51
|
+
}
|
|
52
|
+
getKeyBefore(key) {
|
|
53
|
+
let node = this.keyMap.get(key);
|
|
54
|
+
return node ? node.prevKey : null;
|
|
55
|
+
}
|
|
56
|
+
getKeyAfter(key) {
|
|
57
|
+
let node = this.keyMap.get(key);
|
|
58
|
+
return node ? node.nextKey : null;
|
|
59
|
+
}
|
|
60
|
+
getFirstKey() {
|
|
61
|
+
return this.firstKey;
|
|
62
|
+
}
|
|
63
|
+
getLastKey() {
|
|
64
|
+
return this.lastKey;
|
|
65
|
+
}
|
|
66
|
+
getItem(key) {
|
|
67
|
+
return this.keyMap.get(key);
|
|
68
|
+
}
|
|
69
|
+
at(idx) {
|
|
70
|
+
const keys = [
|
|
71
|
+
...this.getKeys()
|
|
72
|
+
];
|
|
73
|
+
return this.getItem(keys[idx]);
|
|
67
74
|
}
|
|
68
|
-
|
|
69
|
-
this.lastKey = (_last = last) == null ? void 0 : _last.key;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
*[$afa6f708e32ecf7f97d9a58dfd59c$var$_Symbol$iterator]() {
|
|
73
|
-
yield* this.iterable;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get size() {
|
|
77
|
-
return this.keyMap.size;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
getKeys() {
|
|
81
|
-
return this.keyMap.keys();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
getKeyBefore(key) {
|
|
85
|
-
let node = this.keyMap.get(key);
|
|
86
|
-
return node ? node.prevKey : null;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
getKeyAfter(key) {
|
|
90
|
-
let node = this.keyMap.get(key);
|
|
91
|
-
return node ? node.nextKey : null;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
getFirstKey() {
|
|
95
|
-
return this.firstKey;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
getLastKey() {
|
|
99
|
-
return this.lastKey;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
getItem(key) {
|
|
103
|
-
return this.keyMap.get(key);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
75
|
}
|
|
107
76
|
|
|
108
|
-
/**
|
|
109
|
-
* Provides state management for tree-like components. Handles building a collection
|
|
110
|
-
* of items from props, item expanded state, and manages multiple selection state.
|
|
111
|
-
*/
|
|
112
|
-
export function useTreeState(props) {
|
|
113
|
-
let [expandedKeys, setExpandedKeys] = useControlledState(props.expandedKeys ? new Set(props.expandedKeys) : undefined, props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), props.onExpandedChange);
|
|
114
|
-
let selectionState = useMultipleSelectionState(props);
|
|
115
|
-
let disabledKeys = useMemo(() => props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [props.disabledKeys]);
|
|
116
|
-
let tree = useCollection(props, nodes => new $afa6f708e32ecf7f97d9a58dfd59c$export$TreeCollection(nodes, {
|
|
117
|
-
expandedKeys
|
|
118
|
-
})); // Reset focused key if that item is deleted from the collection.
|
|
119
77
|
|
|
120
|
-
useEffect(() => {
|
|
121
|
-
if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) {
|
|
122
|
-
selectionState.setFocusedKey(null);
|
|
123
|
-
}
|
|
124
|
-
}, [tree, selectionState.focusedKey]);
|
|
125
78
|
|
|
126
|
-
let onToggle = key => {
|
|
127
|
-
setExpandedKeys(expandedKeys => $f51dc3c5c900bd3cdb4a06df11d84697$var$toggleKey(expandedKeys, key));
|
|
128
|
-
};
|
|
129
79
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
disabledKeys
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
80
|
+
function $443b2488e6fa10fb$export$728d6ba534403756(props) {
|
|
81
|
+
let [expandedKeys, setExpandedKeys] = $l43NX$useControlledState(props.expandedKeys ? new Set(props.expandedKeys) : undefined, props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), props.onExpandedChange);
|
|
82
|
+
let selectionState = $l43NX$useMultipleSelectionState(props);
|
|
83
|
+
let disabledKeys = $l43NX$useMemo(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set()
|
|
84
|
+
, [
|
|
85
|
+
props.disabledKeys
|
|
86
|
+
]);
|
|
87
|
+
let tree = $l43NX$useCollection(props, (nodes)=>new $62dcb9c0c42c454a$export$863faf230ee2118a(nodes, {
|
|
88
|
+
expandedKeys: expandedKeys
|
|
89
|
+
})
|
|
90
|
+
, null, [
|
|
91
|
+
expandedKeys
|
|
92
|
+
]);
|
|
93
|
+
// Reset focused key if that item is deleted from the collection.
|
|
94
|
+
$l43NX$useEffect(()=>{
|
|
95
|
+
if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) selectionState.setFocusedKey(null);
|
|
96
|
+
}, [
|
|
97
|
+
tree,
|
|
98
|
+
selectionState.focusedKey
|
|
99
|
+
]);
|
|
100
|
+
let onToggle = (key)=>{
|
|
101
|
+
setExpandedKeys($443b2488e6fa10fb$var$toggleKey(expandedKeys, key));
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
collection: tree,
|
|
105
|
+
expandedKeys: expandedKeys,
|
|
106
|
+
disabledKeys: disabledKeys,
|
|
107
|
+
toggleKey: onToggle,
|
|
108
|
+
selectionManager: new $l43NX$SelectionManager(tree, selectionState)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function $443b2488e6fa10fb$var$toggleKey(set, key) {
|
|
112
|
+
let res = new Set(set);
|
|
113
|
+
if (res.has(key)) res.delete(key);
|
|
114
|
+
else res.add(key);
|
|
115
|
+
return res;
|
|
137
116
|
}
|
|
138
117
|
|
|
139
|
-
function $f51dc3c5c900bd3cdb4a06df11d84697$var$toggleKey(set, key) {
|
|
140
|
-
let res = new Set(set);
|
|
141
118
|
|
|
142
|
-
if (res.has(key)) {
|
|
143
|
-
res.delete(key);
|
|
144
|
-
} else {
|
|
145
|
-
res.add(key);
|
|
146
|
-
}
|
|
147
119
|
|
|
148
|
-
|
|
149
|
-
}
|
|
120
|
+
|
|
121
|
+
export {$443b2488e6fa10fb$export$728d6ba534403756 as useTreeState};
|
|
150
122
|
//# sourceMappingURL=module.js.map
|
package/dist/module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;sDAgEIA,MAAM,CAACC,Q;;AAhEX;;;;;;;;;;;AAeO,MAAMC,oDAAN,CAAuD;AAM5DC,EAAAA,WAAW,CAACC,KAAD,SAA2E;AAAA;;AAAA,QAAhD;AAACC,MAAAA;AAAD,KAAgD,sBAAJ,EAAI;AAAA,SAL9EC,MAK8E,GALlD,IAAIC,GAAJ,EAKkD;AAAA,SAJ9EC,QAI8E;AAAA,SAH9EC,QAG8E;AAAA,SAF9EC,OAE8E;AACpF,SAAKF,QAAL,GAAgBJ,KAAhB;AACAC,IAAAA,YAAY,GAAGA,YAAY,IAAI,IAAIM,GAAJ,EAA/B;;AAEA,QAAIC,KAAK,GAAIC,IAAD,IAAmB;AAC7B,WAAKP,MAAL,CAAYQ,GAAZ,CAAgBD,IAAI,CAACE,GAArB,EAA0BF,IAA1B;;AAEA,UAAIA,IAAI,CAACG,UAAL,KAAoBH,IAAI,CAACI,IAAL,KAAc,SAAd,IAA2BZ,YAAY,CAACa,GAAb,CAAiBL,IAAI,CAACE,GAAtB,CAA/C,CAAJ,EAAgF;AAC9E,aAAK,IAAII,KAAT,IAAkBN,IAAI,CAACG,UAAvB,EAAmC;AACjCJ,UAAAA,KAAK,CAACO,KAAD,CAAL;AACD;AACF;AACF,KARD;;AAUA,SAAK,IAAIN,IAAT,IAAiBT,KAAjB,EAAwB;AACtBQ,MAAAA,KAAK,CAACC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,CAAT;;AAMA,MAAIE,QAAQ,GAAIlC,GAAD,IAAc;AAC3BqB,IAAAA,eAAe,CAAC/B,YAAY,IAAI6C,+CAAS,CAAC7C,YAAD,EAAeU,GAAf,CAA1B,CAAf;AACD,GAFD;;AAIA,SAAO;AACLoC,IAAAA,UAAU,EAAEP,IADP;AAELvC,IAAAA,YAFK;AAGLqC,IAAAA,YAHK;AAILQ,IAAAA,SAAS,EAAED,QAJN;AAKLG,IAAAA,gBAAgB,EAAE,IAAIC,gBAAJ,CAAqBT,IAArB,EAA2BJ,cAA3B;AALb,GAAP;AAOD;;AAED,SAASU,+CAAT,CAAmBpC,GAAnB,EAAkCC,GAAlC,EAAsD;AACpD,MAAIuC,GAAG,GAAG,IAAI3C,GAAJ,CAAQG,GAAR,CAAV;;AACA,MAAIwC,GAAG,CAACpC,GAAJ,CAAQH,GAAR,CAAJ,EAAkB;AAChBuC,IAAAA,GAAG,CAACC,MAAJ,CAAWxC,GAAX;AACD,GAFD,MAEO;AACLuC,IAAAA,GAAG,CAACE,GAAJ,CAAQzC,GAAR;AACD;;AAED,SAAOuC,GAAP;AACD","sources":["./packages/@react-stately/tree/src/TreeCollection.ts","./packages/@react-stately/tree/src/useTreeState.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 {Collection, Node} from '@react-types/shared';\nimport {Key} from 'react';\n\nexport class TreeCollection<T> implements Collection<Node<T>> {\n private keyMap: Map<Key, Node<T>> = new Map();\n private iterable: Iterable<Node<T>>;\n private firstKey: Key;\n private lastKey: Key;\n\n constructor(nodes: Iterable<Node<T>>, {expandedKeys}: {expandedKeys?: Set<Key>} = {}) {\n this.iterable = nodes;\n expandedKeys = expandedKeys || new Set();\n\n let visit = (node: Node<T>) => {\n this.keyMap.set(node.key, node);\n\n if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) {\n for (let child of node.childNodes) {\n visit(child);\n }\n }\n };\n\n for (let node of nodes) {\n visit(node);\n }\n\n let last: Node<T>;\n let index = 0;\n for (let [key, node] of this.keyMap) {\n if (last) {\n last.nextKey = key;\n node.prevKey = last.key;\n } else {\n this.firstKey = key;\n node.prevKey = undefined;\n }\n\n if (node.type === 'item') {\n node.index = index++;\n }\n\n last = node;\n\n // Set nextKey as undefined since this might be the last node\n // If it isn't the last node, last.nextKey will properly set at start of new loop\n last.nextKey = undefined;\n }\n\n this.lastKey = last?.key;\n }\n\n *[Symbol.iterator]() {\n yield* this.iterable;\n }\n\n get size() {\n return this.keyMap.size;\n }\n\n getKeys() {\n return this.keyMap.keys();\n }\n\n getKeyBefore(key: Key) {\n let node = this.keyMap.get(key);\n return node ? node.prevKey : null;\n }\n\n getKeyAfter(key: Key) {\n let node = this.keyMap.get(key);\n return node ? node.nextKey : null;\n }\n\n getFirstKey() {\n return this.firstKey;\n }\n\n getLastKey() {\n return this.lastKey;\n }\n\n getItem(key: Key) {\n return this.keyMap.get(key);\n }\n}\n","/*\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 {Collection, CollectionBase, Expandable, MultipleSelection, Node} from '@react-types/shared';\nimport {Key, useEffect, useMemo} from 'react';\nimport {SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {TreeCollection} from './TreeCollection';\nimport {useCollection} from '@react-stately/collections';\nimport {useControlledState} from '@react-stately/utils';\n\ninterface TreeProps<T> extends CollectionBase<T>, Expandable, MultipleSelection {}\nexport interface TreeState<T> {\n /** A collection of items in the tree. */\n readonly collection: Collection<Node<T>>,\n\n /** A set of keys for items that are disabled. */\n readonly disabledKeys: Set<Key>,\n\n /** A set of keys for items that are expanded. */\n readonly expandedKeys: Set<Key>,\n\n /** Toggles the expanded state for an item by its key. */\n toggleKey(key: Key): void,\n\n /** A selection manager to read and update multiple selection state. */\n readonly selectionManager: SelectionManager\n}\n\n/**\n * Provides state management for tree-like components. Handles building a collection\n * of items from props, item expanded state, and manages multiple selection state.\n */\nexport function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T> {\n let [expandedKeys, setExpandedKeys] = useControlledState(\n props.expandedKeys ? new Set(props.expandedKeys) : undefined,\n props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(),\n props.onExpandedChange\n );\n\n let selectionState = useMultipleSelectionState(props);\n let disabledKeys = useMemo(() =>\n props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n , [props.disabledKeys]);\n\n let tree = useCollection(props, nodes => new TreeCollection(nodes, {expandedKeys}));\n\n // Reset focused key if that item is deleted from the collection.\n useEffect(() => {\n if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) {\n selectionState.setFocusedKey(null);\n }\n }, [tree, selectionState.focusedKey]);\n\n let onToggle = (key: Key) => {\n setExpandedKeys(expandedKeys => toggleKey(expandedKeys, key));\n };\n\n return {\n collection: tree,\n expandedKeys,\n disabledKeys,\n toggleKey: onToggle,\n selectionManager: new SelectionManager(tree, selectionState)\n };\n}\n\nfunction toggleKey(set: Set<Key>, key: Key): Set<Key> {\n let res = new Set(set);\n if (res.has(key)) {\n res.delete(key);\n } else {\n res.add(key);\n }\n\n return res;\n}\n"],"names":["Symbol","iterator","TreeCollection","constructor","nodes","expandedKeys","keyMap","Map","iterable","firstKey","lastKey","Set","visit","node","set","key","childNodes","type","has","child","last","index","nextKey","prevKey","undefined","size","getKeys","keys","getKeyBefore","get","getKeyAfter","getFirstKey","getLastKey","getItem","useTreeState","props","setExpandedKeys","useControlledState","defaultExpandedKeys","onExpandedChange","selectionState","useMultipleSelectionState","disabledKeys","useMemo","tree","useCollection","useEffect","focusedKey","setFocusedKey","onToggle","toggleKey","collection","selectionManager","SelectionManager","res","delete","add"],"version":3,"file":"module.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;MEea,yCAAc;gBAMb,KAAwB,EAAE,CAAC,eAAA,YAAY,EAA2B,CAAC,GAAG,CAAC;IAAA,CAAC,CAAE,CAAC;QANlF,IAuFN,CAtFS,MAAM,GAAsB,GAAG,CAAC,GAAG;QAMzC,IAAI,CAAC,QAAQ,GAAG,KAAK;QACrB,YAAY,GAAG,YAAY,IAAI,GAAG,CAAC,GAAG;QAEtC,GAAG,CAAC,KAAK,IAAI,IAAa,GAAK,CAAC;YAC9B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI;YAE9B,EAAE,EAAE,IAAI,CAAC,UAAU,KAAK,IAAI,CAAC,IAAI,KAAK,CAAS,YAAI,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,IAC1E,GAAG,EAAE,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,UAAU,CAC/B,KAAK,CAAC,KAAK;QAGjB,CAAC;QAED,GAAG,EAAE,GAAG,CAAC,KAAI,IAAI,KAAK,CACpB,KAAK,CAAC,KAAI;QAGZ,GAAG,CAAC,IAAI;QACR,GAAG,CAAC,KAAK,GAAG,CAAC;QACb,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,KAAI,KAAK,IAAI,CAAC,MAAM,CAAE,CAAC;YACpC,EAAE,EAAE,IAAI,EAAE,CAAC;gBACT,IAAI,CAAC,OAAO,GAAG,GAAG;gBAClB,KAAI,CAAC,OAAO,GAA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AAQ,GAAK,CAAC;QAC5B,eAAe,CAAC,+BAAS,CAAC,YAAY,EAAE,GAAG;IAC7C,CAAC;IAED,MAAM,CAAC,CAAC;QACN,UAAU,EAAE,IAAI;sBAChB,YAAY;sBACZ,YAAY;QACZ,SAAS,EAAE,QAAQ;QACnB,gBAAgB,EAAE,GAAG,CAAC,uBAAgB,CAAC,IAAI,EAAE,cAAc;IAC7D,CAAC;AACH,CAAC;SAEQ,+BAAS,CAAC,GAAa,EAAE,GAAQ,EAAY,CAAC;IACrD,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG;IACrB,EAAE,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,GACb,GAAG,CAAC,MAAM,CAAC,GAAG;SAEd,GAAG,CAAC,GAAG,CAAC,GAAG;IAGb,MAAM,CAAC,GAAG;AACZ,CAAC","sources":["packages/@react-stately/tree/src/index.ts","packages/@react-stately/tree/src/useTreeState.ts","packages/@react-stately/tree/src/TreeCollection.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\nexport * from './useTreeState';\n","/*\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 {Collection, CollectionBase, Expandable, MultipleSelection, Node} from '@react-types/shared';\nimport {Key, useEffect, useMemo} from 'react';\nimport {SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {TreeCollection} from './TreeCollection';\nimport {useCollection} from '@react-stately/collections';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface TreeProps<T> extends CollectionBase<T>, Expandable, MultipleSelection {}\nexport interface TreeState<T> {\n /** A collection of items in the tree. */\n readonly collection: Collection<Node<T>>,\n\n /** A set of keys for items that are disabled. */\n readonly disabledKeys: Set<Key>,\n\n /** A set of keys for items that are expanded. */\n readonly expandedKeys: Set<Key>,\n\n /** Toggles the expanded state for an item by its key. */\n toggleKey(key: Key): void,\n\n /** A selection manager to read and update multiple selection state. */\n readonly selectionManager: SelectionManager\n}\n\n/**\n * Provides state management for tree-like components. Handles building a collection\n * of items from props, item expanded state, and manages multiple selection state.\n */\nexport function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T> {\n let [expandedKeys, setExpandedKeys] = useControlledState(\n props.expandedKeys ? new Set(props.expandedKeys) : undefined,\n props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(),\n props.onExpandedChange\n );\n\n let selectionState = useMultipleSelectionState(props);\n let disabledKeys = useMemo(() =>\n props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n , [props.disabledKeys]);\n\n let tree = useCollection(props, nodes => new TreeCollection(nodes, {expandedKeys}), null, [expandedKeys]);\n\n // Reset focused key if that item is deleted from the collection.\n useEffect(() => {\n if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) {\n selectionState.setFocusedKey(null);\n }\n }, [tree, selectionState.focusedKey]);\n\n let onToggle = (key: Key) => {\n setExpandedKeys(toggleKey(expandedKeys, key));\n };\n\n return {\n collection: tree,\n expandedKeys,\n disabledKeys,\n toggleKey: onToggle,\n selectionManager: new SelectionManager(tree, selectionState)\n };\n}\n\nfunction toggleKey(set: Set<Key>, key: Key): Set<Key> {\n let res = new Set(set);\n if (res.has(key)) {\n res.delete(key);\n } else {\n res.add(key);\n }\n\n return res;\n}\n","/*\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 {Collection, Node} from '@react-types/shared';\nimport {Key} from 'react';\n\nexport class TreeCollection<T> implements Collection<Node<T>> {\n private keyMap: Map<Key, Node<T>> = new Map();\n private iterable: Iterable<Node<T>>;\n private firstKey: Key;\n private lastKey: Key;\n\n constructor(nodes: Iterable<Node<T>>, {expandedKeys}: {expandedKeys?: Set<Key>} = {}) {\n this.iterable = nodes;\n expandedKeys = expandedKeys || new Set();\n\n let visit = (node: Node<T>) => {\n this.keyMap.set(node.key, node);\n\n if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) {\n for (let child of node.childNodes) {\n visit(child);\n }\n }\n };\n\n for (let node of nodes) {\n visit(node);\n }\n\n let last: Node<T>;\n let index = 0;\n for (let [key, node] of this.keyMap) {\n if (last) {\n last.nextKey = key;\n node.prevKey = last.key;\n } else {\n this.firstKey = key;\n node.prevKey = undefined;\n }\n\n if (node.type === 'item') {\n node.index = index++;\n }\n\n last = node;\n\n // Set nextKey as undefined since this might be the last node\n // If it isn't the last node, last.nextKey will properly set at start of new loop\n last.nextKey = undefined;\n }\n\n this.lastKey = last?.key;\n }\n\n *[Symbol.iterator]() {\n yield* this.iterable;\n }\n\n get size() {\n return this.keyMap.size;\n }\n\n getKeys() {\n return this.keyMap.keys();\n }\n\n getKeyBefore(key: Key) {\n let node = this.keyMap.get(key);\n return node ? node.prevKey : null;\n }\n\n getKeyAfter(key: Key) {\n let node = this.keyMap.get(key);\n return node ? node.nextKey : null;\n }\n\n getFirstKey() {\n return this.firstKey;\n }\n\n getLastKey() {\n return this.lastKey;\n }\n\n getItem(key: Key) {\n return this.keyMap.get(key);\n }\n\n at(idx: number) {\n const keys = [...this.getKeys()];\n return this.getItem(keys[idx]);\n }\n}\n"],"names":[],"version":3,"file":"module.js.map"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Collection, CollectionBase, Expandable, MultipleSelection, Node } from "@react-types/shared";
|
|
2
2
|
import { Key } from "react";
|
|
3
3
|
import { SelectionManager } from "@react-stately/selection";
|
|
4
|
-
interface TreeProps<T> extends CollectionBase<T>, Expandable, MultipleSelection {
|
|
4
|
+
export interface TreeProps<T> extends CollectionBase<T>, Expandable, MultipleSelection {
|
|
5
5
|
}
|
|
6
6
|
export interface TreeState<T> {
|
|
7
7
|
/** A collection of items in the tree. */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;ACmBA,2BAA2B,CAAC,CAAE,SAAQ,eAAe,CAAC,CAAC,EAAE,UAAU,EAAE,iBAAiB;CAAG;AACzF,2BAA2B,CAAC;IAC1B,yCAAyC;IACzC,QAAQ,CAAC,UAAU,EAAE,WAAW,KAAK,CAAC,CAAC,CAAC,CAAC;IAEzC,iDAAiD;IACjD,QAAQ,CAAC,YAAY,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IAEhC,iDAAiD;IACjD,QAAQ,CAAC,YAAY,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IAEhC,yDAAyD;IACzD,SAAS,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC;IAE1B,uEAAuE;IACvE,QAAQ,CAAC,gBAAgB,EAAE,gBAAgB,CAAA;CAC5C;AAED;;;GAGG;AACH,6BAA6B,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAgChF","sources":["packages/@react-stately/tree/src/packages/@react-stately/tree/src/TreeCollection.ts","packages/@react-stately/tree/src/packages/@react-stately/tree/src/useTreeState.ts","packages/@react-stately/tree/src/packages/@react-stately/tree/src/index.ts","packages/@react-stately/tree/src/index.ts"],"sourcesContent":[null,null,null,"/*\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\nexport * from './useTreeState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/tree",
|
|
3
|
-
"version": "3.1
|
|
3
|
+
"version": "3.2.1",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@babel/runtime": "^7.6.2",
|
|
21
|
-
"@react-stately/collections": "^3.
|
|
22
|
-
"@react-stately/selection": "^3.
|
|
23
|
-
"@react-stately/utils": "^3.
|
|
24
|
-
"@react-types/shared": "^3.
|
|
21
|
+
"@react-stately/collections": "^3.3.5",
|
|
22
|
+
"@react-stately/selection": "^3.9.1",
|
|
23
|
+
"@react-stately/utils": "^3.4.0",
|
|
24
|
+
"@react-types/shared": "^3.11.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"react": "^16.8.0 || ^17.0.0-rc.1"
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "54c2366c4f31bd4bf619126131cd583c12972acc"
|
|
33
33
|
}
|
package/src/TreeCollection.ts
CHANGED
package/src/useTreeState.ts
CHANGED
|
@@ -17,7 +17,7 @@ import {TreeCollection} from './TreeCollection';
|
|
|
17
17
|
import {useCollection} from '@react-stately/collections';
|
|
18
18
|
import {useControlledState} from '@react-stately/utils';
|
|
19
19
|
|
|
20
|
-
interface TreeProps<T> extends CollectionBase<T>, Expandable, MultipleSelection {}
|
|
20
|
+
export interface TreeProps<T> extends CollectionBase<T>, Expandable, MultipleSelection {}
|
|
21
21
|
export interface TreeState<T> {
|
|
22
22
|
/** A collection of items in the tree. */
|
|
23
23
|
readonly collection: Collection<Node<T>>,
|
|
@@ -51,7 +51,7 @@ export function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T
|
|
|
51
51
|
props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()
|
|
52
52
|
, [props.disabledKeys]);
|
|
53
53
|
|
|
54
|
-
let tree = useCollection(props, nodes => new TreeCollection(nodes, {expandedKeys}));
|
|
54
|
+
let tree = useCollection(props, nodes => new TreeCollection(nodes, {expandedKeys}), null, [expandedKeys]);
|
|
55
55
|
|
|
56
56
|
// Reset focused key if that item is deleted from the collection.
|
|
57
57
|
useEffect(() => {
|
|
@@ -61,7 +61,7 @@ export function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T
|
|
|
61
61
|
}, [tree, selectionState.focusedKey]);
|
|
62
62
|
|
|
63
63
|
let onToggle = (key: Key) => {
|
|
64
|
-
setExpandedKeys(
|
|
64
|
+
setExpandedKeys(toggleKey(expandedKeys, key));
|
|
65
65
|
};
|
|
66
66
|
|
|
67
67
|
return {
|