@react-stately/tree 3.0.0-nightly.2831 → 3.0.0-nightly.2833
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/import.mjs +9 -27
- package/dist/main.js +9 -27
- package/dist/main.js.map +1 -1
- package/dist/module.js +9 -27
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +3 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/TreeCollection.ts +2 -2
- package/src/useTreeState.ts +11 -32
package/dist/import.mjs
CHANGED
|
@@ -73,7 +73,7 @@ import {useControlledState as $1OoTj$useControlledState} from "@react-stately/ut
|
|
|
73
73
|
expandedKeys = expandedKeys || new Set();
|
|
74
74
|
let visit = (node)=>{
|
|
75
75
|
this.keyMap.set(node.key, node);
|
|
76
|
-
if (node.childNodes && (node.type === "section" || expandedKeys
|
|
76
|
+
if (node.childNodes && (node.type === "section" || expandedKeys.has(node.key))) for (let child of node.childNodes)visit(child);
|
|
77
77
|
};
|
|
78
78
|
for (let node of nodes)visit(node);
|
|
79
79
|
let last;
|
|
@@ -101,8 +101,8 @@ import {useControlledState as $1OoTj$useControlledState} from "@react-stately/ut
|
|
|
101
101
|
|
|
102
102
|
|
|
103
103
|
function $875d6693e12af071$export$728d6ba534403756(props) {
|
|
104
|
-
let {
|
|
105
|
-
let [expandedKeys, setExpandedKeys] = (0, $1OoTj$useControlledState)(
|
|
104
|
+
let { onExpandedChange: onExpandedChange } = props;
|
|
105
|
+
let [expandedKeys, setExpandedKeys] = (0, $1OoTj$useControlledState)(props.expandedKeys ? new Set(props.expandedKeys) : undefined, props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), onExpandedChange);
|
|
106
106
|
let selectionState = (0, $1OoTj$useMultipleSelectionState)(props);
|
|
107
107
|
let disabledKeys = (0, $1OoTj$useMemo)(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [
|
|
108
108
|
props.disabledKeys
|
|
@@ -121,7 +121,7 @@ function $875d6693e12af071$export$728d6ba534403756(props) {
|
|
|
121
121
|
selectionState.focusedKey
|
|
122
122
|
]);
|
|
123
123
|
let onToggle = (key)=>{
|
|
124
|
-
setExpandedKeys($875d6693e12af071$var$toggleKey(expandedKeys, key
|
|
124
|
+
setExpandedKeys($875d6693e12af071$var$toggleKey(expandedKeys, key));
|
|
125
125
|
};
|
|
126
126
|
return {
|
|
127
127
|
collection: tree,
|
|
@@ -132,29 +132,11 @@ function $875d6693e12af071$export$728d6ba534403756(props) {
|
|
|
132
132
|
selectionManager: new (0, $1OoTj$SelectionManager)(tree, selectionState)
|
|
133
133
|
};
|
|
134
134
|
}
|
|
135
|
-
function $875d6693e12af071$var$toggleKey(
|
|
136
|
-
let
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
updatedExpandedKeys = new Set([
|
|
141
|
-
...collection
|
|
142
|
-
].filter((row)=>{
|
|
143
|
-
return row.props.childItems || [
|
|
144
|
-
...collection.getChildren(row.key)
|
|
145
|
-
].filter((child)=>child.type === "item").length > 0;
|
|
146
|
-
}).map((row)=>row.key));
|
|
147
|
-
updatedExpandedKeys.delete(key);
|
|
148
|
-
} else {
|
|
149
|
-
updatedExpandedKeys = new Set(currentExpandedKeys);
|
|
150
|
-
if (updatedExpandedKeys.has(key)) updatedExpandedKeys.delete(key);
|
|
151
|
-
else updatedExpandedKeys.add(key);
|
|
152
|
-
}
|
|
153
|
-
return updatedExpandedKeys;
|
|
154
|
-
}
|
|
155
|
-
function $875d6693e12af071$var$convertExpanded(expanded) {
|
|
156
|
-
if (!expanded) return new Set();
|
|
157
|
-
return expanded === "all" ? "all" : new Set(expanded);
|
|
135
|
+
function $875d6693e12af071$var$toggleKey(set, key) {
|
|
136
|
+
let res = new Set(set);
|
|
137
|
+
if (res.has(key)) res.delete(key);
|
|
138
|
+
else res.add(key);
|
|
139
|
+
return res;
|
|
158
140
|
}
|
|
159
141
|
|
|
160
142
|
|
package/dist/main.js
CHANGED
|
@@ -80,7 +80,7 @@ $parcel$export(module.exports, "TreeCollection", () => $fae56b53b03726ff$export$
|
|
|
80
80
|
expandedKeys = expandedKeys || new Set();
|
|
81
81
|
let visit = (node)=>{
|
|
82
82
|
this.keyMap.set(node.key, node);
|
|
83
|
-
if (node.childNodes && (node.type === "section" || expandedKeys
|
|
83
|
+
if (node.childNodes && (node.type === "section" || expandedKeys.has(node.key))) for (let child of node.childNodes)visit(child);
|
|
84
84
|
};
|
|
85
85
|
for (let node of nodes)visit(node);
|
|
86
86
|
let last;
|
|
@@ -108,8 +108,8 @@ $parcel$export(module.exports, "TreeCollection", () => $fae56b53b03726ff$export$
|
|
|
108
108
|
|
|
109
109
|
|
|
110
110
|
function $38effb299000faaf$export$728d6ba534403756(props) {
|
|
111
|
-
let {
|
|
112
|
-
let [expandedKeys, setExpandedKeys] = (0, $cfIzB$reactstatelyutils.useControlledState)(
|
|
111
|
+
let { onExpandedChange: onExpandedChange } = props;
|
|
112
|
+
let [expandedKeys, setExpandedKeys] = (0, $cfIzB$reactstatelyutils.useControlledState)(props.expandedKeys ? new Set(props.expandedKeys) : undefined, props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), onExpandedChange);
|
|
113
113
|
let selectionState = (0, $cfIzB$reactstatelyselection.useMultipleSelectionState)(props);
|
|
114
114
|
let disabledKeys = (0, $cfIzB$react.useMemo)(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [
|
|
115
115
|
props.disabledKeys
|
|
@@ -128,7 +128,7 @@ function $38effb299000faaf$export$728d6ba534403756(props) {
|
|
|
128
128
|
selectionState.focusedKey
|
|
129
129
|
]);
|
|
130
130
|
let onToggle = (key)=>{
|
|
131
|
-
setExpandedKeys($38effb299000faaf$var$toggleKey(expandedKeys, key
|
|
131
|
+
setExpandedKeys($38effb299000faaf$var$toggleKey(expandedKeys, key));
|
|
132
132
|
};
|
|
133
133
|
return {
|
|
134
134
|
collection: tree,
|
|
@@ -139,29 +139,11 @@ function $38effb299000faaf$export$728d6ba534403756(props) {
|
|
|
139
139
|
selectionManager: new (0, $cfIzB$reactstatelyselection.SelectionManager)(tree, selectionState)
|
|
140
140
|
};
|
|
141
141
|
}
|
|
142
|
-
function $38effb299000faaf$var$toggleKey(
|
|
143
|
-
let
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
updatedExpandedKeys = new Set([
|
|
148
|
-
...collection
|
|
149
|
-
].filter((row)=>{
|
|
150
|
-
return row.props.childItems || [
|
|
151
|
-
...collection.getChildren(row.key)
|
|
152
|
-
].filter((child)=>child.type === "item").length > 0;
|
|
153
|
-
}).map((row)=>row.key));
|
|
154
|
-
updatedExpandedKeys.delete(key);
|
|
155
|
-
} else {
|
|
156
|
-
updatedExpandedKeys = new Set(currentExpandedKeys);
|
|
157
|
-
if (updatedExpandedKeys.has(key)) updatedExpandedKeys.delete(key);
|
|
158
|
-
else updatedExpandedKeys.add(key);
|
|
159
|
-
}
|
|
160
|
-
return updatedExpandedKeys;
|
|
161
|
-
}
|
|
162
|
-
function $38effb299000faaf$var$convertExpanded(expanded) {
|
|
163
|
-
if (!expanded) return new Set();
|
|
164
|
-
return expanded === "all" ? "all" : new Set(expanded);
|
|
142
|
+
function $38effb299000faaf$var$toggleKey(set, key) {
|
|
143
|
+
let res = new Set(set);
|
|
144
|
+
if (res.has(key)) res.delete(key);
|
|
145
|
+
else res.add(key);
|
|
146
|
+
return res;
|
|
165
147
|
}
|
|
166
148
|
|
|
167
149
|
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;ACVD;;;;;;;;;;CAUC,GAIM,MAAM;IAiDX,CAAC,CAAC,OAAO,QAAQ,CAAC,GAAG;QACnB,OAAO,IAAI,CAAC,QAAQ;IACtB;IAEA,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI;IACzB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AE,MAAM;IAC/C;AACF;AAEA,SAAS,gCAAa,mBAAqC,EAAE,GAAQ,EAAE,UAA+B;IACpG,IAAI;IACJ,IAAI,wBAAwB,OAAO;QACjC,wGAAwG;QACxG,uJAAuJ;QACvJ,sBAAsB,IAAI,IAAI;eAAI;SAAW,CAAC,MAAM,CAAC,CAAA;YACnD,OAAO,IAAI,KAAK,CAAC,UAAU,IAAI;mBAAI,WAAW,WAAW,CAAC,IAAI,GAAG;aAAE,CAAC,MAAM,CAAC,CAAA,QAAS,MAAM,IAAI,KAAK,QAAQ,MAAM,GAAG;QACtH,GAAG,GAAG,CAAC,CAAA,MAAO,IAAI,GAAG;QACrB,oBAAoB,MAAM,CAAC;IAC7B,OAAO;QACL,sBAAsB,IAAI,IAAI;QAC9B,IAAI,oBAAoB,GAAG,CAAC,MAC1B,oBAAoB,MAAM,CAAC;aAE3B,oBAAoB,GAAG,CAAC;IAE5B;IACA,OAAO;AACT;AAEA,SAAS,sCAAgB,QAA+B;IACtD,IAAI,CAAC,UACH,OAAO,IAAI;IAGb,OAAO,aAAa,QAChB,QACA,IAAI,IAAI;AACd;;CD3GC","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, CollectionStateBase, DisabledBehavior, Expandable, Key, MultipleSelection, Node} from '@react-types/shared';\nimport {SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {TreeCollection} from './TreeCollection';\nimport {useCallback, useEffect, useMemo} from 'react';\nimport {useCollection} from '@react-stately/collections';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface TreeProps<T> extends CollectionStateBase<T>, Expandable, MultipleSelection {\n /** Whether `disabledKeys` applies to all interactions, or only selection. */\n disabledBehavior?: DisabledBehavior\n}\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: 'all' | Set<Key>,\n\n /** Toggles the expanded state for an item by its key. */\n toggleKey(key: Key): void,\n\n /** Replaces the set of expanded keys. */\n setExpandedKeys(keys: 'all' | Set<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 {\n expandedKeys: propExpandedKeys,\n defaultExpandedKeys: propDefaultExpandedKeys,\n onExpandedChange\n } = props;\n\n let [expandedKeys, setExpandedKeys] = useControlledState(\n propExpandedKeys ? convertExpanded(propExpandedKeys) : undefined,\n propDefaultExpandedKeys ? convertExpanded(propDefaultExpandedKeys) : new Set(),\n 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, useCallback(nodes => new TreeCollection(nodes, {expandedKeys}), [expandedKeys]), null);\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, tree));\n };\n\n return {\n collection: tree,\n expandedKeys,\n disabledKeys,\n toggleKey: onToggle,\n setExpandedKeys,\n selectionManager: new SelectionManager(tree, selectionState)\n };\n}\n\nfunction toggleKey<T>(currentExpandedKeys: 'all' | Set<Key>, key: Key, collection: Collection<Node<T>>): Set<Key> {\n let updatedExpandedKeys: Set<Key>;\n if (currentExpandedKeys === 'all') {\n // TODO: would be nice if the collection row information differentiated between childNodes vs childItems\n // so we didn't have to keep iterating through info, perhaps make the user pass a prop to TreeItem for childItems/hasChildRows even in the static case?\n updatedExpandedKeys = new Set([...collection].filter(row => {\n return row.props.childItems || [...collection.getChildren(row.key)].filter(child => child.type === 'item').length > 0;\n }).map(row => row.key));\n updatedExpandedKeys.delete(key);\n } else {\n updatedExpandedKeys = new Set(currentExpandedKeys);\n if (updatedExpandedKeys.has(key)) {\n updatedExpandedKeys.delete(key);\n } else {\n updatedExpandedKeys.add(key);\n }\n }\n return updatedExpandedKeys;\n}\n\nfunction convertExpanded(expanded: 'all' | Iterable<Key>): 'all' | Set<Key> {\n if (!expanded) {\n return new Set<Key>();\n }\n\n return expanded === 'all'\n ? 'all'\n : new Set(expanded);\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, Key, Node} from '@react-types/shared';\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?: 'all' | 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 === 'all' || 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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AC9E,eAAe,aAAa,CAAC;IAEjC,uDAAuD;IACvD,GAAG;QAAC;QAAM,eAAe,UAAU;KAAC;IAEpC,IAAI,WAAW,CAAC;QACd,gBAAgB,gCAAU,cAAc;IAC1C;IAEA,OAAO;QACL,YAAY;sBACZ;sBACA;QACA,WAAW;yBACX;QACA,kBAAkB,IAAI,CAAA,GAAA,6CAAe,EAAE,MAAM;IAC/C;AACF;AAEA,SAAS,gCAAU,GAAa,EAAE,GAAQ;IACxC,IAAI,MAAM,IAAI,IAAI;IAClB,IAAI,IAAI,GAAG,CAAC,MACV,IAAI,MAAM,CAAC;SAEX,IAAI,GAAG,CAAC;IAGV,OAAO;AACT;;CDtFC","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, CollectionStateBase, DisabledBehavior, Expandable, Key, MultipleSelection, Node} from '@react-types/shared';\nimport {SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {TreeCollection} from './TreeCollection';\nimport {useCallback, useEffect, useMemo} from 'react';\nimport {useCollection} from '@react-stately/collections';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface TreeProps<T> extends CollectionStateBase<T>, Expandable, MultipleSelection {\n /** Whether `disabledKeys` applies to all interactions, or only selection. */\n disabledBehavior?: DisabledBehavior\n}\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 /** Replaces the set of expanded keys. */\n setExpandedKeys(keys: Set<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 {\n onExpandedChange\n } = props;\n\n let [expandedKeys, setExpandedKeys] = useControlledState(\n props.expandedKeys ? new Set(props.expandedKeys) : undefined,\n props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(),\n 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, useCallback(nodes => new TreeCollection(nodes, {expandedKeys}), [expandedKeys]), null);\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 setExpandedKeys,\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, Key, Node} from '@react-types/shared';\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
|
@@ -73,7 +73,7 @@ import {useControlledState as $1OoTj$useControlledState} from "@react-stately/ut
|
|
|
73
73
|
expandedKeys = expandedKeys || new Set();
|
|
74
74
|
let visit = (node)=>{
|
|
75
75
|
this.keyMap.set(node.key, node);
|
|
76
|
-
if (node.childNodes && (node.type === "section" || expandedKeys
|
|
76
|
+
if (node.childNodes && (node.type === "section" || expandedKeys.has(node.key))) for (let child of node.childNodes)visit(child);
|
|
77
77
|
};
|
|
78
78
|
for (let node of nodes)visit(node);
|
|
79
79
|
let last;
|
|
@@ -101,8 +101,8 @@ import {useControlledState as $1OoTj$useControlledState} from "@react-stately/ut
|
|
|
101
101
|
|
|
102
102
|
|
|
103
103
|
function $875d6693e12af071$export$728d6ba534403756(props) {
|
|
104
|
-
let {
|
|
105
|
-
let [expandedKeys, setExpandedKeys] = (0, $1OoTj$useControlledState)(
|
|
104
|
+
let { onExpandedChange: onExpandedChange } = props;
|
|
105
|
+
let [expandedKeys, setExpandedKeys] = (0, $1OoTj$useControlledState)(props.expandedKeys ? new Set(props.expandedKeys) : undefined, props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), onExpandedChange);
|
|
106
106
|
let selectionState = (0, $1OoTj$useMultipleSelectionState)(props);
|
|
107
107
|
let disabledKeys = (0, $1OoTj$useMemo)(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [
|
|
108
108
|
props.disabledKeys
|
|
@@ -121,7 +121,7 @@ function $875d6693e12af071$export$728d6ba534403756(props) {
|
|
|
121
121
|
selectionState.focusedKey
|
|
122
122
|
]);
|
|
123
123
|
let onToggle = (key)=>{
|
|
124
|
-
setExpandedKeys($875d6693e12af071$var$toggleKey(expandedKeys, key
|
|
124
|
+
setExpandedKeys($875d6693e12af071$var$toggleKey(expandedKeys, key));
|
|
125
125
|
};
|
|
126
126
|
return {
|
|
127
127
|
collection: tree,
|
|
@@ -132,29 +132,11 @@ function $875d6693e12af071$export$728d6ba534403756(props) {
|
|
|
132
132
|
selectionManager: new (0, $1OoTj$SelectionManager)(tree, selectionState)
|
|
133
133
|
};
|
|
134
134
|
}
|
|
135
|
-
function $875d6693e12af071$var$toggleKey(
|
|
136
|
-
let
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
updatedExpandedKeys = new Set([
|
|
141
|
-
...collection
|
|
142
|
-
].filter((row)=>{
|
|
143
|
-
return row.props.childItems || [
|
|
144
|
-
...collection.getChildren(row.key)
|
|
145
|
-
].filter((child)=>child.type === "item").length > 0;
|
|
146
|
-
}).map((row)=>row.key));
|
|
147
|
-
updatedExpandedKeys.delete(key);
|
|
148
|
-
} else {
|
|
149
|
-
updatedExpandedKeys = new Set(currentExpandedKeys);
|
|
150
|
-
if (updatedExpandedKeys.has(key)) updatedExpandedKeys.delete(key);
|
|
151
|
-
else updatedExpandedKeys.add(key);
|
|
152
|
-
}
|
|
153
|
-
return updatedExpandedKeys;
|
|
154
|
-
}
|
|
155
|
-
function $875d6693e12af071$var$convertExpanded(expanded) {
|
|
156
|
-
if (!expanded) return new Set();
|
|
157
|
-
return expanded === "all" ? "all" : new Set(expanded);
|
|
135
|
+
function $875d6693e12af071$var$toggleKey(set, key) {
|
|
136
|
+
let res = new Set(set);
|
|
137
|
+
if (res.has(key)) res.delete(key);
|
|
138
|
+
else res.add(key);
|
|
139
|
+
return res;
|
|
158
140
|
}
|
|
159
141
|
|
|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AU,IAAI,QAAQ,CAAC,KAAK,OAAO,CAAC,eAAe,UAAU,GAC9E,eAAe,aAAa,CAAC;IAEjC,uDAAuD;IACvD,GAAG;QAAC;QAAM,eAAe,UAAU;KAAC;IAEpC,IAAI,WAAW,CAAC;QACd,gBAAgB,gCAAU,cAAc,KAAK;IAC/C;IAEA,OAAO;QACL,YAAY;sBACZ;sBACA;QACA,WAAW;yBACX;QACA,kBAAkB,IAAI,CAAA,GAAA,uBAAe,EAAE,MAAM;IAC/C;AACF;AAEA,SAAS,gCAAa,mBAAqC,EAAE,GAAQ,EAAE,UAA+B;IACpG,IAAI;IACJ,IAAI,wBAAwB,OAAO;QACjC,wGAAwG;QACxG,uJAAuJ;QACvJ,sBAAsB,IAAI,IAAI;eAAI;SAAW,CAAC,MAAM,CAAC,CAAA;YACnD,OAAO,IAAI,KAAK,CAAC,UAAU,IAAI;mBAAI,WAAW,WAAW,CAAC,IAAI,GAAG;aAAE,CAAC,MAAM,CAAC,CAAA,QAAS,MAAM,IAAI,KAAK,QAAQ,MAAM,GAAG;QACtH,GAAG,GAAG,CAAC,CAAA,MAAO,IAAI,GAAG;QACrB,oBAAoB,MAAM,CAAC;IAC7B,OAAO;QACL,sBAAsB,IAAI,IAAI;QAC9B,IAAI,oBAAoB,GAAG,CAAC,MAC1B,oBAAoB,MAAM,CAAC;aAE3B,oBAAoB,GAAG,CAAC;IAE5B;IACA,OAAO;AACT;AAEA,SAAS,sCAAgB,QAA+B;IACtD,IAAI,CAAC,UACH,OAAO,IAAI;IAGb,OAAO,aAAa,QAChB,QACA,IAAI,IAAI;AACd;;CD3GC","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, CollectionStateBase, DisabledBehavior, Expandable, Key, MultipleSelection, Node} from '@react-types/shared';\nimport {SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {TreeCollection} from './TreeCollection';\nimport {useCallback, useEffect, useMemo} from 'react';\nimport {useCollection} from '@react-stately/collections';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface TreeProps<T> extends CollectionStateBase<T>, Expandable, MultipleSelection {\n /** Whether `disabledKeys` applies to all interactions, or only selection. */\n disabledBehavior?: DisabledBehavior\n}\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: 'all' | Set<Key>,\n\n /** Toggles the expanded state for an item by its key. */\n toggleKey(key: Key): void,\n\n /** Replaces the set of expanded keys. */\n setExpandedKeys(keys: 'all' | Set<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 {\n expandedKeys: propExpandedKeys,\n defaultExpandedKeys: propDefaultExpandedKeys,\n onExpandedChange\n } = props;\n\n let [expandedKeys, setExpandedKeys] = useControlledState(\n propExpandedKeys ? convertExpanded(propExpandedKeys) : undefined,\n propDefaultExpandedKeys ? convertExpanded(propDefaultExpandedKeys) : new Set(),\n 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, useCallback(nodes => new TreeCollection(nodes, {expandedKeys}), [expandedKeys]), null);\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, tree));\n };\n\n return {\n collection: tree,\n expandedKeys,\n disabledKeys,\n toggleKey: onToggle,\n setExpandedKeys,\n selectionManager: new SelectionManager(tree, selectionState)\n };\n}\n\nfunction toggleKey<T>(currentExpandedKeys: 'all' | Set<Key>, key: Key, collection: Collection<Node<T>>): Set<Key> {\n let updatedExpandedKeys: Set<Key>;\n if (currentExpandedKeys === 'all') {\n // TODO: would be nice if the collection row information differentiated between childNodes vs childItems\n // so we didn't have to keep iterating through info, perhaps make the user pass a prop to TreeItem for childItems/hasChildRows even in the static case?\n updatedExpandedKeys = new Set([...collection].filter(row => {\n return row.props.childItems || [...collection.getChildren(row.key)].filter(child => child.type === 'item').length > 0;\n }).map(row => row.key));\n updatedExpandedKeys.delete(key);\n } else {\n updatedExpandedKeys = new Set(currentExpandedKeys);\n if (updatedExpandedKeys.has(key)) {\n updatedExpandedKeys.delete(key);\n } else {\n updatedExpandedKeys.add(key);\n }\n }\n return updatedExpandedKeys;\n}\n\nfunction convertExpanded(expanded: 'all' | Iterable<Key>): 'all' | Set<Key> {\n if (!expanded) {\n return new Set<Key>();\n }\n\n return expanded === 'all'\n ? 'all'\n : new Set(expanded);\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, Key, Node} from '@react-types/shared';\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?: 'all' | 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 === 'all' || 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":";;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;ACVD;;;;;;;;;;CAUC,GAIM,MAAM;IAiDX,CAAC,CAAC,OAAO,QAAQ,CAAC,GAAG;QACnB,OAAO,IAAI,CAAC,QAAQ;IACtB;IAEA,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI;IACzB;IAEA,UAAU;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI;IACzB;IAEA,aAAa,GAAQ,EAAE;QACrB,IAAI,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC;QAC3B,OAAO,OAAO,KAAK,OAAO,GAAG;IAC/B;IAEA,YAAY,GAAQ,EAAE;QACpB,IAAI,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC;QAC3B,OAAO,OAAO,KAAK,OAAO,GAAG;IAC/B;IAEA,cAAc;QACZ,OAAO,IAAI,CAAC,QAAQ;IACtB;IAEA,aAAa;QACX,OAAO,IAAI,CAAC,OAAO;IACrB;IAEA,QAAQ,GAAQ,EAAE;QAChB,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC;IACzB;IAEA,GAAG,GAAW,EAAE;QACd,MAAM,OAAO;eAAI,IAAI,CAAC,OAAO;SAAG;QAChC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;IAC/B;IAhFA,YAAY,KAAwB,EAAE,gBAAC,YAAY,EAA4B,GAAG,CAAC,CAAC,CAAE;aAL9E,SA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;IAEtB,IAAI,OAAO,CAAA,GAAA,oBAAY,EAAE,OAAO,CAAA,GAAA,kBAAU,EAAE,CAAA,QAAS,IAAI,CAAA,GAAA,yCAAa,EAAE,OAAO;0BAAC;QAAY,IAAI;QAAC;KAAa,GAAG;IAEjH,iEAAiE;IACjE,CAAA,GAAA,gBAAQ,EAAE;QACR,IAAI,eAAe,UAAU,IAAI,QAAQ,CAAC,KAAK,OAAO,CAAC,eAAe,UAAU,GAC9E,eAAe,aAAa,CAAC;IAEjC,uDAAuD;IACvD,GAAG;QAAC;QAAM,eAAe,UAAU;KAAC;IAEpC,IAAI,WAAW,CAAC;QACd,gBAAgB,gCAAU,cAAc;IAC1C;IAEA,OAAO;QACL,YAAY;sBACZ;sBACA;QACA,WAAW;yBACX;QACA,kBAAkB,IAAI,CAAA,GAAA,uBAAe,EAAE,MAAM;IAC/C;AACF;AAEA,SAAS,gCAAU,GAAa,EAAE,GAAQ;IACxC,IAAI,MAAM,IAAI,IAAI;IAClB,IAAI,IAAI,GAAG,CAAC,MACV,IAAI,MAAM,CAAC;SAEX,IAAI,GAAG,CAAC;IAGV,OAAO;AACT;;CDtFC","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, CollectionStateBase, DisabledBehavior, Expandable, Key, MultipleSelection, Node} from '@react-types/shared';\nimport {SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {TreeCollection} from './TreeCollection';\nimport {useCallback, useEffect, useMemo} from 'react';\nimport {useCollection} from '@react-stately/collections';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface TreeProps<T> extends CollectionStateBase<T>, Expandable, MultipleSelection {\n /** Whether `disabledKeys` applies to all interactions, or only selection. */\n disabledBehavior?: DisabledBehavior\n}\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 /** Replaces the set of expanded keys. */\n setExpandedKeys(keys: Set<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 {\n onExpandedChange\n } = props;\n\n let [expandedKeys, setExpandedKeys] = useControlledState(\n props.expandedKeys ? new Set(props.expandedKeys) : undefined,\n props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(),\n 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, useCallback(nodes => new TreeCollection(nodes, {expandedKeys}), [expandedKeys]), null);\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 setExpandedKeys,\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, Key, Node} from '@react-types/shared';\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
|
@@ -2,7 +2,7 @@ import { Collection, Key, Node, CollectionStateBase, DisabledBehavior, Expandabl
|
|
|
2
2
|
import { SelectionManager } from "@react-stately/selection";
|
|
3
3
|
export class TreeCollection<T> implements Collection<Node<T>> {
|
|
4
4
|
constructor(nodes: Iterable<Node<T>>, { expandedKeys }?: {
|
|
5
|
-
expandedKeys?:
|
|
5
|
+
expandedKeys?: Set<Key>;
|
|
6
6
|
});
|
|
7
7
|
[Symbol.iterator](): Generator<Node<T>, void, undefined>;
|
|
8
8
|
get size(): number;
|
|
@@ -24,11 +24,11 @@ export interface TreeState<T> {
|
|
|
24
24
|
/** A set of keys for items that are disabled. */
|
|
25
25
|
readonly disabledKeys: Set<Key>;
|
|
26
26
|
/** A set of keys for items that are expanded. */
|
|
27
|
-
readonly expandedKeys:
|
|
27
|
+
readonly expandedKeys: Set<Key>;
|
|
28
28
|
/** Toggles the expanded state for an item by its key. */
|
|
29
29
|
toggleKey(key: Key): void;
|
|
30
30
|
/** Replaces the set of expanded keys. */
|
|
31
|
-
setExpandedKeys(keys:
|
|
31
|
+
setExpandedKeys(keys: Set<Key>): void;
|
|
32
32
|
/** A selection manager to read and update multiple selection state. */
|
|
33
33
|
readonly selectionManager: SelectionManager;
|
|
34
34
|
}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AAcA,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,
|
|
1
|
+
{"mappings":";;AAcA,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;AClFD,2BAA2B,CAAC,CAAE,SAAQ,oBAAoB,CAAC,CAAC,EAAE,UAAU,EAAE,iBAAiB;IACzF,6EAA6E;IAC7E,gBAAgB,CAAC,EAAE,gBAAgB,CAAA;CACpC;AACD,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,yCAAyC;IACzC,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAEtC,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,CAsChF","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.0.0-nightly.
|
|
3
|
+
"version": "3.0.0-nightly.2833+b2d25ef23",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -22,10 +22,10 @@
|
|
|
22
22
|
"url": "https://github.com/adobe/react-spectrum"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@react-stately/collections": "3.0.0-nightly.
|
|
26
|
-
"@react-stately/selection": "3.0.0-nightly.
|
|
27
|
-
"@react-stately/utils": "3.0.0-nightly.
|
|
28
|
-
"@react-types/shared": "3.0.0-nightly.
|
|
25
|
+
"@react-stately/collections": "3.0.0-nightly.2833+b2d25ef23",
|
|
26
|
+
"@react-stately/selection": "3.0.0-nightly.2833+b2d25ef23",
|
|
27
|
+
"@react-stately/utils": "3.0.0-nightly.2833+b2d25ef23",
|
|
28
|
+
"@react-types/shared": "3.0.0-nightly.2833+b2d25ef23",
|
|
29
29
|
"@swc/helpers": "^0.5.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "b2d25ef23b827ec2427bf47b343e6dbd66326ed3"
|
|
38
38
|
}
|
package/src/TreeCollection.ts
CHANGED
|
@@ -18,14 +18,14 @@ export class TreeCollection<T> implements Collection<Node<T>> {
|
|
|
18
18
|
private firstKey: Key;
|
|
19
19
|
private lastKey: Key;
|
|
20
20
|
|
|
21
|
-
constructor(nodes: Iterable<Node<T>>, {expandedKeys}: {expandedKeys?:
|
|
21
|
+
constructor(nodes: Iterable<Node<T>>, {expandedKeys}: {expandedKeys?: Set<Key>} = {}) {
|
|
22
22
|
this.iterable = nodes;
|
|
23
23
|
expandedKeys = expandedKeys || new Set();
|
|
24
24
|
|
|
25
25
|
let visit = (node: Node<T>) => {
|
|
26
26
|
this.keyMap.set(node.key, node);
|
|
27
27
|
|
|
28
|
-
if (node.childNodes && (node.type === 'section' ||
|
|
28
|
+
if (node.childNodes && (node.type === 'section' || expandedKeys.has(node.key))) {
|
|
29
29
|
for (let child of node.childNodes) {
|
|
30
30
|
visit(child);
|
|
31
31
|
}
|
package/src/useTreeState.ts
CHANGED
|
@@ -29,13 +29,13 @@ export interface TreeState<T> {
|
|
|
29
29
|
readonly disabledKeys: Set<Key>,
|
|
30
30
|
|
|
31
31
|
/** A set of keys for items that are expanded. */
|
|
32
|
-
readonly expandedKeys:
|
|
32
|
+
readonly expandedKeys: Set<Key>,
|
|
33
33
|
|
|
34
34
|
/** Toggles the expanded state for an item by its key. */
|
|
35
35
|
toggleKey(key: Key): void,
|
|
36
36
|
|
|
37
37
|
/** Replaces the set of expanded keys. */
|
|
38
|
-
setExpandedKeys(keys:
|
|
38
|
+
setExpandedKeys(keys: Set<Key>): void,
|
|
39
39
|
|
|
40
40
|
/** A selection manager to read and update multiple selection state. */
|
|
41
41
|
readonly selectionManager: SelectionManager
|
|
@@ -47,14 +47,12 @@ export interface TreeState<T> {
|
|
|
47
47
|
*/
|
|
48
48
|
export function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T> {
|
|
49
49
|
let {
|
|
50
|
-
expandedKeys: propExpandedKeys,
|
|
51
|
-
defaultExpandedKeys: propDefaultExpandedKeys,
|
|
52
50
|
onExpandedChange
|
|
53
51
|
} = props;
|
|
54
52
|
|
|
55
53
|
let [expandedKeys, setExpandedKeys] = useControlledState(
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
props.expandedKeys ? new Set(props.expandedKeys) : undefined,
|
|
55
|
+
props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(),
|
|
58
56
|
onExpandedChange
|
|
59
57
|
);
|
|
60
58
|
|
|
@@ -74,7 +72,7 @@ export function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T
|
|
|
74
72
|
}, [tree, selectionState.focusedKey]);
|
|
75
73
|
|
|
76
74
|
let onToggle = (key: Key) => {
|
|
77
|
-
setExpandedKeys(toggleKey(expandedKeys, key
|
|
75
|
+
setExpandedKeys(toggleKey(expandedKeys, key));
|
|
78
76
|
};
|
|
79
77
|
|
|
80
78
|
return {
|
|
@@ -87,32 +85,13 @@ export function useTreeState<T extends object>(props: TreeProps<T>): TreeState<T
|
|
|
87
85
|
};
|
|
88
86
|
}
|
|
89
87
|
|
|
90
|
-
function toggleKey
|
|
91
|
-
let
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
// so we didn't have to keep iterating through info, perhaps make the user pass a prop to TreeItem for childItems/hasChildRows even in the static case?
|
|
95
|
-
updatedExpandedKeys = new Set([...collection].filter(row => {
|
|
96
|
-
return row.props.childItems || [...collection.getChildren(row.key)].filter(child => child.type === 'item').length > 0;
|
|
97
|
-
}).map(row => row.key));
|
|
98
|
-
updatedExpandedKeys.delete(key);
|
|
88
|
+
function toggleKey(set: Set<Key>, key: Key): Set<Key> {
|
|
89
|
+
let res = new Set(set);
|
|
90
|
+
if (res.has(key)) {
|
|
91
|
+
res.delete(key);
|
|
99
92
|
} else {
|
|
100
|
-
|
|
101
|
-
if (updatedExpandedKeys.has(key)) {
|
|
102
|
-
updatedExpandedKeys.delete(key);
|
|
103
|
-
} else {
|
|
104
|
-
updatedExpandedKeys.add(key);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return updatedExpandedKeys;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function convertExpanded(expanded: 'all' | Iterable<Key>): 'all' | Set<Key> {
|
|
111
|
-
if (!expanded) {
|
|
112
|
-
return new Set<Key>();
|
|
93
|
+
res.add(key);
|
|
113
94
|
}
|
|
114
95
|
|
|
115
|
-
return
|
|
116
|
-
? 'all'
|
|
117
|
-
: new Set(expanded);
|
|
96
|
+
return res;
|
|
118
97
|
}
|