@react-stately/tree 3.3.4 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -8,6 +8,7 @@ function $parcel$export(e, n, v, s) {
8
8
  }
9
9
 
10
10
  $parcel$export(module.exports, "useTreeState", () => $38effb299000faaf$export$728d6ba534403756);
11
+ $parcel$export(module.exports, "TreeCollection", () => $fae56b53b03726ff$export$863faf230ee2118a);
11
12
 
12
13
 
13
14
  var tmp = Symbol.iterator;
@@ -93,6 +94,7 @@ function $38effb299000faaf$export$728d6ba534403756(props) {
93
94
  // Reset focused key if that item is deleted from the collection.
94
95
  $cfIzB$react.useEffect(()=>{
95
96
  if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) selectionState.setFocusedKey(null);
97
+ // eslint-disable-next-line react-hooks/exhaustive-deps
96
98
  }, [
97
99
  tree,
98
100
  selectionState.focusedKey
@@ -118,4 +120,5 @@ function $38effb299000faaf$var$toggleKey(set, key) {
118
120
 
119
121
 
120
122
 
123
+
121
124
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -1 +1 @@
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;Q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KAAK,CAAC,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 */\nexport type {TreeProps, TreeState} from './useTreeState';\nexport {useTreeState} 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"}
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,GAA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C1D,CAAC;QAAA,KAAK,CAAC,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,EAAuD,AAAvD,qDAAuD;IACvD,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 */\nexport type {TreeProps, TreeState} from './useTreeState';\nexport {useTreeState} from './useTreeState';\nexport {TreeCollection} from './TreeCollection';\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 // eslint-disable-next-line react-hooks/exhaustive-deps\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
@@ -88,6 +88,7 @@ function $875d6693e12af071$export$728d6ba534403756(props) {
88
88
  // Reset focused key if that item is deleted from the collection.
89
89
  $1OoTj$useEffect(()=>{
90
90
  if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) selectionState.setFocusedKey(null);
91
+ // eslint-disable-next-line react-hooks/exhaustive-deps
91
92
  }, [
92
93
  tree,
93
94
  selectionState.focusedKey
@@ -113,5 +114,6 @@ function $875d6693e12af071$var$toggleKey(set, key) {
113
114
 
114
115
 
115
116
 
116
- export {$875d6693e12af071$export$728d6ba534403756 as useTreeState};
117
+
118
+ export {$875d6693e12af071$export$728d6ba534403756 as useTreeState, $05ca4cd7c4a5a999$export$863faf230ee2118a as TreeCollection};
117
119
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
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,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG;QAC9B,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO,GAAG,IAAI;IACnC,CAAC;IAED,WAAW,GAAG,CAAC;QACb,MAAM,CAAC,IAAI,CAAC,QAAQ;IACtB,CAAC;IAED,UAAU,GAAG,CAAC;QACZ,MAAM,CAAC,IAAI,CAAC,OAAO;IACrB,CAAC;IAED,OAAO,CAAC,GAAQ,EAAE,CAAC;QACjB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG;IAC5B,CAAC;IAED,EAAE,CAAC,GAAW,EAAE,CAAC;QACf,KAAK,CAAC,IAAI,GAAG,CAAC;eAAG,IAAI,CAAC,OAAO;QAAE,CAAC;QAChC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG;IAC9B,CAAC;gBAhFW,KAAwB,EAAE,CAAC,eAAA,YA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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 */\nexport type {TreeProps, TreeState} from './useTreeState';\nexport {useTreeState} 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
+ {"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,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG;QAC9B,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO,GAAG,IAAI;IACnC,CAAC;IAED,WAAW,GAAG,CAAC;QACb,MAAM,CAAC,IAAI,CAAC,QAAQ;IACtB,CAAC;IAED,UAAU,GAAG,CAAC;QACZ,MAAM,CAAC,IAAI,CAAC,OAAO;IACrB,CAAC;IAED,OAAO,CAAC,GAAQ,EAAE,CAAC;QACjB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG;IAC5B,CAAC;IAED,EAAE,CAAC,GAAW,EAAE,CAAC;QACf,KAAK,CAAC,IAAI,GAAG,CAAC;eAAG,IAAI,CAAC,OAAO;QAAE,CAAC;QAChC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG;IAC9B,CAAC;gBAhFW,KAAwB,EAAE,CAAC,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;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 */\nexport type {TreeProps, TreeState} from './useTreeState';\nexport {useTreeState} from './useTreeState';\nexport {TreeCollection} from './TreeCollection';\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 // eslint-disable-next-line react-hooks/exhaustive-deps\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,6 +1,20 @@
1
- import { Collection, CollectionBase, Expandable, MultipleSelection, Node } from "@react-types/shared";
1
+ import { Collection, Node, CollectionBase, Expandable, MultipleSelection } from "@react-types/shared";
2
2
  import { Key } from "react";
3
3
  import { SelectionManager } from "@react-stately/selection";
4
+ export class TreeCollection<T> implements Collection<Node<T>> {
5
+ constructor(nodes: Iterable<Node<T>>, { expandedKeys }?: {
6
+ expandedKeys?: Set<Key>;
7
+ });
8
+ [Symbol.iterator](): Generator<Node<T>, void, undefined>;
9
+ get size(): number;
10
+ getKeys(): IterableIterator<Key>;
11
+ getKeyBefore(key: Key): Key;
12
+ getKeyAfter(key: Key): Key;
13
+ getFirstKey(): Key;
14
+ getLastKey(): Key;
15
+ getItem(key: Key): Node<T>;
16
+ at(idx: number): Node<T>;
17
+ }
4
18
  export interface TreeProps<T> extends CollectionBase<T>, Expandable, MultipleSelection {
5
19
  }
6
20
  export interface TreeState<T> {
@@ -1 +1 @@
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 */\nexport type {TreeProps, TreeState} from './useTreeState';\nexport {useTreeState} from './useTreeState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":";;;AAeA,4BAA4B,CAAC,CAAE,YAAW,WAAW,KAAK,CAAC,CAAC,CAAC;gBAM/C,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAC,YAAY,EAAC,GAAE;QAAC,YAAY,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,CAAA;KAAM;IA2CnF,CAAC,MAAM,CAAC,QAAQ,CAAC;IAIlB,IAAI,IAAI,WAEP;IAED,OAAO;IAIP,YAAY,CAAC,GAAG,EAAE,GAAG;IAKrB,WAAW,CAAC,GAAG,EAAE,GAAG;IAKpB,WAAW;IAIX,UAAU;IAIV,OAAO,CAAC,GAAG,EAAE,GAAG;IAIhB,EAAE,CAAC,GAAG,EAAE,MAAM;CAIf;ACnFD,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,CAiChF","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 */\nexport type {TreeProps, TreeState} from './useTreeState';\nexport {useTreeState} from './useTreeState';\nexport {TreeCollection} from './TreeCollection';\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.3.4",
3
+ "version": "3.4.0",
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.4.4",
22
- "@react-stately/selection": "^3.11.0",
21
+ "@react-stately/collections": "^3.5.0",
22
+ "@react-stately/selection": "^3.11.1",
23
23
  "@react-stately/utils": "^3.5.1",
24
- "@react-types/shared": "^3.15.0"
24
+ "@react-types/shared": "^3.16.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
@@ -29,5 +29,5 @@
29
29
  "publishConfig": {
30
30
  "access": "public"
31
31
  },
32
- "gitHead": "9202ef59e8c104dd06ffe33148445ef7932a5d1b"
32
+ "gitHead": "2954307ddbefe149241685440c81f80ece6b2c83"
33
33
  }
package/src/index.ts CHANGED
@@ -11,3 +11,4 @@
11
11
  */
12
12
  export type {TreeProps, TreeState} from './useTreeState';
13
13
  export {useTreeState} from './useTreeState';
14
+ export {TreeCollection} from './TreeCollection';
@@ -58,6 +58,7 @@ export function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T
58
58
  if (selectionState.focusedKey != null && !tree.getItem(selectionState.focusedKey)) {
59
59
  selectionState.setFocusedKey(null);
60
60
  }
61
+ // eslint-disable-next-line react-hooks/exhaustive-deps
61
62
  }, [tree, selectionState.focusedKey]);
62
63
 
63
64
  let onToggle = (key: Key) => {