MCPcopy Index your code
hub / github.com/adobe/react-spectrum / updateTree

Function updateTree

packages/react-stately/src/data/useTreeData.ts:191–257  ·  view source on GitHub ↗
(
    items: TreeNode<T>[],
    key: Key | null,
    update: (node: TreeNode<T>) => TreeNode<T> | null,
    originalMap: Map<Key, TreeNode<T>>
  )

Source from the content-addressed store, hash-verified

189 }
190
191 function updateTree(
192 items: TreeNode<T>[],
193 key: Key | null,
194 update: (node: TreeNode<T>) => TreeNode<T> | null,
195 originalMap: Map<Key, TreeNode<T>>
196 ) {
197 let node = key == null ? null : originalMap.get(key);
198 if (node == null) {
199 return {items, nodeMap: originalMap};
200 }
201 let map = new Map<Key, TreeNode<T>>(originalMap);
202
203 // Create a new node. If null, then delete the node, otherwise replace.
204 let newNode = update(node);
205 if (newNode == null) {
206 deleteNode(node, map);
207 } else {
208 addNode(newNode, map);
209 }
210
211 // Walk up the tree and update each parent to refer to the new children.
212 while (node && node.parentKey) {
213 let nextParent = map.get(node.parentKey)!;
214 let copy: TreeNode<T> = {
215 key: nextParent.key,
216 parentKey: nextParent.parentKey,
217 value: nextParent.value,
218 children: null
219 };
220
221 let children = nextParent.children;
222 if (newNode == null && children) {
223 children = children.filter(c => c !== node);
224 }
225
226 copy.children =
227 children?.map(child => {
228 if (child === node) {
229 // newNode cannot be null here due to the above filter.
230 return newNode!;
231 }
232
233 return child;
234 }) ?? null;
235
236 map.set(copy.key, copy);
237
238 newNode = copy;
239 node = nextParent;
240 }
241
242 if (newNode == null) {
243 items = items.filter(c => c !== node);
244 }
245
246 return {
247 items: items.map(item => {
248 if (item === node) {

Callers 6

insertFunction · 0.85
removeFunction · 0.85
moveFunction · 0.85
updateFunction · 0.85
postorderFunction · 0.85
moveItemsFunction · 0.85

Calls 5

deleteNodeFunction · 0.85
addNodeFunction · 0.85
updateFunction · 0.70
filterMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected