@react-stately/tree 3.1.3 → 3.2.2

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 CHANGED
@@ -1,166 +1,139 @@
1
- var {
2
- useControlledState
3
- } = require("@react-stately/utils");
4
-
5
- var {
6
- useCollection
7
- } = require("@react-stately/collections");
8
-
9
- var {
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 $8jMun$react = require("react");
2
+ var $8jMun$reactstatelyselection = require("@react-stately/selection");
3
+ var $8jMun$reactstatelycollections = require("@react-stately/collections");
4
+ var $8jMun$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
- let last;
62
- let index = 0;
63
-
64
- for (let [key, node] of this.keyMap) {
65
- if (last) {
66
- last.nextKey = key;
67
- node.prevKey = last.key;
68
- } else {
69
- this.firstKey = key;
70
- node.prevKey = undefined;
12
+ Object.defineProperty(dest, key, {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return source[key];
71
16
  }
17
+ });
18
+ });
72
19
 
73
- if (node.type === 'item') {
74
- node.index = index++;
75
- }
76
-
77
- last = node; // Set nextKey as undefined since this might be the last node
78
- // If it isn't the last node, last.nextKey will properly set at start of new loop
79
-
80
- last.nextKey = undefined;
81
- }
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
- }
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 $9fca792b9f1dbe9f$exports = {};
111
26
 
112
- getLastKey() {
113
- return this.lastKey;
114
- }
27
+ $parcel$export($9fca792b9f1dbe9f$exports, "useTreeState", () => $9fca792b9f1dbe9f$export$728d6ba534403756);
115
28
 
116
- getItem(key) {
117
- return this.keyMap.get(key);
118
- }
119
29
 
30
+ var tmp = Symbol.iterator;
31
+ class $dd657f4eb6746c96$export$863faf230ee2118a {
32
+ *[tmp]() {
33
+ yield* this.iterable;
34
+ }
35
+ get size() {
36
+ return this.keyMap.size;
37
+ }
38
+ getKeys() {
39
+ return this.keyMap.keys();
40
+ }
41
+ getKeyBefore(key) {
42
+ let node = this.keyMap.get(key);
43
+ return node ? node.prevKey : null;
44
+ }
45
+ getKeyAfter(key) {
46
+ let node = this.keyMap.get(key);
47
+ return node ? node.nextKey : null;
48
+ }
49
+ getFirstKey() {
50
+ return this.firstKey;
51
+ }
52
+ getLastKey() {
53
+ return this.lastKey;
54
+ }
55
+ getItem(key) {
56
+ return this.keyMap.get(key);
57
+ }
58
+ at(idx) {
59
+ const keys = [
60
+ ...this.getKeys()
61
+ ];
62
+ return this.getItem(keys[idx]);
63
+ }
64
+ constructor(nodes, { expandedKeys: expandedKeys } = {
65
+ }){
66
+ this.keyMap = new Map();
67
+ this.iterable = nodes;
68
+ expandedKeys = expandedKeys || new Set();
69
+ let visit = (node)=>{
70
+ this.keyMap.set(node.key, node);
71
+ if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) for (let child of node.childNodes)visit(child);
72
+ };
73
+ for (let node2 of nodes)visit(node2);
74
+ let last;
75
+ let index = 0;
76
+ for (let [key, node1] of this.keyMap){
77
+ if (last) {
78
+ last.nextKey = key;
79
+ node1.prevKey = last.key;
80
+ } else {
81
+ this.firstKey = key;
82
+ node1.prevKey = undefined;
83
+ }
84
+ if (node1.type === 'item') node1.index = index++;
85
+ last = node1;
86
+ // Set nextKey as undefined since this might be the last node
87
+ // If it isn't the last node, last.nextKey will properly set at start of new loop
88
+ last.nextKey = undefined;
89
+ }
90
+ this.lastKey = last === null || last === void 0 ? void 0 : last.key;
91
+ }
120
92
  }
121
93
 
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
94
 
134
- useEffect(() => {
135
- if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) {
136
- selectionState.setFocusedKey(null);
137
- }
138
- }, [tree, selectionState.focusedKey]);
139
95
 
140
- let onToggle = key => {
141
- setExpandedKeys(expandedKeys => $f01b8043d944d5f04fffbeb1df9fd6cb$var$toggleKey(expandedKeys, key));
142
- };
143
96
 
144
- return {
145
- collection: tree,
146
- expandedKeys,
147
- disabledKeys,
148
- toggleKey: onToggle,
149
- selectionManager: new SelectionManager(tree, selectionState)
150
- };
97
+ function $9fca792b9f1dbe9f$export$728d6ba534403756(props) {
98
+ let [expandedKeys, setExpandedKeys] = $8jMun$reactstatelyutils.useControlledState(props.expandedKeys ? new Set(props.expandedKeys) : undefined, props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), props.onExpandedChange);
99
+ let selectionState = $8jMun$reactstatelyselection.useMultipleSelectionState(props);
100
+ let disabledKeys = $8jMun$react.useMemo(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set()
101
+ , [
102
+ props.disabledKeys
103
+ ]);
104
+ let tree = $8jMun$reactstatelycollections.useCollection(props, (nodes)=>new $dd657f4eb6746c96$export$863faf230ee2118a(nodes, {
105
+ expandedKeys: expandedKeys
106
+ })
107
+ , null, [
108
+ expandedKeys
109
+ ]);
110
+ // Reset focused key if that item is deleted from the collection.
111
+ $8jMun$react.useEffect(()=>{
112
+ if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) selectionState.setFocusedKey(null);
113
+ }, [
114
+ tree,
115
+ selectionState.focusedKey
116
+ ]);
117
+ let onToggle = (key)=>{
118
+ setExpandedKeys($9fca792b9f1dbe9f$var$toggleKey(expandedKeys, key));
119
+ };
120
+ return {
121
+ collection: tree,
122
+ expandedKeys: expandedKeys,
123
+ disabledKeys: disabledKeys,
124
+ toggleKey: onToggle,
125
+ selectionManager: new $8jMun$reactstatelyselection.SelectionManager(tree, selectionState)
126
+ };
127
+ }
128
+ function $9fca792b9f1dbe9f$var$toggleKey(set, key) {
129
+ let res = new Set(set);
130
+ if (res.has(key)) res.delete(key);
131
+ else res.add(key);
132
+ return res;
151
133
  }
152
134
 
153
- exports.useTreeState = useTreeState;
154
135
 
155
- function $f01b8043d944d5f04fffbeb1df9fd6cb$var$toggleKey(set, key) {
156
- let res = new Set(set);
136
+ $parcel$exportWildcard(module.exports, $9fca792b9f1dbe9f$exports);
157
137
 
158
- if (res.has(key)) {
159
- res.delete(key);
160
- } else {
161
- res.add(key);
162
- }
163
138
 
164
- return res;
165
- }
166
139
  //# 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,CAAY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AI,GAAGC,aAAa,CAACV,KAAD,EAAQ/B,KAAK,IAAI,4DAAmBA,KAAnB,EAA0B;AAACC,IAAAA;AAAD,GAA1B,CAAjB,CAAxB,CAZgF,CAchF;;AACAyC,EAAAA,SAAS,CAAC,MAAM;AACd,QAAIN,cAAc,CAACO,UAAf,IAA6B,IAA7B,IAAqC,CAACH,IAAI,CAACX,OAAL,CAAaO,cAAc,CAACO,UAA5B,CAA1C,EAAmF;AACjFP,MAAAA,cAAc,CAACQ,aAAf,CAA6B,IAA7B;AACD;AACF,GAJQ,EAIN,CAACJ,IAAD,EAAOJ,cAAc,CAACO,UAAtB,CAJM,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\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}));\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;UEgEI,MAAM,CAAC,QAAQ;MAjDN,yCAAc;aAiDJ,CAAC;eACb,IAAI,CAAC,QAAQ;IACtB,CAAC;QAEG,IAAI,GAAG,CAAC;QACV,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI;IACzB,CAAC;IAED,OAAO,GAAG,CAAC;QACT,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI;IACzB,CAAC;IAED,YAAY,CAAC,GAAQ,EAAE,CAAC;QACtB,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG;QAC9B,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO,GAAG,IAAI;IACnC,CAAC;IAED,WAAW,CAAC,GAAQ,EAAE,CAAC;QACrB,GAAG,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,YAAY;IAAA,CAAC;IAEtB,GAAG,CAAC,IAAI,GAAG,4CAAa,CAAC,KAAK,GAAE,KAAK,GAAI,GAAG,CAAC,yCAAc,CAAC,KAAK,EAAE,CAAC;0BAAA,YAAY;QAAA,CAAC;MAAG,IAAI,EAAE,CAAC;QAAA,YAAY;IAAA,CAAC;IAExG,EAAiE,AAAjE,+DAAiE;IACjE,sBAAS,KAAO,CAAC;QACf,EAAE,EAAE,cAAc,CAAC,UAAU,IAAI,IAAI,KAAK,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,UAAU,GAC9E,cAAc,CAAC,aAAa,CAAC,IAAI;IAErC,CAAC,EAAE,CAAC;QAAA,IAAI;QAAE,cAAc,CAAC,UAAU;IAAA,CAAC;IAEpC,GAAG,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,123 @@
1
- import { useControlledState } from "@react-stately/utils";
2
- import { useCollection } from "@react-stately/collections";
3
- import { SelectionManager, useMultipleSelectionState } from "@react-stately/selection";
4
- import { useEffect, useMemo } from "react";
5
- let $afa6f708e32ecf7f97d9a58dfd59c$var$_Symbol$iterator;
6
- $afa6f708e32ecf7f97d9a58dfd59c$var$_Symbol$iterator = Symbol.iterator;
1
+ import {useMemo as $9TXm6$useMemo, useEffect as $9TXm6$useEffect} from "react";
2
+ import {useMultipleSelectionState as $9TXm6$useMultipleSelectionState, SelectionManager as $9TXm6$SelectionManager} from "@react-stately/selection";
3
+ import {useCollection as $9TXm6$useCollection} from "@react-stately/collections";
4
+ import {useControlledState as $9TXm6$useControlledState} from "@react-stately/utils";
7
5
 
8
- /*
9
- * Copyright 2020 Adobe. All rights reserved.
10
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
11
- * you may not use this file except in compliance with the License. You may obtain a copy
12
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
13
- *
14
- * Unless required by applicable law or agreed to in writing, software distributed under
15
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
16
- * OF ANY KIND, either express or implied. See the License for the specific language
17
- * governing permissions and limitations under the License.
18
- */
19
- class $afa6f708e32ecf7f97d9a58dfd59c$export$TreeCollection {
20
- constructor(nodes, _temp) {
21
- var _last;
6
+ function $parcel$export(e, n, v, s) {
7
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8
+ }
9
+ var $36d99f0640511db5$exports = {};
22
10
 
23
- let {
24
- expandedKeys
25
- } = _temp === void 0 ? {} : _temp;
26
- this.keyMap = new Map();
27
- this.iterable = void 0;
28
- this.firstKey = void 0;
29
- this.lastKey = void 0;
30
- this.iterable = nodes;
31
- expandedKeys = expandedKeys || new Set();
11
+ $parcel$export($36d99f0640511db5$exports, "useTreeState", () => $36d99f0640511db5$export$728d6ba534403756);
32
12
 
33
- let visit = node => {
34
- this.keyMap.set(node.key, node);
35
13
 
36
- if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) {
37
- for (let child of node.childNodes) {
38
- visit(child);
39
- }
40
- }
41
- };
42
-
43
- for (let node of nodes) {
44
- visit(node);
14
+ var tmp = Symbol.iterator;
15
+ class $c8161626733397a0$export$863faf230ee2118a {
16
+ *[tmp]() {
17
+ yield* this.iterable;
45
18
  }
46
-
47
- let last;
48
- let index = 0;
49
-
50
- for (let [key, node] of this.keyMap) {
51
- if (last) {
52
- last.nextKey = key;
53
- node.prevKey = last.key;
54
- } else {
55
- this.firstKey = key;
56
- node.prevKey = undefined;
57
- }
58
-
59
- if (node.type === 'item') {
60
- node.index = index++;
61
- }
62
-
63
- last = node; // Set nextKey as undefined since this might be the last node
64
- // If it isn't the last node, last.nextKey will properly set at start of new loop
65
-
66
- last.nextKey = undefined;
19
+ get size() {
20
+ return this.keyMap.size;
21
+ }
22
+ getKeys() {
23
+ return this.keyMap.keys();
24
+ }
25
+ getKeyBefore(key) {
26
+ let node = this.keyMap.get(key);
27
+ return node ? node.prevKey : null;
28
+ }
29
+ getKeyAfter(key) {
30
+ let node = this.keyMap.get(key);
31
+ return node ? node.nextKey : null;
32
+ }
33
+ getFirstKey() {
34
+ return this.firstKey;
35
+ }
36
+ getLastKey() {
37
+ return this.lastKey;
38
+ }
39
+ getItem(key) {
40
+ return this.keyMap.get(key);
41
+ }
42
+ at(idx) {
43
+ const keys = [
44
+ ...this.getKeys()
45
+ ];
46
+ return this.getItem(keys[idx]);
47
+ }
48
+ constructor(nodes, { expandedKeys: expandedKeys } = {
49
+ }){
50
+ this.keyMap = new Map();
51
+ this.iterable = nodes;
52
+ expandedKeys = expandedKeys || new Set();
53
+ let visit = (node)=>{
54
+ this.keyMap.set(node.key, node);
55
+ if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) for (let child of node.childNodes)visit(child);
56
+ };
57
+ for (let node2 of nodes)visit(node2);
58
+ let last;
59
+ let index = 0;
60
+ for (let [key, node1] of this.keyMap){
61
+ if (last) {
62
+ last.nextKey = key;
63
+ node1.prevKey = last.key;
64
+ } else {
65
+ this.firstKey = key;
66
+ node1.prevKey = undefined;
67
+ }
68
+ if (node1.type === 'item') node1.index = index++;
69
+ last = node1;
70
+ // Set nextKey as undefined since this might be the last node
71
+ // If it isn't the last node, last.nextKey will properly set at start of new loop
72
+ last.nextKey = undefined;
73
+ }
74
+ this.lastKey = last === null || last === void 0 ? void 0 : last.key;
67
75
  }
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
76
  }
107
77
 
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
78
 
120
- useEffect(() => {
121
- if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) {
122
- selectionState.setFocusedKey(null);
123
- }
124
- }, [tree, selectionState.focusedKey]);
125
79
 
126
- let onToggle = key => {
127
- setExpandedKeys(expandedKeys => $f51dc3c5c900bd3cdb4a06df11d84697$var$toggleKey(expandedKeys, key));
128
- };
129
80
 
130
- return {
131
- collection: tree,
132
- expandedKeys,
133
- disabledKeys,
134
- toggleKey: onToggle,
135
- selectionManager: new SelectionManager(tree, selectionState)
136
- };
81
+ function $36d99f0640511db5$export$728d6ba534403756(props) {
82
+ let [expandedKeys, setExpandedKeys] = $9TXm6$useControlledState(props.expandedKeys ? new Set(props.expandedKeys) : undefined, props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), props.onExpandedChange);
83
+ let selectionState = $9TXm6$useMultipleSelectionState(props);
84
+ let disabledKeys = $9TXm6$useMemo(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set()
85
+ , [
86
+ props.disabledKeys
87
+ ]);
88
+ let tree = $9TXm6$useCollection(props, (nodes)=>new $c8161626733397a0$export$863faf230ee2118a(nodes, {
89
+ expandedKeys: expandedKeys
90
+ })
91
+ , null, [
92
+ expandedKeys
93
+ ]);
94
+ // Reset focused key if that item is deleted from the collection.
95
+ $9TXm6$useEffect(()=>{
96
+ if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) selectionState.setFocusedKey(null);
97
+ }, [
98
+ tree,
99
+ selectionState.focusedKey
100
+ ]);
101
+ let onToggle = (key)=>{
102
+ setExpandedKeys($36d99f0640511db5$var$toggleKey(expandedKeys, key));
103
+ };
104
+ return {
105
+ collection: tree,
106
+ expandedKeys: expandedKeys,
107
+ disabledKeys: disabledKeys,
108
+ toggleKey: onToggle,
109
+ selectionManager: new $9TXm6$SelectionManager(tree, selectionState)
110
+ };
111
+ }
112
+ function $36d99f0640511db5$var$toggleKey(set, key) {
113
+ let res = new Set(set);
114
+ if (res.has(key)) res.delete(key);
115
+ else res.add(key);
116
+ return res;
137
117
  }
138
118
 
139
- function $f51dc3c5c900bd3cdb4a06df11d84697$var$toggleKey(set, key) {
140
- let res = new Set(set);
141
119
 
142
- if (res.has(key)) {
143
- res.delete(key);
144
- } else {
145
- res.add(key);
146
- }
147
120
 
148
- return res;
149
- }
121
+
122
+ export {$36d99f0640511db5$export$728d6ba534403756 as useTreeState};
150
123
  //# sourceMappingURL=module.js.map
@@ -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,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ACD,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\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}));\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":";;;;;;;;;;;;;UEgEI,MAAM,CAAC,QAAQ;MAjDN,yCAAc;aAiDJ,CAAC;eACb,IAAI,CAAC,QAAQ;IACtB,CAAC;QAEG,IAAI,GAAG,CAAC;QACV,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI;IACzB,CAAC;IAED,OAAO,GAAG,CAAC;QACT,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI;IACzB,CAAC;IAED,YAAY,CAAC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;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"}
@@ -1 +1 @@
1
- {"mappings":"A;A;A;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;A;A;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"],"sourcesContent":[null,null,null],"names":[],"version":3,"file":"types.d.ts.map"}
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",
3
+ "version": "3.2.2",
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.3.1",
22
- "@react-stately/selection": "^3.4.0",
23
- "@react-stately/utils": "^3.1.1",
24
- "@react-types/shared": "^3.5.0"
21
+ "@react-stately/collections": "^3.3.6",
22
+ "@react-stately/selection": "^3.9.2",
23
+ "@react-stately/utils": "^3.4.1",
24
+ "@react-types/shared": "^3.11.1"
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": "9920ffaa2596a03c4498a15cb940bd2f4ba5cd6a"
32
+ "gitHead": "404d41859b7d6f56201d7fc01bd9f22ae3512937"
33
33
  }
@@ -95,4 +95,9 @@ export class TreeCollection<T> implements Collection<Node<T>> {
95
95
  getItem(key: Key) {
96
96
  return this.keyMap.get(key);
97
97
  }
98
+
99
+ at(idx: number) {
100
+ const keys = [...this.getKeys()];
101
+ return this.getItem(keys[idx]);
102
+ }
98
103
  }
@@ -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(expandedKeys => toggleKey(expandedKeys, key));
64
+ setExpandedKeys(toggleKey(expandedKeys, key));
65
65
  };
66
66
 
67
67
  return {